@tramvai/test-unit
TypeScript icon, indicating that this package has built-in type declarations

3.40.57 • Public • Published

Tramvai test unit

Helpers library for writing tramvai specific unit-tests

It might be even more useful when used with @tramvai/test-mocks

Installation

npm i --save-dev @tramvai/test-unit

How to

Testing reducers

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

Testing actions

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

Testing tramvai module

Testing module in isolation

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);
});

Testing module in conjunction with other modules

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

Adding providers to DI

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',
      },
    ],
  });
});

Create app only for testing

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

Readme

Keywords

none

Package Sidebar

Install

npm i @tramvai/test-unit

Weekly Downloads

166

Version

3.40.57

License

Apache-2.0

Unpacked Size

23.4 kB

Total Files

20

Last publish

Collaborators

  • meskill
  • super_oleg
  • tinkoffbank