Skip to content

Commit

Permalink
test(util): extract global.random to util (#1458)
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy authored Aug 12, 2024
1 parent 8149804 commit c59805c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/__mocks__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ export function setPlatform(platform: NodeJS.Platform) {
value: platform,
});
}

/**
* Ensure stable snapshots for our randomized emoji use-cases
*/
export function ensureStableEmojis() {
global.Math.random = jest.fn(() => 0.1);
}
5 changes: 2 additions & 3 deletions src/components/AccountNotifications.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { act, fireEvent, render, screen } from '@testing-library/react';
import { mockGitHubCloudAccount, mockSettings } from '../__mocks__/state-mocks';
import { ensureStableEmojis } from '../__mocks__/utils';
import { AppContext } from '../context/App';
import { GroupBy } from '../types';
import { mockGitHubNotifications } from '../utils/api/__mocks__/response-mocks';
Expand All @@ -11,10 +12,8 @@ jest.mock('./RepositoryNotifications', () => ({
}));

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

it('should render itself - group notifications by repositories', () => {
Expand Down
5 changes: 2 additions & 3 deletions src/components/AllRead.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { render } from '@testing-library/react';
import { ensureStableEmojis } from '../__mocks__/utils';
import { AllRead } from './AllRead';

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

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

0 comments on commit c59805c

Please sign in to comment.