Skip to content

Commit

Permalink
fix(event-listener): call element cb after didInsertElement
Browse files Browse the repository at this point in the history
  • Loading branch information
buschtoens committed Jan 13, 2019
1 parent 310c8ef commit dd607a8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions addon/event-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import hookDisposablesRunner from './hook-disposables-runner';
import { assert } from '@ember/debug';
import ANONYMOUS from './utils/anonymous-field';
import EmberObject from '@ember/object';
import { beforeMethod } from 'patch-method';
import { afterMethod } from 'patch-method';
import Component from '@ember/component';
import { Constructor } from './utils/type-helpers';
import { IDestroyable } from 'ember-lifeline/interfaces';
Expand All @@ -32,7 +32,7 @@ export default decoratorWithRequiredParams(function(
return {
...desc,
finisher(Class: Constructor<EmberObject>) {
beforeMethod(
afterMethod(
Class as Constructor<Component>,
typeof Class.prototype.didInsertElement === 'function'
? 'didInsertElement'
Expand Down
34 changes: 34 additions & 0 deletions tests/integration/event-listener-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,38 @@ module('@eventListener', function(hooks) {
// eslint-disable-next-line typescript/no-non-null-assertion
await click(this.element.querySelector('#test-component')!);
});

test('it calls the callback after `didInsertElement`', async function(assert) {
assert.expect(1);

let didCall = false;

class TestComponent extends Component {
didInsertElement() {
super.didInsertElement();

assert.notOk(
didCall,
'callback was not called before the body of `didInsertElement`'
);
}
@eventListener(
t => {
didCall = true;
return t.element;
},
'click',
{
once: true
}
)
foo(event: MouseEvent) {
assert.ok(event instanceof MouseEvent);
}
}

this.owner.register('component:test-component', TestComponent);

await render(hbs`{{test-component id='test-component'}}`);
});
});

0 comments on commit dd607a8

Please sign in to comment.