Skip to content

Commit

Permalink
test: make state promise work on different types
Browse files Browse the repository at this point in the history
This will be used for awaiting state on a Stream as well as a Model.
  • Loading branch information
mschristensen committed Oct 17, 2023
1 parent f816d85 commit 1db37b6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/Model.discontinuity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { it, describe, expect, vi, beforeEach } from 'vitest';

import Model from './Model.js';
import { ModelOptions } from './types/model.js';
import { getEventPromises, modelStatePromise } from './utilities/test/promises.js';
import { getEventPromises, statePromise } from './utilities/test/promises.js';

vi.mock('ably/promises');

Expand Down Expand Up @@ -65,10 +65,10 @@ describe('Model', () => {
await subscriptionCalls[0];
expect(subscriptionSpy).toHaveBeenNthCalledWith(1, null, '0');

await modelStatePromise(model, 'ready');
await statePromise(model, 'ready');

suspendChannel();
await modelStatePromise(model, 'ready');
await statePromise(model, 'ready');

await subscriptionCalls[1];
expect(subscriptionSpy).toHaveBeenNthCalledWith(2, null, '1');
Expand Down
8 changes: 4 additions & 4 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, timeout } from './utilities/test/promises.js';
import { getNthEventPromise, getEventPromises, statePromise, timeout } from './utilities/test/promises.js';

vi.mock('ably/promises');

Expand Down Expand Up @@ -99,10 +99,10 @@ describe('Model', () => {
logger,
});
const ready = model.$register({ $sync: sync });
await modelStatePromise(model, 'preparing');
await statePromise(model, 'preparing');
completeSync();
await ready;
await modelStatePromise(model, 'ready');
await statePromise(model, 'ready');
expect(sync).toHaveBeenCalledOnce();
expect(model.optimistic).toEqual(simpleTestData);
expect(model.confirmed).toEqual(simpleTestData);
Expand Down Expand Up @@ -960,7 +960,7 @@ describe('Model', () => {

// The 3rd event throws when applying the update, which should
// trigger a resync and get the latest counter value.
const preparingPromise = modelStatePromise(model, 'preparing');
const preparingPromise = statePromise(model, 'preparing');
events.channelEvents.next(customMessage('id_3', 'testEvent', String(++counter)));
const { reason } = (await preparingPromise) as ModelStateChange;
expect(reason).to.toBeDefined();
Expand Down
13 changes: 8 additions & 5 deletions src/utilities/test/promises.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Subject, lastValueFrom, take } from 'rxjs';

import type Model from '../../Model.js';
import type { ModelState } from '../../types/model.js';
import type { MutationMethods } from '../../types/mutations.js';
import { EventListener } from '../EventEmitter.js';

export const getNthEventPromise = <T>(subject: Subject<T>, n: number) => lastValueFrom(subject.pipe(take(n)));

Expand All @@ -14,7 +12,12 @@ export const getEventPromises = <T>(subject: Subject<T>, n: number) => {
return promises;
};

export const modelStatePromise = <T, M extends MutationMethods>(model: Model<T, M>, state: ModelState) =>
new Promise((resolve) => model.whenState(state, model.state, resolve));
interface StateListener<T, S> {
state: S;
whenState(targetState: S, currentState: S, listener: EventListener<T>, ...listenerArgs: unknown[]);
}

export const statePromise = <T, S>(object: StateListener<T, S>, state: S) =>
new Promise((resolve) => object.whenState(state, object.state, resolve));

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

0 comments on commit 1db37b6

Please sign in to comment.