show.test.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. pokemon: {},
  15. };
  16. function setup(props = defaultProps) {
  17. return shallow(<PokemonShow {...props} />);
  18. }
  19. function mountSetup(props = defaultProps) {
  20. return mount(
  21. <StaticRouter>
  22. <PokemonShow {...props} />
  23. </StaticRouter>
  24. );
  25. }
  26. describe('<PokemonShow />', () => {
  27. //it('sets specifies PropTypes', () => {
  28. // expect(PokemonShow.propTypes.match).toEqual(PropTypes.object.isRequired);
  29. //});
  30. it('sets default state', () => {
  31. const wrapper = setup();
  32. expect(wrapper.state('pokemon')).toEqual({id: null, nickname: null});
  33. });
  34. describe('componentDidMount()', () => {
  35. let mock;
  36. beforeEach(() => {
  37. mock = new MockAdapter(instance);
  38. });
  39. afterEach(() => {
  40. mock.restore();
  41. });
  42. it('persists fetched pokemon to state', async () => {
  43. const mockData = {
  44. id: 1,
  45. nickname: 'Bulbasaur',
  46. };
  47. mock.onGet(`/api/v1/pokemon/${mockData.id}`).reply(200, mockData);
  48. const wrapper = mountSetup();
  49. const component = wrapper.find(PokemonShow);
  50. await component.instance().componentDidMount();
  51. expect(component.state('pokemon')).toEqual(mockData);
  52. });
  53. });
  54. });