From c59805c0d0f2ee482b79fe8ea8cc99a1aaeacd11 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Mon, 12 Aug 2024 06:36:36 -0400 Subject: [PATCH] test(util): extract global.random to util (#1458) --- src/__mocks__/utils.ts | 7 +++++++ src/components/AccountNotifications.test.tsx | 5 ++--- src/components/AllRead.test.tsx | 5 ++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/__mocks__/utils.ts b/src/__mocks__/utils.ts index c4c0c7561..cf1c57cb9 100644 --- a/src/__mocks__/utils.ts +++ b/src/__mocks__/utils.ts @@ -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); +} diff --git a/src/components/AccountNotifications.test.tsx b/src/components/AccountNotifications.test.tsx index 7df675406..d0b3556f4 100644 --- a/src/components/AccountNotifications.test.tsx +++ b/src/components/AccountNotifications.test.tsx @@ -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'; @@ -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', () => { diff --git a/src/components/AllRead.test.tsx b/src/components/AllRead.test.tsx index cd12d4a19..c58becdd6 100644 --- a/src/components/AllRead.test.tsx +++ b/src/components/AllRead.test.tsx @@ -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', () => {