-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathChallenges.spec.js
76 lines (67 loc) · 2.21 KB
/
Challenges.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React from 'react';
import Challenges from '../../src/pages/Challenges';
import {
applySelect,
render,
waitForStateUpdate,
waitForUseEffect,
} from '../__utils__/rtl';
import loadChallenges from '../../src/services/loadChallenges';
import Header from '../../src/components/Header';
import capitalize from '../../src/utils/capitalize';
import ChallengeCard from '../../src/components/ChallengeCard';
jest.mock('../../src/services/loadChallenges');
jest.mock('../../src/components/Header');
jest.mock('../../src/utils/capitalize');
jest.mock('../../src/components/ChallengeCard');
describe(Challenges, () => {
let sut;
const mock = {
challenges: [
{ _id: 1, name: 'ch-1' },
{ _id: 2, name: 'ch-2' },
],
filter: {
default: 'All',
type: 'Frontend',
},
location: {
search: 'type=All',
},
};
beforeEach(async () => {
jest.spyOn(window, 'scrollTo').mockImplementation(jest.fn());
capitalize.mockImplementation((word) => word);
ChallengeCard.mockImplementation(({ challenge }) => (
<div>{challenge.name}</div>
));
Header.mockReturnValue(<header />);
loadChallenges.mockResolvedValue({
challenges: mock.challenges,
});
await waitForUseEffect(() => {
sut = render(Challenges, { location: mock.location });
});
});
it('should call api with default value if no option selected', async () => {
expect(loadChallenges).toHaveBeenCalledWith({
typeFilter: mock.filter.default,
});
});
it('should call api with selected value', async () => {
const selector = sut.element.withTestId(
'challenges__type-select-input'
);
await waitForStateUpdate(() => {
applySelect(selector).withOption(mock.filter.type);
});
expect(loadChallenges).toHaveBeenCalledWith({
typeFilter: mock.filter.type,
});
});
it('should render all the challenges', () => {
mock.challenges.forEach((challenge) => {
expect(sut.element.withText(challenge.name)).toBeTruthy();
});
});
});