2
0

stubs.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. global.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. DATE_FORMATTING: {
  20. DATE_FORMAT: 'MMM. D, YYYY',
  21. SHORT_DATE_FORMAT: 'DD/MM/YYYY',
  22. },
  23. WAGTAIL_I18N_ENABLED: true,
  24. LOCALES: [
  25. {
  26. code: 'en',
  27. display_name: 'English',
  28. },
  29. {
  30. code: 'fr',
  31. display_nam: 'French',
  32. },
  33. ],
  34. ACTIVE_LOCALE: 'en',
  35. };
  36. const script = document.createElement('script');
  37. script.type = 'application/json';
  38. script.id = 'wagtail-config';
  39. script.textContent = JSON.stringify({ CSRF_TOKEN: 'potato' });
  40. document.body.appendChild(script);
  41. global.wagtailVersion = '1.6a1';
  42. global.wagtail = {};
  43. global.chooserUrls = {
  44. documentChooser: '/admin/documents/chooser/',
  45. emailLinkChooser: '/admin/choose-email-link/',
  46. anchorLinkChooser: '/admin/choose-anchor-link',
  47. embedsChooser: '/admin/embeds/chooser/',
  48. externalLinkChooser: '/admin/choose-external-link/',
  49. imageChooser: '/admin/images/chooser/',
  50. pageChooser: '/admin/choose-page/',
  51. };
  52. /* use dummy content for onload handlers just so that we can verify that we've chosen the right one */
  53. global.IMAGE_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'image' };
  54. global.PAGE_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'page' };
  55. global.EMBED_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'embed' };
  56. global.DOCUMENT_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'document' };
  57. class PageChooserModal {}
  58. global.PageChooserModal = PageChooserModal;
  59. /** Mock window.scrollTo as not provided via JSDom */
  60. window.scrollTo = () => {};
  61. /** Mock console.warn to filter out warnings from React due to Draftail legacy Component API usage.
  62. * Draftail/Draft-js is unlikely to support these and the warnings are not useful for unit test output.
  63. */
  64. /* eslint-disable no-console */
  65. const consoleWarnOriginal = console.warn;
  66. console.warn = function filterWarnings(...args) {
  67. /* eslint-enable no-console */
  68. const [warning, component] = args;
  69. const legacyReactWarnings = [
  70. 'Warning: componentWillMount has been renamed, and is not recommended for use.',
  71. 'Warning: componentWillReceiveProps has been renamed, and is not recommended for use.',
  72. 'Warning: componentWillUpdate has been renamed, and is not recommended for use.',
  73. ];
  74. const ignoredComponents = ['DraftEditor', 'PluginEditor'];
  75. if (
  76. legacyReactWarnings.some((_) => warning.includes(_)) &&
  77. ignoredComponents.includes(component)
  78. ) {
  79. return;
  80. }
  81. consoleWarnOriginal.apply(console, args);
  82. };