Skip to content

Commit

Permalink
test: extract timeout promise function
Browse files Browse the repository at this point in the history
  • Loading branch information
mschristensen committed Oct 17, 2023
1 parent f735b69 commit f816d85
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IStreamFactory } from './StreamFactory.js';
import type { ModelState, ModelStateChange, ModelOptions, Event } from './types/model.d.ts';
import type { MutationMethods, EventComparator, MutationContext } from './types/mutations.d.ts';
import { createMessage, customMessage } from './utilities/test/messages.js';
import { getNthEventPromise, getEventPromises, modelStatePromise } from './utilities/test/promises.js';
import { getNthEventPromise, getEventPromises, modelStatePromise, timeout } from './utilities/test/promises.js';

vi.mock('ably/promises');

Expand Down Expand Up @@ -273,7 +273,7 @@ describe('Model', () => {

// wait for the next event loop iteration so that any scheduled tasks on the tasks queue are cleared,
// specifically model state updates scheduled via setTimeout from the model init() call in $register()
await new Promise((resolve) => setTimeout(resolve, 0));
await timeout();

let subscription = new Subject<void>();
const subscriptionCall = getNthEventPromise(subscription, 1);
Expand Down
13 changes: 7 additions & 6 deletions src/SlidingWindow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { it, describe, expect, vi } from 'vitest';

import SlidingWindow from './SlidingWindow.js';
import { createMessage } from './utilities/test/messages.js';
import { timeout } from './utilities/test/promises.js';

describe('SlidingWindow', () => {
it('emits events immediately with no timeout', async () => {
Expand All @@ -24,7 +25,7 @@ describe('SlidingWindow', () => {

expect(onExpire).toHaveBeenCalledTimes(0);

await new Promise((resolve) => setTimeout(resolve, 100));
await timeout(100);

expect(onExpire).toHaveBeenCalledTimes(1);
expect(onExpire).toHaveBeenCalledWith(msg);
Expand All @@ -40,7 +41,7 @@ describe('SlidingWindow', () => {
sliding.addMessage(msg2);
sliding.addMessage(msg1);

await new Promise((resolve) => setTimeout(resolve, 1));
await timeout(1);

expect(onExpire).toHaveBeenCalledTimes(2);
expect(onExpire).toHaveBeenNthCalledWith(1, msg1);
Expand All @@ -65,7 +66,7 @@ describe('SlidingWindow', () => {
sliding.addMessage(msg2);
sliding.addMessage(msg1);

await new Promise((resolve) => setTimeout(resolve, 1));
await timeout(1);

expect(onExpire).toHaveBeenCalledTimes(3);
expect(onExpire).toHaveBeenNthCalledWith(1, msg3);
Expand All @@ -83,13 +84,13 @@ describe('SlidingWindow', () => {

// message 3 added, and expired
sliding.addMessage(msg3);
await new Promise((resolve) => setTimeout(resolve, 1));
await timeout(1);

// then messages 1 and 2 added, reordered, and expired
sliding.addMessage(msg2);
sliding.addMessage(msg1);

await new Promise((resolve) => setTimeout(resolve, 1));
await timeout(1);

expect(onExpire).toHaveBeenCalledTimes(3);
expect(onExpire).toHaveBeenNthCalledWith(1, msg3);
Expand All @@ -107,7 +108,7 @@ describe('SlidingWindow', () => {
sliding.addMessage(msg1a);
sliding.addMessage(msg1b);

await new Promise((resolve) => setTimeout(resolve, 1));
await timeout(1);

expect(onExpire).toHaveBeenCalledTimes(1);
expect(onExpire).toHaveBeenNthCalledWith(1, msg1a);
Expand Down
2 changes: 2 additions & 0 deletions src/utilities/test/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export const getEventPromises = <T>(subject: Subject<T>, n: number) => {

export const modelStatePromise = <T, M extends MutationMethods>(model: Model<T, M>, state: ModelState) =>
new Promise((resolve) => model.whenState(state, model.state, resolve));

export const timeout = (ms: number = 0) => new Promise((resolve) => setTimeout(resolve, ms));

0 comments on commit f816d85

Please sign in to comment.