|
| 1 | +import { module, test, QUnit } from './utils/qunit'; |
| 2 | +import { RenderingTest } from './utils/rendering-test'; |
| 3 | +import { strip } from './utils/strip'; |
| 4 | + |
| 5 | +module('Element Helper Tests', () => { |
| 6 | + class ElementHelperTest extends RenderingTest { |
| 7 | + static suiteName = 'element helper'; |
| 8 | + |
| 9 | + @test |
| 10 | + 'renders a tag with the given tag name'() { |
| 11 | + this.render(strip` |
| 12 | + {{#let (element "h1") as |Tag|}} |
| 13 | + <Tag id="content">hello world!</Tag> |
| 14 | + {{/let}} |
| 15 | + `); |
| 16 | + |
| 17 | + this.assertHTML('<h1 id="content">hello world!</h1>'); |
| 18 | + } |
| 19 | + |
| 20 | + @test |
| 21 | + 'does not render any tags when passed an empty string'() { |
| 22 | + this.render(strip` |
| 23 | + {{#let (element "") as |Tag|}} |
| 24 | + <Tag id="content">hello world!</Tag> |
| 25 | + {{/let}} |
| 26 | + `); |
| 27 | + |
| 28 | + this.assertHTML('hello world!'); |
| 29 | + } |
| 30 | + |
| 31 | + @test |
| 32 | + 'does not render anything when passed null'() { |
| 33 | + this.render(strip` |
| 34 | + {{#let (element null) as |Tag|}} |
| 35 | + <Tag id="content">hello world!</Tag> |
| 36 | + {{/let}} |
| 37 | + `); |
| 38 | + |
| 39 | + this.assertHTML('<!---->'); |
| 40 | + } |
| 41 | + |
| 42 | + @test |
| 43 | + 'does not render anything when passed undefined'() { |
| 44 | + this.render(strip` |
| 45 | + {{#let (element undefined) as |Tag|}} |
| 46 | + <Tag id="content">hello world!</Tag> |
| 47 | + {{/let}} |
| 48 | + `); |
| 49 | + |
| 50 | + this.assertHTML('<!---->'); |
| 51 | + } |
| 52 | + |
| 53 | + @test |
| 54 | + 'works with element modifiers'() { |
| 55 | + let clicked = 0; |
| 56 | + this.registerHelper('increment', () => () => clicked++); |
| 57 | + |
| 58 | + this.render(strip` |
| 59 | + {{#let (element "button") as |Tag|}} |
| 60 | + <Tag type="button" id="action" {{on "click" (helper "increment")}}>hello world!</Tag> |
| 61 | + {{/let}} |
| 62 | + `); |
| 63 | + |
| 64 | + this.assertHTML('<button type="button" id="action">hello world!</button>'); |
| 65 | + this.assert.strictEqual(clicked, 0, 'never clicked'); |
| 66 | + |
| 67 | + this.click('button#action'); |
| 68 | + this.assert.strictEqual(clicked, 1, 'clicked once'); |
| 69 | + |
| 70 | + this.click('button#action'); |
| 71 | + this.assert.strictEqual(clicked, 2, 'clicked twice'); |
| 72 | + } |
| 73 | + |
| 74 | + @test |
| 75 | + 'can be rendered multiple times'() { |
| 76 | + this.render(strip` |
| 77 | + {{#let (element "h1") as |Tag|}} |
| 78 | + <Tag id="content-1">hello</Tag> |
| 79 | + <Tag id="content-2">world</Tag> |
| 80 | + <Tag id="content-3">!!!!!</Tag> |
| 81 | + {{/let}} |
| 82 | + `); |
| 83 | + |
| 84 | + this.assertHTML('<h1 id="content-1">hello</h1><h1 id="content-2">world</h1><h1 id="content-3">!!!!!</h1>'); |
| 85 | + } |
| 86 | + |
| 87 | + @test |
| 88 | + 'renders when the tag name changes'() { |
| 89 | + this.registerHelper('tag-name', () => this.context.tagName); |
| 90 | + |
| 91 | + this.context = { tagName: 'h1' }; |
| 92 | + this.render(strip` |
| 93 | + {{#let (element (helper "tag-name")) as |Tag|}} |
| 94 | + <Tag id="content">hello world!</Tag> |
| 95 | + {{/let}} |
| 96 | + `); |
| 97 | + |
| 98 | + this.assertHTML('<h1 id="content">hello world!</h1>'); |
| 99 | + |
| 100 | + this.context = { tagName: 'h2' }; |
| 101 | + this.rerender(); |
| 102 | + |
| 103 | + this.assertHTML('<h2 id="content">hello world!</h2>'); |
| 104 | + |
| 105 | + this.context = { tagName: 'h3' }; |
| 106 | + this.rerender(); |
| 107 | + |
| 108 | + this.assertHTML('<h3 id="content">hello world!</h3>'); |
| 109 | + |
| 110 | + this.context = { tagName: '' }; |
| 111 | + this.rerender(); |
| 112 | + |
| 113 | + this.assertHTML('hello world!'); |
| 114 | + |
| 115 | + this.context = { tagName: 'h1' }; |
| 116 | + this.rerender(); |
| 117 | + |
| 118 | + this.assertHTML('<h1 id="content">hello world!</h1>'); |
| 119 | + } |
| 120 | + |
| 121 | + @test |
| 122 | + 'throws when passed a non-string value'() { |
| 123 | + this.render(strip` |
| 124 | + {{#let (element 123) as |Tag|}} |
| 125 | + <Tag id="content">hello world!</Tag> |
| 126 | + {{/let}} |
| 127 | + `); |
| 128 | + |
| 129 | + this.assertThrows(() => this.rerender(), /The argument passed to the `element` helper must be a string/); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + module('Element Helper Tests', { |
| 134 | + beforeEach() { |
| 135 | + QUnit.config.current.testEnvironment.owner = {}; |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + ElementHelperTest.run(); |
| 140 | +}); |
0 commit comments