2
0

ModalWorkflowSource.test.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import React from 'react';
  2. import { shallow } from 'enzyme';
  3. import ModalWorkflowSource, { getChooserConfig, filterEntityData } from './ModalWorkflowSource';
  4. import { EditorState, convertFromRaw, AtomicBlockUtils, RichUtils, Modifier } from 'draft-js';
  5. global.ModalWorkflow = () => {};
  6. describe('ModalWorkflowSource', () => {
  7. beforeEach(() => {
  8. jest.spyOn(global, 'ModalWorkflow');
  9. });
  10. afterEach(() => {
  11. jest.restoreAllMocks();
  12. });
  13. it('works', () => {
  14. expect(shallow((
  15. <ModalWorkflowSource
  16. editorState={{}}
  17. entityType={{}}
  18. entity={{}}
  19. onComplete={() => {}}
  20. onClose={() => {}}
  21. />
  22. ))).toMatchSnapshot();
  23. });
  24. describe('#getChooserConfig', () => {
  25. it('IMAGE', () => {
  26. expect(getChooserConfig({ type: 'IMAGE' })).toEqual({
  27. url: '/admin/images/chooser/?select_format=true',
  28. urlParams: {},
  29. });
  30. });
  31. it('EMBED', () => {
  32. expect(getChooserConfig({ type: 'EMBED' })).toEqual({
  33. url: '/admin/embeds/chooser/',
  34. urlParams: {},
  35. });
  36. });
  37. it('DOCUMENT', () => {
  38. expect(getChooserConfig({ type: 'DOCUMENT' })).toEqual({
  39. url: '/admin/documents/chooser/',
  40. urlParams: {},
  41. });
  42. });
  43. describe('LINK', () => {
  44. it('no entity', () => {
  45. expect(getChooserConfig({ type: 'LINK' })).toMatchSnapshot();
  46. });
  47. it('page', () => {
  48. expect(getChooserConfig({ type: 'LINK' }, {
  49. getData: () => ({ id: 1, parentId: 0 })
  50. })).toMatchSnapshot();
  51. });
  52. it('mail', () => {
  53. expect(getChooserConfig({ type: 'LINK' }, {
  54. getData: () => ({ url: 'mailto:test@example.com' })
  55. })).toMatchSnapshot();
  56. });
  57. it('external', () => {
  58. expect(getChooserConfig({ type: 'LINK' }, {
  59. getData: () => ({ url: 'https://www.example.com/' })
  60. })).toMatchSnapshot();
  61. });
  62. });
  63. });
  64. describe('#filterEntityData', () => {
  65. it('IMAGE', () => {
  66. expect(filterEntityData({ type: 'IMAGE' }, {
  67. id: 53,
  68. title: 'Test',
  69. alt: 'Test',
  70. class: 'richtext-image right',
  71. edit_link: '/admin/images/53/',
  72. format: 'right',
  73. preview: {
  74. url: '/media/images/test.width-500.jpg',
  75. }
  76. })).toMatchSnapshot();
  77. });
  78. it('EMBED', () => {
  79. expect(filterEntityData({ type: 'EMBED' }, {
  80. authorName: 'Test',
  81. embedType: 'video',
  82. providerName: 'YouTube',
  83. thumbnail: 'https://i.ytimg.com/vi/pSlVtxLOYiM/hqdefault.jpg',
  84. title: 'Test',
  85. url: 'https://www.youtube.com/watch?v=pSlVtxLOYiM',
  86. })).toMatchSnapshot();
  87. });
  88. it('DOCUMENT', () => {
  89. expect(filterEntityData({ type: 'DOCUMENT' }, {
  90. edit_link: '/admin/documents/edit/1/',
  91. filename: 'test.pdf',
  92. id: 1,
  93. title: 'Test',
  94. url: '/documents/1/test.pdf',
  95. })).toMatchSnapshot();
  96. });
  97. it('OTHER', () => {
  98. expect(filterEntityData({ type: 'OTHER' }, {})).toEqual({});
  99. });
  100. describe('LINK', () => {
  101. it('page', () => {
  102. expect(filterEntityData({ type: 'LINK' }, {
  103. id: 60,
  104. parentId: 1,
  105. url: '/',
  106. editUrl: '/admin/pages/60/edit/',
  107. title: 'Welcome to the Wagtail Bakery!',
  108. })).toMatchSnapshot();
  109. });
  110. it('mail', () => {
  111. expect(filterEntityData({ type: 'LINK' }, {
  112. prefer_this_title_as_link_text: false,
  113. title: 'test@example.com',
  114. url: 'mailto:test@example.com',
  115. })).toMatchSnapshot();
  116. });
  117. it('external', () => {
  118. expect(filterEntityData({ type: 'LINK' }, {
  119. prefer_this_title_as_link_text: false,
  120. title: 'https://www.example.com/',
  121. url: 'https://www.example.com/',
  122. })).toMatchSnapshot();
  123. });
  124. });
  125. });
  126. it('#componentDidMount', () => {
  127. const wrapper = shallow((
  128. <ModalWorkflowSource
  129. editorState={{}}
  130. entityType={{}}
  131. entity={{}}
  132. onComplete={() => {}}
  133. onClose={() => {}}
  134. />
  135. ));
  136. wrapper.instance().onChosen = jest.fn();
  137. wrapper.instance().componentDidMount();
  138. global.ModalWorkflow.mock.calls[0][0].responses.embedChosen('test', {});
  139. expect(global.ModalWorkflow).toHaveBeenCalled();
  140. expect(global.jQuery().on).toHaveBeenCalled();
  141. expect(wrapper.instance().onChosen).toHaveBeenCalled();
  142. });
  143. it('#onError', () => {
  144. window.alert = jest.fn();
  145. const onClose = jest.fn();
  146. const wrapper = shallow((
  147. <ModalWorkflowSource
  148. editorState={{}}
  149. entityType={{}}
  150. entity={{}}
  151. onComplete={() => {}}
  152. onClose={onClose}
  153. />
  154. ));
  155. wrapper.instance().componentDidMount();
  156. global.ModalWorkflow.mock.calls[0][0].onError();
  157. expect(global.ModalWorkflow).toHaveBeenCalled();
  158. expect(global.jQuery().on).toHaveBeenCalled();
  159. expect(window.alert).toHaveBeenCalled();
  160. expect(onClose).toHaveBeenCalled();
  161. });
  162. it('#componentWillUnmount', () => {
  163. const wrapper = shallow((
  164. <ModalWorkflowSource
  165. editorState={{}}
  166. entityType={{}}
  167. entity={{}}
  168. onComplete={() => {}}
  169. onClose={() => {}}
  170. />
  171. ));
  172. wrapper.instance().componentWillUnmount();
  173. expect(global.jQuery().off).toHaveBeenCalled();
  174. });
  175. describe('#onChosen', () => {
  176. it('works', () => {
  177. jest.spyOn(RichUtils, 'toggleLink');
  178. const onComplete = jest.fn();
  179. let editorState = EditorState.createWithContent(convertFromRaw({
  180. entityMap: {},
  181. blocks: [
  182. {
  183. key: 'a',
  184. text: 'test',
  185. }
  186. ]
  187. }));
  188. let selection = editorState.getSelection();
  189. selection = selection.merge({
  190. focusOffset: 4,
  191. });
  192. editorState = EditorState.acceptSelection(editorState, selection);
  193. const wrapper = shallow((
  194. <ModalWorkflowSource
  195. editorState={editorState}
  196. entityType={{}}
  197. entity={{}}
  198. onComplete={onComplete}
  199. onClose={() => {}}
  200. />
  201. ));
  202. wrapper.instance().onChosen({});
  203. expect(onComplete).toHaveBeenCalled();
  204. expect(RichUtils.toggleLink).toHaveBeenCalled();
  205. RichUtils.toggleLink.mockRestore();
  206. });
  207. it('block', () => {
  208. jest.spyOn(AtomicBlockUtils, 'insertAtomicBlock');
  209. const onComplete = jest.fn();
  210. let editorState = EditorState.createWithContent(convertFromRaw({
  211. entityMap: {},
  212. blocks: [
  213. {
  214. key: 'a',
  215. text: 'test',
  216. }
  217. ]
  218. }));
  219. let selection = editorState.getSelection();
  220. selection = selection.merge({
  221. focusOffset: 4,
  222. });
  223. editorState = EditorState.acceptSelection(editorState, selection);
  224. const wrapper = shallow((
  225. <ModalWorkflowSource
  226. editorState={editorState}
  227. entityType={{
  228. block: () => {},
  229. }}
  230. entity={{}}
  231. onComplete={onComplete}
  232. onClose={() => {}}
  233. />
  234. ));
  235. wrapper.instance().onChosen({});
  236. expect(onComplete).toHaveBeenCalled();
  237. expect(AtomicBlockUtils.insertAtomicBlock).toHaveBeenCalled();
  238. AtomicBlockUtils.insertAtomicBlock.mockRestore();
  239. });
  240. it('prefer_this_title_as_link_text', () => {
  241. jest.spyOn(Modifier, 'replaceText');
  242. const onComplete = jest.fn();
  243. let editorState = EditorState.createWithContent(convertFromRaw({
  244. entityMap: {},
  245. blocks: [
  246. {
  247. key: 'a',
  248. text: 'test',
  249. }
  250. ]
  251. }));
  252. let selection = editorState.getSelection();
  253. selection = selection.merge({
  254. focusOffset: 4,
  255. });
  256. editorState = EditorState.acceptSelection(editorState, selection);
  257. const wrapper = shallow((
  258. <ModalWorkflowSource
  259. editorState={editorState}
  260. entityType={{}}
  261. onComplete={onComplete}
  262. onClose={() => {}}
  263. />
  264. ));
  265. wrapper.instance().onChosen({
  266. url: 'example.com',
  267. prefer_this_title_as_link_text: true,
  268. });
  269. expect(onComplete).toHaveBeenCalled();
  270. expect(Modifier.replaceText).toHaveBeenCalled();
  271. Modifier.replaceText.mockRestore();
  272. });
  273. });
  274. it('#onClose', () => {
  275. const onClose = jest.fn();
  276. const wrapper = shallow((
  277. <ModalWorkflowSource
  278. editorState={{}}
  279. entityType={{}}
  280. entity={{}}
  281. onComplete={() => {}}
  282. onClose={onClose}
  283. />
  284. ));
  285. wrapper.instance().onClose({
  286. preventDefault: () => {},
  287. });
  288. expect(onClose).toHaveBeenCalled();
  289. });
  290. });