forked from Swaagie/assume-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (93 loc) · 2.85 KB
/
index.js
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
/* eslint no-invalid-this: 0 */
import TestUtils from 'react-addons-test-utils';
import React from 'react';
export default function pluginAssumeReact(assume, util) {
const a = assume().a;
/**
* Is the value allowed for the React Component.propTypes.
*
* @param {String} prop Propety name
* @param {String} value Value to test against
* @param {String} msg Custom message
* @returns {Assert} reference to self
* @api public
*/
assume.add('allowProp', function allowProp(prop, value, msg) {
let result = this.value.propTypes[prop]({ [prop]: value }, prop);
if (result) {
result = util.format(result.message);
}
this.test(result === null, msg, result);
});
/**
* Is the value allowed for the React Component.propTypes.
*
* @param {Component} type React Component definition.
* @param {String} msg Custom message
* @returns {Assert} reference to self
* @api public
*/
assume.add('elementOfType', function allowProp(element, msg) {
if (typeof element === 'string') {
return this.test(this.value === element || this.value.type.name === element, msg);
}
//
// Unpack the element type if required.
//
element = element.type || element;
return this.test(TestUtils.isElementOfType(this.value, element), msg);
});
/**
* Check if the Component.props have the property with value.
*
* @param {Array} args Prop and value to test against
* @returns {Assert} reference to self
* @api public
*/
assume.add('prop, props', function props(...args) {
const test = this.clone(this.value.props);
return test.property.apply(test, args);
});
/**
* Check if the Component.props.className contains the value.
*
* @param {String} value Value to test against
* @returns {Assert} reference to self
* @api public
*/
assume.add('className', function className(value) {
const test = this.clone(this.value.props.className);
return test.includes(value);
});
/**
* Check if the Component has child.
*
* @param {String} value Value to test against
* @returns {Assert} reference to self
* @api public
*/
assume.add('child, children', function childs(value) {
let children = React.Children.toArray(this.value.props.children);
const self = this;
if (!Array.isArray(value)) {
value = [ value ];
}
return util.each(children, function each(child, i) {
self.clone(child).elementOfType(value[i]);
});
});
/**
* Proxy type of check to test against React Elements.
*
* @param {Mixed} of Value to typeof
* @param {String} msg Custom message
* @returns {Assert} reference to self
* @api public
*/
assume.add('a, an', function an(of, msg) {
if (of === 'element') {
return this.test(TestUtils.isElement(this.value) === true, msg);
}
return a.call(this, of, msg);
});
}