1.0.1 • Published 5 years ago

questrar-test v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

questrar-test

A test utility for testing questrar Request Component with enzyme

NPM Maintainability Test Coverage Build Status

Install

yarn add -D questrar-test
npm install --save-dev questrar-test

Usage

//  Nice.jsx

import React, { Component } from 'react'
import { Request } from 'questrar';

export const PetiteComponent = ({ request }) => {
  return (
    <div
     className="petiteComponent"
     onClick={() => request.actions.remove(request.data.id)}
    >
      <div>{request.data.message}</div>
    </div>
  );
}

export default NiceComponent = () => {
   return (
     <Request id='abc123' inject> 
       <PetiteComponent  />
     </Request>
   )
}
//  Nice.spec.jsx

import React from 'react'
import { shallow } from 'enzyme';
import wrapRequest, { initialRequestState } from 'questrar-test';
import NiceComponent, { PetiteComponent } from '../Nice'

describe('NiceComponent', () => {
  let wrapper;
  let requestState;
  
  const createWrapper = () => {
    wrapper = shallow(<NiceComponent />)
  }
  
  beforeEach(() => {
    requestState = { ...initialRequestState, id: 'abc123' };
    createWrapper();
  });
  
  it('Should render PetiteComponent as default', () => {
    expect(wrapper.is(Request)).toBeTruthy()
  });
  
  it('Should render a loading gear icon on request `pending`', () => {
    requestState.pending = true;
    const wrap = wrapRequest(wrapper)(requestState);
    
    expect(wrap.is(PetiteComponent)).toBeTruthy();
  });
  
  it('Should remove request state `onClick` to close PetiteComponent', () => {
     const mockActions = {
          success: jest.fn(),
          failed: jest.fn(),
          pending: jest.fn(),
          dirty: jest.fn(),
          clean: jest.fn(),
          remove: jest.fn()
        };
    const wrap = wrapRequest(wrapper, mockActions)(requestState);
    wrap.simulate('click');
    expect(mockActions.remove).toHaveBeenNthCalledWith(1, requestState.id);
  });
});

Package exports two ends. wrapRequest as default and a named import { initialRequestState };

function wrapRequest(
  requestComponentNode: ShallowWrapper, //  ShallowWrapper of Request component node
  mockActions?: RequestActions
  )(
    defaultRequestState?: RequestState
   ): ShallowWrapper

License

MIT © orar

1.0.1

5 years ago

1.0.0

6 years ago