import React from 'react';
import PropTypes from 'prop-types';

import PokemonShow from 'packs/frontend/components/pages/pokemon/show';
import {instance} from 'packs/frontend/api';

import {StaticRouter} from 'react-router-dom';
import MockAdapter from 'axios-mock-adapter';

const defaultProps = {
  match: {
    params: {
      id: 1,
    },
    url: '/pokemon/show/1',
  },
};

function setup(props = defaultProps) {
  return shallow(<PokemonShow {...props} />);
}

function mountSetup(props = defaultProps) {
  return mount(
    <StaticRouter>
      <PokemonShow {...props} />
    </StaticRouter>
  );
}

describe('<PokemonShow />', () => {
  it('sets specifies PropTypes', () => {
    expect(PokemonShow.propTypes.match).toEqual(PropTypes.object.isRequired);
  });

  it('sets default state', () => {
    const wrapper = setup();
    expect(wrapper.state('pokemon')).toEqual({});
  });

  describe('componentDidMount()', () => {
    let mock;
    beforeEach(() => {
      mock = new MockAdapter(instance);
    });
    afterEach(() => {
      mock.restore();
    });

    it('persists fetched pokemon to state', async () => {
      const mockData = {
        id: 1,
        nickname: 'Bulbasaur',
      };
      mock.onGet(`/api/v1/pokemon/${mockData.id}`).reply(200, mockData);

      const wrapper = mountSetup();
      const component = wrapper.find(PokemonShow);

      await component.instance().componentDidMount();

      expect(component.state('pokemon')).toEqual(mockData);
    });
  });
});
