|
| 1 | +import { |
| 2 | + GlimmerishComponent, |
| 3 | + jitSuite, |
| 4 | + RenderTest, |
| 5 | + strip, |
| 6 | + test, |
| 7 | + tracked, |
| 8 | +} from '@glimmer-workspace/integration-tests'; |
| 9 | + |
| 10 | +class ElementHelperTest extends RenderTest { |
| 11 | + static suiteName = 'Helpers test: {{element}}'; |
| 12 | + |
| 13 | + @test |
| 14 | + 'renders a tag with the given tag name'() { |
| 15 | + this.render(strip` |
| 16 | + {{#let (element "h1") as |Tag|}} |
| 17 | + <Tag id="content">hello world!</Tag> |
| 18 | + {{/let}} |
| 19 | + `); |
| 20 | + |
| 21 | + this.assertHTML('<h1 id="content">hello world!</h1>'); |
| 22 | + this.assertStableRerender(); |
| 23 | + } |
| 24 | + |
| 25 | + @test |
| 26 | + 'does not render any tags when passed an empty string'() { |
| 27 | + this.render(strip` |
| 28 | + {{#let (element "") as |Tag|}} |
| 29 | + <Tag id="content">hello world!</Tag> |
| 30 | + {{/let}} |
| 31 | + `); |
| 32 | + |
| 33 | + this.assertHTML('hello world!'); |
| 34 | + this.assertStableRerender(); |
| 35 | + } |
| 36 | + |
| 37 | + @test |
| 38 | + 'does not render anything when passed null'() { |
| 39 | + this.render(strip` |
| 40 | + {{#let (element null) as |Tag|}} |
| 41 | + <Tag id="content">hello world!</Tag> |
| 42 | + {{/let}} |
| 43 | + `); |
| 44 | + |
| 45 | + this.assertHTML('<!---->'); |
| 46 | + this.assertStableRerender(); |
| 47 | + } |
| 48 | + |
| 49 | + @test |
| 50 | + 'does not render anything when passed undefined'() { |
| 51 | + this.render(strip` |
| 52 | + {{#let (element undefined) as |Tag|}} |
| 53 | + <Tag id="content">hello world!</Tag> |
| 54 | + {{/let}} |
| 55 | + `); |
| 56 | + |
| 57 | + this.assertHTML('<!---->'); |
| 58 | + this.assertStableRerender(); |
| 59 | + } |
| 60 | + |
| 61 | + @test |
| 62 | + 'works with element modifiers'() { |
| 63 | + let clicked = 0; |
| 64 | + this.registerHelper('increment', () => () => clicked++); |
| 65 | + |
| 66 | + this.render(strip` |
| 67 | + {{#let (element "button") as |Tag|}} |
| 68 | + <Tag type="button" id="action" {{on "click" (helper "increment")}}>hello world!</Tag> |
| 69 | + {{/let}} |
| 70 | + `); |
| 71 | + |
| 72 | + this.assertHTML('<button type="button" id="action">hello world!</button>'); |
| 73 | + this.assert.strictEqual(clicked, 0, 'never clicked'); |
| 74 | + |
| 75 | + this.click('button#action'); |
| 76 | + this.assert.strictEqual(clicked, 1, 'clicked once'); |
| 77 | + |
| 78 | + this.click('button#action'); |
| 79 | + this.assert.strictEqual(clicked, 2, 'clicked twice'); |
| 80 | + } |
| 81 | + |
| 82 | + @test |
| 83 | + 'can be rendered multiple times'() { |
| 84 | + this.render(strip` |
| 85 | + {{#let (element "h1") as |Tag|}} |
| 86 | + <Tag id="content-1">hello</Tag> |
| 87 | + <Tag id="content-2">world</Tag> |
| 88 | + <Tag id="content-3">!!!!!</Tag> |
| 89 | + {{/let}} |
| 90 | + `); |
| 91 | + |
| 92 | + this.assertHTML('<h1 id="content-1">hello</h1><h1 id="content-2">world</h1><h1 id="content-3">!!!!!</h1>'); |
| 93 | + this.assertStableRerender(); |
| 94 | + } |
| 95 | + |
| 96 | + @test |
| 97 | + 'renders when the tag name changes'() { |
| 98 | + this.registerHelper('tag-name', () => this.context.tagName); |
| 99 | + |
| 100 | + this.context = { tagName: 'h1' }; |
| 101 | + this.render(strip` |
| 102 | + {{#let (element (helper "tag-name")) as |Tag|}} |
| 103 | + <Tag id="content">hello world!</Tag> |
| 104 | + {{/let}} |
| 105 | + `); |
| 106 | + |
| 107 | + this.assertHTML('<h1 id="content">hello world!</h1>'); |
| 108 | + this.assertStableRerender(); |
| 109 | + |
| 110 | + this.context = { tagName: 'h2' }; |
| 111 | + this.rerender(); |
| 112 | + |
| 113 | + this.assertHTML('<h2 id="content">hello world!</h2>'); |
| 114 | + |
| 115 | + this.context = { tagName: 'h3' }; |
| 116 | + this.rerender(); |
| 117 | + |
| 118 | + this.assertHTML('<h3 id="content">hello world!</h3>'); |
| 119 | + |
| 120 | + this.context = { tagName: '' }; |
| 121 | + this.rerender(); |
| 122 | + |
| 123 | + this.assertHTML('hello world!'); |
| 124 | + |
| 125 | + this.context = { tagName: 'h1' }; |
| 126 | + this.rerender(); |
| 127 | + |
| 128 | + this.assertHTML('<h1 id="content">hello world!</h1>'); |
| 129 | + } |
| 130 | + |
| 131 | + @test |
| 132 | + 'throws when passed a non-string value'() { |
| 133 | + this.render(strip` |
| 134 | + {{#let (element 123) as |Tag|}} |
| 135 | + <Tag id="content">hello world!</Tag> |
| 136 | + {{/let}} |
| 137 | + `); |
| 138 | + |
| 139 | + this.assertThrows(() => this.rerender(), /The argument passed to the `element` helper must be a string/); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +class ElementHelperStrictModeTest extends RenderTest { |
| 144 | + static suiteName = 'strict mode: {{element}} helper'; |
| 145 | + |
| 146 | + @test |
| 147 | + 'renders a tag with the given tag name'() { |
| 148 | + this.render(strip` |
| 149 | + {{#let (element "h1") as |Tag|}} |
| 150 | + <Tag id="content">hello world!</Tag> |
| 151 | + {{/let}} |
| 152 | + `); |
| 153 | + |
| 154 | + this.assertHTML('<h1 id="content">hello world!</h1>'); |
| 155 | + this.assertStableRerender(); |
| 156 | + } |
| 157 | + |
| 158 | + @test |
| 159 | + 'works with dynamic tag names'() { |
| 160 | + this.context = { tagName: 'h1' }; |
| 161 | + this.render(strip` |
| 162 | + {{#let (element this.tagName) as |Tag|}} |
| 163 | + <Tag id="content">hello world!</Tag> |
| 164 | + {{/let}} |
| 165 | + `); |
| 166 | + |
| 167 | + this.assertHTML('<h1 id="content">hello world!</h1>'); |
| 168 | + this.assertStableRerender(); |
| 169 | + |
| 170 | + this.rerender({ tagName: 'h2' }); |
| 171 | + this.assertHTML('<h2 id="content">hello world!</h2>'); |
| 172 | + |
| 173 | + this.rerender({ tagName: '' }); |
| 174 | + this.assertHTML('hello world!'); |
| 175 | + |
| 176 | + this.rerender({ tagName: 'h1' }); |
| 177 | + this.assertHTML('<h1 id="content">hello world!</h1>'); |
| 178 | + } |
| 179 | + |
| 180 | + @test |
| 181 | + 'can be used with component helper'() { |
| 182 | + this.render(strip` |
| 183 | + {{#let (component (element "h1")) as |Tag|}} |
| 184 | + <Tag id="content">hello world!</Tag> |
| 185 | + {{/let}} |
| 186 | + `); |
| 187 | + |
| 188 | + this.assertHTML('<h1 id="content">hello world!</h1>'); |
| 189 | + this.assertStableRerender(); |
| 190 | + } |
| 191 | + |
| 192 | + @test |
| 193 | + 'can be used with if helper'() { |
| 194 | + this.context = { useDiv: true }; |
| 195 | + this.render(strip` |
| 196 | + {{#let (element (if this.useDiv "div" "span")) as |Tag|}} |
| 197 | + <Tag id="content">hello world!</Tag> |
| 198 | + {{/let}} |
| 199 | + `); |
| 200 | + |
| 201 | + this.assertHTML('<div id="content">hello world!</div>'); |
| 202 | + this.assertStableRerender(); |
| 203 | + |
| 204 | + this.rerender({ useDiv: false }); |
| 205 | + this.assertHTML('<span id="content">hello world!</span>'); |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +jitSuite(ElementHelperTest); |
| 210 | +jitSuite(ElementHelperStrictModeTest); |
0 commit comments