1- import after from './after'
2- import validate from './options'
3- import browser from './util/browser'
4- import Suite from './test'
1+ import after from './after' ;
2+ import validate from './options' ;
3+ import browser from './util/browser' ;
4+ import Suite from './test' ;
55
66export default class A11y {
77
@@ -11,140 +11,136 @@ export default class A11y {
1111 * @arg {object} options - the options
1212 * @returns {A11y } The react-a11y instance
1313 */
14- constructor ( ...args ) {
15- const {
14+ constructor ( ...args ) {
15+ const {
1616 React
1717 , ReactDOM
1818 , ...options
19- } = validate ( ...args )
19+ } = validate ( ...args ) ;
2020
21- this . options = options
22- this . React = React
23- this . ReactDOM = ReactDOM
24- this . suite = new Suite ( React , ReactDOM , this . options )
25- this . patchReact ( )
26- }
21+ this . options = options ;
22+ this . React = React ;
23+ this . ReactDOM = ReactDOM ;
24+ this . suite = new Suite ( React , ReactDOM , this . options ) ;
25+ this . patchReact ( ) ;
26+ }
2727
2828 /**
2929 * Patch React, replacing its createElement by our implementation
3030 * so we can run the tests
3131 * @returns {undefined }
3232 */
33- patchReact ( ) {
34-
35- // save old createElement
36- this . _createElement = this . React . createElement
37-
38- const that = this
39- this . React . createElement = function ( klass , _props = { } , ...children ) {
40-
41- // fix for props = null
42- const props = _props || { }
43-
44- // create a refs object to hold the ref.
45- // this needs to be an object so that it can be passed
46- // by reference, and hold chaning state.
47- const refs = typeof props . ref === 'string' ? props . ref : { }
48- const ref = typeof props . ref === 'string'
49- ? props . ref
50- : function ( node ) {
51- refs . node = node
52-
53- // maintain behaviour when ref prop was already set
54- if ( typeof props . ref === 'function' ) {
55- props . ref ( node )
56- }
57- }
58-
59- const newProps = typeof klass === 'string' ? { ...props , ref } : props
60-
61- const reactEl = that . _createElement ( klass , newProps , ...children )
62-
63- // only test html elements
64- if ( typeof klass === 'string' ) {
65-
66- const handler = that . failureHandler ( reactEl , refs )
67- const childrenForTest = children . length === 0
68- ? props . children || [ ]
69- : children
70-
71- that . suite . test ( klass , props , childrenForTest , handler )
72- }
73-
74- return reactEl
33+ patchReact ( ) {
34+ // save old createElement
35+ this . _createElement = this . React . createElement ;
36+
37+ const that = this ;
38+ this . React . createElement = function ( klass , _props = { } , ...children ) {
39+ // fix for props = null
40+ const props = _props || { } ;
41+
42+ // create a refs object to hold the ref.
43+ // this needs to be an object so that it can be passed
44+ // by reference, and hold chaning state.
45+ const refs = typeof props . ref === 'string' ? props . ref : { } ;
46+ const ref = typeof props . ref === 'string'
47+ ? props . ref
48+ : ( node ) => {
49+ refs . node = node ;
50+
51+ // maintain behaviour when ref prop was already set
52+ if ( typeof props . ref === 'function' ) {
53+ props . ref ( node ) ;
54+ }
55+ } ;
56+
57+ const newProps = typeof klass === 'string' ? { ...props , ref } : props ;
58+
59+ const reactEl = that . _createElement ( klass , newProps , ...children ) ;
60+
61+ // only test html elements
62+ if ( typeof klass === 'string' ) {
63+ const handler = that . failureHandler ( reactEl , refs ) ;
64+ const childrenForTest = children . length === 0
65+ ? props . children || [ ]
66+ : children ;
67+
68+ that . suite . test ( klass , props , childrenForTest , handler ) ;
69+ }
70+
71+ return reactEl ;
72+ } ;
7573 }
76- }
7774
78- /**
79- * Restore React and all components as if we were never here
80- * @returns {undefined }
75+ /**
76+ * Restore React and all components as if we were never here
77+ * @returns {undefined }
8178 */
82- restoreAll ( ) {
83- this . React . createElement = this . _createElement
84- after . restorePatchedMethods ( )
85- }
79+ restoreAll ( ) {
80+ this . React . createElement = this . _createElement ;
81+ after . restorePatchedMethods ( ) ;
82+ }
8683
8784 /**
8885 * Creates a failure handler based on the element that was created
8986 * @arg {object} reactEl - The react element this failure is for
9087 * @arg {object} ref - The object that holds the DOM node (passed by ref)
9188 * @returns {function } A handler that knows everything it needs to know
9289 */
93- failureHandler ( reactEl , ref ) {
94- const {
95- reporter
96- , filterFn
97- } = this . options
98-
99- /**
100- * @arg {string} errInfo - All the error info (see docs what this means)
101- * @returns {undefined }
102- */
103- return function ( errInfo ) {
104-
105- // get the owning component (the one that has
106- // the element in its render fn)
107- const owner = reactEl . _owner
108-
109- // if there is an owner, use its name
110- // if not, use the tagname of the violating elemnent
111- const displayName = owner && owner . getName ( )
112-
113- // stop if we're not allowed to proceed
114- if ( ! filterFn ( displayName , errInfo . props . id , errInfo . msg ) ) {
115- return
116- }
117-
118- // gather all info for the reporter
119- const info = {
120- ...errInfo
121- , displayName
122- }
123-
124- // if we need to include the rendered node, we need to wait until
125- // the owner has rendered
126- // TODO: reduce the number of case where ther is no instance
127- // by forcing every component to have one.
128- if ( browser && ! this . __sync && owner && owner . _instance ) {
129- const instance = owner . _instance
130- // Cannot log a node reference until the component is in the DOM,
131- // so defer the call until componentDidMount or componentDidUpdate.
132- after . render ( instance , function ( ) {
133- // unpack the ref
134- let DOMNode = false
135- if ( typeof ref === 'string' ) {
136- DOMNode = this . ReactDOM . findDOMNode ( instance . refs [ ref ] )
137- } else if ( 'node' in ref ) {
138- DOMNode = ref . node
139- } else {
140- throw new Error ( 'could not resolve ref' )
141- }
142-
143- reporter ( { ...info , DOMNode } )
144- } . bind ( this ) )
145- } else {
146- reporter ( info )
147- }
148- } . bind ( this )
149- }
90+ failureHandler ( reactEl , ref ) {
91+ const {
92+ reporter,
93+ filterFn
94+ } = this . options ;
95+
96+ /**
97+ * @arg {string} errInfo - All the error info (see docs what this means)
98+ * @returns {undefined }
99+ */
100+ return function ( errInfo ) {
101+ // get the owning component (the one that has
102+ // the element in its render fn)
103+ const owner = reactEl . _owner ;
104+
105+ // if there is an owner, use its name
106+ // if not, use the tagname of the violating elemnent
107+ const displayName = owner && owner . getName ( ) ;
108+
109+ // stop if we're not allowed to proceed
110+ if ( ! filterFn ( displayName , errInfo . props . id , errInfo . msg ) ) {
111+ return ;
112+ }
113+
114+ // gather all info for the reporter
115+ const info = {
116+ ...errInfo ,
117+ displayName
118+ } ;
119+
120+ // if we need to include the rendered node, we need to wait until
121+ // the owner has rendered
122+ // TODO: reduce the number of case where ther is no instance
123+ // by forcing every component to have one.
124+ if ( browser && ! this . __sync && owner && owner . _instance ) {
125+ const instance = owner . _instance ;
126+ // Cannot log a node reference until the component is in the DOM,
127+ // so defer the call until componentDidMount or componentDidUpdate.
128+ after . render ( instance , ( ) => {
129+ // unpack the ref
130+ let DOMNode = false ;
131+ if ( typeof ref === 'string' ) {
132+ DOMNode = this . ReactDOM . findDOMNode ( instance . refs [ ref ] ) ; // TODO: replace use of findDOMNode
133+ } else if ( 'node' in ref ) {
134+ DOMNode = ref . node ;
135+ } else {
136+ throw new Error ( 'could not resolve ref' ) ;
137+ }
138+
139+ reporter ( { ...info , DOMNode } ) ;
140+ } ) ;
141+ } else {
142+ reporter ( info ) ;
143+ }
144+ } . bind ( this ) ;
145+ }
150146}
0 commit comments