const JSDOMEnvironment = require('jest-environment-jsdom').default;
const { TextEncoder, TextDecoder } = require('util');

class CustomEnvironment extends JSDOMEnvironment {
  constructor(config, context) {
    super(config, context);

    // Add polyfills to global before any test code runs
    this.global.TextEncoder = TextEncoder;
    this.global.TextDecoder = TextDecoder;

    // BroadcastChannel polyfill
    this.global.BroadcastChannel = class BroadcastChannel {
      constructor(name) {
        this.name = name;
      }
      postMessage() {}
      close() {}
      addEventListener() {}
      removeEventListener() {}
    };
  }
}

module.exports = CustomEnvironment;
