Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Jan 29, 2024
1 parent 06199ce commit 165d05d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/backend/src/studio-api-impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type { ApplicationManager } from './managers/applicationManager';
import type { RecipeStatusRegistry } from './registries/RecipeStatusRegistry';
import { StudioApiImpl } from './studio-api-impl';
import type { PlayGroundManager } from './managers/playground';
import type { Webview } from '@podman-desktop/api';
import type { TelemetryLogger, Webview } from '@podman-desktop/api';
import { CatalogManager } from './managers/catalogManager';
import type { ModelsManager } from './managers/modelsManager';

Expand Down Expand Up @@ -77,6 +77,7 @@ beforeEach(async () => {
{} as unknown as PlayGroundManager,
catalogManager,
{} as unknown as ModelsManager,
{} as TelemetryLogger,
);
vi.resetAllMocks();
vi.mock('node:fs');
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/src/studio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ vi.mock('@podman-desktop/api', async () => {
onDidReceiveMessage: vi.fn(),
postMessage: vi.fn(),
},
onDidChangeViewState: vi.fn(),
}),
},
env: {
createTelemetryLogger: () => ({
logUsage: vi.fn(),
}),
},
};
Expand Down
25 changes: 24 additions & 1 deletion packages/frontend/src/pages/Recipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { vi, test, expect } from 'vitest';
import { vi, test, expect, beforeEach } from 'vitest';
import { screen, render } from '@testing-library/svelte';
import catalog from '../../../backend/src/ai-user-test.json';
import Recipe from './Recipe.svelte';
Expand All @@ -7,6 +7,7 @@ const mocks = vi.hoisted(() => {
return {
getCatalogMock: vi.fn(),
getPullingStatusesMock: vi.fn(),
telemetryLogUsageMock: vi.fn(),
};
});

Expand All @@ -15,6 +16,7 @@ vi.mock('../utils/client', async () => {
studioClient: {
getCatalog: mocks.getCatalogMock,
getPullingStatuses: mocks.getPullingStatusesMock,
telemetryLogUsage: mocks.telemetryLogUsageMock,
},
rpcBrowser: {
subscribe: () => {
Expand All @@ -26,6 +28,10 @@ vi.mock('../utils/client', async () => {
};
});

beforeEach(() => {
vi.resetAllMocks();
});

test('should display recipe information', async () => {
const recipe = catalog.recipes.find(r => r.id === 'recipe 1');
expect(recipe).not.toBeUndefined();
Expand All @@ -40,3 +46,20 @@ test('should display recipe information', async () => {
screen.getByText(recipe!.name);
screen.getByText(recipe!.readme);
});

test('should send telemetry data', async () => {
const recipe = catalog.recipes.find(r => r.id === 'recipe 1');
expect(recipe).not.toBeUndefined();

mocks.getCatalogMock.mockResolvedValue(catalog);
mocks.getPullingStatusesMock.mockResolvedValue(new Map());
render(Recipe, {
recipeId: 'recipe 1',
});
await new Promise(resolve => setTimeout(resolve, 200));

expect(mocks.telemetryLogUsageMock).toHaveBeenNthCalledWith(1, 'recipe.open', {
'recipe.id': 'recipe 1',
'recipe.name': 'Recipe 1',
});
});
3 changes: 1 addition & 2 deletions packages/frontend/src/pages/Recipe.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { getIcon } from '/@/utils/categoriesUtils';
import RecipeModels from './RecipeModels.svelte';
import { catalog } from '/@/stores/catalog';
import { recipes } from '/@/stores/recipe';
import type { Recipe } from '@shared/src/models/IRecipe';
export let recipeId: string;
Expand All @@ -28,7 +27,7 @@ $: recipeStatus = $recipes.get(recipeId);
let recipeTelemetry: string | undefined = undefined;
$: if (recipe && recipe.id !== recipeTelemetry) {
recipeTelemetry = recipe.id;
studioClient.telemetryLogUsage('recipy.open', { 'recipe.id': recipe.id, 'recipe.name': recipe.name });
studioClient.telemetryLogUsage('recipe.open', { 'recipe.id': recipe.id, 'recipe.name': recipe.name });
}
let loading: boolean = false;
Expand Down

0 comments on commit 165d05d

Please sign in to comment.