UNPKG

4.57 kBJavaScriptView Raw
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2015 - present Instructure, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24const runAxeCheck = require('@instructure/ui-axe-check').default
25const { ReactWrapper, mount } = require('enzyme')
26const keycode = require('keycode')
27
28ReactWrapper.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
38ReactWrapper.prototype.unwrap = function () {
39 return this.node
40}
41
42ReactWrapper.prototype.getAttribute = function (attrName) {
43 return this.getDOMNode().getAttribute(attrName)
44}
45
46ReactWrapper.prototype.dispatchNativeEvent = function (type, attrs) {
47 const event = new Event(type)
48 const domNode = this.getDOMNode()
49
50 domNode.dispatchEvent(event, attrs)
51}
52
53ReactWrapper.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
65ReactWrapper.prototype.dispatchNativeMouseEvent = function (type, attrs) {
66 const event = new MouseEvent(type, attrs)
67 const domNode = this.getDOMNode()
68 domNode.dispatchEvent(event)
69}
70
71ReactWrapper.prototype.keyDown = function (code) {
72 const keyCode = keycode(code)
73 this.simulate('keyDown', { keyCode: keyCode, key: keyCode, which: keyCode })
74}
75
76ReactWrapper.prototype.keyUp = function (code) {
77 const keyCode = keycode(code)
78 this.simulate('keyUp', { keyCode: keyCode, key: keyCode, which: keyCode })
79}
80
81ReactWrapper.prototype.click = function () {
82 this.simulate('click')
83}
84
85ReactWrapper.prototype.mouseOver = function () {
86 this.simulate('mouseOver')
87}
88
89ReactWrapper.prototype.mouseOut = function () {
90 this.simulate('mouseOut')
91}
92
93ReactWrapper.prototype.blur = function () {
94 this.simulate('blur')
95}
96
97ReactWrapper.prototype.focus = function () {
98 this.getDOMNode().focus()
99}
100
101ReactWrapper.prototype.focused = function () {
102 const domNode = this.getDOMNode()
103 return domNode && domNode === document.activeElement
104}
105
106ReactWrapper.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
119ReactWrapper.prototype.getKey = function () {
120 return this.key()
121}
122
123ReactWrapper.prototype.getComputedStyle = function () {
124 const domNode = this.getDOMNode()
125 return domNode && window && window.getComputedStyle(domNode)
126}
127
128ReactWrapper.prototype.tagName = function () {
129 const domNode = this.getDOMNode()
130 return domNode && domNode.tagName.toUpperCase()
131}
132
133ReactWrapper.prototype.findText = function (text) {
134 return this.findWhere(n => n.text() === text)
135}
136
137ReactWrapper.prototype.findElementWithText = function (type, text) {
138 return this.findWhere(n => n.type() === type && n.text() === text)
139}
140
141const originalRef = ReactWrapper.prototype.ref
142ReactWrapper.prototype.ref = function () {
143 const ref = arguments[0]
144 const instance = this.instance()
145 // eslint-disable-next-line no-prototype-builtins
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
153module.exports = {
154 ReactWrapper,
155 mount
156}