show.test.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import * as React from 'react';
  2. import {shallow, mount, ReactWrapper, ShallowWrapper} from 'enzyme';
  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. path: '',
  14. isExact: false,
  15. },
  16. location: {} as any,
  17. history: {} as any,
  18. pokemon: {},
  19. };
  20. function setup(props = defaultProps): ShallowWrapper {
  21. return shallow(<PokemonShow {...props} />);
  22. }
  23. function mountSetup(props = defaultProps): ReactWrapper {
  24. return mount(
  25. <StaticRouter>
  26. <PokemonShow {...props} />
  27. </StaticRouter>
  28. );
  29. }
  30. describe('<PokemonShow />', (): void => {
  31. it('sets default state', (): void => {
  32. const wrapper = setup();
  33. expect(wrapper.state('pokemon')).toEqual({id: null, nickname: null});
  34. });
  35. describe('componentDidMount()', (): void => {
  36. let mock;
  37. beforeEach(
  38. (): void => {
  39. mock = new MockAdapter(instance);
  40. }
  41. );
  42. afterEach(
  43. (): void => {
  44. mock.restore();
  45. }
  46. );
  47. it('persists fetched pokemon to state', async (): Promise<any> => {
  48. const mockData = {
  49. id: 1,
  50. nickname: 'Bulbasaur',
  51. };
  52. mock.onGet(`/api/v1/pokemon/${mockData.id}`).reply(200, mockData);
  53. const wrapper = mountSetup();
  54. const component = wrapper.find(PokemonShow);
  55. await component.instance().componentDidMount();
  56. expect(component.state('pokemon')).toEqual(mockData);
  57. });
  58. });
  59. });