From 3da710efac9193f61ea5a85fa2f70957a77c0a5f Mon Sep 17 00:00:00 2001 From: Romain Le Cellier Date: Thu, 7 Mar 2024 16:19:38 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(frontend)=20Joanie=20api=20c?= =?UTF-8?q?lient=20load=20in=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Joanie's api client shouldn't be loaded on test that don't use it. --- .../components/TeacherDashboardCourseList/index.spec.tsx | 2 +- .../js/hooks/useDefaultOrganizationId/index.spec.tsx | 2 +- src/frontend/js/hooks/useResources/index.spec.tsx | 8 +++++--- .../components/CourseLearnersFiltersBar/index.spec.tsx | 2 +- .../hooks/useCourseLearnersFilters/index.spec.tsx | 2 +- src/frontend/js/utils/test/render.tsx | 5 +++-- src/frontend/js/utils/test/wrappers/JoanieAppWrapper.tsx | 4 +++- .../TeacherDashboardCourseSidebar/index.spec.tsx | 2 +- 8 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/frontend/js/components/TeacherDashboardCourseList/index.spec.tsx b/src/frontend/js/components/TeacherDashboardCourseList/index.spec.tsx index e94c38ea9c..41a43a343f 100644 --- a/src/frontend/js/components/TeacherDashboardCourseList/index.spec.tsx +++ b/src/frontend/js/components/TeacherDashboardCourseList/index.spec.tsx @@ -67,7 +67,7 @@ describe('components/TeacherDashboardCourseList', () => { await expectNoSpinner('Loading courses...'); expect( - screen.getByRole('heading', { name: /TeacherDashboardCourseList test title/ }), + await screen.findByRole('heading', { name: /TeacherDashboardCourseList test title/ }), ).toBeInTheDocument(); const calledUrls = fetchMock.calls().map((call) => call[0]); diff --git a/src/frontend/js/hooks/useDefaultOrganizationId/index.spec.tsx b/src/frontend/js/hooks/useDefaultOrganizationId/index.spec.tsx index bdb7ede73c..55024aabae 100644 --- a/src/frontend/js/hooks/useDefaultOrganizationId/index.spec.tsx +++ b/src/frontend/js/hooks/useDefaultOrganizationId/index.spec.tsx @@ -3,7 +3,7 @@ import fetchMock from 'fetch-mock'; import { RichieContextFactory as mockRichieContextFactory } from 'utils/test/factories/richie'; import { OrganizationFactory } from 'utils/test/factories/joanieLegacy'; import { Organization } from 'types/Joanie'; -import { JoanieAppWrapper, setupJoanieSession } from 'utils/test/wrappers/JoanieAppWrapper'; +import JoanieAppWrapper, { setupJoanieSession } from 'utils/test/wrappers/JoanieAppWrapper'; import useDefaultOrganizationId from '.'; jest.mock('utils/context', () => ({ diff --git a/src/frontend/js/hooks/useResources/index.spec.tsx b/src/frontend/js/hooks/useResources/index.spec.tsx index e784ad4333..f1b93ba36b 100644 --- a/src/frontend/js/hooks/useResources/index.spec.tsx +++ b/src/frontend/js/hooks/useResources/index.spec.tsx @@ -106,7 +106,8 @@ describe('useResources (omniscient)', () => { wrapper: Wrapper, }); - await waitFor(() => expect(result.current.states.isPending).toBe(true)); + await waitFor(() => expect(screen.queryByText('loading...')).not.toBeInTheDocument()); + expect(result.current.states.isPending).toBe(true); expect(result.current.states.fetching).toBe(true); expect(result.current.items).toEqual([]); @@ -222,8 +223,9 @@ describe('useResources (omniscient)', () => { wrapper: Wrapper, }, ); - - expect(result.current.states.isPending).toBe(true); + await waitFor(() => { + expect(result.current.states.isPending).toBe(true); + }); expect(result.current.item).toBe(undefined); expect(fetchMock.called('https://example.com/api/todos')).toBe(false); diff --git a/src/frontend/js/pages/TeacherDashboardCourseLearnersLayout/components/CourseLearnersFiltersBar/index.spec.tsx b/src/frontend/js/pages/TeacherDashboardCourseLearnersLayout/components/CourseLearnersFiltersBar/index.spec.tsx index 2276ad9758..958ef94c47 100644 --- a/src/frontend/js/pages/TeacherDashboardCourseLearnersLayout/components/CourseLearnersFiltersBar/index.spec.tsx +++ b/src/frontend/js/pages/TeacherDashboardCourseLearnersLayout/components/CourseLearnersFiltersBar/index.spec.tsx @@ -36,7 +36,7 @@ describe('', () => { ); // Two selects should have rendered - const organizationFilter: HTMLInputElement = screen.getByRole('combobox', { + const organizationFilter: HTMLInputElement = await screen.findByRole('combobox', { name: 'Organization', }); diff --git a/src/frontend/js/pages/TeacherDashboardCourseLearnersLayout/hooks/useCourseLearnersFilters/index.spec.tsx b/src/frontend/js/pages/TeacherDashboardCourseLearnersLayout/hooks/useCourseLearnersFilters/index.spec.tsx index e29d389894..4241878507 100644 --- a/src/frontend/js/pages/TeacherDashboardCourseLearnersLayout/hooks/useCourseLearnersFilters/index.spec.tsx +++ b/src/frontend/js/pages/TeacherDashboardCourseLearnersLayout/hooks/useCourseLearnersFilters/index.spec.tsx @@ -6,7 +6,7 @@ import { CourseProductRelationFactory, OrganizationFactory, } from 'utils/test/factories/joanieLegacy'; -import { JoanieAppWrapper, setupJoanieSession } from 'utils/test/wrappers/JoanieAppWrapper'; +import JoanieAppWrapper, { setupJoanieSession } from 'utils/test/wrappers/JoanieAppWrapper'; import useCourseLearnersFilters from '.'; jest.mock('utils/context', () => ({ diff --git a/src/frontend/js/utils/test/render.tsx b/src/frontend/js/utils/test/render.tsx index 5df309d26d..852ff3165c 100644 --- a/src/frontend/js/utils/test/render.tsx +++ b/src/frontend/js/utils/test/render.tsx @@ -1,8 +1,9 @@ import { RenderResult, screen, render as testingLibraryRender } from '@testing-library/react'; -import React, { ReactElement } from 'react'; +import React, { ReactElement, lazy } from 'react'; import { Nullable } from 'types/utils'; import { AppWrapperProps } from './wrappers/types'; -import { JoanieAppWrapper } from './wrappers/JoanieAppWrapper'; + +const JoanieAppWrapper = lazy(() => import('./wrappers/JoanieAppWrapper')); // ------- setup ------- diff --git a/src/frontend/js/utils/test/wrappers/JoanieAppWrapper.tsx b/src/frontend/js/utils/test/wrappers/JoanieAppWrapper.tsx index 1ca3038e63..2745bef967 100644 --- a/src/frontend/js/utils/test/wrappers/JoanieAppWrapper.tsx +++ b/src/frontend/js/utils/test/wrappers/JoanieAppWrapper.tsx @@ -19,7 +19,7 @@ export const setupJoanieSession = () => { }; }; -export const JoanieAppWrapper = ({ +const JoanieAppWrapper = ({ children, intlOptions, queryOptions, @@ -35,3 +35,5 @@ export const JoanieAppWrapper = ({ ); }; + +export default JoanieAppWrapper; diff --git a/src/frontend/js/widgets/Dashboard/components/TeacherDashboardCourseSidebar/index.spec.tsx b/src/frontend/js/widgets/Dashboard/components/TeacherDashboardCourseSidebar/index.spec.tsx index cf2053dc72..a1adc3b56f 100644 --- a/src/frontend/js/widgets/Dashboard/components/TeacherDashboardCourseSidebar/index.spec.tsx +++ b/src/frontend/js/widgets/Dashboard/components/TeacherDashboardCourseSidebar/index.spec.tsx @@ -54,7 +54,7 @@ describe('', () => { }); await expectNoSpinner('Loading course...'); - const link = screen.getByRole('link', { + const link = await screen.findByRole('link', { name: intl.formatMessage(messages.syllabusLinkLabel), }); expect(link).toHaveAttribute('href', `/redirects/courses/${course.code}`);