Skip to content

Commit c59805c

Browse files
authored
test(util): extract global.random to util (#1458)
1 parent 8149804 commit c59805c

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/__mocks__/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ export function setPlatform(platform: NodeJS.Platform) {
33
value: platform,
44
});
55
}
6+
7+
/**
8+
* Ensure stable snapshots for our randomized emoji use-cases
9+
*/
10+
export function ensureStableEmojis() {
11+
global.Math.random = jest.fn(() => 0.1);
12+
}

src/components/AccountNotifications.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react';
22
import { mockGitHubCloudAccount, mockSettings } from '../__mocks__/state-mocks';
3+
import { ensureStableEmojis } from '../__mocks__/utils';
34
import { AppContext } from '../context/App';
45
import { GroupBy } from '../types';
56
import { mockGitHubNotifications } from '../utils/api/__mocks__/response-mocks';
@@ -11,10 +12,8 @@ jest.mock('./RepositoryNotifications', () => ({
1112
}));
1213

1314
describe('components/AccountNotifications.tsx', () => {
14-
// The read emoji randomly rotates, but then the snapshots would never match
15-
// Have to make it consistent so the emojis are always the same
1615
beforeEach(() => {
17-
global.Math.random = jest.fn(() => 0.1);
16+
ensureStableEmojis();
1817
});
1918

2019
it('should render itself - group notifications by repositories', () => {

src/components/AllRead.test.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { render } from '@testing-library/react';
2+
import { ensureStableEmojis } from '../__mocks__/utils';
23
import { AllRead } from './AllRead';
34

45
describe('components/AllRead.tsx', () => {
5-
// The read emoji randomly rotates, but then the snapshots would never match
6-
// Have to make it consistent so the emojis are always the same
76
beforeEach(() => {
8-
global.Math.random = jest.fn(() => 0.1);
7+
ensureStableEmojis();
98
});
109

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

0 commit comments

Comments
 (0)