Helpers library for writing tramvai specific unit-tests
It might be even more useful when used with @tramvai/test-mocks
npm i --save-dev @tramvai/test-unit
import { testReducer } from '@tramvai/test-unit';
it('test', async () => {
const { dispatch, getState } = testReducer(reducer);
expect(getState()).toEqual([]);
dispatch(event(1));
expect(getState()).toEqual([1]);
});
More examples
@inline src/state/testReducer.spec.ts
Tramvai context and DI will be created automatically, otherwise you can directly pass it as an argument.
import { testAction } from '@tramvai/test-unit';
it('test', async () => {
const { run, context } = testAction(action, { initialState: { a: 1 } });
expect(await run(true)).toBe('hello');
expect(await run(false)).toBe('world');
context.getState(); // { a: 1 }
});
More examples
@inline src/state/testAction.spec.ts
import { testModule } from '@tramvai/test-unit';
it('test', async () => {
const { di, module, runLine } = testModule(TestModule);
expect(module).toBeInstanceOf(TestModule);
expect(di.get('testToken')).toEqual({ a: 1 });
// Run only specific command line in order to execute handlers for this line inside module
await runLine(commandLineListTokens.generatePage);
});
import { createTestApp } from '@tramvai/test-unit';
it('test', async () => {
const { app } = await createTestApp({ modules: [TestModule, DependentModule] });
// get tokens from di implemented by module
expect(app.di.get('testToken')).toEqual({ a: 1 });
});
More examples
@inline src/module/testModule.spec.ts
Most of the helpers accepts option providers
which allows to redefine already existing providers or add new.
For example, passing providers
to helper testAction
allows to access this provider inside action itself:
import { declareAction } from '@tramvai/core';
import { testAction } from '@tramvai/test-unit';
const action = declareAction({
name: 'action',
fn() {
console.log(this.deps.test); // token value
},
deps: {
test: 'token name',
},
});
it('test', async () => {
const { run } = testAction(action, {
providers: [
{
provide: 'token name',
useValue: 'token value',
},
],
});
});
import { createTestApp } from '@tramvai/test-unit';
it('test', async () => {
const { app } = await createTestApp({ modules: [TestModule, DependentModule] });
// get tokens from di implemented by module
expect(app.di.get('testToken')).toEqual({ a: 1 });
});
More examples
@inline src/app/createTestApp.spec.ts