Skip to content

Commit 31caa6a

Browse files
Add test cases for SimilarProjects component (#5981)
1 parent 8a95c01 commit 31caa6a

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import '@testing-library/jest-dom';
2+
import { screen } from '@testing-library/react';
3+
4+
import { SimilarProjects } from '../similarProjects';
5+
import { ReduxIntlProviders, renderWithRouter } from '../../../utils/testWithIntl';
6+
import { setupFaultyHandlers } from '../../../network/tests/server';
7+
import messages from '../messages';
8+
9+
describe('Similar Projects', () => {
10+
it('should fetch and display similar projects', async () => {
11+
renderWithRouter(
12+
<ReduxIntlProviders>
13+
<SimilarProjects projectId={123} />
14+
</ReduxIntlProviders>,
15+
);
16+
expect((await screen.findAllByRole('article')).length).toBe(4);
17+
expect(
18+
screen.getByRole('heading', {
19+
name: 'Similar Project 1',
20+
}),
21+
).toBeInTheDocument();
22+
});
23+
24+
it('should display error message', async () => {
25+
setupFaultyHandlers();
26+
renderWithRouter(
27+
<ReduxIntlProviders>
28+
<SimilarProjects projectId={123} />
29+
</ReduxIntlProviders>,
30+
);
31+
expect(
32+
await screen.findByText(messages.noSimilarProjectsFound.defaultMessage),
33+
).toBeInTheDocument();
34+
});
35+
});

frontend/src/network/tests/mockData/projects.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,86 @@ export const stopValidation = {
482482
},
483483
],
484484
};
485+
486+
export const similarProjects = {
487+
mapResults: [],
488+
results: [
489+
{
490+
projectId: 17,
491+
locale: 'en',
492+
name: 'Similar Project 1',
493+
shortDescription: 'asdasdasd',
494+
difficulty: 'MODERATE',
495+
priority: 'MEDIUM',
496+
organisationName: 'asdasdsadasdasd,ll,ll',
497+
organisationLogo: null,
498+
campaigns: [],
499+
percentMapped: 6,
500+
percentValidated: 0,
501+
status: 'PUBLISHED',
502+
activeMappers: 0,
503+
lastUpdated: '2023-06-28T10:45:01.841598Z',
504+
dueDate: null,
505+
totalContributors: 2,
506+
country: [],
507+
},
508+
{
509+
projectId: 14,
510+
locale: 'en',
511+
name: 'Similar Project 2',
512+
shortDescription: 'asdasd',
513+
difficulty: 'MODERATE',
514+
priority: 'MEDIUM',
515+
organisationName: 'MÉDECINS SANS FRONTIÈRES (MSF)',
516+
organisationLogo: null,
517+
campaigns: [],
518+
percentMapped: 0,
519+
percentValidated: 0,
520+
status: 'PUBLISHED',
521+
activeMappers: 0,
522+
lastUpdated: '2023-06-21T04:37:03.788468Z',
523+
dueDate: null,
524+
totalContributors: 1,
525+
country: ['Bolivia'],
526+
},
527+
{
528+
projectId: 16,
529+
locale: 'en',
530+
name: 'Newchi',
531+
shortDescription: 'asdasd',
532+
difficulty: 'EASY',
533+
priority: 'MEDIUM',
534+
organisationName: 'asdasdasdasdsssssss',
535+
organisationLogo: null,
536+
campaigns: [],
537+
percentMapped: 100,
538+
percentValidated: 0,
539+
status: 'PUBLISHED',
540+
activeMappers: 0,
541+
lastUpdated: '2023-06-26T11:02:31.390857Z',
542+
dueDate: null,
543+
totalContributors: 1,
544+
country: ['Brazil'],
545+
},
546+
{
547+
projectId: 18,
548+
locale: 'en',
549+
name: 'asdasdasd',
550+
shortDescription: 'asdasd',
551+
difficulty: 'MODERATE',
552+
priority: 'MEDIUM',
553+
organisationName: 'asdasdsadasdasd,ll,ll',
554+
organisationLogo: null,
555+
campaigns: [],
556+
percentMapped: 0,
557+
percentValidated: 0,
558+
status: 'PUBLISHED',
559+
activeMappers: 0,
560+
lastUpdated: '2023-07-06T06:22:12.105538Z',
561+
dueDate: null,
562+
totalContributors: 1,
563+
country: ['Peru'],
564+
},
565+
],
566+
pagination: null,
567+
};

frontend/src/network/tests/server-handlers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
taskDetail,
1414
stopMapping,
1515
stopValidation,
16+
similarProjects,
1617
} from './mockData/projects';
1718
import { featuredProjects } from './mockData/featuredProjects';
1819
import {
@@ -143,6 +144,9 @@ const handlers = [
143144
return res(ctx.json(stopValidation));
144145
},
145146
),
147+
rest.get(API_URL + 'projects/queries/:projectId/similar-projects/', async (req, res, ctx) => {
148+
return res(ctx.json(similarProjects));
149+
}),
146150
// AUTHENTICATION
147151
rest.get(API_URL + 'system/authentication/login/', async (req, res, ctx) => {
148152
return res(ctx.json(authLogin));
@@ -384,6 +388,7 @@ const faultyHandlers = [
384388
),
385389
rest.post(API_URL + 'projects/:projectId/tasks/actions/extend/', failedToConnectError),
386390
rest.post(API_URL + 'projects/:projectId/tasks/actions/split/:taskId/', failedToConnectError),
391+
rest.get(API_URL + 'projects/queries/:projectId/similar-projects/', failedToConnectError),
387392
rest.post(API_URL + 'licenses', failedToConnectError),
388393
rest.patch(API_URL + 'licenses/:id', failedToConnectError),
389394
rest.post(API_URL + 'interests', failedToConnectError),

0 commit comments

Comments
 (0)