From bc4fef28b2b43a806b13912f66a96a86da7f84d0 Mon Sep 17 00:00:00 2001
From: Adam Setch <adam.setch@outlook.com>
Date: Sun, 11 Aug 2024 10:22:15 -0400
Subject: [PATCH] test(util): extract random to test util

Signed-off-by: Adam Setch <adam.setch@outlook.com>
---
 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', () => {