Skip to content

Instantly share code, notes, and snippets.

@SuperOleg39
Created August 20, 2020 14:00
Show Gist options
  • Select an option

  • Save SuperOleg39/d8855a42299da9598abbea9a010e09a7 to your computer and use it in GitHub Desktop.

Select an option

Save SuperOleg39/d8855a42299da9598abbea9a010e09a7 to your computer and use it in GitHub Desktop.
import { appSimulator } from '@tramvai/test';
import { createAction } from '@tramvai/core';
import { createReducer, createEvent } from '@tramvai/state';
import { CommonModule } from '@tramvai/module-common';
import { actionServerStateEvent } from '@tramvai/module-common/lib/actions/actionTramvaiReducer';
import { CONTEXT_TOKEN, COMBINE_REDUCERS } from '@tramvai/tokens-common';
const incrementEvent = createEvent('increment');
const testReducer = createReducer('test', {
count: 0,
}).on(incrementEvent, (state) => ({
count: state.count + 1,
}));
const action = createAction({
name: 'test',
async fn(ctx) {
await ctx.dispatch(
actionServerStateEvent({
foo: true,
})
);
await ctx.dispatch(incrementEvent());
},
});
describe('test', () => {
it('context', async () => {
const { getToken } = appSimulator({
modules: [CommonModule],
providers: [
{
provide: COMBINE_REDUCERS,
multi: true,
useValue: testReducer,
},
],
});
const context = getToken(CONTEXT_TOKEN);
expect(context.getState().actionTramvai.serverState.foo).toBeUndefined();
expect(context.getState().test.count).toBe(0);
await context.executeAction(action);
expect(context.getState().actionTramvai.serverState.foo).toBe(true);
expect(context.getState().test.count).toBe(1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment