aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@concordance/react/lib/testJsonFactory.js
blob: c3e97a620faaf8b8b951a5f728a3b8aa8ce3ca07 (plain)
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
'use strict'

const arrify = require('arrify')

function factory (api, element) {
  const tag = Symbol('@concordance/react.TestJsonValue')

  function describe (props) {
    const obj = props.value

    const name = obj.type
    const children = arrify(obj.children)
    const properties = Object.assign({}, obj.props)
    const hasProperties = Object.keys(properties).length > 0

    return new DescribedTestJsonValue(Object.assign({
      children,
      hasProperties,
      hasTypeFn: false,
      name,
      properties,
      typeFn: null,
      isList: children.length > 0
    }, props))
  }

  function deserialize (state, recursor) {
    return new DeserializedTestJsonValue(state, recursor)
  }

  class TestJsonValue extends element.ElementValue {
    compare (expected) {
      // Allow expected value to be a React element.
      return (this.tag === expected.tag || expected.tag === element.tag) && this.name === expected.name
        ? api.SHALLOW_EQUAL
        : api.UNEQUAL
    }

    prepareDiff (expected) {
      return {
        // Allow expected value to be a React element.
        compareResult: this.tag === expected.tag || expected.tag === element.tag
          ? api.SHALLOW_EQUAL
          : api.UNEQUAL
      }
    }
  }
  Object.defineProperty(TestJsonValue.prototype, 'tag', {value: tag})

  const DescribedTestJsonValue = element.DescribedMixin(TestJsonValue)
  const DeserializedTestJsonValue = element.DeserializedMixin(TestJsonValue)

  return {
    describe,
    deserialize,
    tag
  }
}
module.exports = factory