2
0

stubs.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_name: '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({
  40. CSRF_HEADER_NAME: 'x-xsrf-token',
  41. CSRF_TOKEN: 'potato',
  42. });
  43. document.body.appendChild(script);
  44. global.wagtailVersion = '1.6a1';
  45. global.wagtail = {};
  46. /* use dummy content for onload handlers just so that we can verify that we've chosen the right one */
  47. global.IMAGE_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'image' };
  48. global.PAGE_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'page' };
  49. global.EMBED_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'embed' };
  50. global.DOCUMENT_CHOOSER_MODAL_ONLOAD_HANDLERS = { type: 'document' };
  51. class PageChooserModal {}
  52. global.PageChooserModal = PageChooserModal;
  53. /** Mock window.scrollTo as not provided via JSDom */
  54. window.scrollTo = () => {};
  55. /** Mock console.warn to filter out warnings from React due to Draftail legacy Component API usage.
  56. * Draftail/Draft-js is unlikely to support these and the warnings are not useful for unit test output.
  57. */
  58. /* eslint-disable no-console */
  59. const consoleWarnOriginal = console.warn;
  60. console.warn = function filterWarnings(...args) {
  61. /* eslint-enable no-console */
  62. const [warning, component] = args;
  63. const legacyReactWarnings = [
  64. 'Warning: componentWillMount has been renamed, and is not recommended for use.',
  65. 'Warning: componentWillReceiveProps has been renamed, and is not recommended for use.',
  66. 'Warning: componentWillUpdate has been renamed, and is not recommended for use.',
  67. ];
  68. const ignoredComponents = ['DraftEditor', 'PluginEditor'];
  69. if (
  70. legacyReactWarnings.some((_) => warning.includes(_)) &&
  71. ignoredComponents.includes(component)
  72. ) {
  73. return;
  74. }
  75. consoleWarnOriginal.apply(console, args);
  76. };