Skip to content

Commit 50918bd

Browse files
committed
chore: PR feedback for tests
1 parent 6a704b1 commit 50918bd

File tree

7 files changed

+67
-45
lines changed

7 files changed

+67
-45
lines changed

src/components/skills-quiz-v2/tests/SkillsQuiz.test.jsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { renderWithRouter } from '../../../utils/tests';
88
import SkillsQuizV2 from '../SkillsQuiz';
99
import { useAlgoliaSearch, useDefaultSearchFilters, useEnterpriseCustomer } from '../../app/data';
1010
import { authenticatedUserFactory, enterpriseCustomerFactory } from '../../app/data/services/data/__factories__';
11-
import { setFakeHits } from '../../skills-quiz/__mocks__/react-instantsearch-dom';
11+
import { resetMockReactInstantSearch, setFakeHits } from '../../skills-quiz/__mocks__/react-instantsearch-dom';
1212

1313
// [tech debt] We appear to attempting to call legit Algolia APIs in these tests; lots
1414
// of test output related to Algolia errors. Does not appear to impact the test results.
@@ -58,15 +58,17 @@ const hits = [
5858
],
5959
},
6060
];
61-
setFakeHits(hits);
6261
describe('<SkillsQuizV2 />', () => {
6362
beforeEach(() => {
6463
jest.clearAllMocks();
6564
useEnterpriseCustomer.mockReturnValue({ data: mockEnterpriseCustomer });
6665
useAlgoliaSearch.mockReturnValue(mockAlgoliaSearch);
6766
useDefaultSearchFilters.mockReturnValue(`enterprise_customer_uuids:${mockEnterpriseCustomer.uuid}`);
67+
setFakeHits(hits);
68+
});
69+
afterEach(() => {
70+
resetMockReactInstantSearch();
6871
});
69-
7072
it('renders SkillsQuizV2 component correctly', () => {
7173
renderWithRouter(
7274
<IntlProvider locale="en">

src/components/skills-quiz-v2/tests/SkillsQuizForm.test.jsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { GOAL_DROPDOWN_DEFAULT_OPTION } from '../../skills-quiz/constants';
1111
import SkillQuizForm from '../SkillsQuizForm';
1212
import { authenticatedUserFactory, enterpriseCustomerFactory } from '../../app/data/services/data/__factories__';
1313
import { useAlgoliaSearch, useDefaultSearchFilters, useEnterpriseCustomer } from '../../app/data';
14-
import { setFakeHits } from '../../skills-quiz/__mocks__/react-instantsearch-dom';
14+
import { resetMockReactInstantSearch, setFakeHits } from '../../skills-quiz/__mocks__/react-instantsearch-dom';
1515

1616
jest.mock('algoliasearch/lite', () => jest.fn());
1717
const mockSearch = jest.fn().mockResolvedValue({ hits: [] });
@@ -85,15 +85,17 @@ const hits = [
8585
],
8686
},
8787
];
88-
setFakeHits(hits);
8988
describe('<SkillQuizForm />', () => {
9089
beforeEach(() => {
9190
jest.clearAllMocks();
9291
useEnterpriseCustomer.mockReturnValue({ data: mockEnterpriseCustomer });
9392
useAlgoliaSearch.mockReturnValue(mockAlgoliaSearch);
9493
useDefaultSearchFilters.mockReturnValue(`enterprise_customer_uuids:${mockEnterpriseCustomer.uuid}`);
94+
setFakeHits(hits);
95+
});
96+
afterEach(() => {
97+
resetMockReactInstantSearch();
9598
});
96-
9799
it('renders skills quiz v2 page', async () => {
98100
renderWithRouter(
99101
<SkillsQuizFormWrapper />,

src/components/skills-quiz/__mocks__/react-instantsearch-dom.jsx

+37-29
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,52 @@ const MockReactInstantSearch = jest.genMockFromModule(
66
'react-instantsearch-dom',
77
);
88

9-
let fakeHits = [
10-
{
11-
objectID: '1',
12-
title: 'bla',
13-
key: 'Bees101',
14-
type: 'course',
15-
aggregation_key: 'course:Bees101',
16-
authoring_organizations: [],
17-
card_image_url: 'https://fake.image',
18-
course_keys: [],
19-
},
20-
{
21-
objectID: '2',
22-
title: 'blp',
23-
key: 'Wasps200',
24-
type: 'course',
25-
aggregation_key: 'course:Wasps200',
26-
authoring_organizations: [],
27-
card_image_url: 'https://fake.image',
28-
course_keys: [],
29-
},
30-
];
9+
let mockState = {
10+
hits: [
11+
{
12+
objectID: '1',
13+
title: 'bla',
14+
key: 'Bees101',
15+
type: 'course',
16+
aggregation_key: 'course:Bees101',
17+
authoring_organizations: [],
18+
card_image_url: 'https://fake.image',
19+
course_keys: [],
20+
},
21+
{
22+
objectID: '2',
23+
title: 'blp',
24+
key: 'Wasps200',
25+
type: 'course',
26+
aggregation_key: 'course:Wasps200',
27+
authoring_organizations: [],
28+
card_image_url: 'https://fake.image',
29+
course_keys: [],
30+
},
31+
],
32+
nbHits: 2,
33+
};
3134

3235
// This allows you to override the built-in hits object
3336
const setFakeHits = hits => {
34-
fakeHits = hits;
37+
mockState.hits = hits;
38+
mockState.nbHits = hits.length;
3539
};
3640

37-
MockReactInstantSearch.configure = {
38-
nbHits: 2,
41+
const resetMockReactInstantSearch = () => {
42+
mockState = {
43+
hits: [...mockState.hits], // deep clone if needed
44+
nbHits: 2,
45+
};
3946
};
4047

4148
MockReactInstantSearch.connectStateResults = Component => (props) => (
4249
<Component
4350
searchResults={{
44-
hits: MockReactInstantSearch.configure.nbHits === 0 ? [] : fakeHits,
51+
hits: mockState.hits,
4552
hitsPerPage: 25,
46-
nbHits: MockReactInstantSearch.configure.nbHits,
47-
nbPages: MockReactInstantSearch.configure.nbHits === 0 ? 0 : 1,
53+
nbHits: mockState.nbHits,
54+
nbPages: mockState.nbHits === 0 ? 0 : 1,
4855
page: 1,
4956
}}
5057
isSearchStalled={false}
@@ -57,7 +64,7 @@ MockReactInstantSearch.connectStateResults = Component => (props) => (
5764

5865
MockReactInstantSearch.connectPagination = Component => (props) => (
5966
<Component
60-
nbPages={MockReactInstantSearch.configure.nbHits === 0 ? 0 : 1}
67+
nbPages={mockState.nbHits === 0 ? 0 : 1}
6168
currentRefinement={1}
6269
maxPagesDisplayed={5}
6370
{...props}
@@ -84,4 +91,5 @@ MockReactInstantSearch.Index = function Index({ children }) { return children; }
8491
module.exports = {
8592
...MockReactInstantSearch,
8693
setFakeHits,
94+
resetMockReactInstantSearch,
8795
};

src/components/skills-quiz/tests/SearchCurrentJobCard.test.jsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { SkillsContextProvider } from '../SkillsContextProvider';
99
import SearchCurrentJobCard from '../SearchCurrentJobCard';
1010
import { useAlgoliaSearch, useEnterpriseCustomer } from '../../app/data';
1111
import { authenticatedUserFactory, enterpriseCustomerFactory } from '../../app/data/services/data/__factories__';
12-
import { setFakeHits } from '../__mocks__/react-instantsearch-dom';
12+
import { resetMockReactInstantSearch, setFakeHits } from '../__mocks__/react-instantsearch-dom';
1313

1414
jest.mock('react-loading-skeleton', () => ({
1515
__esModule: true,
@@ -94,12 +94,15 @@ const mockAlgoliaSearch = {
9494
},
9595
};
9696

97-
setFakeHits(hitObject.hits);
9897
describe('<SearchCurrentJobCard />', () => {
9998
beforeEach(() => {
10099
jest.clearAllMocks();
101100
useEnterpriseCustomer.mockReturnValue({ data: mockEnterpriseCustomer });
102101
useAlgoliaSearch.mockReturnValue(mockAlgoliaSearch);
102+
setFakeHits(hitObject.hits);
103+
});
104+
afterEach(() => {
105+
resetMockReactInstantSearch();
103106
});
104107
test('renders the data in job cards correctly', async () => {
105108
renderWithRouter(

src/components/skills-quiz/tests/SearchJobCard.test.jsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { SkillsContextProvider } from '../SkillsContextProvider';
88
import SearchJobCard from '../SearchJobCard';
99
import { useAlgoliaSearch, useEnterpriseCustomer } from '../../app/data';
1010
import { authenticatedUserFactory, enterpriseCustomerFactory } from '../../app/data/services/data/__factories__';
11-
import { setFakeHits } from '../__mocks__/react-instantsearch-dom';
11+
import { resetMockReactInstantSearch, setFakeHits } from '../__mocks__/react-instantsearch-dom';
1212

1313
jest.mock('react-loading-skeleton', () => ({
1414
__esModule: true,
@@ -91,12 +91,15 @@ const mockAlgoliaSearch = {
9191
},
9292
};
9393

94-
setFakeHits(hitObject.hits);
9594
describe('<SearchJobCard />', () => {
9695
beforeEach(() => {
9796
jest.clearAllMocks();
9897
useEnterpriseCustomer.mockReturnValue({ data: mockEnterpriseCustomer });
9998
useAlgoliaSearch.mockReturnValue(mockAlgoliaSearch);
99+
setFakeHits(hitObject.hits);
100+
});
101+
afterEach(() => {
102+
resetMockReactInstantSearch();
100103
});
101104
test('renders the data in job cards correctly', async () => {
102105
render(

src/components/skills-quiz/tests/SkillsCourses.test.jsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { NO_COURSES_ALERT_MESSAGE_AGAINST_SKILLS } from '../constants';
1313
import { SkillsContext } from '../SkillsContextProvider';
1414
import { useAlgoliaSearch, useDefaultSearchFilters, useEnterpriseCustomer } from '../../app/data';
1515
import { authenticatedUserFactory, enterpriseCustomerFactory } from '../../app/data/services/data/__factories__';
16-
import { setFakeHits } from '../__mocks__/react-instantsearch-dom';
16+
import { resetMockReactInstantSearch, setFakeHits } from '../__mocks__/react-instantsearch-dom';
1717

1818
jest.mock('@edx/frontend-enterprise-utils', () => ({
1919
...jest.requireActual('@edx/frontend-enterprise-utils'),
@@ -106,15 +106,17 @@ const mockAlgoliaSearch = {
106106
},
107107
};
108108

109-
setFakeHits(courses.hits);
110109
describe('<SkillsCourses />', () => {
111110
beforeEach(() => {
112111
jest.clearAllMocks();
113112
useEnterpriseCustomer.mockReturnValue({ data: mockEnterpriseCustomer });
114113
useDefaultSearchFilters.mockReturnValue(`enterprise_customer_uuids:${mockEnterpriseCustomer.uuid}`);
115114
useAlgoliaSearch.mockReturnValue(mockAlgoliaSearch);
115+
setFakeHits(courses.hits);
116+
});
117+
afterEach(() => {
118+
resetMockReactInstantSearch();
116119
});
117-
118120
test('renders the correct data', async () => {
119121
const { container } = renderWithRouter(
120122
<SkillsCoursesWithContext />,

src/components/skills-quiz/tests/SkillsQuiz.test.jsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import SkillsQuiz from '../SkillsQuiz';
1111
import { SkillsContextProvider } from '../SkillsContextProvider';
1212
import { useAlgoliaSearch, useDefaultSearchFilters, useEnterpriseCustomer } from '../../app/data';
1313
import { authenticatedUserFactory, enterpriseCustomerFactory } from '../../app/data/services/data/__factories__';
14-
import { setFakeHits } from '../__mocks__/react-instantsearch-dom';
14+
import { resetMockReactInstantSearch, setFakeHits } from '../__mocks__/react-instantsearch-dom';
1515

1616
jest.mock('@edx/frontend-enterprise-utils', () => ({
1717
...jest.requireActual('@edx/frontend-enterprise-utils'),
@@ -72,15 +72,17 @@ const hits = [
7272
],
7373
},
7474
];
75-
setFakeHits(hits);
7675
describe('<SkillsQuiz />', () => {
7776
beforeEach(() => {
7877
jest.clearAllMocks();
7978
useEnterpriseCustomer.mockReturnValue({ data: mockEnterpriseCustomer });
8079
useAlgoliaSearch.mockReturnValue(mockAlgoliaSearch);
8180
useDefaultSearchFilters.mockReturnValue(`enterprise_customer_uuids:${mockEnterpriseCustomer.uuid}`);
81+
setFakeHits(hits);
82+
});
83+
afterEach(() => {
84+
resetMockReactInstantSearch();
8285
});
83-
8486
it('renders skills quiz V1 page successfully.', () => {
8587
renderWithRouter(
8688
<SkillsQuizWithContext />,

0 commit comments

Comments
 (0)