Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to write unit test cases in react js

# Given a component
import React from 'react';
const Hi = ({ userName }) => (
  <div className="sayhello">Howdy, {userName}!</div>
);
export default Hi;

# Install required deps
npm install --save-dev riteway

# Unit test
import React from 'react';
import { describe } from 'riteway';
import render from 'riteway/render-component';
import match from 'riteway/match';
import Hi from '../hi';
describe('Hi component', async assert => {
  const userName = 'MeMyself';
  const $ = render(<Hello userName={userName} />);
  const contains = match($('.sayhello').html());
  assert({
    given: 'A user',
    should: 'It should render a correct username.',
    actual: contains(userName),
    expected: `Hi, ${userName}!`
  });
});
 
PREVIOUS NEXT
Tagged: #write #unit #test #cases #react #js
ADD COMMENT
Topic
Name
4+7 =