Skip to content

Commit 141b499

Browse files
gtamanahaalexs-mparticle
authored andcommitted
test(apiClient): Add unit tests for sendLogToServer method with FetchUploader and XHRUploader
1 parent e8421c3 commit 141b499

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

test/jest/apiClient.spec.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { describe, test, expect, beforeEach, afterEach, jest } from '@jest/globals';
2+
import { LogRequest, LogRequestSeverity } from '../../src/logging/logRequest';
3+
import { ErrorCodes } from '../../src/logging/errorCodes';
4+
import APIClient from '../../src/apiClient';
5+
6+
jest.mock('../../src/uploaders', () => {
7+
const fetchUploadMock = jest.fn(() => Promise.resolve({} as Response));
8+
const xhrUploadMock = jest.fn(() => Promise.resolve({} as Response));
9+
10+
class MockFetchUploader {
11+
constructor(public url: string) {}
12+
upload = fetchUploadMock;
13+
}
14+
class MockXHRUploader {
15+
constructor(public url: string) {}
16+
upload = xhrUploadMock;
17+
}
18+
19+
(global as any).__fetchUploadSpy = fetchUploadMock;
20+
(global as any).__xhrUploadSpy = xhrUploadMock;
21+
22+
return {
23+
AsyncUploader: class {},
24+
FetchUploader: MockFetchUploader,
25+
XHRUploader: MockXHRUploader,
26+
};
27+
});
28+
29+
describe('apiClient.sendLogToServer', () => {
30+
let mpInstance: any;
31+
let logRequest: LogRequest;
32+
let originalWindow: any;
33+
let originalFetch: any;
34+
let kitBlocker: any = {
35+
kitBlockingEnabled: false,
36+
dataPlanMatchLookups: {},
37+
};
38+
39+
beforeEach(() => {
40+
jest.resetModules();
41+
42+
originalWindow = (global as any).window;
43+
originalFetch = (global as any).window?.fetch;
44+
45+
mpInstance = {
46+
_Helpers: {
47+
createServiceUrl: jest.fn((url: string, token: string) => `https://api.fake.com/${token}`)
48+
},
49+
_Store: {
50+
SDKConfig: { v2SecureServiceUrl: 'someUrl' },
51+
devToken: 'testToken123'
52+
}
53+
};
54+
55+
logRequest = {
56+
additionalInformation: {
57+
message: 'test',
58+
version: '1.0.0'
59+
},
60+
severity: LogRequestSeverity.Error,
61+
code: ErrorCodes.UNHANDLED_EXCEPTION,
62+
url: 'https://example.com',
63+
deviceInfo: 'test',
64+
stackTrace: 'test',
65+
reporter: 'test',
66+
integration: 'test'
67+
};
68+
69+
// @ts-ignore
70+
(global as any).window = {};
71+
72+
const fetchSpy = (global as any).__fetchUploadSpy as jest.Mock;
73+
const xhrSpy = (global as any).__xhrUploadSpy as jest.Mock;
74+
if (fetchSpy) fetchSpy.mockClear();
75+
if (xhrSpy) xhrSpy.mockClear();
76+
});
77+
78+
afterEach(() => {
79+
(global as any).window = originalWindow;
80+
if (originalFetch !== undefined) {
81+
(global as any).window.fetch = originalFetch;
82+
}
83+
jest.clearAllMocks();
84+
});
85+
86+
test('should use FetchUploader if window.fetch is available', () => {
87+
(global as any).window.fetch = jest.fn();
88+
89+
const uploadSpy = (global as any).__fetchUploadSpy as jest.Mock;
90+
const client = new APIClient(mpInstance, kitBlocker);
91+
92+
client.sendLogToServer(logRequest);
93+
94+
validateUploadCall(uploadSpy, logRequest, mpInstance);
95+
});
96+
97+
test('should use XHRUploader if window.fetch is not available', () => {
98+
delete (global as any).window.fetch;
99+
100+
const uploadSpy = (global as any).__xhrUploadSpy as jest.Mock;
101+
const client = new APIClient(mpInstance, kitBlocker);
102+
103+
client.sendLogToServer(logRequest);
104+
105+
validateUploadCall(uploadSpy, logRequest, mpInstance);
106+
});
107+
108+
function validateUploadCall(uploadSpy: jest.Mock, expectedLogRequest: LogRequest, mpInstance: any) {
109+
expect(uploadSpy).toHaveBeenCalledTimes(1);
110+
expect(uploadSpy.mock.calls.length).toBeGreaterThan(0);
111+
const firstCall = uploadSpy.mock.calls[0] as any[];
112+
expect(firstCall).toBeDefined();
113+
const call = firstCall[0];
114+
expect(call).toBeDefined();
115+
expect((call as any).method).toBe('POST');
116+
expect((call as any).body).toBe(JSON.stringify(expectedLogRequest));
117+
expect((call as any).headers).toMatchObject({
118+
Accept: 'text/plain;charset=UTF-8',
119+
'Content-Type': 'text/plain;charset=UTF-8'
120+
});
121+
expect(mpInstance._Helpers.createServiceUrl).toHaveBeenCalledWith(
122+
mpInstance._Store.SDKConfig.v2SecureServiceUrl,
123+
mpInstance._Store.devToken
124+
);
125+
}
126+
});
127+
128+
129+

0 commit comments

Comments
 (0)