overmind
Version:
Frictionless state management
122 lines • 3.39 kB
JavaScript
import { createOvermindMock } from './';
describe('Mock', () => {
test('should run action tests', () => {
const state = {
foo: 'bar',
};
const test = ({ state, effects }) => {
state.foo = effects.effect('bar2');
};
const actions = { test };
const effect = (arg) => arg;
const effects = { effect };
const config = {
state,
actions,
effects,
};
const overmind = createOvermindMock(config, {
effect(arg) {
return arg + '!!!';
},
});
overmind.actions.test();
expect(overmind.mutations).toEqual([
{
method: 'set',
path: 'foo',
args: ['bar2!!!'],
hasChangedValue: true,
delimiter: '.',
},
]);
});
test('should test onInitialize explicitly', () => {
const state = {
foo: 'bar',
};
const onInitializeOvermind = ({ state }) => {
state.foo += '!';
};
const actions = {
onInitializeOvermind,
};
const config = {
actions,
state,
};
const overmind = createOvermindMock(config, {
effect() {
return 'bar3';
},
});
overmind.onInitialize();
expect(overmind.mutations).toEqual([
{
method: 'set',
path: 'foo',
args: ['bar!'],
hasChangedValue: true,
delimiter: '.',
},
]);
});
test('should preserve getters', async (done) => {
expect.assertions(1);
const state = {
value: 0,
get valuePlusTwo() {
return this.value + 2;
},
};
const updateValue = (context) => {
context.state.value = 15;
};
const actions = { updateValue };
const config = { state, actions };
const mock = createOvermindMock(config);
await mock.actions.updateValue();
expect(mock.state.valuePlusTwo).toEqual(17);
done();
});
test('should allow setting initial state', async () => {
expect.assertions(1);
const state = {
value: 0,
};
const config = { state };
const mock = createOvermindMock(config, (state) => {
state.value = 1;
});
expect(mock.state.value).toBe(1);
});
test('should allow setting initial and mock effect', async () => {
expect.assertions(2);
const state = {
value: 0,
};
const config = {
state,
actions: {
runFoo({ effects }) {
return effects.foo();
},
},
effects: {
foo() {
return 'bar';
},
},
};
const mock = createOvermindMock(config, {
foo() {
return 'bar2';
},
}, (state) => {
state.value = 1;
});
expect(mock.state.value).toBe(1);
expect(mock.actions.runFoo()).toBe('bar2');
});
});
//# sourceMappingURL=mock.test.js.map