|
| 1 | +import { |
| 2 | + GlimmerishComponent, |
| 3 | + jitSuite, |
| 4 | + RenderTest, |
| 5 | + strip, |
| 6 | + test, |
| 7 | + tracked, |
| 8 | +} from '@glimmer-workspace/integration-tests'; |
| 9 | + |
| 10 | +class IfInlineTest extends RenderTest { |
| 11 | + static suiteName = 'Helpers test: inline {{if}}'; |
| 12 | + |
| 13 | + @test |
| 14 | + 'inline if helper updates when tracked property changes'() { |
| 15 | + class Person { |
| 16 | + @tracked isActive = false; |
| 17 | + |
| 18 | + toggle() { |
| 19 | + this.isActive = !this.isActive; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + const person = new Person(); |
| 24 | + |
| 25 | + this.render( |
| 26 | + strip`<div>{{if this.person.isActive "Active" "Inactive"}}</div>`, |
| 27 | + { person } |
| 28 | + ); |
| 29 | + |
| 30 | + this.assertHTML('<div>Inactive</div>', 'Initial render shows inactive state'); |
| 31 | + this.assertStableRerender(); |
| 32 | + |
| 33 | + person.toggle(); |
| 34 | + this.rerender(); |
| 35 | + this.assertHTML('<div>Active</div>', 'Updates when tracked property changes to true'); |
| 36 | + |
| 37 | + person.toggle(); |
| 38 | + this.rerender(); |
| 39 | + this.assertHTML('<div>Inactive</div>', 'Updates when tracked property changes to false'); |
| 40 | + } |
| 41 | + |
| 42 | + @test |
| 43 | + 'inline if helper with only truthy value updates when tracked property changes'() { |
| 44 | + class Person { |
| 45 | + @tracked isActive = false; |
| 46 | + |
| 47 | + toggle() { |
| 48 | + this.isActive = !this.isActive; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + const person = new Person(); |
| 53 | + |
| 54 | + this.render( |
| 55 | + strip`<div>{{if this.person.isActive "Active"}}</div>`, |
| 56 | + { person } |
| 57 | + ); |
| 58 | + |
| 59 | + this.assertHTML('<div></div>', 'Initial render shows empty when false'); |
| 60 | + this.assertStableRerender(); |
| 61 | + |
| 62 | + person.toggle(); |
| 63 | + this.rerender(); |
| 64 | + this.assertHTML('<div>Active</div>', 'Updates when tracked property changes to true'); |
| 65 | + |
| 66 | + person.toggle(); |
| 67 | + this.rerender(); |
| 68 | + this.assertHTML('<div></div>', 'Updates when tracked property changes to false'); |
| 69 | + } |
| 70 | + |
| 71 | + @test |
| 72 | + 'inline if helper updates when component argument changes'() { |
| 73 | + let testComponent: TestComponent; |
| 74 | + |
| 75 | + class TestComponent extends GlimmerishComponent { |
| 76 | + @tracked isActive = false; |
| 77 | + |
| 78 | + constructor(owner: object, args: Record<string, unknown>) { |
| 79 | + super(owner, args); |
| 80 | + testComponent = this; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + this.registerComponent( |
| 85 | + 'Glimmer', |
| 86 | + 'TestComponent', |
| 87 | + '{{if @isActive "Active" "Inactive"}}', |
| 88 | + TestComponent |
| 89 | + ); |
| 90 | + |
| 91 | + this.render( |
| 92 | + strip`<TestComponent @isActive={{this.isActive}} />`, |
| 93 | + { isActive: false } |
| 94 | + ); |
| 95 | + |
| 96 | + this.assertHTML('Inactive', 'Initial render shows inactive state'); |
| 97 | + this.assertStableRerender(); |
| 98 | + |
| 99 | + this.rerender({ isActive: true }); |
| 100 | + this.assertHTML('Active', 'Updates when argument changes to true'); |
| 101 | + |
| 102 | + this.rerender({ isActive: false }); |
| 103 | + this.assertHTML('Inactive', 'Updates when argument changes to false'); |
| 104 | + } |
| 105 | + |
| 106 | + @test |
| 107 | + 'comparison with block form if helper'() { |
| 108 | + class Person { |
| 109 | + @tracked isActive = false; |
| 110 | + |
| 111 | + toggle() { |
| 112 | + this.isActive = !this.isActive; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + const person = new Person(); |
| 117 | + |
| 118 | + this.render( |
| 119 | + strip` |
| 120 | + <div> |
| 121 | + Inline: {{if this.person.isActive "Active" "Inactive"}} |
| 122 | + Block: {{#if this.person.isActive}}Active{{else}}Inactive{{/if}} |
| 123 | + </div> |
| 124 | + `, |
| 125 | + { person } |
| 126 | + ); |
| 127 | + |
| 128 | + this.assertHTML('<div>Inline: Inactive Block: Inactive</div>', 'Initial render both show inactive'); |
| 129 | + this.assertStableRerender(); |
| 130 | + |
| 131 | + person.toggle(); |
| 132 | + this.rerender(); |
| 133 | + this.assertHTML('<div>Inline: Active Block: Active</div>', 'Both update when tracked property changes to true'); |
| 134 | + |
| 135 | + person.toggle(); |
| 136 | + this.rerender(); |
| 137 | + this.assertHTML('<div>Inline: Inactive Block: Inactive</div>', 'Both update when tracked property changes to false'); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +jitSuite(IfInlineTest); |
0 commit comments