Skip to content
This repository was archived by the owner on Jan 20, 2022. It is now read-only.

Commit 1350cc8

Browse files
authored
fix(conformance-tests): Fix event handler tests (#98)
* fix tests for events, introduce tests for handlers * remove unnecessary import * rename 'wrappedComponent' to 'component'
1 parent 22fc624 commit 1350cc8

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

test/specs/commonTests/isConformant.tsx

+45-4
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,6 @@ export default (Component, options: any = {}) => {
253253
// onKeyDown => keyDown
254254
const eventName = _.camelCase(listenerName.replace('on', ''))
255255

256-
// onKeyDown => handleKeyDown
257-
const handlerName = _.camelCase(listenerName.replace('on', 'handle'))
258-
259256
const handlerSpy = jest.fn()
260257
const props = {
261258
...requiredProps,
@@ -286,6 +283,11 @@ export default (Component, options: any = {}) => {
286283
if (customHandler) {
287284
customHandler(eventShape)
288285
} else {
286+
if (Component.propTypes[listenerName]) {
287+
throw new Error(
288+
`Handler for '${listenerName}' is not passed to child event emitter element <${eventTarget.type()} />`,
289+
)
290+
}
289291
return
290292
}
291293

@@ -298,6 +300,9 @@ export default (Component, options: any = {}) => {
298300
// ^ was not called once on "blur"
299301
const leftPad = ' '.repeat(info.displayName.length + listenerName.length + 3)
300302

303+
// onKeyDown => handleKeyDown
304+
const handlerName = _.camelCase(listenerName.replace('on', 'handle'))
305+
301306
try {
302307
expect(handlerSpy).toHaveBeenCalled()
303308
} catch (err) {
@@ -313,7 +318,10 @@ export default (Component, options: any = {}) => {
313318

314319
if (_.has(Component.propTypes, listenerName)) {
315320
expectedArgs = [eventShape, component.props()]
316-
errorMessage = 'was not called with (event, data)'
321+
errorMessage =
322+
'was not called with (event, data).\n' +
323+
`Ensure that 'props' object is passed to '${listenerName}'\n` +
324+
`event handler of <${Component.displayName} />.`
317325
}
318326

319327
// Components should return the event first, then any data
@@ -438,4 +446,37 @@ export default (Component, options: any = {}) => {
438446
expect(Component.displayName).toEqual(info.constructorName)
439447
})
440448
})
449+
450+
const validListenerNames = _.reduce(
451+
syntheticEvent.types,
452+
(result, { listeners }) => [...result, ...listeners],
453+
[],
454+
)
455+
456+
// ---------------------------------------
457+
// Opt-in tests
458+
// ---------------------------------------
459+
return {
460+
// -------------------------------------
461+
// Ensure that props are passed as a
462+
// second argument to event handler
463+
// -------------------------------------
464+
hasExtendedHandlerFor(onEventName) {
465+
describe(`has extended handler for '${onEventName}' event`, () => {
466+
test(`'${onEventName}' is a valid event listener name`, () => {
467+
expect(validListenerNames).toContain(onEventName)
468+
})
469+
470+
test(`is declared in props`, () => {
471+
expect(Component.propTypes[onEventName]).toBeTruthy()
472+
})
473+
})
474+
475+
// -----------------------------------
476+
// Allows chained calls for optional
477+
// test suites
478+
// -----------------------------------
479+
return this
480+
},
481+
}
441482
}

0 commit comments

Comments
 (0)