stubs.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* eslint no-restricted-globals: ["error", { "name": "jest", "message": "jest is not available in Storybook." }] */
  2. /**
  3. * Test stubs to mirror available global variables in Jest tests
  4. * and Storybook, avoid using the jest global as this is not
  5. * available in Storybook.
  6. * Those variables usually come from the back-end via templates.
  7. * See /wagtailadmin/templates/wagtailadmin/admin_base.html.
  8. */
  9. const wagtailConfig = {
  10. ADMIN_API: {
  11. DOCUMENTS: '/admin/api/main/documents/',
  12. IMAGES: '/admin/api/main/images/',
  13. PAGES: '/admin/api/main/pages/',
  14. EXTRA_CHILDREN_PARAMETERS: '',
  15. },
  16. ADMIN_URLS: {
  17. PAGES: '/admin/pages/',
  18. },
  19. CSRF_HEADER_NAME: 'x-xsrf-token',
  20. CSRF_TOKEN: 'potato',
  21. DATE_FORMATTING: {
  22. DATE_FORMAT: 'MMM. D, YYYY',
  23. SHORT_DATE_FORMAT: 'DD/MM/YYYY',
  24. },
  25. WAGTAIL_I18N_ENABLED: true,
  26. LOCALES: [
  27. {
  28. code: 'en',
  29. display_name: 'English',
  30. },
  31. {
  32. code: 'fr',
  33. display_name: 'French',
  34. },
  35. ],
  36. ACTIVE_CONTENT_LOCALE: 'en',
  37. };
  38. const configScript = Object.assign(document.createElement('script'), {
  39. id: 'wagtail-config',
  40. textContent: JSON.stringify(wagtailConfig),
  41. type: 'application/json',
  42. });
  43. document.body.appendChild(configScript);
  44. global.wagtail = {};
  45. /* use dummy content for onload handlers just so that we can verify that we've chosen the right one */
  46. global.IMAGE_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'image' };
  47. global.PAGE_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'page' };
  48. global.EMBED_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'embed' };
  49. global.DOCUMENT_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'document' };
  50. class PageChooserModal {}
  51. global.PageChooserModal = PageChooserModal;
  52. /** Mock window.scrollTo as not provided via JSDom */
  53. window.scrollTo = () => {};
  54. /** Mock console.warn to filter out warnings from React due to Draftail legacy Component API usage.
  55. * Draftail/Draft-js is unlikely to support these and the warnings are not useful for unit test output.
  56. */
  57. /* eslint-disable no-console */
  58. const consoleWarnOriginal = console.warn;
  59. console.warn = function filterWarnings(...args) {
  60. /* eslint-enable no-console */
  61. const [warning, component] = args;
  62. const legacyReactWarnings = [
  63. 'Warning: componentWillMount has been renamed, and is not recommended for use.',
  64. 'Warning: componentWillReceiveProps has been renamed, and is not recommended for use.',
  65. 'Warning: componentWillUpdate has been renamed, and is not recommended for use.',
  66. ];
  67. const ignoredComponents = ['DraftEditor', 'PluginEditor'];
  68. if (
  69. legacyReactWarnings.some((_) => warning.includes(_)) &&
  70. ignoredComponents.includes(component)
  71. ) {
  72. return;
  73. }
  74. consoleWarnOriginal.apply(console, args);
  75. };