setupTests.jsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { defaultConfig } from 'antd/lib/theme/internal';
  2. defaultConfig.hashed = false;
  3. const localStorageMock = {
  4. getItem: jest.fn(),
  5. setItem: jest.fn(),
  6. removeItem: jest.fn(),
  7. clear: jest.fn(),
  8. };
  9. global.localStorage = localStorageMock;
  10. Object.defineProperty(URL, 'createObjectURL', {
  11. writable: true,
  12. value: jest.fn(),
  13. });
  14. class Worker {
  15. constructor(stringUrl) {
  16. this.url = stringUrl;
  17. this.onmessage = () => {};
  18. }
  19. postMessage(msg) {
  20. this.onmessage(msg);
  21. }
  22. }
  23. window.Worker = Worker;
  24. if (typeof window !== 'undefined') {
  25. // ref: https://github.com/ant-design/ant-design/issues/18774
  26. if (!window.matchMedia) {
  27. Object.defineProperty(global.window, 'matchMedia', {
  28. writable: true,
  29. configurable: true,
  30. value: jest.fn(() => ({
  31. matches: false,
  32. addListener: jest.fn(),
  33. removeListener: jest.fn(),
  34. })),
  35. });
  36. }
  37. if (!window.matchMedia) {
  38. Object.defineProperty(global.window, 'matchMedia', {
  39. writable: true,
  40. configurable: true,
  41. value: jest.fn((query) => ({
  42. matches: query.includes('max-width'),
  43. addListener: jest.fn(),
  44. removeListener: jest.fn(),
  45. })),
  46. });
  47. }
  48. }
  49. const errorLog = console.error;
  50. Object.defineProperty(global.window.console, 'error', {
  51. writable: true,
  52. configurable: true,
  53. value: (...rest) => {
  54. const logStr = rest.join('');
  55. if (
  56. logStr.includes(
  57. 'Warning: An update to %s inside a test was not wrapped in act(...)',
  58. )
  59. ) {
  60. return;
  61. }
  62. errorLog(...rest);
  63. },
  64. });