Skip to content

Commit

Permalink
Update batchLink tests to use fake timers (apollographql#10612)
Browse files Browse the repository at this point in the history
* Update batchLink tests to use fake timers

* fix: refactor batch interval test to support fake timers

---------

Co-authored-by: Alessia Bellisario <alessia@apollographql.com>
  • Loading branch information
TrevinAvery and alessbell authored Mar 3, 2023
1 parent ebee40b commit 260914a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
42 changes: 28 additions & 14 deletions src/link/batch/__tests__/batchLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function getKey(operation: GraphQLRequest) {
return JSON.stringify([operationName, query, variables]);
}

const delay = (time: number) => new Promise(r => setTimeout(r, time));

function createOperation(
starting: any,
operation: GraphQLRequest,
Expand Down Expand Up @@ -135,8 +137,10 @@ function createMockBatchHandler(...mockedResponses: MockedResponse[]) {
return mockBatchHandler;
}

beforeEach(() => jest.useFakeTimers());
afterEach(() => jest.useRealTimers());

describe('OperationBatcher', () => {
afterEach(() => jest.useRealTimers());

it('should construct', () => {
expect(() => {
Expand All @@ -151,9 +155,7 @@ describe('OperationBatcher', () => {
it('should not do anything when faced with an empty queue', () => {
const batcher = new OperationBatcher({
batchInterval: 10,
batchHandler: () => {
return null;
},
batchHandler: () => null,
batchKey: () => 'yo',
});

Expand All @@ -167,9 +169,7 @@ describe('OperationBatcher', () => {
it('should be able to add to the queue', () => {
const batcher = new OperationBatcher({
batchInterval: 10,
batchHandler: () => {
return null;
},
batchHandler: () => null,
});

const query = gql`
Expand Down Expand Up @@ -380,6 +380,8 @@ describe('OperationBatcher', () => {
notify = true;
}
});

jest.runAllTimers()
});

itAsync('should return a promise when we enqueue a request and resolve it with a result', (resolve, reject) => {
Expand All @@ -401,7 +403,6 @@ describe('OperationBatcher', () => {
});

itAsync('should be able to debounce requests', (resolve, reject) => {
jest.useFakeTimers();
const batchInterval = 10;
const myBatcher = new OperationBatcher({
batchDebounce: true,
Expand Down Expand Up @@ -474,6 +475,8 @@ describe('OperationBatcher', () => {
}),
20,
);

jest.runAllTimers();
});

itAsync('should cancel single query in queue when unsubscribing', (resolve, reject) => {
Expand Down Expand Up @@ -585,6 +588,8 @@ describe('OperationBatcher', () => {
const subscription = batcher.enqueueRequest({
operation: createOperation({}, { query }),
}).subscribe(() => reject('next should never be called'));

jest.runAllTimers();
});

itAsync('should correctly batch multiple queries', (resolve, reject) => {
Expand Down Expand Up @@ -641,6 +646,8 @@ describe('OperationBatcher', () => {
}),
20,
);

jest.runAllTimers();
});

itAsync('should cancel multiple queries in queue when unsubscribing and let pass still subscribed one', (resolve, reject) => {
Expand Down Expand Up @@ -694,6 +701,8 @@ describe('OperationBatcher', () => {
sub3.unsubscribe();
expect(batcher["batchesByKey"].get('')!.size).toBe(1);
}, 5);

jest.runAllTimers();
});

itAsync('should reject the promise if there is a network error', (resolve, reject) => {
Expand Down Expand Up @@ -874,8 +883,7 @@ describe('BatchLink', () => {
});

itAsync('correctly follows batch interval', (resolve, reject) => {
const TIME_SCALE = 100;
const intervals = [1*TIME_SCALE, 2*TIME_SCALE, 3*TIME_SCALE];
const intervals = [10, 20, 30];

const runBatchInterval = () => {
const mock = jest.fn();
Expand Down Expand Up @@ -924,7 +932,9 @@ describe('BatchLink', () => {
},
});

setTimeout(() => {
const delayedBatchInterval = async () => {
await delay(batchInterval);

const checkCalls = mock.mock.calls.slice(0, -1);
try {
expect(checkCalls.length).toBe(2);
Expand All @@ -936,10 +946,14 @@ describe('BatchLink', () => {
}

runBatchInterval();
}, batchInterval + (.5*TIME_SCALE));
};

delayedBatchInterval();

mock(batchHandler.mock.calls.length);
mock(batchHandler.mock.calls.length);

setTimeout(() => mock(batchHandler.mock.calls.length), batchInterval - (.5*TIME_SCALE));
setTimeout(() => mock(batchHandler.mock.calls.length), batchInterval / 2);
jest.runOnlyPendingTimers();
};
runBatchInterval();
});
Expand Down
2 changes: 1 addition & 1 deletion src/testing/core/itAsync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function wrap<TResult>(key?: "only" | "skip" | "todo") {
function wrap(key?: "only" | "skip" | "todo") {
return (
message: string,
callback: (
Expand Down

0 comments on commit 260914a

Please sign in to comment.