Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Testing React Applications

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

Testing React Applications

Avatar for Max Stoiber

Max Stoiber

March 01, 2017
Tweet

More Decks by Max Stoiber

Other Decks in Technology

Transcript

  1. // add.test.js import { add } from './add.js'; describe('add()', ()

    => { it('adds two numbers', () => { expect(add(2, 3)).toEqual(5); }); it('doesnt add the third number', () => { expect(add(2, 3, 5)).toEqual(add(2, 3)); }); });
  2. // add.test.js import { add } from './add.js'; describe('add()', ()

    => { it('adds two numbers', () => { expect(add(2, 3)).toEqual(5); }); it('doesnt add the third number', () => { expect(add(2, 3, 5)).toEqual(add(2, 3)); }); });
  3. // add.test.js import { add } from './add.js'; describe('add()', ()

    => { it('adds two numbers', () => { expect(add(2, 3)).toEqual(5); }); it('doesnt add the third number', () => { expect(add(2, 3, 5)).toEqual(add(2, 3)); }); });
  4. // add.test.js import { add } from './add.js'; describe('add()', ()

    => { it('adds two numbers', () => { expect(add(2, 3)).toEqual(5); }); it('doesnt add the third number', () => { expect(add(2, 3, 5)).toEqual(add(2, 3)); }); });
  5. // add.test.js import { add } from './add.js'; describe('add()', ()

    => { it('adds two numbers', () => { expect(add(2, 3)).toEqual(5); }); it('doesnt add the third number', () => { expect(add(2, 3, 5)).toEqual(add(2, 3)); }); });
  6. NavBar !"" NavBar.react.js # React component # !"" NavBar.actions.js #

    Actions !"" NavBar.constants.js # Constants !"" NavBar.reducer.js # Reducer # !"" NavBar.actions.test.js # Actions tests $"" NavBar.reducer.test.js # Reducer tests
  7. describe('NavBar actions', () => { describe('toggleNav', () => { it('should

    return the correct constant', () => { expect(toggleNav()).toEqual({ type: "TOGGLE_NAV" }); }); }); });
  8. describe('NavBar actions', () => { describe('toggleNav', () => { it('should

    return the correct constant', () => { expect(toggleNav()).toEqual({ type: "TOGGLE_NAV" }); }); }); }); export function toggleNav() { return { type: "TOGGLE_NAV" }; }
  9. describe('NavBar actions', () => { describe('toggleNav', () => { it('should

    return the correct constant', () => { expect(toggleNav()).toEqual({ type: "TOGGLE_NAV" }); }); }); });
  10. // NavBar.reducer.js const initialState = { open: false }; export

    default function NavBarReducer(state = initialState, action) { switch (action.type) { case "TOGGLE_NAV": return { …state, open: !state.open }; default: return state; } }
  11. // NavBar.reducer.js const initialState = { open: false }; export

    default function NavBarReducer(state, action) { switch (action.type) { case "TOGGLE_NAV": return Object.assign({}, state, { open: !state.open }); default: return state; } }
  12. describe('NavBarReducer', () => { it('returns the initial state', () =>

    { expect(NavBarReducer(undefined, {})).toEqual({ open: false }); }); it('handles the toggleNav action', () => { expect(NavBarReducer(undefined, toggleNav())).toEqual({ open: true }); }); });
  13. describe('NavBarReducer', () => { it('returns the initial state', () =>

    { expect(NavBarReducer(undefined, {})).toEqual({ open: false }); }); it('handles the toggleNav action', () => { expect(NavBarReducer(undefined, toggleNav())).toEqual({ open: true }); }); });
  14. describe('NavBarReducer', () => { it('returns the initial state', () =>

    { expect(NavBarReducer(undefined, {})).toEqual({ open: false }); }); it('handles the toggleNav action', () => { expect(NavBarReducer(undefined, toggleNav())).toEqual({ open: true }); }); });
  15. describe('NavBarReducer', () => { it('returns the initial state', () =>

    { expect(NavBarReducer(undefined, {})).toEqual({ open: false }); }); it('handles the toggleNav action', () => { expect(NavBarReducer(undefined, toggleNav())).toEqual({ open: true }); }); });
  16. // Button.test.js import React from 'react'; import renderer from 'react-test-renderer';

    import Button from './Button.react'; describe('<Button />', () => { it('should render a button with a classname', () => {}); it('should attach an onclick handler passed in', () => {}); it('should render its children', () => {}); });
  17. // Button.test.js import React from 'react'; import renderer from 'react-test-renderer';

    import Button from './Button.react'; describe('<Button />', () => { it('should render a button with a classname', () => {}); it('should attach an onclick handler passed in', () => {}); it('should render its children', () => {}); }); const Button = (props) => ( <button className="btn" onClick={props.onClick} > {props.children} </button> );
  18. // Button.test.js describe('<Button />', () => { it('should render a

    button with a classname', () => { const tree = renderer.create( <Button></Button> ).toJSON(); expect(tree).toMatchSnapshot(); }); it('should attach an onclick handler passed in', () => {}); it('should render its children', () => {}); });
  19. // Button.test.js.snap exports[`<Button /> should render a button with a

    classname 1`] = ` <button className="btn" onClick={undefined} /> `;
  20. // Button.test.js describe('<Button />', () => { it('should render a

    button with a classname', () => {}); it('should attach an onclick handler passed in', () => { const tree = renderer.create( <Button onClick={() => {}}></Button> ).toJSON(); expect(tree).toMatchSnapshot(); }); it('should render its children', () => {}); });
  21. // Button.test.js describe('<Button />', () => { it('should render a

    button with a classname', () => {}); it('should attach an onclick handler passed in', () => { const tree = renderer.create( <Button onClick={() => {}}></Button> ).toJSON(); expect(tree).toMatchSnapshot(); }); it('should render its children', () => {}); });
  22. exports[`<Button /> should attach an onclick handler passed in 1`]

    = ` <button className="btn" onClick={[Function]} /> `;
  23. exports[`<Button /> should attach an onclick handler passed in 1`]

    = ` <button className="btn" onClick={[Function]} /> `;
  24. // Button.test.js describe('<Button />', () => { it('should render a

    button with a classname', () => {}); it('should attach an onclick handler passed in', () => {}); it('should render its children', () => { const tree = renderer.create( <Button>Some text</Button> ).toJSON(); expect(tree).toMatchSnapshot(); }); });
  25. // Button.test.js describe('<Button />', () => { it('should render a

    button with a classname', () => {}); it('should attach an onclick handler passed in', () => {}); it('should render its children', () => { const tree = renderer.create( <Button>Some text</Button> ).toJSON(); expect(tree).toMatchSnapshot(); }); });
  26. exports[`<Button /> should render its children 1`] = ` <button

    className="btn" onClick={undefined}> Some text </button> `;
  27. exports[`<Button /> should render its children 1`] = ` <button

    className="btn" onClick={undefined}> Some text </button> `;