sprite-selector-item.test.jsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React from 'react';
  2. import {mountWithIntl, shallowWithIntl, componentWithIntl} from '../../helpers/intl-helpers.jsx';
  3. import SpriteSelectorItemComponent from '../../../src/components/sprite-selector-item/sprite-selector-item';
  4. import DeleteButton from '../../../src/components/delete-button/delete-button';
  5. describe('SpriteSelectorItemComponent', () => {
  6. let className;
  7. let costumeURL;
  8. let name;
  9. let onClick;
  10. let onDeleteButtonClick;
  11. let selected;
  12. let number;
  13. let details;
  14. // Wrap this in a function so it gets test specific states and can be reused.
  15. const getComponent = function () {
  16. return (
  17. <SpriteSelectorItemComponent
  18. className={className}
  19. costumeURL={costumeURL}
  20. details={details}
  21. name={name}
  22. number={number}
  23. selected={selected}
  24. onClick={onClick}
  25. onDeleteButtonClick={onDeleteButtonClick}
  26. />
  27. );
  28. };
  29. beforeEach(() => {
  30. className = 'ponies';
  31. costumeURL = 'https://scratch.mit.edu/foo/bar/pony';
  32. name = 'Pony sprite';
  33. onClick = jest.fn();
  34. onDeleteButtonClick = jest.fn();
  35. selected = true;
  36. // Reset to undefined since they are optional props
  37. number = undefined; // eslint-disable-line no-undefined
  38. details = undefined; // eslint-disable-line no-undefined
  39. });
  40. test('matches snapshot when selected', () => {
  41. const component = componentWithIntl(getComponent());
  42. expect(component.toJSON()).toMatchSnapshot();
  43. });
  44. test('matches snapshot when given a number and details to show', () => {
  45. number = 5;
  46. details = '480 x 360';
  47. const component = componentWithIntl(getComponent());
  48. expect(component.toJSON()).toMatchSnapshot();
  49. });
  50. test('does not have a close box when not selected', () => {
  51. selected = false;
  52. const wrapper = shallowWithIntl(getComponent());
  53. expect(wrapper.find(DeleteButton).exists()).toBe(false);
  54. });
  55. test('triggers callback when Box component is clicked', () => {
  56. // Use `mount` here because of the way ContextMenuTrigger consumes onClick
  57. const wrapper = mountWithIntl(getComponent());
  58. wrapper.simulate('click');
  59. expect(onClick).toHaveBeenCalled();
  60. });
  61. test('triggers callback when CloseButton component is clicked', () => {
  62. const wrapper = shallowWithIntl(getComponent());
  63. wrapper.find(DeleteButton).simulate('click');
  64. expect(onDeleteButtonClick).toHaveBeenCalled();
  65. });
  66. test('it has a context menu with delete menu item and callback', () => {
  67. const wrapper = mountWithIntl(getComponent());
  68. const contextMenu = wrapper.find('ContextMenu');
  69. expect(contextMenu.exists()).toBe(true);
  70. contextMenu.find('[children="delete"]').simulate('click');
  71. expect(onDeleteButtonClick).toHaveBeenCalled();
  72. });
  73. });