Skip to content

Commit

Permalink
Switched to vi.useFakeTimers() from spy
Browse files Browse the repository at this point in the history
  • Loading branch information
mkimberlin committed Nov 5, 2024
1 parent bedd97f commit 4399622
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
10 changes: 6 additions & 4 deletions web-ui/src/pages/AnniversaryReportPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,28 @@ const userStateWithPermission = {

it('renders correctly', () => {
const mockDate = new Date(2022, 1, 1);
const spy = vi.spyOn(global, 'Date').mockImplementation(() => mockDate);
vi.useFakeTimers();
vi.setSystemTime(mockDate);

snapshot(
<AppContextProvider value={userStateWithPermission}>
<AnniversaryReportPage />
</AppContextProvider>
);

spy.mockRestore();
vi.useRealTimers();
});

it('renders an error if user does not have appropriate permission', () => {
const mockDate = new Date(2022, 1, 1);
const spy = vi.spyOn(global, 'Date').mockImplementation(() => mockDate);
vi.useFakeTimers();
vi.setSystemTime(mockDate);

snapshot(
<AppContextProvider>
<AnniversaryReportPage />
</AppContextProvider>
);

spy.mockRestore();
vi.useRealTimers();
});
11 changes: 7 additions & 4 deletions web-ui/src/pages/BirthdayReportPage.test.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { vi } from 'vitest';
import BirthdayReportPage from './BirthdayReportPage';
import { AppContextProvider } from '../context/AppContext';

Expand All @@ -14,26 +15,28 @@ const userStateWithPermission = {

it('renders correctly', () => {
const mockDate = new Date(2022, 1, 1);
const spy = vi.spyOn(global, 'Date').mockImplementation(() => mockDate);
vi.useFakeTimers();
vi.setSystemTime(mockDate);

snapshot(
<AppContextProvider value={userStateWithPermission}>
<BirthdayReportPage />
</AppContextProvider>
);

spy.mockRestore();
vi.useRealTimers();
});

it('renders an error if user does not have appropriate permission', () => {
const mockDate = new Date(2022, 1, 1);
const spy = vi.spyOn(global, 'Date').mockImplementation(() => mockDate);
vi.useFakeTimers();
vi.setSystemTime(mockDate);

snapshot(
<AppContextProvider>
<BirthdayReportPage />
</AppContextProvider>
);

spy.mockRestore();
vi.useRealTimers();
});

0 comments on commit 4399622

Please sign in to comment.