Source: utility/host.test.js

import {Host} from './host';

const originalInnerHtml = document.body.innerHTML;
afterEach(() => {
  document.body.innerHTML = originalInnerHtml;
});

/** Perform actions manually before each test
 *
 * We cannot use Jest's `beforeEach`,
 * because it does not support passing parameters.
 * @see https://github.com/jasmine/jasmine/issues/1274
 * @param {Location} [scriptSrc] - Value for the `src` attribute of the script
 */
function beforeEachCustom(scriptSrc) {
  const defaultScriptSrc = '/js/gf-site.js';
  let html = '';

  // The page to load should have a standard doctype and load our JavaScript
  scriptSrc = scriptSrc || defaultScriptSrc;
  html = '<!DOCTYPE html><html><head><script src="' + scriptSrc + '" /></head><body><p>Hello, World!</p></body></html>';
  document.body.innerHTML = html;
}

const domain = 'goldenfrog.com';
test(domain, () => {
  jsdom.reconfigure({
    url: 'https://' + domain + '/vyprvpn'
  });
  beforeEachCustom();

  const host = new Host();
  expect(host.isPrimaryDomain).toBeTruthy();
  expect(host.isProduction).toBeTruthy();
  expect(host.isStaging).toBeFalsy();
  expect(host.isTesting).toBeFalsy();
  expect(host.isDevelopment).toBeFalsy();
  expect(host.environment).toEqual('production');
  expect(host.locationDomain).toEqual('goldenfrog.com');
});

const domain2 = 'not_us.com';
const domain2b = 'my.cdn.of.choice.com';
test(domain2, () => {
  jsdom.reconfigure({
    url: 'https://' + domain2 + '/vyprvpn'
  });
  const scriptSrc = 'https://' + domain2b + '/js/gf-site.js';
  beforeEachCustom(scriptSrc);

  const host = new Host();
  expect(host.isPrimaryDomain).toBeFalsy();
  expect(host.isProduction).toBeTruthy();
  expect(host.isStaging).toBeFalsy();
  expect(host.isTesting).toBeFalsy();
  expect(host.isDevelopment).toBeFalsy();
  expect(host.environment).toEqual('production');
  expect(host.locationDomain).toEqual('not_us.com');
});

const domain3 = 'www.test.vyprvpn.com';
test(domain3, () => {
  jsdom.reconfigure({
    url: 'https://' + domain3 + '/vyprvpn',
  });
  beforeEachCustom();

  const host = new Host();
  expect(host.isPrimaryDomain).toBeTruthy();
  expect(host.isProduction).toBeFalsy();
  expect(host.isStaging).toBeFalsy();
  expect(host.isTesting).toBeTruthy();
  expect(host.isDevelopment).toBeFalsy();
  expect(host.environment).toEqual('testing');
  expect(host.locationDomain).toEqual('vyprvpn.com');
});

const domain4 = 'www.staging.static.vyprvpn.com';
test(domain4, () => {
  jsdom.reconfigure({
    url: 'https://' + domain4 + '/vyprvpn',
  });
  beforeEachCustom();

  const host = new Host();
  expect(host.isPrimaryDomain).toBeTruthy();
  expect(host.isProduction).toBeFalsy();
  expect(host.isStaging).toBeTruthy();
  expect(host.isTesting).toBeFalsy();
  expect(host.isDevelopment).toBeFalsy();
  expect(host.environment).toEqual('staging');
  expect(host.locationDomain).toEqual('vyprvpn.com');
});

const domain5 = '10.47.52.37';
test(domain5, () => {
  jsdom.reconfigure({
    url: 'https://' + domain5 + '/vyprvpn',
  });
  beforeEachCustom();

  const host = new Host();
  expect(host.isPrimaryDomain).toBeFalsy();
  expect(host.isProduction).toBeFalsy();
  expect(host.isStaging).toBeFalsy();
  expect(host.isTesting).toBeFalsy();
  expect(host.isDevelopment).toBeTruthy();
  expect(host.environment).toEqual('development');
  expect(host.locationDomain).toEqual('52.37');
});


const domain6 = 'vyprvpn.com';
test(domain6, () => {
  jsdom.reconfigure({
    url: 'https://' + domain6,
  });
  beforeEachCustom();

  const host = new Host();
  expect(host.isPrimaryDomain).toBeTruthy();
  expect(host.isProduction).toBeTruthy();
  expect(host.isStaging).toBeFalsy();
  expect(host.isTesting).toBeFalsy();
  expect(host.isDevelopment).toBeFalsy();
  expect(host.environment).toEqual('production');
  expect(host.locationDomain).toEqual('vyprvpn.com');
});