Skip to content

Commit

Permalink
Test subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Sep 25, 2023
1 parent 83c5cf8 commit 3fc5d4e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
36 changes: 29 additions & 7 deletions packages/vest/src/suite/__tests__/subscribe.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import wait from 'wait';

import { SuiteSerializer } from 'SuiteSerializer';
import * as vest from 'vest';

Expand All @@ -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<undefined>());
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);
});
});
2 changes: 1 addition & 1 deletion packages/vest/src/suite/createSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function createSuite<
reset: Bus.usePrepareEmitter(Events.RESET_SUITE),
resetField: Bus.usePrepareEmitter<string>(Events.RESET_FIELD),
resume: VestRuntime.persist(useLoadSuite),
subscribe: VestRuntime.persist(subscribe),
subscribe: subscribe,
...bindSuiteSelectors<F, G>(VestRuntime.persist(useCreateSuiteResult)),
...getTypedMethods<F, G>(),
}
Expand Down

0 comments on commit 3fc5d4e

Please sign in to comment.