|
1 | 1 | import { RenderingTestCase, strip, moduleFor, runTask } from 'internal-test-helpers'; |
2 | 2 | import { setProperties } from '@ember/object'; |
3 | | -import { EMBER_UNIQUE_ID_HELPER } from '@ember/canary-features'; |
4 | | - |
5 | | -if (EMBER_UNIQUE_ID_HELPER) { |
6 | | - moduleFor( |
7 | | - 'Helpers test: {{unique-id}}', |
8 | | - class extends RenderingTestCase { |
9 | | - ['@test it generates a unique id (string) each time']() { |
10 | | - let { first, second } = this.render(`<p>{{unique-id}}</p><p>{{unique-id}}</p>`, () => { |
| 3 | + |
| 4 | +moduleFor( |
| 5 | + 'Helpers test: {{unique-id}}', |
| 6 | + class extends RenderingTestCase { |
| 7 | + ['@test it generates a unique id (string) each time']() { |
| 8 | + let { first, second } = this.render(`<p>{{unique-id}}</p><p>{{unique-id}}</p>`, () => { |
| 9 | + let first = this.asElement(this.firstChild); |
| 10 | + let second = this.asElement(this.nthChild(1)); |
| 11 | + |
| 12 | + return { |
| 13 | + first: this.asTextContent(first.firstChild), |
| 14 | + second: this.asTextContent(second.firstChild), |
| 15 | + }; |
| 16 | + }); |
| 17 | + |
| 18 | + this.assert.notStrictEqual( |
| 19 | + first, |
| 20 | + second, |
| 21 | + `different invocations of {{unique-id}} should produce different values` |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + [`@test when unique-id is used with #let, it remains stable when it's used`]() { |
| 26 | + let { first, second } = this.render( |
| 27 | + strip` |
| 28 | + {{#let (unique-id) as |id|}} |
| 29 | + <p>{{id}}</p><p>{{id}}</p> |
| 30 | + {{/let}} |
| 31 | + `, |
| 32 | + () => { |
11 | 33 | let first = this.asElement(this.firstChild); |
12 | 34 | let second = this.asElement(this.nthChild(1)); |
13 | 35 |
|
14 | 36 | return { |
15 | 37 | first: this.asTextContent(first.firstChild), |
16 | 38 | second: this.asTextContent(second.firstChild), |
17 | 39 | }; |
18 | | - }); |
19 | | - |
20 | | - this.assert.notStrictEqual( |
21 | | - first, |
22 | | - second, |
23 | | - `different invocations of {{unique-id}} should produce different values` |
24 | | - ); |
25 | | - } |
26 | | - |
27 | | - [`@test when unique-id is used with #let, it remains stable when it's used`]() { |
28 | | - let { first, second } = this.render( |
29 | | - strip` |
30 | | - {{#let (unique-id) as |id|}} |
31 | | - <p>{{id}}</p><p>{{id}}</p> |
32 | | - {{/let}} |
33 | | - `, |
34 | | - () => { |
35 | | - let first = this.asElement(this.firstChild); |
36 | | - let second = this.asElement(this.nthChild(1)); |
37 | | - |
38 | | - return { |
39 | | - first: this.asTextContent(first.firstChild), |
40 | | - second: this.asTextContent(second.firstChild), |
41 | | - }; |
42 | | - } |
43 | | - ); |
| 40 | + } |
| 41 | + ); |
44 | 42 |
|
45 | | - this.assert.strictEqual( |
46 | | - first, |
47 | | - second, |
48 | | - `when unique-id is used as a variable, it remains the same` |
49 | | - ); |
50 | | - } |
| 43 | + this.assert.strictEqual( |
| 44 | + first, |
| 45 | + second, |
| 46 | + `when unique-id is used as a variable, it remains the same` |
| 47 | + ); |
| 48 | + } |
51 | 49 |
|
52 | | - [`@test unique-id doesn't change if it's concatenated with a value that does change`]() { |
53 | | - class Elements { |
54 | | - constructor(label, input, assert) { |
55 | | - this.label = label; |
56 | | - this.input = input; |
57 | | - this.assert = assert; |
58 | | - } |
| 50 | + [`@test unique-id doesn't change if it's concatenated with a value that does change`]() { |
| 51 | + class Elements { |
| 52 | + constructor(label, input, assert) { |
| 53 | + this.label = label; |
| 54 | + this.input = input; |
| 55 | + this.assert = assert; |
| 56 | + } |
59 | 57 |
|
60 | | - id(regex) { |
61 | | - let forAttr = this.label.getAttribute('for'); |
| 58 | + id(regex) { |
| 59 | + let forAttr = this.label.getAttribute('for'); |
62 | 60 |
|
63 | | - this.assert.strictEqual( |
64 | | - forAttr, |
65 | | - this.input.getAttribute('id'), |
66 | | - `the label's 'for' attribute should be the same as the input's 'id' attribute` |
67 | | - ); |
| 61 | + this.assert.strictEqual( |
| 62 | + forAttr, |
| 63 | + this.input.getAttribute('id'), |
| 64 | + `the label's 'for' attribute should be the same as the input's 'id' attribute` |
| 65 | + ); |
68 | 66 |
|
69 | | - let match = forAttr.match(regex); |
| 67 | + let match = forAttr.match(regex); |
70 | 68 |
|
71 | | - this.assert.ok(match, 'the id starts with the prefix'); |
| 69 | + this.assert.ok(match, 'the id starts with the prefix'); |
72 | 70 |
|
73 | | - return match[1]; |
74 | | - } |
| 71 | + return match[1]; |
75 | 72 | } |
| 73 | + } |
76 | 74 |
|
77 | | - let { elements, id } = this.render( |
78 | | - strip` |
79 | | - {{#let (unique-id) as |id|}} |
80 | | - <label for="{{this.prefix}}-{{id}}">Enable Feature</label> |
81 | | - <input id="{{this.prefix}}-{{id}}" type="checkbox"> |
82 | | - {{/let}}`, |
83 | | - { prefix: 'app' }, |
84 | | - () => { |
85 | | - let label = this.asElement(this.firstChild, 'label'); |
86 | | - let input = this.asElement(this.nthChild(1), 'input'); |
87 | | - |
88 | | - let elements = new Elements(label, input, this.assert); |
89 | | - |
90 | | - return { elements, id: elements.id(/^app-(.*)$/) }; |
91 | | - } |
92 | | - ); |
| 75 | + let { elements, id } = this.render( |
| 76 | + strip` |
| 77 | + {{#let (unique-id) as |id|}} |
| 78 | + <label for="{{this.prefix}}-{{id}}">Enable Feature</label> |
| 79 | + <input id="{{this.prefix}}-{{id}}" type="checkbox"> |
| 80 | + {{/let}}`, |
| 81 | + { prefix: 'app' }, |
| 82 | + () => { |
| 83 | + let label = this.asElement(this.firstChild, 'label'); |
| 84 | + let input = this.asElement(this.nthChild(1), 'input'); |
93 | 85 |
|
94 | | - this.update({ prefix: 'melanie' }, () => { |
95 | | - let newId = elements.id(/^melanie-(.*)$/); |
| 86 | + let elements = new Elements(label, input, this.assert); |
96 | 87 |
|
97 | | - this.assert.strictEqual( |
98 | | - id, |
99 | | - newId, |
100 | | - `the unique-id part of a concatenated attribute shouldn't change just because a dynamic part of it changed` |
101 | | - ); |
102 | | - }); |
103 | | - } |
| 88 | + return { elements, id: elements.id(/^app-(.*)$/) }; |
| 89 | + } |
| 90 | + ); |
104 | 91 |
|
105 | | - ['@test it only generates valid selectors']() { |
106 | | - let iterations = 1000; |
107 | | - let reNumericStart = /^\d/; |
| 92 | + this.update({ prefix: 'melanie' }, () => { |
| 93 | + let newId = elements.id(/^melanie-(.*)$/); |
108 | 94 |
|
109 | | - let template = '<p>{{unique-id}}</p>'.repeat(iterations); |
110 | | - super.render(template); |
| 95 | + this.assert.strictEqual( |
| 96 | + id, |
| 97 | + newId, |
| 98 | + `the unique-id part of a concatenated attribute shouldn't change just because a dynamic part of it changed` |
| 99 | + ); |
| 100 | + }); |
| 101 | + } |
111 | 102 |
|
112 | | - for (let i = 0; i < iterations; i++) { |
113 | | - let textNode = this.nthChild(i).firstChild; |
114 | | - let text = textNode.data; |
| 103 | + ['@test it only generates valid selectors']() { |
| 104 | + let iterations = 1000; |
| 105 | + let reNumericStart = /^\d/; |
115 | 106 |
|
116 | | - this.assert.false( |
117 | | - reNumericStart.test(text), |
118 | | - `{{unique-id}} should produce valid selectors` + text |
119 | | - ); |
120 | | - } |
121 | | - } |
| 107 | + let template = '<p>{{unique-id}}</p>'.repeat(iterations); |
| 108 | + super.render(template); |
122 | 109 |
|
123 | | - render(template, ...rest) { |
124 | | - // If there are three parameters to `render`, the second parameter is the |
125 | | - // template's arguments. |
126 | | - let args = rest.length === 2 ? rest[0] : {}; |
127 | | - // If there are two parameters to `render`, the second parameter is the |
128 | | - // postcondition. Otherwise, the third parameter is the postcondition. |
129 | | - let postcondition = rest.length === 2 ? rest[1] : rest[0]; |
130 | | - |
131 | | - super.render(template, args); |
132 | | - let result = postcondition(); |
133 | | - this.assertStableRerender(); |
134 | | - return result; |
135 | | - } |
| 110 | + for (let i = 0; i < iterations; i++) { |
| 111 | + let textNode = this.nthChild(i).firstChild; |
| 112 | + let text = textNode.data; |
136 | 113 |
|
137 | | - update(args, postcondition) { |
138 | | - runTask(() => setProperties(this.context, args)); |
139 | | - postcondition(); |
140 | | - this.assertStableRerender(); |
| 114 | + this.assert.false( |
| 115 | + reNumericStart.test(text), |
| 116 | + `{{unique-id}} should produce valid selectors` + text |
| 117 | + ); |
141 | 118 | } |
| 119 | + } |
142 | 120 |
|
143 | | - asElement(node, tag) { |
144 | | - this.assert.ok(node !== null && node.nodeType === 1); |
| 121 | + render(template, ...rest) { |
| 122 | + // If there are three parameters to `render`, the second parameter is the |
| 123 | + // template's arguments. |
| 124 | + let args = rest.length === 2 ? rest[0] : {}; |
| 125 | + // If there are two parameters to `render`, the second parameter is the |
| 126 | + // postcondition. Otherwise, the third parameter is the postcondition. |
| 127 | + let postcondition = rest.length === 2 ? rest[1] : rest[0]; |
| 128 | + |
| 129 | + super.render(template, args); |
| 130 | + let result = postcondition(); |
| 131 | + this.assertStableRerender(); |
| 132 | + return result; |
| 133 | + } |
145 | 134 |
|
146 | | - if (tag) { |
147 | | - this.assert.strictEqual(node.tagName.toLowerCase(), tag, `Element is <${tag}>`); |
148 | | - } |
| 135 | + update(args, postcondition) { |
| 136 | + runTask(() => setProperties(this.context, args)); |
| 137 | + postcondition(); |
| 138 | + this.assertStableRerender(); |
| 139 | + } |
149 | 140 |
|
150 | | - return node; |
151 | | - } |
| 141 | + asElement(node, tag) { |
| 142 | + this.assert.ok(node !== null && node.nodeType === 1); |
152 | 143 |
|
153 | | - asTextNode(node) { |
154 | | - this.assert.ok(node !== null && node.nodeType === 3); |
155 | | - return node; |
| 144 | + if (tag) { |
| 145 | + this.assert.strictEqual(node.tagName.toLowerCase(), tag, `Element is <${tag}>`); |
156 | 146 | } |
157 | 147 |
|
158 | | - asTextContent(node) { |
159 | | - let data = this.asTextNode(node).data; |
160 | | - this.assert.ok(data.trim().length > 0, `The text node has content`); |
161 | | - return data; |
162 | | - } |
| 148 | + return node; |
| 149 | + } |
| 150 | + |
| 151 | + asTextNode(node) { |
| 152 | + this.assert.ok(node !== null && node.nodeType === 3); |
| 153 | + return node; |
| 154 | + } |
| 155 | + |
| 156 | + asTextContent(node) { |
| 157 | + let data = this.asTextNode(node).data; |
| 158 | + this.assert.ok(data.trim().length > 0, `The text node has content`); |
| 159 | + return data; |
163 | 160 | } |
164 | | - ); |
165 | | -} |
| 161 | + } |
| 162 | +); |
0 commit comments