Skip to content

Commit f78359a

Browse files
authored
Merge pull request #99 from open-craft/farhaan/moe-fix-test
Fix: Fixing tests for backport
2 parents e1cb877 + e765fe5 commit f78359a

3 files changed

Lines changed: 176 additions & 88 deletions

File tree

src/optimizer-page/CourseOptimizerPage.test.js

Lines changed: 114 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ const OptimizerPage = () => (
3737
</AppProvider>
3838
);
3939

40+
const setupOptimizerPage = async () => {
41+
axiosMock.onGet(getLinkCheckStatusApiUrl(courseId)).reply(200, mockApiResponse);
42+
const optimizerPage = render(<OptimizerPage />);
43+
44+
// Click the scan button
45+
fireEvent.click(optimizerPage.getByText(messages.buttonTitle.defaultMessage));
46+
47+
// Wait for the scan results to load
48+
await waitFor(() => {
49+
expect(optimizerPage.getByText('Introduction to Programming')).toBeInTheDocument();
50+
});
51+
52+
// Click on filters button
53+
fireEvent.click(optimizerPage.getByText(scanResultsMessages.filterButtonLabel.defaultMessage));
54+
55+
return optimizerPage;
56+
};
57+
4058
describe('CourseOptimizerPage', () => {
4159
describe('pollLinkCheckDuringScan', () => {
4260
let mockFetchLinkCheckStatus;
@@ -71,6 +89,7 @@ describe('CourseOptimizerPage', () => {
7189
pollLinkCheckDuringScan(linkCheckInProgress, interval, dispatch, courseId);
7290
expect(interval.current).toBeTruthy();
7391
});
92+
7493
it('should not start polling if link check is not in progress', () => {
7594
const linkCheckInProgress = false;
7695
const interval = { current: null };
@@ -79,6 +98,7 @@ describe('CourseOptimizerPage', () => {
7998
pollLinkCheckDuringScan(linkCheckInProgress, interval, dispatch, courseId);
8099
expect(interval.current).toBeFalsy();
81100
});
101+
82102
it('should clear the interval if link check is finished', () => {
83103
const linkCheckInProgress = false;
84104
const interval = { current: 1 };
@@ -127,74 +147,124 @@ describe('CourseOptimizerPage', () => {
127147
});
128148
});
129149

130-
it('should list broken links results', async () => {
131-
const {
132-
getByText, queryAllByText, getAllByText, container,
133-
} = render(<OptimizerPage />);
150+
it('should show no broken links found message', async () => {
151+
axiosMock
152+
.onGet(getLinkCheckStatusApiUrl(courseId))
153+
.reply(200, { LinkCheckStatus: 'Succeeded' });
154+
const { getByText } = render(<OptimizerPage />);
134155
expect(getByText(messages.headingTitle.defaultMessage)).toBeInTheDocument();
135156
fireEvent.click(getByText(messages.buttonTitle.defaultMessage));
136157
await waitFor(() => {
137-
expect(getByText('5 broken links')).toBeInTheDocument();
138-
expect(getByText('5 locked links')).toBeInTheDocument();
158+
expect(getByText(scanResultsMessages.noBrokenLinksCard.defaultMessage)).toBeInTheDocument();
139159
});
160+
});
161+
162+
it('should show error message if request does not go through', async () => {
163+
axiosMock
164+
.onPost(postLinkCheckCourseApiUrl(courseId))
165+
.reply(500);
166+
render(<OptimizerPage />);
167+
expect(screen.getByText(messages.headingTitle.defaultMessage)).toBeInTheDocument();
168+
fireEvent.click(screen.getByText(messages.buttonTitle.defaultMessage));
169+
await waitFor(() => {
170+
expect(screen.getByText(generalMessages.supportText.defaultMessage)).toBeInTheDocument();
171+
});
172+
});
173+
174+
it('should show only broken links when brokenLinks filter is selected', async () => {
175+
const {
176+
getByText,
177+
getByLabelText,
178+
queryByText,
179+
container,
180+
} = await setupOptimizerPage();
181+
// Check if the modal is opened
182+
expect(getByText('Broken')).toBeInTheDocument();
183+
// Select the broken links checkbox
184+
fireEvent.click(getByLabelText(scanResultsMessages.brokenLabel.defaultMessage));
185+
140186
const collapsibleTrigger = container.querySelector('.collapsible-trigger');
141187
expect(collapsibleTrigger).toBeInTheDocument();
142188
fireEvent.click(collapsibleTrigger);
189+
143190
await waitFor(() => {
144-
expect(getAllByText(scanResultsMessages.brokenLinkStatus.defaultMessage)[0]).toBeInTheDocument();
145-
expect(queryAllByText(scanResultsMessages.lockedLinkStatus.defaultMessage)[0]).toBeInTheDocument();
146-
expect(queryAllByText(scanResultsMessages.recommendedManualCheckText.defaultMessage)[0]).toBeInTheDocument();
147-
const brokenLinks = getAllByText('https://example.com/broken-link-algo');
148-
expect(brokenLinks.length).toBeGreaterThan(0);
149-
fireEvent.click(brokenLinks[0]);
150-
const lockedLinks = getAllByText('https://example.com/locked-link-algo');
151-
expect(lockedLinks.length).toBeGreaterThan(0);
152-
fireEvent.click(lockedLinks[0]);
153-
fireEvent.click((getAllByText('Go to Block'))[0]);
191+
expect(getByText('Test Broken Links')).toBeInTheDocument();
192+
expect(queryByText('Test Locked Links')).not.toBeInTheDocument();
193+
expect(queryByText('Test Manual Links')).not.toBeInTheDocument();
154194
});
155195
});
156196

157-
it('should not list locked links results when show locked links is unchecked', async () => {
197+
it('should show only manual links when manualLinks filter is selected', async () => {
158198
const {
159-
getByText, getAllByText, getByLabelText, queryAllByText, queryByText, container,
160-
} = render(<OptimizerPage />);
161-
expect(getByText(messages.headingTitle.defaultMessage)).toBeInTheDocument();
162-
fireEvent.click(getByText(messages.buttonTitle.defaultMessage));
163-
await waitFor(() => {
164-
expect(getByText('5 broken links')).toBeInTheDocument();
165-
});
166-
fireEvent.click(getByLabelText(scanResultsMessages.lockedCheckboxLabel.defaultMessage));
199+
getByText,
200+
getByLabelText,
201+
queryByText,
202+
container,
203+
} = await setupOptimizerPage();
204+
// Check if the modal is opened
205+
expect(getByText('Manual')).toBeInTheDocument();
206+
// Select the manual links checkbox
207+
fireEvent.click(getByLabelText(scanResultsMessages.manualLabel.defaultMessage));
208+
167209
const collapsibleTrigger = container.querySelector('.collapsible-trigger');
168210
expect(collapsibleTrigger).toBeInTheDocument();
169211
fireEvent.click(collapsibleTrigger);
212+
170213
await waitFor(() => {
171-
expect(queryByText('5 locked links')).not.toBeInTheDocument();
172-
expect(getAllByText(scanResultsMessages.brokenLinkStatus.defaultMessage)[0]).toBeInTheDocument();
173-
expect(queryAllByText(scanResultsMessages.lockedLinkStatus.defaultMessage)?.[0]).toBeUndefined();
214+
expect(getByText('Test Manual Links')).toBeInTheDocument();
215+
expect(queryByText('Test Broken Links')).not.toBeInTheDocument();
216+
expect(queryByText('Test Locked Links')).not.toBeInTheDocument();
174217
});
175218
});
176219

177-
it('should show no broken links found message', async () => {
178-
axiosMock
179-
.onGet(getLinkCheckStatusApiUrl(courseId))
180-
.reply(200, { LinkCheckStatus: 'Succeeded' });
181-
const { getByText } = render(<OptimizerPage />);
182-
expect(getByText(messages.headingTitle.defaultMessage)).toBeInTheDocument();
183-
fireEvent.click(getByText(messages.buttonTitle.defaultMessage));
220+
it('should show only manual & locked links when manual & locked Links filters are selected, ignore broken links', async () => {
221+
const {
222+
getByText,
223+
getByLabelText,
224+
queryByText,
225+
container,
226+
} = await setupOptimizerPage();
227+
// Check if the modal is opened
228+
expect(getByText('Manual')).toBeInTheDocument();
229+
expect(getByText('Locked')).toBeInTheDocument();
230+
// Select the manual & locked links checkbox
231+
fireEvent.click(getByLabelText(scanResultsMessages.manualLabel.defaultMessage));
232+
fireEvent.click(getByLabelText(scanResultsMessages.lockedLabel.defaultMessage));
233+
234+
const collapsibleTrigger = container.querySelector('.collapsible-trigger');
235+
expect(collapsibleTrigger).toBeInTheDocument();
236+
fireEvent.click(collapsibleTrigger);
237+
184238
await waitFor(() => {
185-
expect(getByText(scanResultsMessages.noBrokenLinksCard.defaultMessage)).toBeInTheDocument();
239+
expect(getByText('Test Manual Links')).toBeInTheDocument();
240+
expect(getByText('Test Locked Links')).toBeInTheDocument();
241+
expect(queryByText('Test Broken Links')).not.toBeInTheDocument();
186242
});
187243
});
188244

189-
it('should show error message if request does not go through', async () => {
190-
axiosMock
191-
.onPost(postLinkCheckCourseApiUrl(courseId))
192-
.reply(500);
193-
render(<OptimizerPage />);
194-
expect(screen.getByText(messages.headingTitle.defaultMessage)).toBeInTheDocument();
195-
fireEvent.click(screen.getByText(messages.buttonTitle.defaultMessage));
245+
it('should show all links when all filters are selected', async () => {
246+
const {
247+
getByText,
248+
getByLabelText,
249+
container,
250+
} = await setupOptimizerPage();
251+
// Check if the modal is opened
252+
expect(getByText('Broken')).toBeInTheDocument();
253+
expect(getByText('Manual')).toBeInTheDocument();
254+
expect(getByText('Locked')).toBeInTheDocument();
255+
// Select the all checkboxes
256+
fireEvent.click(getByLabelText(scanResultsMessages.brokenLabel.defaultMessage));
257+
fireEvent.click(getByLabelText(scanResultsMessages.lockedLabel.defaultMessage));
258+
fireEvent.click(getByLabelText(scanResultsMessages.manualLabel.defaultMessage));
259+
260+
const collapsibleTrigger = container.querySelector('.collapsible-trigger');
261+
expect(collapsibleTrigger).toBeInTheDocument();
262+
fireEvent.click(collapsibleTrigger);
263+
196264
await waitFor(() => {
197-
expect(screen.getByText(generalMessages.supportText.defaultMessage)).toBeInTheDocument();
265+
expect(getByText('Test Broken Links')).toBeInTheDocument();
266+
expect(getByText('Test Manual Links')).toBeInTheDocument();
267+
expect(getByText('Test Locked Links')).toBeInTheDocument();
198268
});
199269
});
200270
});

src/optimizer-page/mocks/mockApiResponse.js

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,85 +13,103 @@ const mockApiResponse = {
1313
units: [
1414
{
1515
id: 'unit-1-1-1',
16-
displayName: 'Welcome Video',
16+
displayName: 'Test Broken Links',
1717
blocks: [
1818
{
19-
id: 'block-1-1-1-1',
19+
id: 'block-1-1-1-5',
2020
url: 'https://example.com/welcome-video',
21-
brokenLinks: ['https://example.com/broken-link-algo'],
22-
lockedLinks: ['https://example.com/locked-link-algo'],
21+
brokenLinks: ['https://example.com/broken-link-algo1'],
22+
lockedLinks: [],
23+
externalForbiddenLinks: [],
2324
},
25+
],
26+
},
27+
{
28+
id: 'unit-1-1-3',
29+
displayName: 'Test Manual Links',
30+
blocks: [
2431
{
25-
id: 'block-1-1-1-2',
26-
url: 'https://example.com/intro-guide',
27-
brokenLinks: ['https://example.com/broken-link-algo'],
28-
lockedLinks: ['https://example.com/locked-link-algo'],
32+
id: 'block-1-1-1-1',
33+
url: 'https://example.com/welcome-video',
34+
brokenLinks: [],
35+
lockedLinks: [],
2936
externalForbiddenLinks: ['https://outsider.com/forbidden-link-algo'],
3037
},
3138
],
3239
},
3340
{
3441
id: 'unit-1-1-2',
35-
displayName: 'Course Overview',
42+
displayName: 'Test Locked Links',
3643
blocks: [
3744
{
3845
id: 'block-1-1-2-1',
3946
url: 'https://example.com/course-overview',
40-
brokenLinks: ['https://example.com/broken-link-algo'],
47+
brokenLinks: [],
4148
lockedLinks: ['https://example.com/locked-link-algo'],
49+
externalForbiddenLinks: [],
4250
},
4351
],
4452
},
4553
],
4654
},
55+
],
56+
},
57+
{
58+
id: 'section-2',
59+
displayName: 'Introduction to Programming 2',
60+
subsections: [
4761
{
4862
id: 'subsection-1-2',
49-
displayName: 'Basic Concepts',
63+
displayName: 'Getting Started 2',
5064
units: [
5165
{
52-
id: 'unit-1-2-1',
53-
displayName: 'Variables and Data Types',
66+
id: 'unit-2-2-2',
67+
displayName: 'unit',
5468
blocks: [
5569
{
56-
id: 'block-1-2-1-1',
57-
url: 'https://example.com/variables',
58-
brokenLinks: ['https://example.com/broken-link-algo'],
59-
lockedLinks: ['https://example.com/locked-link-algo'],
70+
id: 'block-1-1-1-6',
71+
url: 'https://example.com/welcome-video',
72+
brokenLinks: ['https://example.com/broken-link-algo1'],
73+
lockedLinks: [],
74+
externalForbiddenLinks: [],
6075
},
6176
{
62-
id: 'block-1-2-1-2',
63-
url: 'https://example.com/broken-link',
64-
brokenLinks: ['https://example.com/broken-link'],
77+
id: 'block-1-1-1-6',
78+
url: 'https://example.com/welcome-video',
79+
brokenLinks: ['https://example.com/broken-link-algo1'],
6580
lockedLinks: ['https://example.com/locked-link-algo'],
81+
externalForbiddenLinks: [],
82+
},
83+
{
84+
id: 'block-1-1-1-6',
85+
url: 'https://example.com/welcome-video',
86+
brokenLinks: ['https://example.com/broken-link-algo1'],
87+
lockedLinks: [],
88+
externalForbiddenLinks: ['https://outsider.com/forbidden-link-algo'],
6689
},
6790
],
6891
},
69-
],
70-
},
71-
],
72-
},
73-
{
74-
id: 'section-2',
75-
displayName: 'Advanced Topics',
76-
subsections: [
77-
{
78-
id: 'subsection-2-1',
79-
displayName: 'Algorithms and Data Structures',
80-
units: [
8192
{
82-
id: 'unit-2-1-1',
83-
displayName: 'Sorting Algorithms',
93+
id: 'unit-2-2-3',
94+
displayName: 'unit',
8495
blocks: [
8596
{
86-
id: 'block-2-1-1-1',
87-
url: 'https://example.com/sorting-algorithms',
88-
brokenLinks: ['https://example.com/broken-link-algo'],
89-
lockedLinks: ['https://example.com/locked-link-algo'],
97+
id: 'block-1-1-1-7',
98+
url: 'https://example.com/welcome-video',
99+
brokenLinks: ['https://example.com/broken-link-algo1'],
100+
lockedLinks: [],
101+
externalForbiddenLinks: ['https://outsider.com/forbidden-link-algo'],
90102
},
103+
],
104+
},
105+
{
106+
id: 'unit-2-2-4',
107+
displayName: 'Test Locked Links',
108+
blocks: [
91109
{
92-
id: 'block-2-1-1-2',
93-
url: 'https://example.com/broken-link-algo',
94-
brokenLinks: ['https://example.com/broken-link-algo'],
110+
id: 'block-1-1-7-1',
111+
url: 'https://example.com/course-overview',
112+
brokenLinks: ['https://example.com/broken-link-algo1'],
95113
lockedLinks: ['https://example.com/locked-link-algo'],
96114
externalForbiddenLinks: ['https://outsider.com/forbidden-link-algo'],
97115
},

src/optimizer-page/utils.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ describe('countBrokenLinks', () => {
66
const data = mockApiResponse.LinkCheckOutput;
77
expect(countBrokenLinks(data)).toStrictEqual(
88
{
9-
brokenLinksCounts: [5, 2],
10-
lockedLinksCounts: [5, 2],
11-
externalForbiddenLinksCounts: [1, 1],
9+
brokenLinksCounts: [1, 5],
10+
lockedLinksCounts: [1, 2],
11+
externalForbiddenLinksCounts: [1, 3],
1212
},
1313
);
1414
});

0 commit comments

Comments
 (0)