Skip to content

Commit

Permalink
tests refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Dec 2, 2024
1 parent f3bd745 commit 0d4ff46
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/test/__tests__/store/utils/slice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { createBaseSlice } from '@src/store/utils/slice';
describe('createBaseSlice', () => {
type KeyBasic = 'testKey';
type KeySingleItem = 'item';
let set: jest.Mock;
let get: jest.Mock;
const set = jest.fn();
const get = jest.fn();
let store: any;

beforeEach(() => {
set = jest.fn();
get = jest.fn();
store = {};
});

Expand Down Expand Up @@ -55,27 +53,28 @@ describe('createBaseSlice', () => {
});

test('updates the state when value is an object', () => {
const initialValue = { key1: 'value1' } as Record<string, string>;
type StateEntry = Record<string, string>;
const initialValue = { key1: 'value_1' } as StateEntry;
const sliceTitle = 'testSlice';
const baseSlice = createBaseSlice<KeyBasic, Record<string, string>, KeySingleItem, Record<string, string>>(
const baseSlice = createBaseSlice<KeyBasic, StateEntry, KeySingleItem, StateEntry>(
keys,
initialValue,
sliceTitle,
true,
)(set, get, store);

baseSlice.addItem?.({ key2: 'value2' });
baseSlice.addItem?.({ key2: 'value_2' });
expect(set).toHaveBeenCalledWith(expect.any(Function), false, 'testSlice/addTestKey');

const state = { testKey: { key1: 'value1' } };
const state = { testKey: { key1: 'value_1' } };
const updater = set.mock.calls[0][0];
expect(updater(state)).toEqual({
testKey: { key1: 'value1', key2: 'value2' },
testKey: { key1: 'value_1', key2: 'value_2' },
});
});

test('updates the state when value is a Set', () => {
const initialValue = new Set(['value1']);
const initialValue = new Set(['value_1']);
const sliceTitle = 'testSlice';
const baseSlice = createBaseSlice<KeyBasic, Set<string>, KeySingleItem, string>(
keys,
Expand All @@ -84,13 +83,13 @@ describe('createBaseSlice', () => {
true,
)(set, get, store);

baseSlice.addItem?.('value2');
baseSlice.addItem?.('value_2');
expect(set).toHaveBeenCalledWith(expect.any(Function), false, 'testSlice/addTestKey');

const state = { testKey: new Set(['value1']) };
const state = { testKey: new Set(['value_1']) };
const updater = set.mock.calls[0][0];
const updatedState = updater(state);
expect(updatedState.testKey).toEqual(new Set(['value1', 'value2']));
expect(updatedState.testKey).toEqual(new Set(['value_1', 'value_2']));
});
});
});

0 comments on commit 0d4ff46

Please sign in to comment.