1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | const runAxeCheck = require('@instructure/ui-axe-check').default
|
25 | const { ReactWrapper, mount } = require('enzyme')
|
26 | const keycode = require('keycode')
|
27 |
|
28 | ReactWrapper.prototype.getA11yViolations = function (done, options) {
|
29 | runAxeCheck(this.getDOMNode(), options).then((result) => {
|
30 | if (result instanceof Error) {
|
31 | done(result)
|
32 | } else {
|
33 | done()
|
34 | }
|
35 | }, done)
|
36 | }
|
37 |
|
38 | ReactWrapper.prototype.unwrap = function () {
|
39 | return this.node
|
40 | }
|
41 |
|
42 | ReactWrapper.prototype.getAttribute = function (attrName) {
|
43 | return this.getDOMNode().getAttribute(attrName)
|
44 | }
|
45 |
|
46 | ReactWrapper.prototype.dispatchNativeEvent = function (type, attrs) {
|
47 | const event = new Event(type)
|
48 | const domNode = this.getDOMNode()
|
49 |
|
50 | domNode.dispatchEvent(event, attrs)
|
51 | }
|
52 |
|
53 | ReactWrapper.prototype.dispatchNativeKeyboardEvent = function (type, keyCode) {
|
54 | const event = document.createEventObject ? document.createEventObject() : document.createEvent('Events')
|
55 |
|
56 | if (event.initEvent) {
|
57 | event.initEvent(type, true, true)
|
58 | }
|
59 |
|
60 | event.keyCode = keycode(keyCode)
|
61 |
|
62 | this.getDOMNode().dispatchEvent(event)
|
63 | }
|
64 |
|
65 | ReactWrapper.prototype.dispatchNativeMouseEvent = function (type, attrs) {
|
66 | const event = new MouseEvent(type, attrs)
|
67 | const domNode = this.getDOMNode()
|
68 | domNode.dispatchEvent(event)
|
69 | }
|
70 |
|
71 | ReactWrapper.prototype.keyDown = function (code) {
|
72 | const keyCode = keycode(code)
|
73 | this.simulate('keyDown', { keyCode: keyCode, key: keyCode, which: keyCode })
|
74 | }
|
75 |
|
76 | ReactWrapper.prototype.keyUp = function (code) {
|
77 | const keyCode = keycode(code)
|
78 | this.simulate('keyUp', { keyCode: keyCode, key: keyCode, which: keyCode })
|
79 | }
|
80 |
|
81 | ReactWrapper.prototype.click = function () {
|
82 | this.simulate('click')
|
83 | }
|
84 |
|
85 | ReactWrapper.prototype.mouseOver = function () {
|
86 | this.simulate('mouseOver')
|
87 | }
|
88 |
|
89 | ReactWrapper.prototype.mouseOut = function () {
|
90 | this.simulate('mouseOut')
|
91 | }
|
92 |
|
93 | ReactWrapper.prototype.blur = function () {
|
94 | this.simulate('blur')
|
95 | }
|
96 |
|
97 | ReactWrapper.prototype.focus = function () {
|
98 | this.getDOMNode().focus()
|
99 | }
|
100 |
|
101 | ReactWrapper.prototype.focused = function () {
|
102 | const domNode = this.getDOMNode()
|
103 | return domNode && domNode === document.activeElement
|
104 | }
|
105 |
|
106 | ReactWrapper.prototype.setValue = function (value) {
|
107 | const domNode = this.getDOMNode()
|
108 |
|
109 | this.simulate('focus')
|
110 |
|
111 | domNode.value = value
|
112 |
|
113 | this.simulate('keyUp')
|
114 | this.simulate('keyDown')
|
115 |
|
116 | this.simulate('change', { target: { value } })
|
117 | }
|
118 |
|
119 | ReactWrapper.prototype.getKey = function () {
|
120 | return this.key()
|
121 | }
|
122 |
|
123 | ReactWrapper.prototype.getComputedStyle = function () {
|
124 | const domNode = this.getDOMNode()
|
125 | return domNode && window && window.getComputedStyle(domNode)
|
126 | }
|
127 |
|
128 | ReactWrapper.prototype.tagName = function () {
|
129 | const domNode = this.getDOMNode()
|
130 | return domNode && domNode.tagName.toUpperCase()
|
131 | }
|
132 |
|
133 | ReactWrapper.prototype.findText = function (text) {
|
134 | return this.findWhere(n => n.text() === text)
|
135 | }
|
136 |
|
137 | ReactWrapper.prototype.findElementWithText = function (type, text) {
|
138 | return this.findWhere(n => n.type() === type && n.text() === text)
|
139 | }
|
140 |
|
141 | const originalRef = ReactWrapper.prototype.ref
|
142 | ReactWrapper.prototype.ref = function () {
|
143 | const ref = arguments[0]
|
144 | const instance = this.instance()
|
145 |
|
146 | if (instance.hasOwnProperty(ref)) {
|
147 | return new ReactWrapper(instance[ref], true)
|
148 | } else if (typeof originalRef === 'function') {
|
149 | return originalRef.apply(this, arguments)
|
150 | }
|
151 | }
|
152 |
|
153 | module.exports = {
|
154 | ReactWrapper,
|
155 | mount
|
156 | }
|