avocado/packages/state/test/timeline.js
2022-06-22 15:44:27 -05:00

56 lines
1.4 KiB
JavaScript

import {expect} from 'chai';
import {createStore} from '@reduxjs/toolkit';
import {timelineEnhancer} from '../src';
const createAction = (type) => {
const f = (timestamp, payload) => ({
meta: {timestamp},
type,
payload,
});
f.toString = () => type;
return f;
};
const move = createAction('move');
const setVelocity = createAction('setVelocity');
const reducer = (state, action) => {
switch (action.type) {
case move.toString():
return {
...state,
x: state.x + state.vel,
};
case setVelocity.toString():
return {
...state,
vel: action.payload,
};
default:
return state;
}
};
it('can time travel', () => {
const store = createStore(reducer, {x: 0, vel: 1}, timelineEnhancer);
// x = 1 + 1
store.dispatch(move(10));
store.dispatch(move(11));
expect(store.getState()).to.deep.equal({x: 2, vel: 1});
// x = 2 + 2
store.dispatch(setVelocity(9, 2));
expect(store.getState()).to.deep.equal({x: 4, vel: 2});
// x = 2 + 2 + 2 + 2
store.dispatch(move(12));
store.dispatch(move(13));
expect(store.getState()).to.deep.equal({x: 8, vel: 2});
// x = 2 + 2 + 4 + 4
store.dispatch(setVelocity(11, 4));
expect(store.getState()).to.deep.equal({x: 12, vel: 4});
// x = 2 + 5 + 4 + 4
store.dispatch(setVelocity(10.5, 5));
expect(store.getState()).to.deep.equal({x: 15, vel: 4});
});