require('@testing-library/jest-dom');

const { default: Adapter } = require('@cfaester/enzyme-adapter-react-18');
const Enzyme = require('enzyme');
const util = require('node:util');

global.fetch = require('jest-fetch-mock');

Enzyme.configure({ adapter: new Adapter() });

global.requestAnimationFrame = (callback) => callback();

Object.defineProperty(global, 'TextEncoder', {
  value: util.TextEncoder,
});

// https://github.com/esphen/jest-prop-type-error/blob/master/index.js
// This mock will make tests fail when props error occurs.
const { error, warn } = console;
// eslint-disable-next-line no-console
console.error = (message, ...args) => {
  if (/(Invalid prop|Failed prop type)/i.test(message)) {
    throw new Error(message);
  }

  error.apply(console, [message, ...args]);
};

// eslint-disable-next-line no-console
console.warn = (message, ...args) => {
  if (/(Call to useTheme outside a ThemeProvider)/i.test(message)) {
    return;
  }

  warn.apply(console, [message, ...args]);
};
