All files / src/utility runnerDetection.js

96.97% Statements 32/33
83.33% Branches 10/12
100% Functions 3/3
96.88% Lines 31/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71          15x   15x   15x 1x 1x       1x 1x   1x                 5x         5x 1x     1x 1x 1x 1x   1x       4x 1x 1x     3x 3x   3x 1x 1x     2x 1x 1x     1x 1x 1x   1x      
/* eslint no-prototype-builtins: "off" */
 
import { assign, isNil } from 'lodash';
import logger from './logger';
 
const RUNNER_NAME = 'ChameleonRunner';
 
const validateRunner = runner => typeof runner.sendMessage === 'function';
 
const handleRunnerOptions = () => {
  const urlParams = new URLSearchParams(window.location.search);
  Iif (!urlParams.has('options')) {
    return false;
  }
 
  const options = JSON.parse(urlParams.get('options'));
  options.os = urlParams.get('os');
 
  return options;
};
 
export default {
  /*
  Detects is app running inside runner device.
  Basic check is to check does ChameleonRunner global exist.
  */
  detectRunner() {
    const runnerDetection = {
      detected: false,
      valid: false,
    };
 
    if (window.location.search) {
      const options = handleRunnerOptions();
 
      // If options are present, don't validate further
      Eif (options) {
        runnerDetection.detected = true;
        runnerDetection.valid = true;
        runnerDetection.options = options;
 
        return runnerDetection;
      }
    }
 
    if (!window.hasOwnProperty(RUNNER_NAME)) {
      logger.error('ChameleonRunner not detected.');
      return runnerDetection;
    }
 
    const runner = window[RUNNER_NAME];
    runnerDetection.detected = true;
 
    if (isNil(runner)) {
      logger.warn('ChameleonRunner detected but has nullish value.', runner);
      return runnerDetection;
    }
 
    if (!validateRunner(runner)) {
      logger.warn('ChameleonRunner detected but is missing sendMessage method.', runner);
      return runnerDetection;
    }
 
    logger.info('ChameleonRunner detected.', runner);
    runnerDetection.valid = true;
    assign(runner, runnerDetection);
 
    return runner;
  },
};