sb-file-uploader-hoc.test.jsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'web-audio-test-api';
  2. import React from 'react';
  3. import configureStore from 'redux-mock-store';
  4. import {mountWithIntl, shallowWithIntl} from '../../helpers/intl-helpers.jsx';
  5. import {LoadingState} from '../../../src/reducers/project-state';
  6. import VM from 'scratch-vm';
  7. import SBFileUploaderHOC from '../../../src/lib/sb-file-uploader-hoc.jsx';
  8. describe('SBFileUploaderHOC', () => {
  9. const mockStore = configureStore();
  10. let store;
  11. let vm;
  12. // Wrap this in a function so it gets test specific states and can be reused.
  13. const getContainer = function () {
  14. const Component = () => <div />;
  15. return SBFileUploaderHOC(Component);
  16. };
  17. const shallowMountWithContext = component => (
  18. shallowWithIntl(component, {context: {store}})
  19. );
  20. const unwrappedInstance = () => {
  21. const WrappedComponent = getContainer();
  22. // default starting state: looking at a project you created, not logged in
  23. const wrapper = shallowMountWithContext(
  24. <WrappedComponent
  25. projectChanged
  26. canSave={false}
  27. cancelFileUpload={jest.fn()}
  28. closeFileMenu={jest.fn()}
  29. requestProjectUpload={jest.fn()}
  30. userOwnsProject={false}
  31. vm={vm}
  32. onLoadingFinished={jest.fn()}
  33. onLoadingStarted={jest.fn()}
  34. onUpdateProjectTitle={jest.fn()}
  35. />
  36. );
  37. return wrapper
  38. .dive() // unwrap intl
  39. .dive() // unwrap redux Connect(SBFileUploaderComponent)
  40. .instance(); // SBFileUploaderComponent
  41. };
  42. beforeEach(() => {
  43. vm = new VM();
  44. store = mockStore({
  45. scratchGui: {
  46. projectState: {
  47. loadingState: LoadingState.SHOWING_WITHOUT_ID
  48. },
  49. vm: {}
  50. },
  51. locales: {
  52. locale: 'en'
  53. }
  54. });
  55. });
  56. test('correctly sets title with .sb3 filename', () => {
  57. const projectName = unwrappedInstance().getProjectTitleFromFilename('my project is great.sb3');
  58. expect(projectName).toBe('my project is great');
  59. });
  60. test('correctly sets title with .sb2 filename', () => {
  61. const projectName = unwrappedInstance().getProjectTitleFromFilename('my project is great.sb2');
  62. expect(projectName).toBe('my project is great');
  63. });
  64. test('correctly sets title with .sb filename', () => {
  65. const projectName = unwrappedInstance().getProjectTitleFromFilename('my project is great.sb');
  66. expect(projectName).toBe('my project is great');
  67. });
  68. test('sets blank title with filename with no extension', () => {
  69. const projectName = unwrappedInstance().getProjectTitleFromFilename('my project is great');
  70. expect(projectName).toBe('');
  71. });
  72. test('if isLoadingUpload becomes true, without fileToUpload set, will call cancelFileUpload', () => {
  73. const mockedCancelFileUpload = jest.fn();
  74. const WrappedComponent = getContainer();
  75. const mounted = mountWithIntl(
  76. <WrappedComponent
  77. projectChanged
  78. canSave={false}
  79. cancelFileUpload={mockedCancelFileUpload}
  80. closeFileMenu={jest.fn()}
  81. isLoadingUpload={false}
  82. requestProjectUpload={jest.fn()}
  83. store={store}
  84. userOwnsProject={false}
  85. vm={vm}
  86. onLoadingFinished={jest.fn()}
  87. onLoadingStarted={jest.fn()}
  88. onUpdateProjectTitle={jest.fn()}
  89. />
  90. );
  91. mounted.setProps({
  92. isLoadingUpload: true
  93. });
  94. expect(mockedCancelFileUpload).toHaveBeenCalled();
  95. });
  96. });