-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdid-insert-test.js
94 lines (75 loc) · 2.91 KB
/
did-insert-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, setupOnerror } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import Component from '@ember/component';
module('Integration | Modifier | did-insert', function(hooks) {
setupRenderingTest(hooks);
test('it basically works', async function(assert) {
assert.expect(2);
this.someMethod = element => {
assert.equal(element.tagName, 'DIV', 'correct element tagName');
assert.dom(element).hasAttribute('data-foo', 'some-thing');
};
await render(hbs`<div data-foo="some-thing" {{did-insert this.someMethod}}></div>`);
});
test('it can accept arguments', async function(assert) {
assert.expect(4);
this.someMethod = (element, positional, named) => {
assert.equal(element.tagName, 'DIV', 'correct element tagName');
assert.dom(element).hasAttribute('data-foo', 'some-thing');
assert.namedArgsEqual(named, { some: 'hash-value' }, 'named args match');
assert.deepEqual(positional, ['some-positional-value'], 'positional args match');
};
await render(
hbs`<div data-foo="some-thing" {{did-insert this.someMethod "some-positional-value" some="hash-value"}}></div>`
);
});
test('it is not invoked again when arguments change', async function(assert) {
assert.expect(4);
this.someMethod = (element, positional, named) => {
assert.equal(element.tagName, 'DIV', 'correct element tagName');
assert.dom(element).hasAttribute('data-foo', 'some-thing');
assert.namedArgsEqual(named, {}, 'named args match');
assert.deepEqual(positional, ['initial'], 'positional args match');
};
this.set('firstArg', 'initial');
await render(
hbs`<div data-foo="some-thing" {{did-insert this.someMethod this.firstArg}}></div>`
);
this.set('firstArg', 'updated');
});
test('adding class on insert (RFC example)', async function(assert) {
this.owner.register(
'component:sometimes-fades-in',
Component.extend({
fadeIn(element) {
element.classList.add('fade-in');
},
})
);
this.owner.register(
'template:components/sometimes-fades-in',
hbs`
{{#if shouldShow}}
<div {{did-insert this.fadeIn}} class="alert">
{{yield}}
</div>
{{/if}}
`
);
await render(hbs`{{sometimes-fades-in shouldShow=true}}`);
assert.dom('.alert').hasClass('fade-in');
});
test('throws meaningful error when did-insert does not receive a function', async function(assert) {
assert.expect(1);
this.notAFunction = null;
setupOnerror(function(error) {
assert.equal(
error.message,
'Assertion Failed: You need to pass a function as the first argument to `did-insert` you passed null'
);
});
await render(hbs`<div {{did-insert this.notAFunction}}></div>`);
});
});