Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Поправил тесты
Browse files Browse the repository at this point in the history
LeMarck committed Mar 4, 2024
1 parent 256ec49 commit c335082
Showing 3 changed files with 29 additions and 16 deletions.
7 changes: 2 additions & 5 deletions src/effect.test.ts
Original file line number Diff line number Diff line change
@@ -104,14 +104,11 @@ describe('createEffect', () => {
})
);

fetchApiFx.pending.watch(watcherMock);
fetchApiFx.pending.watch((value) => watcherMock(value));

await fetchApiFx(100);

expect(watcherMock.mock.calls).toEqual([
[true, true],
[false, false],
]);
expect(watcherMock.mock.calls).toEqual([[true], [false]]);
});

it('`.done/doneData`: события, которое срабатывают с результатом выполнения эффекта', async () => {
36 changes: 25 additions & 11 deletions src/store.test.ts
Original file line number Diff line number Diff line change
@@ -22,37 +22,51 @@ describe('createStore', () => {
expect($store.getState()).toBe(1);
});

it('Не обновлять стор если редьюсер возвращает `undefined`', () => {
const eventMock = jest.fn(<T>() => createEvent<T>())();
const $store = createStore(0).on(eventMock, (state) => state);

expect($store.getState()).toBe(0);

eventMock(undefined);

expect($store.getState()).toBe(0);
});

it('`.watch`: вызывает функцию с сайд-эффектами при каждом обновлении стора', () => {
const watcherMock = jest.fn();
const triggerWatcherMock = jest.fn();
const add = createEvent<number>();
const foo = createEvent<number>();
const $store = createStore(0).on(add, (state, payload) => state + payload);

$store.watch(watcherMock);
$store.watch(foo, watcherMock);
$store.watch((value) => watcherMock(value));
$store.watch(foo, triggerWatcherMock);

add(4);
expect(watcherMock).toHaveBeenCalledWith(4, 4);
expect(watcherMock).toHaveBeenCalledWith(4);
expect(triggerWatcherMock).not.toHaveBeenCalled();
expect($store.getState()).toBe(4);

add(3);
expect(watcherMock).toHaveBeenCalledWith(7, 3);
expect(watcherMock).toHaveBeenCalledWith(7);
expect(triggerWatcherMock).not.toHaveBeenCalled();
expect($store.getState()).toBe(7);

foo(42);
expect(watcherMock).toHaveBeenCalledWith(7, 42);
expect($store.getState()).toBe(7);
expect(triggerWatcherMock).toHaveBeenCalledWith(7, 42);
});

it('`unwatch` прекращает отслеживание', () => {
const watcherMock = jest.fn();
const add = createEvent<number>();
const $store = createStore(0).on(add, (state, payload) => state + payload);

const unwatch = $store.watch(watcherMock);
const unwatch = $store.watch((value) => watcherMock(value));

add(42);
expect(watcherMock).toHaveBeenCalledWith(42, 42);
expect(watcherMock).toHaveBeenCalledWith(42);
expect($store.getState()).toBe(42);

unwatch();
add(123);
@@ -65,17 +79,17 @@ describe('createStore', () => {
const changed = createEvent<string>();
const $title = createStore('').on(changed, (_, newTitle) => newTitle);

$title.map((title) => title.length).watch(watcherMock);
$title.map((title) => title.length).watch((length) => watcherMock(length));

changed('hello');
expect(watcherMock).toHaveBeenCalledWith(5, 5);
expect(watcherMock).toHaveBeenCalledWith(5);
expect(watcherMock).toHaveBeenCalledTimes(1);

changed('world');
expect(watcherMock).toHaveBeenCalledTimes(1);

changed('hello world');
expect(watcherMock).toHaveBeenCalledWith(11, 11);
expect(watcherMock).toHaveBeenCalledWith(11);
expect(watcherMock).toHaveBeenCalledTimes(2);
});

2 changes: 2 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -27,6 +27,8 @@ export function createStore<State>(defaultState: State): Store<State> {
let state = defaultState;

function update<Payload>(newState: State, payload: Payload): void {
if (newState === undefined || newState === state) return;

state = newState;
watchers.forEach((watcher) => watcher(state, payload));
}

0 comments on commit c335082

Please sign in to comment.