all files / test-utils/ mocks.js

100% Statements 67/67
100% Branches 46/46
100% Functions 19/19
100% Lines 63/63
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138           96× 303×     96× 305×                         84×   84× 62× 62× 62×     84×   67× 67× 96×   67× 67× 16×   16× 16×     60× 60× 78×   76×   60×     84×   134× 68× 68× 93× 93× 89× 89×   93× 93× 93×     40×   80×     52×   28×       93×     429×   990×     628×   362×         68×     66× 65×   323× 35×   29×     288× 28×   260×                       61×          
var camelCase = require('./exposed').camelCase
var deCamelCase = require('./exposed').deCamelCase
 
// These mocks provide the bare minimal functionality to enable testing
// They are incorrect by design. By no way are they intended to
// provide an accurate implementation of the corresponding DOM APIs.
 
function firstMatch(prefix, ary) {
  for (var i = 0; i < ary.length; i++) {
    if (ary[i].indexOf(prefix) === 0) return i
  }
}
function lastMatch(prefix, ary) {
  for (var i = ary.length; i--;) {
    if (ary[i].indexOf(prefix) === 0) return i
  }
}
 
// options:
// computedStyleAsArray (boolean) determines whether getComputedStyle()
//                                returns a plain array or an object.
// properties (object): a map of property names to either a string or
//                      string arrays (representing the values that can
//                      be set) or null if the property should appear as
//                      not supported
// rules (array<string>): the strings represent the rules supported
 
module.exports = function (global, options) {
  if (options == null) options = {}
 
  global.cleanupMocks = function(){
    delete global.cleanupMocks
    delete global.getComputedStyle
    delete global.document
  }
 
  global.getComputedStyle = function() {
    // sorting may not be necessary...
    var allProps = Object.keys(options.properties || {})
    var nonShortcuts = allProps.filter(function(v) {
      return firstMatch(v, allProps) === lastMatch(v, allProps)
    })
    var res
    if (options.computedStyleAsArray) {
      res = nonShortcuts.filter(function(p){
        return options.properties[p] != null
      }).map(deCamelCase)
      res.forEach(function(_p){
        var P = camelCase(_p)
        res[P] = typeof options.properties[P] === 'string' ? options.properties[P] : options.properties[P][0]
      })
      return res
    } else {
      res = {}
      nonShortcuts.filter(function(p){
        return options.properties[p] != null
      }).forEach(function(p){
        res[p] = typeof options.properties[p] === 'string' ? options.properties[p] : options.properties[p][0]
      })
      return res
    }
  }
  global.document = {
    createElement: function(tag) {
      if (tag === 'div') {
        var proxy = {}, style = {}, possibleValues = {}
        Object.keys(options.properties || {}).forEach(function(prop) {
          var value = options.properties[prop]
          if (value != null) {
            if (typeof value === 'string') value = [value]
            value.push('')
          }
          possibleValues[prop] = value
          style[prop] = value ? '' : null
          Object.defineProperty(proxy, prop, {
            configurable: true,
            enumerable: true,
            get: function() {return style[prop]},
            set: function(value) {
              if (
                possibleValues[prop] != null && possibleValues[prop].indexOf(value) > -1
              ) {
                style[prop] = '' + value
              } else {
                style[prop] = null
              }
            }
          })
          Object.defineProperty(proxy, deCamelCase(prop), {
            configurable: true,
            enumerable: true,
            get: function() {return style[prop] == null ? null: style[prop]},
            set: function(value) {
              if (
                possibleValues[prop] != null && possibleValues[prop].indexOf(value) > -1
              ) {
                style[prop] = '' + value
              } else {
                style[prop] = null
              }
            }
          })
        })
        return {
          style: proxy
        }
      } else if (tag === 'style') {
        return {
          set textContent(txt) {
            if (/^@media/.test(txt)) {
              if (options.rules != null && options.rules.indexOf(txt) > -1) {
                this.sheet.cssRules[0] = {cssText: txt}
              } else {
                this.sheet.cssRules[0] = {cssText: '@media not all {\n}'}
              }
            } else {
              if (options.rules != null && options.rules.indexOf(txt) > -1) {
                this.sheet.cssRules.length = 1
              } else {
                this.sheet.cssRules.length = 0
              }
            }
          },
          sheet: {
            cssRules: []
          }
        }
      } else {
        throw new Error('[DOM mocks] unsupported tag: '+ tag)
      }
    },
    documentElement : {
      appendChild: function(a) {return a},
      removeChild: function() {}
    }
  }
}