Show.test.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import * as React from 'react';
  2. import {shallow, mount, ReactWrapper, ShallowWrapper} from 'enzyme';
  3. import {createMockEnvironment, MockPayloadGenerator} from 'relay-test-utils';
  4. import {QueryRenderer, graphql} from 'react-relay';
  5. import * as ReactTestRenderer from 'react-test-renderer';
  6. import FragmentedPokemonShow, {PokemonShow, Props} from 'packs/frontend/components/pages/pokemon/Show';
  7. import {Show_pokemon} from 'packs/frontend/components/pages/pokemon/__generated__/Show_pokemon.graphql';
  8. const pokemon: Show_pokemon = {
  9. id: '1',
  10. iid: 'x1',
  11. nickname: 'Bulbasaur',
  12. pokedexNumber: 1,
  13. ' $refType': '' as any,
  14. };
  15. const defaultProps = {
  16. pokemon: pokemon,
  17. };
  18. function setup(props = defaultProps): ShallowWrapper {
  19. return shallow(<PokemonShow {...props} />);
  20. }
  21. function mountSetup(props = defaultProps): ReactWrapper {
  22. return mount(<PokemonShow {...props} />);
  23. }
  24. describe('<PokemonShow />', () => {
  25. it('displays a pokemon', () => {
  26. const wrapper = setup();
  27. expect(wrapper).toMatchSnapshot();
  28. });
  29. it('requests data from a GraphQL fragment', () => {
  30. const environment = createMockEnvironment();
  31. const mockId = 'mock-id-1';
  32. const TestRenderer = () => (
  33. <QueryRenderer
  34. environment={environment}
  35. query={graphql`
  36. query Show_pokemonSuccessQuery($mockId: ID!) @relay_test_operation {
  37. pokemon: node(id: $mockId) {
  38. ...Show_pokemon
  39. }
  40. }
  41. `}
  42. variables={{mockId}}
  43. render={args => {
  44. const {error, props} = args as {error: Error; props: Props};
  45. if (props) {
  46. // Prevent TS Compiler from complaining about unsupplied props
  47. const Component = FragmentedPokemonShow as any;
  48. return <Component pokemon={props.pokemon} />;
  49. } else if (error) {
  50. return error.message;
  51. }
  52. return 'Loading...';
  53. }}
  54. />
  55. );
  56. environment.mock.queueOperationResolver(operation =>
  57. MockPayloadGenerator.generate(operation, {
  58. Pokemon: () => ({
  59. id: mockId,
  60. iid: '1',
  61. nickname: 'Bulba',
  62. pokedexNumber: 3,
  63. }),
  64. })
  65. );
  66. const renderer = ReactTestRenderer.create(<TestRenderer />);
  67. expect(renderer).toMatchSnapshot();
  68. });
  69. });