stubs.js 2.7 KB

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