diff --git a/src/Model.discontinuity.test.ts b/src/Model.discontinuity.test.ts index fd28465e..dbf822be 100644 --- a/src/Model.discontinuity.test.ts +++ b/src/Model.discontinuity.test.ts @@ -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'); @@ -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'); diff --git a/src/Model.test.ts b/src/Model.test.ts index 68421152..8b30ecd4 100644 --- a/src/Model.test.ts +++ b/src/Model.test.ts @@ -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'); @@ -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); @@ -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(); diff --git a/src/utilities/test/promises.ts b/src/utilities/test/promises.ts index a9942e5a..d6fdd9bd 100644 --- a/src/utilities/test/promises.ts +++ b/src/utilities/test/promises.ts @@ -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 = (subject: Subject, n: number) => lastValueFrom(subject.pipe(take(n))); @@ -14,7 +12,12 @@ export const getEventPromises = (subject: Subject, n: number) => { return promises; }; -export const modelStatePromise = (model: Model, state: ModelState) => - new Promise((resolve) => model.whenState(state, model.state, resolve)); +interface StateListener { + state: S; + whenState(targetState: S, currentState: S, listener: EventListener, ...listenerArgs: unknown[]); +} + +export const statePromise = (object: StateListener, state: S) => + new Promise((resolve) => object.whenState(state, object.state, resolve)); export const timeout = (ms: number = 0) => new Promise((resolve) => setTimeout(resolve, ms));