Skip to content

Commit

Permalink
Correctly mock the holiday function
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptex committed Oct 31, 2024
1 parent 289e2bf commit 939208e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
18 changes: 1 addition & 17 deletions src/pages/stats/github.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Button, GithubSkyline, Layout, Loader, Section, StatsEntry, StatsError,
import { Routes } from '@data/routes';
import { getData, queryGithub } from '@lib/mongodb';
import { getFundingFromCMS, getGithubSkylineFromCMS, getPartnersFromCMS, GithubSkylineData } from '@scripts/cms';
import { formatDate } from '@scripts/shared';
import { formatDate, getHoliday } from '@scripts/shared';
import { addTitles, GeneralInsight, sectionStatsProps } from '@scripts/stats';
import type { GithubInsights, GithubProfileData, GithubRepository, GithubStatsPageData } from '@scripts/types';

Expand Down Expand Up @@ -96,22 +96,6 @@ const registerMutationObserver = (element: HTMLDivElement | null): MutationObser
}
});

const getHoliday = () => {
const today = new Date();
const day = today.getDate();
const month = today.getMonth();

if (month === 9 && day === 31) {
return 'halloween';
}

if (month === 11 && day >= 24 && day <= 26) {
return 'winter';
}

return undefined;
};

type Props = {
data: GithubProfileData;
error: boolean;
Expand Down
16 changes: 16 additions & 0 deletions src/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,19 @@ export const useCurrentPageParam = (): number => {

return page;
};

export const getHoliday = () => {
const today = new Date();
const day = today.getDate();
const month = today.getMonth();

if (month === 9 && day === 31) {
return 'halloween';
}

if (month === 11 && day >= 24 && day <= 26) {
return 'winter';
}

return undefined;
};
1 change: 0 additions & 1 deletion tests/src/pages/stats/__snapshots__/github.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9428,7 +9428,6 @@ exports[`Test the GithubStats page with fake timers 1`] = `
</h3>
<div
class="c-calendar__outer c-calendar--github"
data-holiday="halloween"
>
<div />
</div>
Expand Down
11 changes: 8 additions & 3 deletions tests/src/pages/stats/github.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ import { act } from 'react';

import { GithubStats } from '@pages/stats/github';

import * as shared from '@scripts/shared';
import type { GithubContribution, GithubCount, GithubInsights, GithubProfileData } from '@scripts/types';
import { mockFetch, snapshotTest, test } from '@test-config/helpers';
import { funding, partners } from '@test-config/mocks';
import { onAfterEach, onBeforeEach } from '@test-config/utils';

jest.mock('@lib/mongodb', () => ({
getData: jest.fn(() => Promise.resolve({ props: { data: [] } }))
}));

beforeEach(onBeforeEach);
afterEach(onAfterEach);
jest.spyOn(shared, 'getHoliday');

(shared.getHoliday as jest.Mock).mockImplementation(() => undefined);

afterAll(() => {
jest.clearAllMocks();
});

mockFetch<GithubProfileData>({
days: [
Expand Down

0 comments on commit 939208e

Please sign in to comment.