// @flow
import '@babel/polyfill';
import 'jest-styled-components';
import Adapter from 'enzyme-adapter-react-16';
import { configure } from 'enzyme';
import registerRequireContextHook from 'babel-plugin-require-context-hook/register';

const FONT_SIZE_REGEX = new RegExp(/[0-9]+(\.[0-9]+)?(px)/);

const HEX_CODE_REGEX = new RegExp(
  /#?([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})/,
);

expect.extend({
  toBeHexCode(code) {
    if (HEX_CODE_REGEX.test(code)) {
      return {
        pass: true,
        message: () => '',
      };
    }
    return {
      pass: false,
      message: () => `${code} is not a valid hex code`,
    };
  },
  toBeFontSize(size) {
    if (FONT_SIZE_REGEX.test(size)) {
      return {
        pass: true,
        message: () => '',
      };
    }

    return {
      pass: false,
      message: () => `${size} is not a valid font size`,
    };
  },
});

if (window.matchMedia === undefined) {
  window.matchMedia = () => ({
    matches: true,
    addListener() {},
  });
}

registerRequireContextHook();

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