Skip to content

Commit 94de0c4

Browse files
committed
[FEATURE tagless-event-assert] Update assertion for events in tagless component
1 parent 9c48578 commit 94de0c4

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

packages/@ember/-internals/glimmer/lib/component.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ViewStateSupport,
1313
} from '@ember/-internals/views';
1414
import { assert } from '@ember/debug';
15+
import { DEBUG } from '@glimmer/env';
1516
import { DirtyableTag } from '@glimmer/reference';
1617
import { normalizeProperty, SVG_NAMESPACE } from '@glimmer/runtime';
1718

@@ -727,27 +728,26 @@ const Component = CoreView.extend(
727728
this[ROOT_REF] = new RootReference(this);
728729
this[BOUNDS] = null;
729730

730-
// If in a tagless component, assert that no event handlers are defined
731-
assert(
732-
// tslint:disable-next-line:max-line-length
733-
`You can not define a function that handles DOM events in the \`${this}\` tagless component since it doesn't have any DOM element.`,
734-
this.tagName !== '' ||
735-
!this.renderer._destinedForDOM ||
736-
!(() => {
737-
let eventDispatcher = getOwner(this).lookup<any | undefined>('event_dispatcher:main');
738-
let events = (eventDispatcher && eventDispatcher._finalEvents) || {};
739-
740-
// tslint:disable-next-line:forin
741-
for (let key in events) {
742-
let methodName = events[key];
743-
744-
if (typeof this[methodName] === 'function') {
745-
return true; // indicate that the assertion should be triggered
746-
}
747-
}
748-
return false;
749-
})()
750-
);
731+
if (DEBUG && this.renderer._destinedForDOM && this.tagName === '') {
732+
let eventNames = [];
733+
let eventDispatcher = getOwner(this).lookup<any | undefined>('event_dispatcher:main');
734+
let events = (eventDispatcher && eventDispatcher._finalEvents) || {};
735+
736+
// tslint:disable-next-line:forin
737+
for (let key in events) {
738+
let methodName = events[key];
739+
740+
if (typeof this[methodName] === 'function') {
741+
eventNames.push(methodName);
742+
}
743+
}
744+
// If in a tagless component, assert that no event handlers are defined
745+
assert(
746+
// tslint:disable-next-line:max-line-length
747+
`You can not define \`${eventNames}\` function(s) to handle DOM event in the \`${this}\` tagless component since it doesn't have any DOM element.`,
748+
!eventNames.length
749+
);
750+
}
751751
},
752752

753753
rerender() {

packages/@ember/-internals/glimmer/tests/integration/components/fragment-components-test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ moduleFor(
5757
let FooBarComponent = Component.extend({
5858
tagName: '',
5959
click() {},
60+
mouseEnter() {},
6061
});
6162

6263
this.registerComponent('foo-bar', {
@@ -66,7 +67,7 @@ moduleFor(
6667

6768
expectAssertion(() => {
6869
this.render(`{{#foo-bar}}{{/foo-bar}}`);
69-
}, /You can not define a function that handles DOM events in the .* tagless component since it doesn't have any DOM element./);
70+
}, /You can not define `click,mouseEnter` function\(s\) to handle DOM event in the .* tagless component since it doesn't have any DOM element./);
7071
}
7172

7273
['@test throws an error if a custom defined event function is defined in a tagless component']() {
@@ -83,7 +84,7 @@ moduleFor(
8384

8485
expectAssertion(() => {
8586
this.render(`{{#foo-bar}}{{/foo-bar}}`);
86-
}, /You can not define a function that handles DOM events in the .* tagless component since it doesn't have any DOM element./);
87+
}, /You can not define `folks` function\(s\) to handle DOM event in the .* tagless component since it doesn't have any DOM element./);
8788
}
8889

8990
['@test throws an error if `tagName` is an empty string and `classNameBindings` are specified']() {

0 commit comments

Comments
 (0)