Sink Testing

redux-sink provide a simple testing flow, here is the example of testing with `jest` along with `enzyme` and `typescript`

Test sink class

Testing sink classes are simple because sink provides the injections based on containers, simply inject mock objects then you can test the logic of sink

import { mocked } from 'ts-jest/utils';

import { HttpClient } from './http-client';
import { PatientAddSink } from './patient-add-sink';

jest.mock('./http-client');

// create mock object of httpclient which does external requests
const mockHttpClient = mocked(new HttpClient(), true);

describe('sink:: patient-add-sink', () => {
  beforeEach(() => {
    // create mock implementation of post to return a promise object
    // which is the same behavior as normal request call
    mockHttpClient.post.mockImplementation(x => {
      return Promise.resolve({} as any);
    });
  });

  it('should call api when submit', async () => {
    // create sink with mock httpclient
    const sink = new PatientAddSink(mockHttpClient as any);
    
    // set starting state
    sink.hospital = { name: 'test', id: 1, waitTime: 1 };
    sink.illness = { name: 'test', id: 1 };
    
    // run sink effect
    const promise = sink.submit();
    
    // check state before request finishes
    expect(sink.submitting).toBeTruthy();
    await promise;
    
    // check state after request completed
    expect(sink.submitting).toBeFalsy();
    expect(mockHttpClient.post.mock.calls).toHaveLength(1);
  });
});

Test react component

Test the components by mock the useSink and SinkContainer without Provider wrapper

Last updated

Was this helpful?