show.test.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import * as React from 'react';
  2. import {shallow, mount} from 'enzyme';
  3. import PokemonShow from 'packs/frontend/components/pages/pokemon/show';
  4. import {instance} from 'packs/frontend/api';
  5. import {Route, 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) {
  21. return shallow(<PokemonShow {...props} />);
  22. }
  23. function mountSetup(props = defaultProps) {
  24. return mount(
  25. <StaticRouter>
  26. <PokemonShow {...props} />
  27. </StaticRouter>
  28. );
  29. }
  30. describe('<PokemonShow />', () => {
  31. it('sets default state', () => {
  32. const wrapper = setup();
  33. expect(wrapper.state('pokemon')).toEqual({id: null, nickname: null});
  34. });
  35. describe('componentDidMount()', () => {
  36. let mock;
  37. beforeEach(() => {
  38. mock = new MockAdapter(instance);
  39. });
  40. afterEach(() => {
  41. mock.restore();
  42. });
  43. it('persists fetched pokemon to state', async () => {
  44. const mockData = {
  45. id: 1,
  46. nickname: 'Bulbasaur',
  47. };
  48. mock.onGet(`/api/v1/pokemon/${mockData.id}`).reply(200, mockData);
  49. const wrapper = mountSetup();
  50. const component = wrapper.find(PokemonShow);
  51. await component.instance().componentDidMount();
  52. expect(component.state('pokemon')).toEqual(mockData);
  53. });
  54. });
  55. });