Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasadi committed Oct 22, 2024
1 parent b70b700 commit a618136
Showing 1 changed file with 222 additions and 0 deletions.
222 changes: 222 additions & 0 deletions tests/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NotificationAPIClientSDK } from '../lib/client';
import { api } from '../lib/api';
import { PostUserRequest } from '../lib/interfaces';

// Mock the fetch function
global.fetch = jest.fn();
Expand Down Expand Up @@ -42,7 +43,146 @@ describe('NotificationAPIClientSDK', () => {
expect.any(Object)
);
});
test('getInAppNotifications should correctly reduce oldestReceived', async () => {
const mockResponse = {
json: jest.fn().mockResolvedValue({
notifications: [
{ id: '1', date: '2023-01-02T00:00:00Z' },
{ id: '2', date: '2023-01-01T00:00:00Z' },
{ id: '3', date: '2023-01-03T00:00:00Z' }
]
})
};
(global.fetch as jest.Mock).mockResolvedValue(mockResponse);

const sdk = NotificationAPIClientSDK.init({
userId: 'testUser',
clientId: 'testClient'
});

const result = await sdk.getInAppNotifications({
before: '2023-01-04T00:00:00Z'
});

expect(result.oldestReceived).toBe('2023-01-01T00:00:00Z');
});
test('updateInAppNotifications should set archived to null when archived is false', async () => {
const mockResponse = {
json: jest.fn().mockResolvedValue({})
};
(global.fetch as jest.Mock).mockResolvedValue(mockResponse);

const sdk = NotificationAPIClientSDK.init({
userId: 'testUser',
clientId: 'testClient'
});

await sdk.updateInAppNotifications({
ids: ['notification1', 'notification2'],
archived: false
});

expect(global.fetch).toHaveBeenCalledWith(
'https://api.notificationapi.com/testClient/users/testUser/notifications/INAPP_WEB',
expect.objectContaining({
method: 'PATCH',
body: JSON.stringify({
trackingIds: ['notification1', 'notification2'],
archived: null
}),
headers: {
Authorization: expect.any(String)
}
})
);
});
test('updateInAppNotifications should set clicked to null when clicked is false', async () => {
const mockResponse = {
json: jest.fn().mockResolvedValue({})
};
(global.fetch as jest.Mock).mockResolvedValue(mockResponse);

const sdk = NotificationAPIClientSDK.init({
userId: 'testUser',
clientId: 'testClient'
});

await sdk.updateInAppNotifications({
ids: ['notification1', 'notification2'],
clicked: false
});

expect(global.fetch).toHaveBeenCalledWith(
'https://api.notificationapi.com/testClient/users/testUser/notifications/INAPP_WEB',
expect.objectContaining({
method: 'PATCH',
body: JSON.stringify({
trackingIds: ['notification1', 'notification2'],
clicked: null
}),
headers: {
Authorization: expect.any(String)
}
})
);
});
test('updateInAppNotifications should set opened to current date when opened is true', async () => {
const mockResponse = {
json: jest.fn().mockResolvedValue({})
};
(global.fetch as jest.Mock).mockResolvedValue(mockResponse);

const sdk = NotificationAPIClientSDK.init({
userId: 'testUser',
clientId: 'testClient'
});

await sdk.updateInAppNotifications({
ids: ['notification1', 'notification2'],
opened: true
});

expect(global.fetch).toHaveBeenCalledWith(
'https://api.notificationapi.com/testClient/users/testUser/notifications/INAPP_WEB',
expect.objectContaining({
method: 'PATCH',
body: expect.stringContaining('"opened":"'),
headers: {
Authorization: expect.any(String)
}
})
);
});
test('updateInAppNotifications should set opened to null when opened is false', async () => {
const mockResponse = {
json: jest.fn().mockResolvedValue({})
};
(global.fetch as jest.Mock).mockResolvedValue(mockResponse);

const sdk = NotificationAPIClientSDK.init({
userId: 'testUser',
clientId: 'testClient'
});

await sdk.updateInAppNotifications({
ids: ['notification1', 'notification2'],
opened: false
});

expect(global.fetch).toHaveBeenCalledWith(
'https://api.notificationapi.com/testClient/users/testUser/notifications/INAPP_WEB',
expect.objectContaining({
method: 'PATCH',
body: JSON.stringify({
trackingIds: ['notification1', 'notification2'],
opened: null
}),
headers: {
Authorization: expect.any(String)
}
})
);
});
test('updateInAppNotifications should call api with correct parameters', async () => {
const mockResponse = {
json: jest.fn().mockResolvedValue({})
Expand Down Expand Up @@ -132,6 +272,72 @@ describe('NotificationAPIClientSDK', () => {
})
);
});

test('identify should call api with correct parameters', async () => {
const mockResponse = {
json: jest.fn().mockResolvedValue({ preferences: [] })
};
(global.fetch as jest.Mock).mockResolvedValue(mockResponse);

const sdk = NotificationAPIClientSDK.init({
userId: 'testUser',
clientId: 'testClient'
});
const params: PostUserRequest = {
id: 'testUser',
email: '[email protected]'
};
await sdk.identify(params);

expect(global.fetch).toHaveBeenCalledWith(
expect.stringContaining(
'https://api.notificationapi.com/testClient/users/testUser/'
),
expect.objectContaining({
method: 'POST',
body: JSON.stringify(params)
})
);
});
test('identify should not call api with wrong parameters', async () => {
const mockResponse = {
json: jest.fn().mockResolvedValue({ preferences: [] })
};
(global.fetch as jest.Mock).mockResolvedValue(mockResponse);

const sdk = NotificationAPIClientSDK.init({
userId: 'testUser',
clientId: 'testClient'
});
const params: PostUserRequest = {
id: 'testUser2',
email: '[email protected]'
};
await expect(sdk.identify(params)).rejects.toThrow(
'The id in the parameters does not match the initialized userId.'
);
});
test('getUserAccountMetadata should call api with correct parameters', async () => {
const mockResponse = {
json: jest.fn().mockResolvedValue({ preferences: [] })
};
(global.fetch as jest.Mock).mockResolvedValue(mockResponse);

const sdk = NotificationAPIClientSDK.init({
userId: 'testUser',
clientId: 'testClient'
});
await sdk.getUserAccountMetadata();

expect(global.fetch).toHaveBeenCalledWith(
expect.stringContaining(
'https://api.notificationapi.com/testClient/users/testUser/account_metadata'
),
expect.objectContaining({
method: 'GET'
})
);
});
});

describe('api function', () => {
Expand Down Expand Up @@ -180,4 +386,20 @@ describe('api function', () => {

expect(result).toEqual('error');
});
test('should return undefined if response is not JSON', async () => {
const mockResponse = {
json: jest.fn().mockRejectedValue(new Error('Invalid JSON'))
};
(global.fetch as jest.Mock).mockResolvedValue(mockResponse);

const result = await api(
'GET',
'api.notificationapi.com',
'testResource',
'testClient',
'testUser'
);

expect(result).toBeUndefined();
});
});

0 comments on commit a618136

Please sign in to comment.