From 574664135bf6df7c14edf2caae8a7337cc0731e4 Mon Sep 17 00:00:00 2001 From: Romain Jamet Date: Fri, 4 Oct 2024 16:55:59 +0200 Subject: [PATCH] feat(kms): add kms list test ref: MANAGER-15107 Signed-off-by: Romain Jamet --- .../apps/key-management-service/package.json | 9 +- .../src/components/Listing/ListingCells.tsx | 6 +- .../src/data/api/okms.ts | 14 --- .../src/data/hooks/useOKMS.ts | 37 ++----- ...eServiceKeyOperationsTranslations.spec.tsx | 11 +- .../useServiceKeyTypeTranslations.spec.tsx | 11 +- .../src/mocks/okms.mock.ts | 42 ++++++++ .../src/mocks/services.mock.ts | 36 +++++++ .../pages/listing/KmsListing.page.spec.tsx | 102 ++++++++++++++++++ .../src/pages/listing/index.tsx | 70 ++++++------ .../key-management-service/src/setupTests.ts | 11 -- .../key-management-service/src/setupTests.tsx | 39 +++++++ .../src/utils/tests/TestApp.tsx | 21 ++++ .../src/utils/tests/init.i18n.ts | 75 +++++++++++++ .../src/utils/tests/renderTestApp.tsx | 54 ++++++++++ .../key-management-service/vitest.config.js | 13 ++- 16 files changed, 450 insertions(+), 101 deletions(-) create mode 100644 packages/manager/apps/key-management-service/src/mocks/okms.mock.ts create mode 100644 packages/manager/apps/key-management-service/src/mocks/services.mock.ts create mode 100644 packages/manager/apps/key-management-service/src/pages/listing/KmsListing.page.spec.tsx delete mode 100644 packages/manager/apps/key-management-service/src/setupTests.ts create mode 100644 packages/manager/apps/key-management-service/src/setupTests.tsx create mode 100644 packages/manager/apps/key-management-service/src/utils/tests/TestApp.tsx create mode 100644 packages/manager/apps/key-management-service/src/utils/tests/init.i18n.ts create mode 100644 packages/manager/apps/key-management-service/src/utils/tests/renderTestApp.tsx diff --git a/packages/manager/apps/key-management-service/package.json b/packages/manager/apps/key-management-service/package.json index f5c40b5aad3a..6bc23ae92883 100644 --- a/packages/manager/apps/key-management-service/package.json +++ b/packages/manager/apps/key-management-service/package.json @@ -26,7 +26,7 @@ "@ovh-ux/manager-core-api": "*", "@ovh-ux/manager-core-utils": "*", "@ovh-ux/manager-module-order": "^0.5.3", - "@ovh-ux/manager-react-components": "^1.36.0", + "@ovh-ux/manager-react-components": "*", "@ovh-ux/manager-react-core-application": "*", "@ovh-ux/manager-react-shell-client": "*", "@ovh-ux/manager-tailwind-config": "*", @@ -53,9 +53,12 @@ "@playwright/test": "^1.34.3", "@tanstack/react-query-devtools": "^5.51.21", "@testing-library/jest-dom": "^6.4.6", - "@testing-library/react": "^14.1.2", + "@testing-library/react": "^16.0.0", + "@testing-library/user-event": "^14.5.2", "@vitejs/plugin-react": "^4.2.1", - "typescript": "^4.3.2", + "element-internals-polyfill": "^1.3.11", + "msw": "2.1.7", + "typescript": "^5.1.6", "vite": "^5.2.13", "vitest": "^1.2.0" }, diff --git a/packages/manager/apps/key-management-service/src/components/Listing/ListingCells.tsx b/packages/manager/apps/key-management-service/src/components/Listing/ListingCells.tsx index 30fbfa380778..c9b220258484 100644 --- a/packages/manager/apps/key-management-service/src/components/Listing/ListingCells.tsx +++ b/packages/manager/apps/key-management-service/src/components/Listing/ListingCells.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { ActionMenu, - DataGridClipboardCell, + Clipboard, DataGridTextCell, Links, } from '@ovh-ux/manager-react-components'; @@ -26,7 +26,7 @@ import { useKMSServiceInfos } from '@/data/hooks/useKMSServiceInfos'; import { OkmsServiceState } from '../layout-helpers/Dashboard/okmsServiceState/OkmsServiceState.component'; export const DatagridCellId = (props: OKMS | OkmsAllServiceKeys) => { - return ; + return ; }; export const DatagridCellName = (props: OKMS) => { @@ -97,7 +97,7 @@ export const DatagridServiceKeyCellName = (props: OkmsAllServiceKeys) => { }; export const DatagridServiceKeyCellId = (props: OkmsAllServiceKeys) => { - return ; + return ; }; export const DatagridCellType = (props: OkmsAllServiceKeys) => { diff --git a/packages/manager/apps/key-management-service/src/data/api/okms.ts b/packages/manager/apps/key-management-service/src/data/api/okms.ts index caa5e0f830f9..f5f9865186f8 100644 --- a/packages/manager/apps/key-management-service/src/data/api/okms.ts +++ b/packages/manager/apps/key-management-service/src/data/api/okms.ts @@ -92,20 +92,6 @@ export const getListingIcebergV2 = async ({ } }; -/** - * Get okms listing with iceberg V2 - */ -export const getListingIceberg = async () => { - try { - const List = await fetchIcebergV2({ - route: '/okms/resource', - }); - return List.data as OKMS[]; - } catch (error) { - return null; - } -}; - export const getOkmsResourceQueryKey = (okmsId: string) => [ `get/okms/resource/${okmsId}`, ]; diff --git a/packages/manager/apps/key-management-service/src/data/hooks/useOKMS.ts b/packages/manager/apps/key-management-service/src/data/hooks/useOKMS.ts index 2a326e9bf6de..621f0d1a8dd3 100644 --- a/packages/manager/apps/key-management-service/src/data/hooks/useOKMS.ts +++ b/packages/manager/apps/key-management-service/src/data/hooks/useOKMS.ts @@ -1,13 +1,12 @@ import { useQuery } from '@tanstack/react-query'; import apiClient from '@ovh-ux/manager-core-api'; +import { useResourcesIcebergV2 } from '@ovh-ux/manager-react-components'; -import { OKMS, OKMSOptions } from '@/types/okms.type'; +import { OKMS } from '@/types/okms.type'; import { ErrorResponse } from '@/types/api.type'; import { - getListingIceberg, getOkmsResourceQueryKey, getOkmsServicesResourceListQueryKey, - sortOKMS, } from '../api/okms'; export const getOKMSResource = async ( @@ -16,18 +15,6 @@ export const getOKMSResource = async ( return apiClient.v2.get(`okms/resource/${okmsId}`); }; -export const useAllOKMS = () => { - return useQuery({ - queryKey: getOkmsServicesResourceListQueryKey, - queryFn: () => getListingIceberg(), - retry: false, - ...{ - keepPreviousData: true, - }, - refetchInterval: 5000, - }); -}; - export const useOKMSById = (okmsId: string) => { return useQuery<{ data: OKMS }, ErrorResponse>({ queryKey: getOkmsResourceQueryKey(okmsId), @@ -40,17 +27,9 @@ export const useOKMSById = (okmsId: string) => { }); }; -export const useOKMS = ({ sorting }: OKMSOptions) => { - // retrieve All OKMS from API - const { - data: okms, - error: allOKMSError, - isLoading: allOKMSLoading, - } = useAllOKMS(); - - return { - isLoading: allOKMSLoading, - error: allOKMSError, - data: sortOKMS(okms || [], sorting), - }; -}; +export const useOKMSList = ({ pageSize }: { pageSize?: number }) => + useResourcesIcebergV2({ + route: '/okms/resource', + queryKey: getOkmsServicesResourceListQueryKey, + pageSize, + }); diff --git a/packages/manager/apps/key-management-service/src/hooks/serviceKey/useServiceKeyOperationsTranslations.spec.tsx b/packages/manager/apps/key-management-service/src/hooks/serviceKey/useServiceKeyOperationsTranslations.spec.tsx index c5a30fe9462c..13b98ec1e7d9 100644 --- a/packages/manager/apps/key-management-service/src/hooks/serviceKey/useServiceKeyOperationsTranslations.spec.tsx +++ b/packages/manager/apps/key-management-service/src/hooks/serviceKey/useServiceKeyOperationsTranslations.spec.tsx @@ -1,7 +1,16 @@ -import { describe, expect, it, test } from 'vitest'; +import { describe, expect, it, test, vi } from 'vitest'; import { OkmsServiceKeyOperations } from '@/types/okmsServiceKey.type'; import { useServiceKeyOperationsTranslations } from './useServiceKeyOperationsTranslations'; +vi.mock('react-i18next', () => ({ + useTranslation: () => ({ + t: (translationKey: string) => translationKey, + i18n: { + changeLanguage: () => new Promise(() => {}), + }, + }), +})); + describe('get service key operations translation ', () => { const useCases: { operation: OkmsServiceKeyOperations; diff --git a/packages/manager/apps/key-management-service/src/hooks/serviceKey/useServiceKeyTypeTranslations.spec.tsx b/packages/manager/apps/key-management-service/src/hooks/serviceKey/useServiceKeyTypeTranslations.spec.tsx index 1c08795cd6b4..a916f9533012 100644 --- a/packages/manager/apps/key-management-service/src/hooks/serviceKey/useServiceKeyTypeTranslations.spec.tsx +++ b/packages/manager/apps/key-management-service/src/hooks/serviceKey/useServiceKeyTypeTranslations.spec.tsx @@ -1,7 +1,16 @@ -import { describe, expect, it, test } from 'vitest'; +import { describe, expect, it, test, vi } from 'vitest'; import { OkmsKeyTypes } from '@/types/okmsServiceKey.type'; import { useServiceKeyTypeTranslations } from './useServiceKeyTypeTranslations'; +vi.mock('react-i18next', () => ({ + useTranslation: () => ({ + t: (translationKey: string) => translationKey, + i18n: { + changeLanguage: () => new Promise(() => {}), + }, + }), +})); + describe('get service key type translation ', () => { const useCases: { type: OkmsKeyTypes; diff --git a/packages/manager/apps/key-management-service/src/mocks/okms.mock.ts b/packages/manager/apps/key-management-service/src/mocks/okms.mock.ts new file mode 100644 index 000000000000..c7a1103b4b51 --- /dev/null +++ b/packages/manager/apps/key-management-service/src/mocks/okms.mock.ts @@ -0,0 +1,42 @@ +import { PathParams } from 'msw'; +import { Handler } from '../../../../../../playwright-helpers'; +import { OKMS } from '@/types/okms.type'; + +export const okmsList: OKMS[] = [ + { + iam: { + displayName: 'kms-1', + id: '1b4e7c8e-d1b8-4b46-a584-52c8b4b0225c', + urn: `urn:v1:eu:resource:okms:1b4e7c8e-d1b8-4b46-a584-52c8b4b0225c`, + }, + id: '7f3a82ac-a8d8-4c2a-ab0c-f6e86ddf6a7c', + kmipEndpoint: 'eu-west-rbx.okms.ovh.net:1234', + region: 'EU_WEST_RBX', + restEndpoint: 'https://eu-west-rbx.okms.ovh.net', + swaggerEndpoint: '"https://swagger-eu-west-rbx.okms.ovh.net', + }, +]; + +export type GetOkmsMocksParams = { + nbOkms?: number; +}; + +const findOkmsById = (params: PathParams) => + okmsList.find(({ id }) => id === params.id); + +export const getOkmsMocks = ({ + nbOkms = okmsList.length, +}: GetOkmsMocksParams): Handler[] => [ + { + url: '/okms/resource/:id', + response: (_: unknown, params: PathParams) => findOkmsById(params), + status: 200, + api: 'v2', + }, + { + url: '/okms/resource', + response: okmsList.slice(0, nbOkms), + status: 200, + api: 'v2', + }, +]; diff --git a/packages/manager/apps/key-management-service/src/mocks/services.mock.ts b/packages/manager/apps/key-management-service/src/mocks/services.mock.ts new file mode 100644 index 000000000000..19fe0b3e8db6 --- /dev/null +++ b/packages/manager/apps/key-management-service/src/mocks/services.mock.ts @@ -0,0 +1,36 @@ +import { KMSServiceInfos, OkmsState } from '@/types/okmsService.type'; +import { Handler } from '../../../../../../playwright-helpers'; + +const serviceList: KMSServiceInfos[] = [ + { + billing: { + lifecycle: { current: { creationDate: '2024-04-12T18:00:00.000Z' } }, + nextBillingDate: '2024-05-12T18:00:00.000Z', + }, + customer: { contacts: [{ customerCode: 'code', type: 'type' }] }, + resource: { + displayName: 'name', + name: 'name', + product: { name: 'name', description: 'descripton' }, + resellingProvider: 'test', + state: OkmsState.Active, + }, + }, +]; + +export const getServicesMocks = (): Handler[] => [ + { + url: '/services/:id', + response: serviceList[0], + status: 200, + method: 'get', + api: 'v6', + }, + { + url: '/services', + response: () => [1234567890], + status: 200, + method: 'get', + api: 'v6', + }, +]; diff --git a/packages/manager/apps/key-management-service/src/pages/listing/KmsListing.page.spec.tsx b/packages/manager/apps/key-management-service/src/pages/listing/KmsListing.page.spec.tsx new file mode 100644 index 000000000000..03b5be5a8455 --- /dev/null +++ b/packages/manager/apps/key-management-service/src/pages/listing/KmsListing.page.spec.tsx @@ -0,0 +1,102 @@ +import { act, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { renderTestApp } from '@/utils/tests/renderTestApp'; +import '@testing-library/jest-dom'; +import { labels } from '@/utils/tests/init.i18n'; +import { okmsList } from '@/mocks/okms.mock'; + +describe('KMS listing test suite', () => { + it('should redirect to the onboarding page when the kms list is empty', async () => { + await renderTestApp({ nbOkms: 0 }); + + expect(screen.getByText(labels.onboarding.title)).toBeVisible(); + + expect( + screen.queryByText(labels.listing.key_management_service_listing_title), + ).not.toBeInTheDocument(); + }); + + it('should display the kms listing page', async () => { + await renderTestApp(); + + expect( + screen.getByText(labels.listing.key_management_service_listing_title), + ).toBeVisible(); + + expect( + screen.queryByText(labels.onboarding.description), + ).not.toBeInTheDocument(); + }); + + it(`should navigate to the kms creation form on click on "${labels.listing.key_management_service_listing_add_kms_button}" button`, async () => { + await renderTestApp(); + + await waitFor( + () => + expect( + screen.getByText( + labels.listing.key_management_service_listing_add_kms_button, + ), + ).toBeEnabled(), + { + timeout: 30_000, + }, + ); + + await act(() => + userEvent.click( + screen.getByText( + labels.listing.key_management_service_listing_add_kms_button, + ), + ), + ); + + await waitFor( + () => + expect( + screen.getByText( + labels.create.key_management_service_create_subtitle, + ), + ).toBeVisible(), + { timeout: 30_000 }, + ); + }); + + it('should navigate to a kms dashboard on click on kms name', async () => { + await renderTestApp(); + + await act(() => + userEvent.click(screen.getByText(okmsList[0].iam.displayName)), + ); + + await waitFor( + () => + expect( + screen.getByText(labels.dashboard.general_informations), + ).toBeVisible(), + { timeout: 30_000 }, + ); + }); + + it(`should navigate to the kms delete modal on click on "${labels.listing.key_management_service_listing_terminate}" list action button`, async () => { + await renderTestApp(); + + await act(() => + userEvent.click( + screen.getByText( + labels.listing.key_management_service_listing_terminate, + ), + ), + ); + + await waitFor( + () => + expect( + screen.getByText( + labels.terminate.key_management_service_terminate_cancel, + ), + ).toBeVisible(), + { timeout: 30_000 }, + ); + }); +}); diff --git a/packages/manager/apps/key-management-service/src/pages/listing/index.tsx b/packages/manager/apps/key-management-service/src/pages/listing/index.tsx index 2a33977bfaf3..1962ace06416 100644 --- a/packages/manager/apps/key-management-service/src/pages/listing/index.tsx +++ b/packages/manager/apps/key-management-service/src/pages/listing/index.tsx @@ -1,24 +1,19 @@ -import React, { Suspense, useEffect } from 'react'; +import React from 'react'; import { Outlet, useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { - ODS_SPINNER_SIZE, ODS_MESSAGE_TYPE, ODS_BUTTON_SIZE, ODS_BUTTON_VARIANT, } from '@ovhcloud/ods-components'; -import { - OsdsButton, - OsdsMessage, - OsdsSpinner, -} from '@ovhcloud/ods-components/react'; +import { OsdsButton, OsdsMessage } from '@ovhcloud/ods-components/react'; import { ODS_THEME_COLOR_INTENT } from '@ovhcloud/ods-common-theming'; import { BaseLayout, Datagrid, HeadersProps, Notifications, - useDatagridSearchParams, + RedirectionGuard, useNotifications, } from '@ovh-ux/manager-react-components'; import { @@ -26,7 +21,7 @@ import { PageLocation, useOvhTracking, } from '@ovh-ux/manager-react-shell-client'; -import { useOKMS } from '@/data/hooks/useOKMS'; +import { useOKMSList } from '@/data/hooks/useOKMS'; import { ROUTES_URLS } from '@/routes/routes.constants'; import { DatagridActionMenu, @@ -36,7 +31,6 @@ import { DatagridCellStatus, } from '@/components/Listing/ListingCells'; import KmsGuidesHeader from '@/components/Guide/KmsGuidesHeader'; -import Loading from '@/components/Loading/Loading'; export default function Listing() { const { t } = useTranslation('key-management-service/listing'); @@ -74,24 +68,35 @@ export default function Listing() { }, ]; - const { sorting, setSorting } = useDatagridSearchParams(); - - const { error, data: okms, isLoading } = useOKMS({ - sorting, + const { + data, + flattenData, + fetchNextPage, + hasNextPage, + isError, + isLoading, + status, + } = useOKMSList({ + pageSize: 10, }); - useEffect(() => { - if (okms.length === 0 && !isLoading) { - navigate(ROUTES_URLS.onboarding); - } - }, [okms.length, isLoading]); - const headerProps: HeadersProps = { title: t('key_management_service_listing_title'), headerButton: , }; + return ( - }> + + {tError('manager_error_page_default')} + + } + > }>
- {error && ( - - {tError('manager_error_page_default')} - - )} - - {isLoading && !error && ( -
- -
- )} - {!isLoading && !error && ( + {flattenData && (
fetchNextPage()} contentAlignLeft />
)}
-
+ ); } diff --git a/packages/manager/apps/key-management-service/src/setupTests.ts b/packages/manager/apps/key-management-service/src/setupTests.ts deleted file mode 100644 index 39d524c2e0e0..000000000000 --- a/packages/manager/apps/key-management-service/src/setupTests.ts +++ /dev/null @@ -1,11 +0,0 @@ -import '@testing-library/jest-dom'; -import { vi } from 'vitest'; - -vi.mock('react-i18next', () => ({ - useTranslation: () => ({ - t: (translationKey: string) => translationKey, - i18n: { - changeLanguage: () => new Promise(() => {}), - }, - }), -})); diff --git a/packages/manager/apps/key-management-service/src/setupTests.tsx b/packages/manager/apps/key-management-service/src/setupTests.tsx new file mode 100644 index 000000000000..141b5063ffb0 --- /dev/null +++ b/packages/manager/apps/key-management-service/src/setupTests.tsx @@ -0,0 +1,39 @@ +import { odsSetup } from '@ovhcloud/ods-common-core'; +import { SetupServer, setupServer } from 'msw/node'; +import { toMswHandlers } from '../../../../../playwright-helpers'; +import { getAuthenticationMocks } from '../../../../../playwright-helpers/mocks/auth'; +import '@testing-library/jest-dom'; +import 'element-internals-polyfill'; + +odsSetup(); + +declare global { + // eslint-disable-next-line vars-on-top, no-var + var server: SetupServer; + // eslint-disable-next-line vars-on-top, no-var, @typescript-eslint/naming-convention + var __VERSION__: string; +} + +const server = setupServer( + ...toMswHandlers([ + ...getAuthenticationMocks({ isAuthMocked: true, region: 'EU' }), + ]), +); + +beforeAll(() => { + server.listen({ onUnhandledRequest: 'warn' }); + + delete global.server; + global.__VERSION__ = null; + global.server = server; +}); + +afterAll(() => { + server.close(); + + delete global.__VERSION__; +}); + +afterEach(() => { + server.resetHandlers(); +}); diff --git a/packages/manager/apps/key-management-service/src/utils/tests/TestApp.tsx b/packages/manager/apps/key-management-service/src/utils/tests/TestApp.tsx new file mode 100644 index 000000000000..c61879281812 --- /dev/null +++ b/packages/manager/apps/key-management-service/src/utils/tests/TestApp.tsx @@ -0,0 +1,21 @@ +import React from 'react'; +import { QueryClientProvider, QueryClient } from '@tanstack/react-query'; +import { createMemoryRouter, RouterProvider } from 'react-router-dom'; +import routes from '@/routes/routes'; + +export function TestApp() { + const router = createMemoryRouter(routes, { + initialEntries: ['/'], + initialIndex: 0, + }); + + const queryClient = new QueryClient({ + defaultOptions: { queries: { retry: false }, mutations: { retry: false } }, + }); + + return ( + + + + ); +} diff --git a/packages/manager/apps/key-management-service/src/utils/tests/init.i18n.ts b/packages/manager/apps/key-management-service/src/utils/tests/init.i18n.ts new file mode 100644 index 000000000000..d43dbd8afec9 --- /dev/null +++ b/packages/manager/apps/key-management-service/src/utils/tests/init.i18n.ts @@ -0,0 +1,75 @@ +import i18next, { i18n } from 'i18next'; +import common from '../../../public/translations/key-management-service/common/Messages_fr_FR.json'; +import create from '../../../public/translations/key-management-service/create/Messages_fr_FR.json'; +import dashboard from '../../../public/translations/key-management-service/dashboard/Messages_fr_FR.json'; +import error from '../../../public/translations/key-management-service/error/Messages_fr_FR.json'; +import guide from '../../../public/translations/key-management-service/guide/Messages_fr_FR.json'; +import listing from '../../../public/translations/key-management-service/listing/Messages_fr_FR.json'; +import onboarding from '../../../public/translations/key-management-service/onboarding/Messages_fr_FR.json'; +import serviceKeys from '../../../public/translations/key-management-service/serviceKeys/Messages_fr_FR.json'; +import terminate from '../../../public/translations/key-management-service/terminate/Messages_fr_FR.json'; + +export const defaultLocale = 'fr_FR'; +export const defaultAvailableLocales = [defaultLocale]; + +function addTranslations() { + i18next + .addResources(defaultLocale, 'key-management-service/common', common) + .addResources(defaultLocale, 'key-management-service/create', create) + .addResources(defaultLocale, 'key-management-service/dashboard', dashboard) + .addResources(defaultLocale, 'key-management-service/error', error) + .addResources(defaultLocale, 'key-management-service/guide', guide) + .addResources(defaultLocale, 'key-management-service/listing', listing) + .addResources(defaultLocale, 'key-management-service/terminate', terminate) + .addResources( + defaultLocale, + 'key-management-service/onboarding', + onboarding, + ) + .addResources( + defaultLocale, + 'key-management-service/serviceKeys', + serviceKeys, + ) + .use({ + type: 'postProcessor', + name: 'normalize', + process: (value: string) => + value ? value.replace(/&/g, '&') : value, + }); +} + +export const initTestI18n = () => + new Promise((resolve) => { + i18next.init({ + lng: defaultLocale, + defaultNS: 'key-management-service', + ns: [], + supportedLngs: defaultAvailableLocales, + postProcess: 'normalize', + interpolation: { + escapeValue: false, + }, + }); + + if (i18next.isInitialized) { + addTranslations(); + } else { + i18next.on('initialized', () => { + addTranslations(); + resolve(i18next); + }); + } + }); + +export const labels = { + common, + create, + dashboard, + error, + guide, + listing, + onboarding, + serviceKeys, + terminate, +}; diff --git a/packages/manager/apps/key-management-service/src/utils/tests/renderTestApp.tsx b/packages/manager/apps/key-management-service/src/utils/tests/renderTestApp.tsx new file mode 100644 index 000000000000..8849615ba54a --- /dev/null +++ b/packages/manager/apps/key-management-service/src/utils/tests/renderTestApp.tsx @@ -0,0 +1,54 @@ +import React from 'react'; +import { + initShellContext, + ShellContext, + ShellContextType, +} from '@ovh-ux/manager-react-shell-client'; +import { i18n } from 'i18next'; +import { I18nextProvider } from 'react-i18next'; +import { render, waitFor, screen } from '@testing-library/react'; +import { TestApp } from './TestApp'; +import { initTestI18n } from './init.i18n'; +import { toMswHandlers } from '../../../../../../../playwright-helpers'; +import { getAuthenticationMocks } from '../../../../../../../playwright-helpers/mocks/auth'; +import { getOkmsMocks, GetOkmsMocksParams } from '@/mocks/okms.mock'; +import { getServicesMocks } from '@/mocks/services.mock'; + +let context: ShellContextType; +let i18nValue: i18n; + +export const renderTestApp = async (mockParams: GetOkmsMocksParams = {}) => { + global.server?.resetHandlers( + ...toMswHandlers([ + ...getAuthenticationMocks({ isAuthMocked: true }), + ...getOkmsMocks(mockParams), + ...getServicesMocks(), + ]), + ); + + if (!context) { + context = await initShellContext('key-management-service'); + } + + if (!i18nValue) { + i18nValue = await initTestI18n(); + } + + const result = render( + + + + + , + ); + + await waitFor( + () => + expect( + screen.getAllByText('Key Management Service', { exact: false }).length, + ).toBeGreaterThan(0), + { timeout: 30000 }, + ); + + return result; +}; diff --git a/packages/manager/apps/key-management-service/vitest.config.js b/packages/manager/apps/key-management-service/vitest.config.js index 156daa6627e5..51634113d665 100644 --- a/packages/manager/apps/key-management-service/vitest.config.js +++ b/packages/manager/apps/key-management-service/vitest.config.js @@ -8,7 +8,7 @@ export default defineConfig({ test: { globals: true, environment: 'jsdom', - setupFiles: './src/setupTests.ts', + setupFiles: './src/setupTests.tsx', coverage: { include: ['src'], exclude: [ @@ -25,6 +25,17 @@ export default defineConfig({ 'src/queryClient.ts', ], }, + testTimeout: 60_000, + fileParallelism: false, + maxWorkers: 1, + pollOptions: { + forks: { + singleFork: true, + }, + threads: { + singleThread: true, + }, + }, }, resolve: { alias: {