This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
/
UnifiedPushAdminClient.test.ts
107 lines (86 loc) · 4.3 KB
/
UnifiedPushAdminClient.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import axios from 'axios';
import { mockData } from './mockData';
import { PushApplication, UnifiedPushAdminClient } from '../src';
import { mocked } from 'ts-jest/dist/util/testing';
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
mocked(axios).create = () => mockedAxios;
const upsClient = new UnifiedPushAdminClient('http://localhost:9999');
describe('Applications Admin', () => {
it('test find without filters', async () => {
mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData }));
const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.find();
expect(res).toHaveLength(mockData.length);
expect(mockedAxios.get).toHaveBeenCalledWith('/applications');
});
it('test find with single filter', async () => {
mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData }));
const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.find({
developer: 'Test Developer 2',
});
expect(res).toHaveLength(2);
expect(mockedAxios.get).toHaveBeenCalledWith('/applications');
});
it('test find with multiple filter', async () => {
mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData }));
const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.find({
developer: 'Test Developer 2',
name: 'Application 4',
});
expect(res).toHaveLength(1);
expect(res[0].name).toEqual('Application 4');
expect(mockedAxios.get).toHaveBeenCalledWith('/applications');
});
it('test create app', async () => {
const appToBeCreated = mockData[3];
mockedAxios.post.mockImplementationOnce((url: string, app: PushApplication) => Promise.resolve({ data: app }));
const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.create(appToBeCreated);
expect(res).toEqual(appToBeCreated);
expect(mockedAxios.post).toHaveBeenCalledWith('/applications', appToBeCreated);
});
});
describe('Variants Admin', () => {
it('test find without filters', async () => {
const appId = '1';
const selectedApp = mockData.filter(app => app.id === appId)[0];
mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: selectedApp }));
const res = await upsClient.variants.find(appId);
expect(res).toHaveLength(selectedApp!.variants!.length);
expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`);
});
it('test find with single filter', async () => {
const appId = '1';
const selectedApp = mockData.filter(app => app.id === appId)[0];
mockedAxios.get.mockImplementation(() => Promise.resolve({ data: selectedApp }));
let res = await upsClient.variants.find(appId, { developer: 'developer 2' });
expect(res).toHaveLength(1);
expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`);
res = await upsClient.variants.find(appId, { developer: 'developer 1' });
expect(res).toHaveLength(2);
expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`);
});
it('test find one variant/no filters', async () => {
const appId = '3';
const selectedApp = mockData.filter(app => app.id === appId)[0];
mockedAxios.get.mockImplementation(() => Promise.resolve({ data: selectedApp }));
const res = await upsClient.variants.find(appId);
expect(res).toHaveLength(1);
expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`);
});
it('test find with bad filter', async () => {
const appId = '1';
const selectedApp = mockData.filter(app => app.id === appId)[0];
mockedAxios.get.mockImplementation(() => Promise.resolve({ data: selectedApp }));
const res = await upsClient.variants.find(appId, { developer: 'developer 84' });
expect(res).toHaveLength(0);
expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`);
});
it('test app without variants', async () => {
const appId = '4';
const selectedApp = mockData.filter(app => app.id === appId)[0];
mockedAxios.get.mockImplementation(() => Promise.resolve({ data: selectedApp }));
const res = await upsClient.variants.find(appId, { developer: 'developer 84' });
expect(res).toHaveLength(0);
expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`);
});
});