browser.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import '@babel/polyfill';
  2. import path from 'path';
  3. import Enzyme from 'enzyme';
  4. import Adapter from 'enzyme-adapter-react-16';
  5. Enzyme.configure({ adapter: new Adapter() });
  6. import { JSDOM } from 'jsdom';
  7. import { EventEmitter } from 'events';
  8. import register from 'ignore-styles';
  9. // eslint-disable-next-line no-undefined
  10. register(['.scss', '.png', '.wav', '.mp3', '.mp4', '.svg'], (module, filename) => {
  11. if (['.png', '.wav', '.mp3', '.mp4', '.svg'].some(extension => filename.endsWith(extension))) {
  12. module.exports = path.basename(filename);
  13. }
  14. });
  15. const exposedProperties = ['window', 'navigator', 'document'];
  16. const blacklistProperties = ['localStorage', 'sessionStorage'];
  17. global.window = (new JSDOM('')).window;
  18. global.document = window.document;
  19. class URL {
  20. static createObjectURL() {
  21. return 'foo';
  22. }
  23. }
  24. global.URL = URL;
  25. Object.keys(window).forEach(property => {
  26. if (!blacklistProperties.includes(property) && typeof global[property] === 'undefined') {
  27. exposedProperties.push(property);
  28. global[property] = window[property];
  29. }
  30. });
  31. const geolocation = {
  32. getCurrentPosition: () => null
  33. };
  34. global.navigator = {
  35. userAgent: 'node.js',
  36. geolocation
  37. };
  38. class MockImage extends EventEmitter {
  39. constructor() {
  40. super();
  41. this.src = null;
  42. this.onload = () => null;
  43. setTimeout(() => {
  44. this.onload();
  45. }, 50);
  46. }
  47. }
  48. global.Image = MockImage;
  49. window.HTMLMediaElement.prototype.load = () => null;
  50. window.HTMLMediaElement.prototype.play = () => null;
  51. window.HTMLMediaElement.prototype.pause = () => null;
  52. window.HTMLMediaElement.prototype.addTextTrack = () => null;
  53. window.requestAnimationFrame = () => null;
  54. window.cancelAnimationFrame = () => null;
  55. class MockFormData {
  56. constructor() {
  57. this.data = {};
  58. }
  59. set(key, value) {
  60. this.data[key] = value;
  61. }
  62. }
  63. global.FormData = MockFormData;
  64. global.documentRef = document; // eslint-disable-line no-undef