Skip to content

Commit

Permalink
mock global.__dirname
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Setch <adam.setch@outlook.com>
  • Loading branch information
setchy committed Aug 9, 2024
1 parent 46d6ac9 commit c0cc48c
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/__mocks__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function setPlatform(platform: NodeJS.Platform) {
Object.defineProperty(process, 'platform', {
value: platform,
});
import * as helpers from '../utils/helpers';

export function mockDirectoryPath() {
jest.spyOn(helpers, 'getDirectoryPath').mockReturnValue('/mocked/dir/name');
}
3 changes: 3 additions & 0 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 { mockDirectoryPath } from '../__mocks__/utils';
import { AppContext } from '../context/App';
import { GroupBy } from '../types';
import { mockGitHubNotifications } from '../utils/api/__mocks__/response-mocks';
Expand All @@ -15,6 +16,8 @@ describe('components/AccountNotifications.tsx', () => {
// Have to make it consistent so the emojis are always the same
beforeEach(() => {
global.Math.random = jest.fn(() => 0.1);

mockDirectoryPath();
});

it('should render itself - group notifications by repositories', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/components/AllRead.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { render } from '@testing-library/react';
import { mockDirectoryPath } 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);

mockDirectoryPath();
});

it('should render itself & its children', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/components/Oops.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { render } from '@testing-library/react';
import { mockDirectoryPath } from '../__mocks__/utils';
import { Oops } from './Oops';

describe('components/Oops.tsx', () => {
beforeEach(() => {
mockDirectoryPath();
});

it('should render itself & its children', () => {
const mockError = {
title: 'Error title',
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/components/__snapshots__/AllRead.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/components/__snapshots__/Oops.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/components/icons/Emoji.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { render } from '@testing-library/react';
import { mockDirectoryPath } from '../../__mocks__/utils';
import { Emoji, type IEmoji } from './Emoji';

describe('components/icons/Emoji.tsx', () => {
beforeEach(() => {
mockDirectoryPath();
});

it('should render', () => {
const props: IEmoji = {
emoji: '🍺',
Expand Down
4 changes: 3 additions & 1 deletion src/components/icons/Emoji.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'node:path';
import twemoji from '@discordapp/twemoji';
import type { FC } from 'react';
import { getDirectoryPath } from '../../utils/helpers';

export interface IEmoji {
emoji: string;
Expand All @@ -19,7 +20,8 @@ export const Emoji: FC<IEmoji> = (props: IEmoji) => {
ext: '.svg',
callback: (icon: string, options: TwemojiOptions, _variant: string) => {
const source = path.resolve(
`${__dirname}/../../node_modules/@discordapp/twemoji/dist`,
getDirectoryPath(),
'../../node_modules/@discordapp/twemoji/dist',
);

return ''.concat(source, '/', options.size, '/', icon, options.ext);
Expand Down
4 changes: 2 additions & 2 deletions src/components/icons/__snapshots__/Emoji.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,7 @@ export function getFilterCount(settings: SettingsState): number {

return count;
}

export function getDirectoryPath(): string {
return `${__dirname}`;
}

0 comments on commit c0cc48c

Please sign in to comment.