Skip to content

Commit

Permalink
feat(vest): add state subscription (ealush#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronen-e authored and ealush committed May 11, 2021
1 parent 8a8434c commit 32b1d05
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/vest/src/core/state/__tests__/state.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ describe('state', () => {
});
});
});

describe('onStateChange', () => {
const onStateChange = jest.fn();
beforeEach(() => {

stateRef = state.createRef({
keyWithObject: state.registerHandler(() => ({ a: true })),
}, onStateChange);
});

it('should run callback on state change', () => {
expect(onStateChange).toHaveBeenCalledTimes(1);
stateRef.set('key', 'value');
expect(onStateChange).toHaveBeenCalledTimes(2);
});

it('should run callback with updated state value', () => {
stateRef.set('key', 'value');
expect(onStateChange).toHaveBeenCalledWith(stateRef.current(),'key', 'value');
});
})
});

const initialState = {
Expand Down
5 changes: 4 additions & 1 deletion packages/vest/src/core/state/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default (function createState() {
return use;
}

function createRef(handlers) {
function createRef(handlers, onStateChange) {
let state = {};
const registeredHandlers = [];

Expand All @@ -48,6 +48,9 @@ export default (function createState() {

function set(key, value) {
state[key] = value;
if (isFunction(onStateChange)) {
onStateChange(state, key, value);
}
}

function reset() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`suite.subscribe Should call handler on suite subscription initialization 1`] = `
Array [
Object {
"suiteState": Object {
"usePending": Object {
"lagging": Array [],
"pending": Array [],
},
"useSuiteId": Object {
"id": "1",
"name": undefined,
},
"useTestCallbacks": Object {
"doneCallbacks": Array [],
"fieldCallbacks": Array [],
},
"useTestObjects": Array [],
},
"type": "suiteSubscribeInit",
},
]
`;
31 changes: 31 additions & 0 deletions packages/vest/src/core/suite/__tests__/subscribe.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import vest from 'vest';

describe('suite.subscribe', () => {
let suite;
let handler;
beforeEach(() => {
handler = jest.fn();
suite = vest.create(() => {

});

});

it('Should call handler on suite subscription initialization', () => {

suite();

expect(handler).toHaveBeenCalledTimes(0);
suite.subscribe(handler);
expect(handler).toHaveBeenCalledTimes(1);
});

it('Should call handler on suite subscription initialization', () => {

suite();

suite.subscribe(handler);
expect(handler.mock.calls[0]).toMatchSnapshot();
});

});
20 changes: 20 additions & 0 deletions packages/vest/src/core/suite/createSuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ const createSuite = withArgs(args => {
);
}

const handlers = [];
const stateRef = state.createRef({
usePending,
useSuiteId: [useSuiteId, [genId(), name]],
useTestCallbacks,
useTestObjects,
}, (state, key, value) => {
handlers.forEach(fn => fn({
key,
suiteState: state,
type: 'suiteStateUpdate',
value
}))
});

/*
Expand Down Expand Up @@ -67,6 +75,18 @@ const createSuite = withArgs(args => {
}
});
});

suite.subscribe = function(handler){
if (!isFunction(handler)) return;

handlers.push(handler);

handler({
type: 'suiteSubscribeInit',
suiteState: stateRef.current()
});
}

return suite;
});

Expand Down
10 changes: 10 additions & 0 deletions packages/vest/src/typings/vest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,21 @@ interface IOnly {
*/
group(groupName?: ExclusionArg): void;
}

interface ISubscribePayload {
type: string;
suiteState: Record<string, any>;
key?: string;
value?: any;
}

interface ICreateResult {
(...args: any[]): IVestResult;
get: () => DraftResult;
reset: () => void;
remove: (fieldName: string) => void;

subscribe: (payload: ISubscribePayload) => void;
}

declare namespace vest {
Expand Down

0 comments on commit 32b1d05

Please sign in to comment.