Show.test.tsx 2.6 KB

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