Skip to content

Commit

Permalink
[FEATURE tagless-event-assert] Update assertion for events in tagless…
Browse files Browse the repository at this point in the history
… component
  • Loading branch information
raido committed Apr 16, 2019
1 parent 9c48578 commit 5130b4a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
27 changes: 16 additions & 11 deletions packages/@ember/-internals/glimmer/lib/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DirtyableTag } from '@glimmer/reference';
import { normalizeProperty, SVG_NAMESPACE } from '@glimmer/runtime';

import { RootReference, UPDATE } from './utils/references';
import { DEBUG } from '@glimmer/env';

export const DIRTY_TAG = symbol('DIRTY_TAG');
export const ARGS = symbol('ARGS');
Expand Down Expand Up @@ -727,13 +728,10 @@ const Component = CoreView.extend(
this[ROOT_REF] = new RootReference(this);
this[BOUNDS] = null;

// If in a tagless component, assert that no event handlers are defined
assert(
// tslint:disable-next-line:max-line-length
`You can not define a function that handles DOM events in the \`${this}\` tagless component since it doesn't have any DOM element.`,
this.tagName !== '' ||
!this.renderer._destinedForDOM ||
!(() => {
if (DEBUG) {
let eventName = '';
if (this.tagName === '') {
eventName = (() => {
let eventDispatcher = getOwner(this).lookup<any | undefined>('event_dispatcher:main');
let events = (eventDispatcher && eventDispatcher._finalEvents) || {};

Expand All @@ -742,12 +740,19 @@ const Component = CoreView.extend(
let methodName = events[key];

if (typeof this[methodName] === 'function') {
return true; // indicate that the assertion should be triggered
return methodName;
}
}
return false;
})()
);
return null;
})();
}
// If in a tagless component, assert that no event handlers are defined
assert(
// tslint:disable-next-line:max-line-length
`You can not define a function \`${eventName}\` to handle DOM event in the \`${this}\` tagless component since it doesn't have any DOM element.`,
!this.renderer._destinedForDOM || !eventName
);
}
},

rerender() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ moduleFor(

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

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

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

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

0 comments on commit 5130b4a

Please sign in to comment.