show.test.jsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import PokemonShow from 'packs/frontend/components/pages/pokemon/show';
  4. import {instance} from 'packs/frontend/api';
  5. import {StaticRouter} from 'react-router-dom';
  6. import MockAdapter from 'axios-mock-adapter';
  7. const defaultProps = {
  8. match: {
  9. params: {
  10. id: 1,
  11. },
  12. url: '/pokemon/show/1',
  13. },
  14. };
  15. function setup(props = defaultProps) {
  16. return shallow(<PokemonShow {...props} />);
  17. }
  18. function mountSetup(props = defaultProps) {
  19. return mount(
  20. <StaticRouter>
  21. <PokemonShow {...props} />
  22. </StaticRouter>
  23. );
  24. }
  25. describe('<PokemonShow />', () => {
  26. it('sets specifies PropTypes', () => {
  27. expect(PokemonShow.propTypes.match).toEqual(PropTypes.object.isRequired);
  28. });
  29. it('sets default state', () => {
  30. const wrapper = setup();
  31. expect(wrapper.state('pokemon')).toEqual({});
  32. });
  33. describe('componentDidMount()', () => {
  34. let mock;
  35. beforeEach(() => {
  36. mock = new MockAdapter(instance);
  37. });
  38. afterEach(() => {
  39. mock.restore();
  40. });
  41. it('persists fetched pokemon to state', async () => {
  42. const mockData = {
  43. id: 1,
  44. nickname: 'Bulbasaur',
  45. };
  46. mock.onGet(`/api/v1/pokemon/${mockData.id}`).reply(200, mockData);
  47. const wrapper = mountSetup();
  48. const component = wrapper.find(PokemonShow);
  49. await component.instance().componentDidMount();
  50. expect(component.state('pokemon')).toEqual(mockData);
  51. });
  52. });
  53. });