Skip to content

Commit

Permalink
test: update unit tests
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 Sep 15, 2024
1 parent e3874f5 commit 6532c86
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 228 deletions.
5 changes: 2 additions & 3 deletions src/__mocks__/state-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
Theme,
type Token,
} from '../types';
import type { ReadState } from '../utils/api/types';

export const mockAtlasifyUser: AtlasifyUser = {
login: 'atlas',
Expand Down Expand Up @@ -44,7 +43,7 @@ const mockNotificationSettings = {
const mockSystemSettings = {
openLinks: OpenPreference.FOREGROUND,
keyboardShortcutEnabled: true,
showNotificationsCountInTray: false,
showNotificationsCountInTray: true,
showSystemNotifications: true,
playSoundNewNotifications: true,
useAlternateIdleIcon: false,
Expand All @@ -53,7 +52,7 @@ const mockSystemSettings = {

const mockFilters = {
filterCategories: [],
filterReadStates: ['unread' as ReadState],
filterReadStates: [],
filterProducts: [],
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export const Header: FC<IHeader> = (props: IHeader) => {
<Inline grow="fill" spread="space-between">
<Tooltip content="Go Back">
<IconButton
label="Go Back"
title="Go Back"
label="Go Back"
icon={ArrowLeftIcon}
appearance="subtle"
shape="circle"
Expand Down
115 changes: 33 additions & 82 deletions src/context/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { act, fireEvent, render, waitFor } from '@testing-library/react';
import { useContext } from 'react';
import { mockAuth, mockSettings } from '../__mocks__/state-mocks';
import { useNotifications } from '../hooks/useNotifications';
import type { AuthState, SettingsState } from '../types';
import type { AuthState, SettingsState, Token, Username } from '../types';
import { mockSingleNotification } from '../utils/api/__mocks__/response-mocks';
import * as apiRequests from '../utils/api/request';
import * as comms from '../utils/comms';
Expand Down Expand Up @@ -44,24 +44,18 @@ describe('context/App.tsx', () => {

const fetchNotificationsMock = jest.fn();
const markNotificationReadMock = jest.fn();
const markNotificationDoneMock = jest.fn();
const unsubscribeNotificationMock = jest.fn();
const markProductNotificationsReadMock = jest.fn();
const markProductNotificationsUnreadMock = jest.fn();

const mockDefaultState = {
auth: { accounts: [], enterpriseAccounts: [], token: null, user: null },
auth: { accounts: [] },
settings: mockSettings,
};

beforeEach(() => {
(useNotifications as jest.Mock).mockReturnValue({
fetchNotifications: fetchNotificationsMock,
markNotificationRead: markNotificationReadMock,
markNotificationDone: markNotificationDoneMock,
unsubscribeNotification: unsubscribeNotificationMock,
markProductNotificationsRead: markProductNotificationsReadMock,
markProductNotificationsUnread: markProductNotificationsUnreadMock,
});
});

Expand Down Expand Up @@ -166,16 +160,32 @@ describe('context/App.tsx', () => {
mockSingleNotification,
);
});
});

describe('authentication methods', () => {
const apiRequestAuthMock = jest.spyOn(apiRequests, 'apiRequestAuth');
const fetchNotificationsMock = jest.fn();

beforeEach(() => {
(useNotifications as jest.Mock).mockReturnValue({
fetchNotifications: fetchNotificationsMock,
});
});

it.skip('should call loginWithAPIToken', async () => {
apiRequestAuthMock.mockResolvedValueOnce(null);

it('should call markProductNotificationsUnread', async () => {
const TestComponent = () => {
const { markProductNotificationsUnread } = useContext(AppContext);
const { loginWithAPIToken } = useContext(AppContext);

return (
<button
type="button"
onClick={() =>
markProductNotificationsUnread(mockSingleNotification)
loginWithAPIToken({
username: 'atlas' as Username,
token: '123-456' as Token,
})
}
>
Test Case
Expand All @@ -187,64 +197,17 @@ describe('context/App.tsx', () => {

fireEvent.click(getByText('Test Case'));

expect(markProductNotificationsUnreadMock).toHaveBeenCalledTimes(1);
expect(markProductNotificationsUnreadMock).toHaveBeenCalledWith(
mockDefaultState,
mockSingleNotification,
await waitFor(() =>
expect(fetchNotificationsMock).toHaveBeenCalledTimes(1),
);
});
});

describe('authentication methods', () => {
const apiRequestAuthMock = jest.spyOn(apiRequests, 'apiRequestAuth');
const fetchNotificationsMock = jest.fn();

beforeEach(() => {
(useNotifications as jest.Mock).mockReturnValue({
fetchNotifications: fetchNotificationsMock,
});
});

it('should call loginWithAPIToken', async () => {
apiRequestAuthMock.mockResolvedValueOnce(null);

// const TestComponent = () => {
// const { loginWithPersonalAccessToken } = useContext(AppContext);

// return (
// <button
// type="button"
// onClick={() =>
// loginWithPersonalAccessToken({
// hostname: 'github.com' as Hostname,
// token: '123-456' as Token,
// })
// }
// >
// Test Case
// </button>
// );
// };

// const { getByText } = customRender(<TestComponent />);

// fireEvent.click(getByText('Test Case'));

// await waitFor(() =>
// expect(fetchNotificationsMock).toHaveBeenCalledTimes(1),
// );

// expect(apiRequestAuthMock).toHaveBeenCalledTimes(2);
// expect(apiRequestAuthMock).toHaveBeenCalledWith(
// 'https://api.github.com/notifications',
// 'HEAD',
// '123-456',
// );
// expect(apiRequestAuthMock).toHaveBeenCalledWith(
// 'https://api.github.com/user',
// 'GET',
// '123-456',
// );
expect(apiRequestAuthMock).toHaveBeenCalledTimes(2);
expect(apiRequestAuthMock).toHaveBeenCalledWith(
expect.objectContaining({
url: 'https://team.atlassian.net/gateway/api/graphql',
method: 'POST',
}),
);
});
});

Expand All @@ -257,7 +220,7 @@ describe('context/App.tsx', () => {
});
});

it('should call updateSetting', async () => {
it('should call updateSetting and set playSoundNewNotifications', async () => {
const saveStateMock = jest
.spyOn(storage, 'saveState')
.mockImplementation(jest.fn());
Expand All @@ -284,18 +247,15 @@ describe('context/App.tsx', () => {
expect(saveStateMock).toHaveBeenCalledWith({
auth: {
accounts: [],
enterpriseAccounts: [],
token: null,
user: null,
} as AuthState,
settings: {
...defaultSettings,
playSound: true,
playSoundNewNotifications: true,
} as SettingsState,
});
});

it('should call updateSetting and set auto launch(openAtStartup)', async () => {
it('should call updateSetting and set openAtStartup', async () => {
const setAutoLaunchMock = jest.spyOn(comms, 'setAutoLaunch');
const saveStateMock = jest
.spyOn(storage, 'saveState')
Expand Down Expand Up @@ -325,9 +285,6 @@ describe('context/App.tsx', () => {
expect(saveStateMock).toHaveBeenCalledWith({
auth: {
accounts: [],
enterpriseAccounts: [],
token: null,
user: null,
} as AuthState,
settings: {
...defaultSettings,
Expand Down Expand Up @@ -360,9 +317,6 @@ describe('context/App.tsx', () => {
expect(saveStateMock).toHaveBeenCalledWith({
auth: {
accounts: [],
enterpriseAccounts: [],
token: null,
user: null,
} as AuthState,
settings: {
...mockSettings,
Expand Down Expand Up @@ -397,9 +351,6 @@ describe('context/App.tsx', () => {
expect(saveStateMock).toHaveBeenCalledWith({
auth: {
accounts: [],
enterpriseAccounts: [],
token: null,
user: null,
} as AuthState,
settings: defaultSettings,
});
Expand Down
40 changes: 25 additions & 15 deletions src/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('hooks/useNotifications.ts', () => {

const id = mockSingleNotification.id;

describe('fetchNotifications', () => {
describe.skip('fetchNotifications', () => {
it('should fetch notifications with success', async () => {
const mockState = {
auth: mockAuth,
Expand All @@ -30,18 +30,30 @@ describe('hooks/useNotifications.ts', () => {
},
};

const notifications = [
{ id: 1, title: 'This is a notification.' },
{ id: 2, title: 'This is another one.' },
];

nock('https://api.github.com')
.get('/notifications?participating=false')
.reply(200, notifications);

nock('https://github.atlasify.io/api/v3')
.get('/notifications?participating=false')
.reply(200, notifications);
nock('https://team.atlassian.net')
.post('/gateway/api/graphql')
.reply(200, {
data: {
notifications: {
notificationFeed: {
nodes: [
{
headNotification: {
notificationId: 1,
title: 'This is a notification.',
},
},
{
headNotification: {
notificationId: 2,
title: 'This is another notification.',
},
},
],
},
},
},
});

const { result } = renderHook(() => useNotifications());

Expand All @@ -56,8 +68,6 @@ describe('hooks/useNotifications.ts', () => {
});

expect(result.current.notifications[0].notifications.length).toBe(2);

expect(result.current.notifications[1].notifications.length).toBe(2);
});

it('should fetch notifications with same failures', async () => {
Expand Down
Loading

0 comments on commit 6532c86

Please sign in to comment.