jtn-ms / react-redux-thunk-unitest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

react-template

npx create-react-app javascript-redux --template javascript-redux
npm i react-redux connected-react-router redux redux-devtools-extension history redux-thunk redux-actions
npm i @mui/material @mui/icons-material
npm i @emotion/react @emotion/styled
npm i prop-types react-number-format

testing

npm i -D @testing-library/react@12.0 @testing-library/jest-dom
npm i -D enzyme sinon
npm i --save-dev redux-mock-store
npm i -D @wojtekmaj/enzyme-adapter-react-17
npm i --save-dev @testing-library/user-event @testing-library/dom

Testing List

  • component
  • component with state
  • component with state and props
  • component with redux-thunk
  • redux-thunk actions npm test -- -t "counter" or npm test src/store/reducers/add.test.js
  • redux-thunk real store
  • redux-thunk mock store
  • redux-toolkit
  • rxjs
  • console.log with enzyme working with npm test -- -t <regex>

Report

  • Not working
component method expect
muiButton -
jest toHaveBeenCalled always return false
testing-library toHaveBeenCalledTimes always return 0
enzyme find returns 5 nodes for a single data-testid for while it return 1 for classic button - <button>

Ref

import Adapter from 'enzyme-adapter-react-16';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';

TakeAway

  • enzyme sucks. Instead, use testing-library
  • enzyme doesn't support react 17+, mui well
  • npm test -- -t <regex> works well with console.log
  • components defined as just export, not export default, produces rendering issue in test with testing-library & jest
  • mockStore and realStore can't be used together.

About


Languages

Language:JavaScript 97.1%Language:HTML 2.4%Language:Makefile 0.5%Language:Shell 0.0%