Skip to content
Draft
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
4 changes: 0 additions & 4 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ const config: Config = {
collectCoverage: true,
collectCoverageFrom: ['src/**/*', '!**/__snapshots__/**'],
moduleNameMapper: {
// Force CommonJS build for http adapter to be available.
// via https://github.com/axios/axios/issues/5101#issuecomment-1276572468
'^axios$': require.resolve('axios'),

// Atlassian Design System - @atlaskit Compiled CSS in JS - https://compiledcssinjs.com/
'\\.compiled.css$': 'identity-obj-proxy',
},
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
"@types/react": "19.2.2",
"@types/react-dom": "19.2.2",
"@types/react-router-dom": "5.3.3",
"axios": "1.12.2",
"clsx": "2.1.1",
"concurrently": "9.2.1",
"copy-webpack-plugin": "13.0.1",
Expand All @@ -120,7 +119,6 @@
"jest": "30.2.0",
"jest-environment-jsdom": "30.2.0",
"mini-css-extract-plugin": "2.9.4",
"nock": "13.5.6",
"postcss": "8.5.6",
"postcss-loader": "8.2.0",
"rimraf": "6.0.1",
Expand Down Expand Up @@ -148,4 +146,4 @@
"*": "biome check --fix --no-errors-on-unmatched",
"*.{js,ts,tsx}": "pnpm test --findRelatedTests --passWithNoTests --updateSnapshot"
}
}
}
54 changes: 33 additions & 21 deletions src/renderer/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { act, renderHook, waitFor } from '@testing-library/react';

import axios from 'axios';
import nock from 'nock';

import { mockSingleAtlassifyNotification } from '../__mocks__/notifications-mocks';
import { mockState } from '../__mocks__/state-mocks';
import { useNotifications } from './useNotifications';

describe('renderer/hooks/useNotifications.ts', () => {
beforeEach(() => {
// axios will default to using the XHR adapter which can't be intercepted
// by nock. So, configure axios to use the node adapter.
axios.defaults.adapter = 'http';
const originalFetch = globalThis.fetch;
afterEach(() => {
jest.clearAllMocks();
globalThis.fetch = originalFetch;
});
beforeEach(() => {});

describe('fetchNotifications', () => {
it('fetchNotifications - unread only', async () => {
nock('https://team.atlassian.net//')
.post('gateway/api/graphql')
.reply(200, {
globalThis.fetch = jest.fn().mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
data: {
notifications: {
notificationFeed: {
Expand All @@ -33,7 +32,8 @@ describe('renderer/hooks/useNotifications.ts', () => {
},
},
},
});
}),
} as unknown as Response);

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

Expand All @@ -58,9 +58,10 @@ describe('renderer/hooks/useNotifications.ts', () => {
it('fetchNotifications - all notifications read/unread', async () => {
mockState.settings.fetchOnlyUnreadNotifications = false;

nock('https://team.atlassian.net//')
.post('gateway/api/graphql')
.reply(200, {
globalThis.fetch = jest.fn().mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
data: {
notifications: {
notificationFeed: {
Expand All @@ -75,7 +76,8 @@ describe('renderer/hooks/useNotifications.ts', () => {
},
},
},
});
}),
} as unknown as Response);

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

Expand All @@ -100,17 +102,19 @@ describe('renderer/hooks/useNotifications.ts', () => {
it('fetchNotifications - handles missing extensions response object', async () => {
mockState.settings.fetchOnlyUnreadNotifications = false;

nock('https://team.atlassian.net//')
.post('gateway/api/graphql')
.reply(200, {
globalThis.fetch = jest.fn().mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
data: {
notifications: {
notificationFeed: {
nodes: [],
},
},
},
});
}),
} as unknown as Response);

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

Expand All @@ -134,7 +138,11 @@ describe('renderer/hooks/useNotifications.ts', () => {
});

it('markNotificationsRead', async () => {
nock('https://team.atlassian.net//').post('gateway/api/graphql').reply(200);
globalThis.fetch = jest.fn().mockResolvedValue({
ok: true,
status: 200,
json: async () => ({}),
} as unknown as Response);

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

Expand All @@ -152,7 +160,11 @@ describe('renderer/hooks/useNotifications.ts', () => {
});

it('markNotificationsUnread', async () => {
nock('https://team.atlassian.net//').post('gateway/api/graphql').reply(200);
globalThis.fetch = jest.fn().mockResolvedValue({
ok: true,
status: 200,
json: async () => ({}),
} as unknown as Response);

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

Expand Down
126 changes: 39 additions & 87 deletions src/renderer/utils/api/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import axios from 'axios';

import { mockSingleAtlassifyNotification } from '../../__mocks__/notifications-mocks';
import {
mockAtlassianCloudAccount,
mockSettings,
} from '../../__mocks__/state-mocks';
import { Constants } from '../../constants';
import type { CloudID, Hostname, JiraProjectKey } from '../../types';
import {
checkIfCredentialsAreValid,
Expand All @@ -20,19 +17,20 @@ import {

// Experimental API tests moved to experimental/client.test.ts

jest.mock('axios');
const originalFetch = globalThis.fetch;

describe('renderer/utils/api/client.ts', () => {
beforeEach(() => {
(axios as jest.MockedFunction<typeof axios>).mockResolvedValue({
data: {
data: {},
},
});
globalThis.fetch = jest.fn().mockResolvedValue({
ok: true,
status: 200,
json: async () => ({ data: {} }),
} as unknown as Response);
});

afterEach(() => {
jest.clearAllMocks();
globalThis.fetch = originalFetch;
});

it('checkIfCredentialsAreValid - should validate credentials', async () => {
Expand All @@ -41,48 +39,35 @@ describe('renderer/utils/api/client.ts', () => {
mockAtlassianCloudAccount.token,
);

expect(axios).toHaveBeenCalledWith(
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://team.atlassian.net/gateway/api/graphql',
expect.objectContaining({
url: 'https://team.atlassian.net/gateway/api/graphql',
method: 'POST',
data: {
query: expect.stringContaining('query Me'),
variables: undefined,
},
body: expect.stringContaining('query Me'),
}),
);
});

it('getAuthenticatedUser - should fetch authenticated user details', async () => {
await getAuthenticatedUser(mockAtlassianCloudAccount);

expect(axios).toHaveBeenCalledWith(
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://team.atlassian.net/gateway/api/graphql',
expect.objectContaining({
url: 'https://team.atlassian.net/gateway/api/graphql',
method: 'POST',
data: {
query: expect.stringContaining('query Me'),
variables: undefined,
},
body: expect.stringContaining('query Me'),
}),
);
});

it('listNotificationsForAuthenticatedUser - should list notifications for user', async () => {
await getNotificationsForUser(mockAtlassianCloudAccount, mockSettings);

expect(axios).toHaveBeenCalledWith(
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://team.atlassian.net/gateway/api/graphql',
expect.objectContaining({
url: 'https://team.atlassian.net/gateway/api/graphql',
method: 'POST',
data: {
query: expect.stringContaining('query MyNotifications'),
variables: {
first: Constants.MAX_NOTIFICATIONS_PER_ACCOUNT,
flat: !mockSettings.groupNotificationsByTitle,
readState: 'unread',
},
},
body: expect.stringContaining('query MyNotifications'),
}),
);
});
Expand All @@ -92,16 +77,11 @@ describe('renderer/utils/api/client.ts', () => {
mockSingleAtlassifyNotification.id,
]);

expect(axios).toHaveBeenCalledWith(
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://team.atlassian.net/gateway/api/graphql',
expect.objectContaining({
url: 'https://team.atlassian.net/gateway/api/graphql',
method: 'POST',
data: {
query: expect.stringContaining('mutation MarkAsRead'),
variables: {
notificationIDs: [mockSingleAtlassifyNotification.id],
},
},
body: expect.stringContaining('mutation MarkAsRead'),
}),
);
});
Expand All @@ -111,16 +91,11 @@ describe('renderer/utils/api/client.ts', () => {
mockSingleAtlassifyNotification.id,
]);

expect(axios).toHaveBeenCalledWith(
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://team.atlassian.net/gateway/api/graphql',
expect.objectContaining({
url: 'https://team.atlassian.net/gateway/api/graphql',
method: 'POST',
data: {
query: expect.stringContaining('mutation MarkAsUnread'),
variables: {
notificationIDs: [mockSingleAtlassifyNotification.id],
},
},
body: expect.stringContaining('mutation MarkAsUnread'),
}),
);
});
Expand All @@ -136,20 +111,11 @@ describe('renderer/utils/api/client.ts', () => {
mockGroupSize,
);

expect(axios).toHaveBeenCalledWith(
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://team.atlassian.net/gateway/api/graphql',
expect.objectContaining({
url: 'https://team.atlassian.net/gateway/api/graphql',
method: 'POST',
data: {
query: expect.stringContaining(
'query RetrieveNotificationsByGroupId',
),
variables: {
groupId: mockSingleAtlassifyNotification.notificationGroup.id,
first: mockGroupSize,
readState: 'unread',
},
},
body: expect.stringContaining('query RetrieveNotificationsByGroupId'),
}),
);
});
Expand All @@ -164,20 +130,11 @@ describe('renderer/utils/api/client.ts', () => {
mockGroupSize,
);

expect(axios).toHaveBeenCalledWith(
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://team.atlassian.net/gateway/api/graphql',
expect.objectContaining({
url: 'https://team.atlassian.net/gateway/api/graphql',
method: 'POST',
data: {
query: expect.stringContaining(
'query RetrieveNotificationsByGroupId',
),
variables: {
groupId: mockSingleAtlassifyNotification.notificationGroup.id,
first: mockGroupSize,
readState: null,
},
},
body: expect.stringContaining('query RetrieveNotificationsByGroupId'),
}),
);
});
Expand All @@ -188,37 +145,32 @@ describe('renderer/utils/api/client.ts', () => {

await getCloudIDsForHostnames(mockAtlassianCloudAccount, mockHostnames);

expect(axios).toHaveBeenCalledWith(
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://team.atlassian.net/gateway/api/graphql',
expect.objectContaining({
url: 'https://team.atlassian.net/gateway/api/graphql',
method: 'POST',
data: {
query: expect.stringContaining('query RetrieveCloudIDsForHostnames'),
variables: {
hostNames: mockHostnames,
},
},
body: expect.stringContaining('query RetrieveCloudIDsForHostnames'),
}),
);
});

it('getJiraProjectTypeByKey - should fetch jira project type', async () => {
const mockProjectKey = 'PROJ' as JiraProjectKey;
const mockCloudID = 'mock-cloud-id' as CloudID;
(axios as jest.MockedFunction<typeof axios>).mockResolvedValueOnce({
data: { projectTypeKey: 'service_desk' },
});
(globalThis.fetch as jest.Mock).mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ projectTypeKey: 'service_desk' }),
} as unknown as Response);
const result = await getJiraProjectTypeByKey(
mockAtlassianCloudAccount,
mockCloudID,
mockProjectKey,
);

expect(axios).toHaveBeenCalledWith(
expect.objectContaining({
method: 'GET',
url: `https://api.atlassian.com/ex/jira/${mockCloudID}/rest/api/3/project/${mockProjectKey}`,
}),
expect(globalThis.fetch).toHaveBeenCalledWith(
`https://api.atlassian.com/ex/jira/${mockCloudID}/rest/api/3/project/${mockProjectKey}`,
expect.objectContaining({ method: 'GET' }),
);
expect(result).toBe('service_desk');
});
Expand Down
Loading
Loading