Skip to content

Commit fd24b40

Browse files
authored
test: consistent use of jest (#1308)
1 parent 46e8274 commit fd24b40

15 files changed

+154
-170
lines changed

src/components/AllRead.test.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ import { render } from '@testing-library/react';
22
import { AllRead } from './AllRead';
33

44
describe('components/AllRead.tsx', () => {
5-
const originalMathRandom = Math.random;
6-
75
// The read emoji randomly rotates, but then the snapshots would never match
86
// Have to make it consistent so the emojis are always the same
97
beforeEach(() => {
108
global.Math.random = jest.fn(() => 0.1);
119
});
1210

13-
afterEach(() => {
14-
global.Math.random = originalMathRandom;
15-
});
16-
1711
it('should render itself & its children', () => {
1812
const tree = render(<AllRead />);
1913

src/components/RepositoryNotifications.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ describe('components/Repository.tsx', () => {
2323
repoNotifications: mockGitHubNotifications,
2424
};
2525

26-
beforeEach(() => {
27-
markRepoNotificationsRead.mockReset();
26+
afterEach(() => {
27+
jest.clearAllMocks();
2828
});
2929

3030
it('should render itself & its children', () => {

src/components/Sidebar.test.tsx

Lines changed: 128 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@ jest.mock('react-router-dom', () => ({
1515

1616
describe('components/Sidebar.tsx', () => {
1717
const fetchNotifications = jest.fn();
18-
19-
beforeEach(() => {
20-
fetchNotifications.mockReset();
21-
22-
jest.spyOn(window, 'clearInterval');
23-
});
18+
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
2419

2520
afterEach(() => {
2621
jest.clearAllMocks();
@@ -39,6 +34,7 @@ describe('components/Sidebar.tsx', () => {
3934
</MemoryRouter>
4035
</AppContext.Provider>,
4136
);
37+
4238
expect(tree).toMatchSnapshot();
4339
});
4440

@@ -52,9 +48,127 @@ describe('components/Sidebar.tsx', () => {
5248
</MemoryRouter>
5349
</AppContext.Provider>,
5450
);
51+
5552
expect(tree).toMatchSnapshot();
5653
});
5754

55+
it('should open the gitify repository', () => {
56+
render(
57+
<AppContext.Provider value={{ isLoggedIn: false, notifications: [] }}>
58+
<MemoryRouter>
59+
<Sidebar />
60+
</MemoryRouter>
61+
</AppContext.Provider>,
62+
);
63+
64+
fireEvent.click(screen.getByTestId('gitify-logo'));
65+
66+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
67+
expect(openExternalLinkMock).toHaveBeenCalledWith(
68+
'https://github.com/gitify-app/gitify',
69+
);
70+
});
71+
72+
describe('quick links', () => {
73+
describe('notifications icon', () => {
74+
it('when there are 0 notifications', () => {
75+
render(
76+
<AppContext.Provider value={{ isLoggedIn: true, notifications: [] }}>
77+
<MemoryRouter>
78+
<Sidebar />
79+
</MemoryRouter>
80+
</AppContext.Provider>,
81+
);
82+
83+
const notificationsIcon = screen.getByTitle('0 Unread Notifications');
84+
85+
expect(notificationsIcon.className).toContain('text-white');
86+
expect(notificationsIcon.childNodes.length).toBe(1);
87+
expect(notificationsIcon.childNodes[0].nodeName).toBe('svg');
88+
89+
fireEvent.click(screen.getByLabelText('0 Unread Notifications'));
90+
91+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
92+
expect(openExternalLinkMock).toHaveBeenCalledWith(
93+
'https://github.com/notifications',
94+
);
95+
});
96+
97+
it('when there are more than 0 notifications', () => {
98+
render(
99+
<AppContext.Provider
100+
value={{
101+
isLoggedIn: true,
102+
notifications: mockAccountNotifications,
103+
}}
104+
>
105+
<MemoryRouter>
106+
<Sidebar />
107+
</MemoryRouter>
108+
</AppContext.Provider>,
109+
);
110+
111+
const notificationsIcon = screen.getByTitle('4 Unread Notifications');
112+
113+
expect(notificationsIcon.className).toContain(IconColor.GREEN);
114+
expect(notificationsIcon.childNodes.length).toBe(2);
115+
expect(notificationsIcon.childNodes[0].nodeName).toBe('svg');
116+
expect(notificationsIcon.childNodes[1].nodeValue).toBe('4');
117+
118+
fireEvent.click(screen.getByLabelText('4 Unread Notifications'));
119+
120+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
121+
expect(openExternalLinkMock).toHaveBeenCalledWith(
122+
'https://github.com/notifications',
123+
);
124+
});
125+
});
126+
});
127+
128+
it('opens my github issues page', () => {
129+
render(
130+
<AppContext.Provider
131+
value={{
132+
isLoggedIn: true,
133+
notifications: mockAccountNotifications,
134+
}}
135+
>
136+
<MemoryRouter>
137+
<Sidebar />
138+
</MemoryRouter>
139+
</AppContext.Provider>,
140+
);
141+
142+
fireEvent.click(screen.getByLabelText('My Issues'));
143+
144+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
145+
expect(openExternalLinkMock).toHaveBeenCalledWith(
146+
'https://github.com/issues',
147+
);
148+
});
149+
150+
it('opens my github pull requests page', () => {
151+
render(
152+
<AppContext.Provider
153+
value={{
154+
isLoggedIn: true,
155+
notifications: mockAccountNotifications,
156+
}}
157+
>
158+
<MemoryRouter>
159+
<Sidebar />
160+
</MemoryRouter>
161+
</AppContext.Provider>,
162+
);
163+
164+
fireEvent.click(screen.getByLabelText('My Pull Requests'));
165+
166+
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
167+
expect(openExternalLinkMock).toHaveBeenCalledWith(
168+
'https://github.com/pulls',
169+
);
170+
});
171+
58172
describe('Refresh Notifications', () => {
59173
it('should refresh the notifications when status is not loading', () => {
60174
render(
@@ -71,7 +185,7 @@ describe('components/Sidebar.tsx', () => {
71185
</MemoryRouter>
72186
</AppContext.Provider>,
73187
);
74-
fetchNotifications.mockReset();
188+
75189
fireEvent.click(screen.getByTitle('Refresh Notifications'));
76190

77191
expect(fetchNotifications).toHaveBeenCalledTimes(1);
@@ -92,7 +206,7 @@ describe('components/Sidebar.tsx', () => {
92206
</MemoryRouter>
93207
</AppContext.Provider>,
94208
);
95-
fetchNotifications.mockReset();
209+
96210
fireEvent.click(screen.getByTitle('Refresh Notifications'));
97211

98212
expect(fetchNotifications).not.toHaveBeenCalled();
@@ -108,7 +222,9 @@ describe('components/Sidebar.tsx', () => {
108222
</MemoryRouter>
109223
</AppContext.Provider>,
110224
);
225+
111226
fireEvent.click(screen.getByTitle('Settings'));
227+
112228
expect(mockNavigate).toHaveBeenCalledWith('/settings');
113229
});
114230

@@ -120,77 +236,13 @@ describe('components/Sidebar.tsx', () => {
120236
</MemoryRouter>
121237
</AppContext.Provider>,
122238
);
239+
123240
fireEvent.click(screen.getByTitle('Settings'));
241+
124242
expect(mockNavigate).toHaveBeenCalledWith('/', { replace: true });
125243
});
126244
});
127245

128-
it('opens github in the notifications page', () => {
129-
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
130-
131-
render(
132-
<AppContext.Provider
133-
value={{
134-
isLoggedIn: true,
135-
notifications: mockAccountNotifications,
136-
}}
137-
>
138-
<MemoryRouter>
139-
<Sidebar />
140-
</MemoryRouter>
141-
</AppContext.Provider>,
142-
);
143-
fireEvent.click(screen.getByLabelText('4 Unread Notifications'));
144-
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
145-
expect(openExternalLinkMock).toHaveBeenCalledWith(
146-
'https://github.com/notifications',
147-
);
148-
});
149-
150-
it('opens my github issues page', () => {
151-
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
152-
153-
render(
154-
<AppContext.Provider
155-
value={{
156-
isLoggedIn: true,
157-
notifications: mockAccountNotifications,
158-
}}
159-
>
160-
<MemoryRouter>
161-
<Sidebar />
162-
</MemoryRouter>
163-
</AppContext.Provider>,
164-
);
165-
fireEvent.click(screen.getByLabelText('My Issues'));
166-
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
167-
expect(openExternalLinkMock).toHaveBeenCalledWith(
168-
'https://github.com/issues',
169-
);
170-
});
171-
172-
it('opens my github pull requests page', () => {
173-
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
174-
175-
render(
176-
<AppContext.Provider
177-
value={{
178-
isLoggedIn: true,
179-
notifications: mockAccountNotifications,
180-
}}
181-
>
182-
<MemoryRouter>
183-
<Sidebar />
184-
</MemoryRouter>
185-
</AppContext.Provider>,
186-
);
187-
fireEvent.click(screen.getByLabelText('My Pull Requests'));
188-
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
189-
expect(openExternalLinkMock).toHaveBeenCalledWith(
190-
'https://github.com/pulls',
191-
);
192-
});
193-
194246
it('should quit the app', () => {
195247
const quitAppMock = jest.spyOn(comms, 'quitApp');
196248

@@ -201,62 +253,9 @@ describe('components/Sidebar.tsx', () => {
201253
</MemoryRouter>
202254
</AppContext.Provider>,
203255
);
204-
fireEvent.click(screen.getByTitle('Quit Gitify'));
205-
expect(quitAppMock).toHaveBeenCalledTimes(1);
206-
});
207-
208-
it('should open the gitify repository', () => {
209-
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');
210-
211-
render(
212-
<AppContext.Provider value={{ isLoggedIn: false, notifications: [] }}>
213-
<MemoryRouter>
214-
<Sidebar />
215-
</MemoryRouter>
216-
</AppContext.Provider>,
217-
);
218-
fireEvent.click(screen.getByTestId('gitify-logo'));
219-
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
220-
expect(openExternalLinkMock).toHaveBeenCalledWith(
221-
'https://github.com/gitify-app/gitify',
222-
);
223-
});
224-
225-
describe('should render the notifications icon', () => {
226-
it('when there are 0 notifications', () => {
227-
render(
228-
<AppContext.Provider value={{ isLoggedIn: true, notifications: [] }}>
229-
<MemoryRouter>
230-
<Sidebar />
231-
</MemoryRouter>
232-
</AppContext.Provider>,
233-
);
234-
235-
const notificationsIcon = screen.getByTitle('0 Unread Notifications');
236-
expect(notificationsIcon.className).toContain('text-white');
237-
expect(notificationsIcon.childNodes.length).toBe(1);
238-
expect(notificationsIcon.childNodes[0].nodeName).toBe('svg');
239-
});
240256

241-
it('when there are more than 0 notifications', () => {
242-
render(
243-
<AppContext.Provider
244-
value={{
245-
isLoggedIn: true,
246-
notifications: mockAccountNotifications,
247-
}}
248-
>
249-
<MemoryRouter>
250-
<Sidebar />
251-
</MemoryRouter>
252-
</AppContext.Provider>,
253-
);
257+
fireEvent.click(screen.getByTitle('Quit Gitify'));
254258

255-
const notificationsIcon = screen.getByTitle('4 Unread Notifications');
256-
expect(notificationsIcon.className).toContain(IconColor.GREEN);
257-
expect(notificationsIcon.childNodes.length).toBe(2);
258-
expect(notificationsIcon.childNodes[0].nodeName).toBe('svg');
259-
expect(notificationsIcon.childNodes[1].nodeValue).toBe('4');
260-
});
259+
expect(quitAppMock).toHaveBeenCalledTimes(1);
261260
});
262261
});

src/components/buttons/Button.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ describe('components/buttons/Button.tsx', () => {
1313
size: Size.MEDIUM,
1414
};
1515

16-
beforeEach(() => {
17-
openExternalMock.mockReset();
16+
afterEach(() => {
17+
jest.clearAllMocks();
1818
});
1919

2020
it('should render without icon', () => {

0 commit comments

Comments
 (0)