From 3fc5d4e7019e435893c2305e95679120a1751f81 Mon Sep 17 00:00:00 2001 From: Evyatar Date: Mon, 25 Sep 2023 17:07:43 -0400 Subject: [PATCH] Test subscribe --- .../src/suite/__tests__/subscribe.test.ts | 36 +++++++++++++++---- packages/vest/src/suite/createSuite.ts | 2 +- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/packages/vest/src/suite/__tests__/subscribe.test.ts b/packages/vest/src/suite/__tests__/subscribe.test.ts index 4aa0d6174..1e8fcfc09 100644 --- a/packages/vest/src/suite/__tests__/subscribe.test.ts +++ b/packages/vest/src/suite/__tests__/subscribe.test.ts @@ -1,3 +1,5 @@ +import wait from 'wait'; + import { SuiteSerializer } from 'SuiteSerializer'; import * as vest from 'vest'; @@ -8,23 +10,43 @@ describe('suite.subscribe', () => { expect(typeof suite.subscribe).toBe('function'); }); - it('Should call the callback on suite updates', () => { + it('Should call the callback on suite updates', async () => { + const cb = jest.fn(() => { + dumps.push(SuiteSerializer.serialize(suite)); + }); + let callCount = cb.mock.calls.length; + const suite = vest.create('suite', () => { + expect(cb.mock.calls.length).toBeGreaterThan(callCount); + callCount = cb.mock.calls.length; vest.test('field', () => {}); + expect(cb.mock.calls.length).toBeGreaterThan(callCount); + callCount = cb.mock.calls.length; vest.test('field2', () => {}); + expect(cb.mock.calls.length).toBeGreaterThan(callCount); + callCount = cb.mock.calls.length; vest.test('field3', () => false); + expect(cb.mock.calls.length).toBeGreaterThan(callCount); + callCount = cb.mock.calls.length; vest.test('field4', async () => Promise.reject()); + expect(cb.mock.calls.length).toBeGreaterThan(callCount); + callCount = cb.mock.calls.length; }); - const dumps = []; - const cb = jest.fn(() => { - console.log(SuiteSerializer.serialize(suite)); - }); + const dumps: string[] = []; suite.subscribe(cb); + expect(cb.mock.calls).toHaveLength(0); suite(); - expect(cb.mock.calls.length).toBeGreaterThan(10); + expect(cb.mock.calls.length).toBeGreaterThan(callCount); + callCount = cb.mock.calls.length; + + // expect some of the dumps to be different + expect(dumps.some((dump, i) => dump !== dumps[i - 1])).toBe(true); + + await wait(10); - // console.log(dumps[0]); + // now also after resolving the async test + expect(cb.mock.calls.length).toBeGreaterThan(callCount); }); }); diff --git a/packages/vest/src/suite/createSuite.ts b/packages/vest/src/suite/createSuite.ts index e9998d190..f19e791b3 100644 --- a/packages/vest/src/suite/createSuite.ts +++ b/packages/vest/src/suite/createSuite.ts @@ -80,7 +80,7 @@ function createSuite< reset: Bus.usePrepareEmitter(Events.RESET_SUITE), resetField: Bus.usePrepareEmitter(Events.RESET_FIELD), resume: VestRuntime.persist(useLoadSuite), - subscribe: VestRuntime.persist(subscribe), + subscribe: subscribe, ...bindSuiteSelectors(VestRuntime.persist(useCreateSuiteResult)), ...getTypedMethods(), }