Skip to content

Commit

Permalink
Add test for cycle of updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrHoroshih committed Dec 12, 2023
1 parent fee6ff3 commit 1aee5df
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion packages/redux-interop/src/lib/redux-interop.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createReduxInterop } from './redux-interop';
import { legacy_createStore } from 'redux';
import { configureStore, createSlice } from '@reduxjs/toolkit';
import { createEvent, fork, allSettled } from 'effector';
import { createEvent, fork, allSettled, createStore, sample } from 'effector';

describe('@withease/redux', () => {
test('Should throw if setup is not an effector unit', () => {
Expand Down Expand Up @@ -146,6 +146,75 @@ describe('@withease/redux', () => {

expect(scope.getState($state)).toEqual('lol');
});

test('edge case: should allow synchronous cycle update', async () => {
/**
* This is an edge case, where we have a cycle between effector and redux,
* it is useful for cases, when a feature is not entierly migrated to effector,
* so it is still needed to keep redux and effector parts in sync.
*/

const reduxStore = legacy_createStore<
{ c: number },
{ type: string; c: number }
>((s, a) => ({ c: (s || { c: 0 }).c + (a.c || 0) }), { c: 0 });

const setup = createEvent();
const interop = createReduxInterop({
reduxStore,
setup,
});

const updateCount = createEvent<number>();
const $count = createStore(0).on(updateCount, (s, a) => s + a);

const $reduxCount = interop.fromState((x) => x.c);

// effector updates redux
const updateReduxCount = interop.dispatch.prepend((x: number) => ({
type: 'test',
c: x,
}));
sample({
clock: $count,
source: $reduxCount,
filter: (r, e) => r !== e,
fn: (_r, e) => e,
target: updateReduxCount,
});

// redux updates effector - cycle
sample({
clock: $reduxCount,
source: $count,
filter: (r, e) => r !== e,
fn: (_s, reduxCount) => reduxCount,
target: $count,
});

const scope = fork();

expect(scope.getState($count)).toEqual(0);
expect(scope.getState($reduxCount)).toEqual(0);

await allSettled(setup, { scope });

await allSettled(updateCount, {
scope,
params: 1,
});

expect(scope.getState($count)).toEqual(1);
expect(scope.getState($reduxCount)).toEqual(1);

await allSettled(updateReduxCount, {
scope,
params: 1,
});

expect(scope.getState($count)).toEqual(2);
expect(scope.getState($reduxCount)).toEqual(2);
});
});

describe('Redux Toolkit', () => {
Expand Down

0 comments on commit 1aee5df

Please sign in to comment.