Skip to content

Commit

Permalink
feat(vrack-services): components unit tests
Browse files Browse the repository at this point in the history
ref: MANAGER-16104

Signed-off-by: Quentin Pavy <[email protected]>
  • Loading branch information
Quentin Pavy committed Dec 6, 2024
1 parent 7b1f4fb commit 54880ec
Show file tree
Hide file tree
Showing 23 changed files with 685 additions and 25 deletions.
19 changes: 12 additions & 7 deletions packages/manager/apps/vrack-services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
"author": "OVH SAS",
"scripts": {
"beta-test:e2e": "tsc && node ../../../../scripts/run-playwright-bdd.js",
"beta-test:e2e:ci": "tsc && node ../../../../scripts/run-playwright-bdd.js --ci",
"beta-test:e2e:cii": "tsc && node ../../../../scripts/run-playwright-bdd.js --ci",
"build": "tsc && vite build",
"coverage": "vitest run --coverage",
"dev": "tsc && vite",
"start": "lerna exec --stream --scope='@ovh-ux/manager-vrack-services-app' --include-dependencies -- npm run build --if-present",
"start:dev": "lerna exec --stream --scope='@ovh-ux/manager-vrack-services-app' --include-dependencies -- npm run dev --if-present",
"start:watch": "lerna exec --stream --parallel --scope='@ovh-ux/manager-vrack-services-app' --include-dependencies -- npm run dev:watch --if-present",
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"test:e2e": "tsc && node ../../../../scripts/run-playwright-bdd.js",
"test:e2e:cii": "tsc && node ../../../../scripts/run-playwright-bdd.js --ci"
"test:coverage": "vitest run --coverage"
},
"dependencies": {
"@ovh-ux/manager-config": "^8.0.0",
"@ovh-ux/manager-core-api": "^0.9.0",
"@ovh-ux/manager-core-utils": "*",
"@ovh-ux/manager-module-order": "^0.8.0",
"@ovh-ux/manager-react-components": "^1.41.1",
"@ovh-ux/manager-react-core-application": "^0.11.1",
Expand All @@ -51,17 +51,22 @@
"@ovh-ux/manager-vite-config": "^0.8.3",
"@playwright/test": "^1.41.2",
"@tanstack/react-query-devtools": "^5.51.21",
"@testing-library/jest-dom": "^6.5.0",
"@testing-library/react": "^16.0.1",
"@testing-library/user-event": "^14.5.2",
"@types/jest": "27.x",
"@vitejs/plugin-react": "^4.2.1",
"@vitest/coverage-v8": "^1.2.0",
"@vitejs/plugin-react": "^4.3.0",
"@vitest/coverage-v8": "^2.1.4",
"c8": "^9.1.0",
"element-internals-polyfill": "^1.3.11",
"jest": "27.x",
"msw": "2.1.7",
"ts-jest": "27.x",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.1",
"typescript": "^4.3.2",
"vite": "^5.2.13",
"vitest": "^1.2.0"
"vitest": "^2.1.4"
},
"msw": {
"workerDirectory": "./public"
Expand Down
5 changes: 5 additions & 0 deletions packages/manager/apps/vrack-services/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ export default defineConfig({
// Collect trace when retrying the failed test.
trace: 'retain-on-failure',
},
testMatch: '**/*.e2e.ts',
webServer: {
command: 'yarn run dev',
url: 'http://localhost:9000/',
},
});
29 changes: 29 additions & 0 deletions packages/manager/apps/vrack-services/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { vi } from 'vitest';
import '@testing-library/jest-dom';

vi.mock('react-i18next', () => ({
useTranslation: () => ({
t: (translationKey: string) => translationKey,
i18n: { language: 'fr_FR' },
}),
}));

vi.mock('@ovh-ux/manager-react-shell-client', async (importOriginal) => {
const original: typeof import('@ovh-ux/manager-react-shell-client') = await importOriginal();
return {
...original,
useOvhTracking: () => ({ trackClick: vi.fn(), trackPage: vi.fn() }),
};
});

vi.mock('react-router-dom', async (importOriginal) => {
const original: typeof import('react-router-dom') = await importOriginal();
return {
...original,
useSearchParams: () => [{ get: (str: string) => str }],
useNavigate: vi.fn(),
useLocation: vi.fn().mockReturnValue({
pathname: 'pathname',
}),
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { render, waitFor } from '@testing-library/react';
import { Breadcrumb, BreadcrumbProps } from './Breadcrumb.component';

/** Render */
const renderComponent = ({ items, overviewUrl }: BreadcrumbProps) => {
return render(<Breadcrumb items={items} overviewUrl={overviewUrl} />);
};

/** END RENDER */

describe('Breadcrumb Component', () => {
it('should display ODS breadcrumb', async () => {
const { getByRole } = renderComponent({
items: [],
});

await waitFor(() => {
expect(getByRole('navigation')).toBeDefined();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { render, fireEvent, waitFor, act } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { DetailedOrder } from '@ovh-ux/manager-module-order';
import {
ShellContext,
ShellContextType,
} from '@ovh-ux/manager-react-shell-client';
import { CreateVrack, CreateVrackProps } from './CreateVrack.component';
import { createVrackOnlyCart } from '@/utils/cart';

const queryClient = new QueryClient();

const shellContext = {
environment: {
user: { ovhSubsidiary: 'FR' },
},
};

const renderComponent = ({ closeModal }: CreateVrackProps) => {
return render(
<QueryClientProvider client={queryClient}>
<ShellContext.Provider
value={(shellContext as unknown) as ShellContextType}
>
<CreateVrack closeModal={closeModal} />
</ShellContext.Provider>
</QueryClientProvider>,
);
};

/** MOCKS */
const closeModalMock = vi.fn();

vi.mock('@/utils/cart', async (importOriginal) => {
const original: typeof import('@/utils/cart') = await importOriginal();
return {
...original,
createVrackOnlyCart: vi.fn(),
};
});

vi.mock('@ovh-ux/manager-module-order', async (importOriginal) => {
const original: typeof import('@ovh-ux/manager-module-order') = await importOriginal();
return {
...original,
useOrderPollingStatus: () => ({
data: [] as DetailedOrder[],
}),
};
});

/** END MOCKS */

describe('CreateVrack Component', () => {
it('should display the contracts after click the button create a vrack', async () => {
vi.mocked(createVrackOnlyCart).mockResolvedValue({
contractList: [
{
name: 'test',
url: 'test',
content: 'test',
},
{
name: 'test2',
url: 'test2',
content: 'test2',
},
],
cartId: '1',
});

const { getByText } = renderComponent({ closeModal: closeModalMock });
const button = await getByText('modalCreateNewVrackButtonLabel');
await act(() => {
fireEvent.click(button);
});

await waitFor(() => {
expect(createVrackOnlyCart).toHaveBeenCalledWith('FR');
expect(getByText('modalConfirmContractsCheckboxLabel')).not.toBeNull();
expect(
getByText('modalVrackCreationSubmitOrderButtonLabel'),
).toBeDefined();
});
});

it('should display an error message if cart creation fail', async () => {
vi.mocked(createVrackOnlyCart).mockRejectedValue({
response: { data: { message: 'api-error' } },
});

const { getByText } = renderComponent({ closeModal: closeModalMock });
const button = await getByText('modalCreateNewVrackButtonLabel');

await act(() => {
fireEvent.click(button);
});

await waitFor(() => {
expect(createVrackOnlyCart).toHaveBeenCalledWith('FR');
expect(getByText('api-error')).toBeDefined();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { render } from '@testing-library/react';
import {
ShellContext,
ShellContextType,
} from '@ovh-ux/manager-react-shell-client';
import { DetailedOrder } from '@ovh-ux/manager-module-order';
import { ErrorBannerProps, ErrorPage } from './ErrorPage.component';
import {
DeliveringMessages,
DeliveringMessagesProps,
} from './DeliveringMessages.component';

/** MOCKS */
vi.mock('react-i18next', () => ({
useTranslation: () => ({
t: (translationKey: string, params?: any) => {
const flattenParams = params
? Object.keys(params).reduce((previous, current) => {
return `${previous} | ${current}:${params[current]}`;
}, '')
: undefined;
return flattenParams
? `${translationKey}${flattenParams}`
: translationKey;
},
i18n: { language: 'fr_FR' },
}),
}));
/** END MOCKS */
/** Render */
const renderComponent = ({ orders }: { orders?: DetailedOrder[] }) => {
return render(<DeliveringMessages messageKey="order-text" orders={orders} />);
};

/** END RENDER */

describe('DeliveringMessages Component', () => {
it('should display list of ongoing orders', async () => {
const { getByText } = renderComponent({
orders: [
{
date: '2024-12-06T12:10:00.000Z',
orderId: 1,
status: 'delivering',
} as DetailedOrder,
],
});

const date = new Date('2024-12-06T12:10:00.000Z');

expect(
getByText(
`order-text | date:${date.toLocaleDateString(
'fr-FR',
)} | time:${date.getHours()}:${date.getMinutes()} | status:orderStatus-delivering`,
),
).toBeDefined();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { render } from '@testing-library/react';
import {
ShellContext,
ShellContextType,
} from '@ovh-ux/manager-react-shell-client';
import { ErrorBannerProps, ErrorPage } from './ErrorPage.component';

/** Render */
const shellContext = {
environment: {
user: { ovhSubsidiary: 'FR' },
},
shell: {
tracking: {
trackClick: vi.fn(),
trackPage: vi.fn(),
init: vi.fn(),
},
},
};

const renderComponent = ({ error }: ErrorBannerProps) => {
return render(
<ShellContext.Provider
value={(shellContext as unknown) as ShellContextType}
>
<ErrorPage error={error} />
</ShellContext.Provider>,
);
};

/** END RENDER */

describe('ErrorPage Component', () => {
it('should display an api error message with queryid', async () => {
const { getByText } = renderComponent({
error: {
response: {
data: { message: 'api-error-message' },
headers: {
'x-ovh-queryid': 'api-error-queryid',
},
},
},
});
expect(getByText('api-error-message')).toBeDefined();
expect(getByText('api-error-queryid', { exact: false })).toBeDefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const FormField: React.FC<FormFieldProps> = ({
{children}
{helperText && (
<div slot="helper">
<OsdsText>{label}</OsdsText>
<OsdsText>{helperText}</OsdsText>
</div>
)}
{visualHint && (
Expand Down
Loading

0 comments on commit 54880ec

Please sign in to comment.