Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/components/AllRead.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ import { render } from '@testing-library/react';
import { AllRead } from './AllRead';

describe('components/AllRead.tsx', () => {
const originalMathRandom = Math.random;

// 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);
});

afterEach(() => {
global.Math.random = originalMathRandom;
});

it('should render itself & its children', () => {
const tree = render(<AllRead />);

Expand Down
4 changes: 2 additions & 2 deletions src/components/RepositoryNotifications.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe('components/Repository.tsx', () => {
repoNotifications: mockGitHubNotifications,
};

beforeEach(() => {
markRepoNotificationsRead.mockReset();
afterEach(() => {
jest.clearAllMocks();
});

it('should render itself & its children', () => {
Expand Down
257 changes: 128 additions & 129 deletions src/components/Sidebar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ jest.mock('react-router-dom', () => ({

describe('components/Sidebar.tsx', () => {
const fetchNotifications = jest.fn();

beforeEach(() => {
fetchNotifications.mockReset();

jest.spyOn(window, 'clearInterval');
});
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

afterEach(() => {
jest.clearAllMocks();
Expand All @@ -39,6 +34,7 @@ describe('components/Sidebar.tsx', () => {
</MemoryRouter>
</AppContext.Provider>,
);

expect(tree).toMatchSnapshot();
});

Expand All @@ -52,9 +48,127 @@ describe('components/Sidebar.tsx', () => {
</MemoryRouter>
</AppContext.Provider>,
);

expect(tree).toMatchSnapshot();
});

it('should open the gitify repository', () => {
render(
<AppContext.Provider value={{ isLoggedIn: false, notifications: [] }}>
<MemoryRouter>
<Sidebar />
</MemoryRouter>
</AppContext.Provider>,
);

fireEvent.click(screen.getByTestId('gitify-logo'));

expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/gitify-app/gitify',
);
});

describe('quick links', () => {
describe('notifications icon', () => {
it('when there are 0 notifications', () => {
render(
<AppContext.Provider value={{ isLoggedIn: true, notifications: [] }}>
<MemoryRouter>
<Sidebar />
</MemoryRouter>
</AppContext.Provider>,
);

const notificationsIcon = screen.getByTitle('0 Unread Notifications');

expect(notificationsIcon.className).toContain('text-white');
expect(notificationsIcon.childNodes.length).toBe(1);
expect(notificationsIcon.childNodes[0].nodeName).toBe('svg');

fireEvent.click(screen.getByLabelText('0 Unread Notifications'));

expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/notifications',
);
});

it('when there are more than 0 notifications', () => {
render(
<AppContext.Provider
value={{
isLoggedIn: true,
notifications: mockAccountNotifications,
}}
>
<MemoryRouter>
<Sidebar />
</MemoryRouter>
</AppContext.Provider>,
);

const notificationsIcon = screen.getByTitle('4 Unread Notifications');

expect(notificationsIcon.className).toContain(IconColor.GREEN);
expect(notificationsIcon.childNodes.length).toBe(2);
expect(notificationsIcon.childNodes[0].nodeName).toBe('svg');
expect(notificationsIcon.childNodes[1].nodeValue).toBe('4');

fireEvent.click(screen.getByLabelText('4 Unread Notifications'));

expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/notifications',
);
});
});
});

it('opens my github issues page', () => {
render(
<AppContext.Provider
value={{
isLoggedIn: true,
notifications: mockAccountNotifications,
}}
>
<MemoryRouter>
<Sidebar />
</MemoryRouter>
</AppContext.Provider>,
);

fireEvent.click(screen.getByLabelText('My Issues'));

expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/issues',
);
});

it('opens my github pull requests page', () => {
render(
<AppContext.Provider
value={{
isLoggedIn: true,
notifications: mockAccountNotifications,
}}
>
<MemoryRouter>
<Sidebar />
</MemoryRouter>
</AppContext.Provider>,
);

fireEvent.click(screen.getByLabelText('My Pull Requests'));

expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/pulls',
);
});

describe('Refresh Notifications', () => {
it('should refresh the notifications when status is not loading', () => {
render(
Expand All @@ -71,7 +185,7 @@ describe('components/Sidebar.tsx', () => {
</MemoryRouter>
</AppContext.Provider>,
);
fetchNotifications.mockReset();

fireEvent.click(screen.getByTitle('Refresh Notifications'));

expect(fetchNotifications).toHaveBeenCalledTimes(1);
Expand All @@ -92,7 +206,7 @@ describe('components/Sidebar.tsx', () => {
</MemoryRouter>
</AppContext.Provider>,
);
fetchNotifications.mockReset();

fireEvent.click(screen.getByTitle('Refresh Notifications'));

expect(fetchNotifications).not.toHaveBeenCalled();
Expand All @@ -108,7 +222,9 @@ describe('components/Sidebar.tsx', () => {
</MemoryRouter>
</AppContext.Provider>,
);

fireEvent.click(screen.getByTitle('Settings'));

expect(mockNavigate).toHaveBeenCalledWith('/settings');
});

Expand All @@ -120,77 +236,13 @@ describe('components/Sidebar.tsx', () => {
</MemoryRouter>
</AppContext.Provider>,
);

fireEvent.click(screen.getByTitle('Settings'));

expect(mockNavigate).toHaveBeenCalledWith('/', { replace: true });
});
});

it('opens github in the notifications page', () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

render(
<AppContext.Provider
value={{
isLoggedIn: true,
notifications: mockAccountNotifications,
}}
>
<MemoryRouter>
<Sidebar />
</MemoryRouter>
</AppContext.Provider>,
);
fireEvent.click(screen.getByLabelText('4 Unread Notifications'));
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/notifications',
);
});

it('opens my github issues page', () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

render(
<AppContext.Provider
value={{
isLoggedIn: true,
notifications: mockAccountNotifications,
}}
>
<MemoryRouter>
<Sidebar />
</MemoryRouter>
</AppContext.Provider>,
);
fireEvent.click(screen.getByLabelText('My Issues'));
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/issues',
);
});

it('opens my github pull requests page', () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

render(
<AppContext.Provider
value={{
isLoggedIn: true,
notifications: mockAccountNotifications,
}}
>
<MemoryRouter>
<Sidebar />
</MemoryRouter>
</AppContext.Provider>,
);
fireEvent.click(screen.getByLabelText('My Pull Requests'));
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/pulls',
);
});

it('should quit the app', () => {
const quitAppMock = jest.spyOn(comms, 'quitApp');

Expand All @@ -201,62 +253,9 @@ describe('components/Sidebar.tsx', () => {
</MemoryRouter>
</AppContext.Provider>,
);
fireEvent.click(screen.getByTitle('Quit Gitify'));
expect(quitAppMock).toHaveBeenCalledTimes(1);
});

it('should open the gitify repository', () => {
const openExternalLinkMock = jest.spyOn(comms, 'openExternalLink');

render(
<AppContext.Provider value={{ isLoggedIn: false, notifications: [] }}>
<MemoryRouter>
<Sidebar />
</MemoryRouter>
</AppContext.Provider>,
);
fireEvent.click(screen.getByTestId('gitify-logo'));
expect(openExternalLinkMock).toHaveBeenCalledTimes(1);
expect(openExternalLinkMock).toHaveBeenCalledWith(
'https://github.com/gitify-app/gitify',
);
});

describe('should render the notifications icon', () => {
it('when there are 0 notifications', () => {
render(
<AppContext.Provider value={{ isLoggedIn: true, notifications: [] }}>
<MemoryRouter>
<Sidebar />
</MemoryRouter>
</AppContext.Provider>,
);

const notificationsIcon = screen.getByTitle('0 Unread Notifications');
expect(notificationsIcon.className).toContain('text-white');
expect(notificationsIcon.childNodes.length).toBe(1);
expect(notificationsIcon.childNodes[0].nodeName).toBe('svg');
});

it('when there are more than 0 notifications', () => {
render(
<AppContext.Provider
value={{
isLoggedIn: true,
notifications: mockAccountNotifications,
}}
>
<MemoryRouter>
<Sidebar />
</MemoryRouter>
</AppContext.Provider>,
);
fireEvent.click(screen.getByTitle('Quit Gitify'));

const notificationsIcon = screen.getByTitle('4 Unread Notifications');
expect(notificationsIcon.className).toContain(IconColor.GREEN);
expect(notificationsIcon.childNodes.length).toBe(2);
expect(notificationsIcon.childNodes[0].nodeName).toBe('svg');
expect(notificationsIcon.childNodes[1].nodeValue).toBe('4');
});
expect(quitAppMock).toHaveBeenCalledTimes(1);
});
});
4 changes: 2 additions & 2 deletions src/components/buttons/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('components/buttons/Button.tsx', () => {
size: Size.MEDIUM,
};

beforeEach(() => {
openExternalMock.mockReset();
afterEach(() => {
jest.clearAllMocks();
});

it('should render without icon', () => {
Expand Down
Loading