Skip to content

Commit

Permalink
Remove real XMLHttpRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-el committed Oct 10, 2023
1 parent 9d1d194 commit 9f92a7d
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions libraries/browser-tracker-core/test/out_queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,20 @@ const readPostQueue = () => {
describe('OutQueueManager', () => {
const maxQueueSize = 2;

var xhrMock: XMLHttpRequest;
var xhrMock: Partial<XMLHttpRequest>;
var xhrOpenMock: jest.Mock;
beforeEach(() => {
localStorage.clear();

xhrOpenMock = jest.fn();
xhrMock = {
...new XMLHttpRequest(),
open: xhrOpenMock,
send: jest.fn(),
setRequestHeader: jest.fn(),
withCredentials: true,
};

jest.spyOn(window, 'XMLHttpRequest').mockImplementation(() => xhrMock);
jest.spyOn(window, 'XMLHttpRequest').mockImplementation(() => xhrMock as XMLHttpRequest);
});

const respondMockRequest = (status: number) => {
Expand Down Expand Up @@ -371,7 +370,7 @@ describe('OutQueueManager', () => {
let retrievedQueue = readPostQueue();
expect(retrievedQueue).toHaveLength(1);

xhrMock.onabort?.(new ProgressEvent('abort'));
(xhrMock as XMLHttpRequest).ontimeout?.(new ProgressEvent('abort'));

retrievedQueue = readPostQueue();
expect(retrievedQueue).toHaveLength(1);
Expand All @@ -384,7 +383,7 @@ describe('OutQueueManager', () => {
let retrievedQueue = readPostQueue();
expect(retrievedQueue).toHaveLength(1);

xhrMock.ontimeout?.(new ProgressEvent('timeout'));
(xhrMock as XMLHttpRequest).ontimeout?.(new ProgressEvent('timeout'));

retrievedQueue = readPostQueue();
expect(retrievedQueue).toHaveLength(1);
Expand All @@ -397,7 +396,7 @@ describe('OutQueueManager', () => {
let retrievedQueue = readPostQueue();
expect(retrievedQueue).toHaveLength(1);

xhrMock.onerror?.(new ProgressEvent('error'));
(xhrMock as XMLHttpRequest).onerror?.(new ProgressEvent('error'));

retrievedQueue = readPostQueue();
expect(retrievedQueue).toHaveLength(1);
Expand Down Expand Up @@ -435,7 +434,7 @@ describe('OutQueueManager', () => {
let retrievedQueue = readPostQueue();
expect(retrievedQueue).toHaveLength(1);

xhrMock.onabort?.(new ProgressEvent('abort'));
(xhrMock as XMLHttpRequest).ontimeout?.(new ProgressEvent('abort'));

retrievedQueue = readPostQueue();
expect(retrievedQueue).toHaveLength(0);
Expand All @@ -448,7 +447,7 @@ describe('OutQueueManager', () => {
let retrievedQueue = readPostQueue();
expect(retrievedQueue).toHaveLength(1);

xhrMock.ontimeout?.(new ProgressEvent('timeout'));
(xhrMock as XMLHttpRequest).ontimeout?.(new ProgressEvent('timeout'));

retrievedQueue = readPostQueue();
expect(retrievedQueue).toHaveLength(0);
Expand All @@ -461,7 +460,7 @@ describe('OutQueueManager', () => {
let retrievedQueue = readPostQueue();
expect(retrievedQueue).toHaveLength(1);

xhrMock.onerror?.(new ProgressEvent('error'));
(xhrMock as XMLHttpRequest).onerror?.(new ProgressEvent('error'));

retrievedQueue = readPostQueue();
expect(retrievedQueue).toHaveLength(0);
Expand Down

0 comments on commit 9f92a7d

Please sign in to comment.