Skip to content

Commit

Permalink
Add RTK integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrHoroshih committed Dec 11, 2023
1 parent 1708e39 commit f37593c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
75 changes: 72 additions & 3 deletions packages/redux-interop/src/lib/redux-interop.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createReduxInterop } from './redux-interop';
import { legacy_createStore } from 'redux';
import { configureStore } from '@reduxjs/toolkit';
import { configureStore, createSlice } from '@reduxjs/toolkit';
import { createEvent, fork, allSettled } from 'effector';

describe('@withease/redux', () => {
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('@withease/redux', () => {
const interop = createReduxInterop({ reduxStore, setup });
setup();

const $state = interop.fromState((x) => x['value']);
const $state = interop.fromState((x) => (x as any).value);

expect($state.getState()).toEqual('kek');

Expand All @@ -72,7 +72,7 @@ describe('@withease/redux', () => {
const setup = createEvent();
const interop = createReduxInterop({ reduxStore, setup });

const $state = interop.fromState((x) => x['value']);
const $state = interop.fromState((x) => (x as any).value);

const mockStore = legacy_createStore((_, x) => ({
value: (x as any).value || 'kek',
Expand All @@ -94,4 +94,73 @@ describe('@withease/redux', () => {
expect(scope.getState($state)).toEqual('lol');
});
});

describe('Redux Toolkit', () => {
test('Should work with basic Redux Toolkit', () => {
const testSlice = createSlice({
name: 'test',
initialState: 'kek',
reducers: {
test: () => 'lol',
},
});
const reduxStore = configureStore({
reducer: {
test: testSlice.reducer,
},
});
const setup = createEvent();
const interop = createReduxInterop({ reduxStore, setup });
const $test = interop.fromState((x) => x.test);
setup();

expect(interop.$store.getState()).toBe(reduxStore);

expect($test.getState()).toEqual('kek');

interop.dispatch(testSlice.actions.test());

expect($test.getState()).toEqual('lol');
});

test('Should work with Fork API', async () => {
const reduxStore = legacy_createStore(() => ({}), {});
const testSlice = createSlice({
name: 'test',
initialState: 'kek',
reducers: {
test: () => 'lol',
},
});
const mockStore = configureStore({
reducer: {
test: testSlice.reducer,
},
});
const setup = createEvent();
const interop = createReduxInterop({ reduxStore, setup });
const $test = interop.fromState((x) => x.test);

const scope = fork({
values: [[interop.$store, mockStore]],
});

await allSettled(setup, { scope });

expect(scope.getState(interop.$store)).toBe(mockStore);

expect(scope.getState($test)).toEqual('kek');
expect($test.getState()).toEqual(undefined);

interop.dispatch(testSlice.actions.test());

await allSettled(interop.dispatch, {
scope,
params: testSlice.actions.test(),
});

expect(scope.getState($test)).toEqual('lol');
expect($test.getState()).toEqual(undefined);
});
});
});
2 changes: 1 addition & 1 deletion packages/redux-interop/src/lib/redux-interop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function createReduxInterop<
BaseState = unknown,
// Record type is used here instead of UnknownAction, because compatibility with redux ^4.0.0 is also needed
Action = Record<string, unknown>,
StateExt = Record<string, unknown>
StateExt = unknown
>(config: {
reduxStore: ReduxStore;
// We don't care about the type of the setup unit here
Expand Down

0 comments on commit f37593c

Please sign in to comment.