|
| 1 | +import { castToBrowser } from '@glimmer/debug-util'; |
| 2 | +import { |
| 3 | + GlimmerishComponent, |
| 4 | + jitSuite, |
| 5 | + RenderTest, |
| 6 | + test, |
| 7 | +} from '@glimmer-workspace/integration-tests'; |
| 8 | + |
| 9 | +import { template } from '@ember/template-compiler/runtime'; |
| 10 | + |
| 11 | +class KeywordHashRuntime extends RenderTest { |
| 12 | + static suiteName = 'keyword helper: hash (runtime)'; |
| 13 | + |
| 14 | + @test |
| 15 | + 'explicit scope'(assert: Assert) { |
| 16 | + let receivedData: Record<string, unknown> | undefined; |
| 17 | + |
| 18 | + let capture = (data: Record<string, unknown>) => { |
| 19 | + receivedData = data; |
| 20 | + assert.step('captured'); |
| 21 | + }; |
| 22 | + |
| 23 | + const compiled = template('{{capture (hash greeting="hello" farewell="goodbye")}}', { |
| 24 | + strictMode: true, |
| 25 | + scope: () => ({ |
| 26 | + capture, |
| 27 | + }), |
| 28 | + }); |
| 29 | + |
| 30 | + this.renderComponent(compiled); |
| 31 | + |
| 32 | + assert.verifySteps(['captured']); |
| 33 | + assert.strictEqual(receivedData?.['greeting'], 'hello'); |
| 34 | + assert.strictEqual(receivedData?.['farewell'], 'goodbye'); |
| 35 | + } |
| 36 | + |
| 37 | + @test |
| 38 | + 'implicit scope'(assert: Assert) { |
| 39 | + let receivedData: Record<string, unknown> | undefined; |
| 40 | + |
| 41 | + let capture = (data: Record<string, unknown>) => { |
| 42 | + receivedData = data; |
| 43 | + assert.step('captured'); |
| 44 | + }; |
| 45 | + |
| 46 | + hide(capture); |
| 47 | + |
| 48 | + const compiled = template('{{capture (hash greeting="hello")}}', { |
| 49 | + strictMode: true, |
| 50 | + eval() { |
| 51 | + return eval(arguments[0]); |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + this.renderComponent(compiled); |
| 56 | + |
| 57 | + assert.verifySteps(['captured']); |
| 58 | + assert.strictEqual(receivedData?.['greeting'], 'hello'); |
| 59 | + } |
| 60 | + |
| 61 | + @test |
| 62 | + 'implicit scope (shadowing)'(assert: Assert) { |
| 63 | + let receivedData: string | undefined; |
| 64 | + |
| 65 | + const hash = (data: string) => { |
| 66 | + receivedData = data; |
| 67 | + }; |
| 68 | + |
| 69 | + hide(hash); |
| 70 | + |
| 71 | + const compiled = template('{{hash "hello"}}', { |
| 72 | + strictMode: true, |
| 73 | + eval() { |
| 74 | + return eval(arguments[0]); |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + this.renderComponent(compiled); |
| 79 | + |
| 80 | + assert.strictEqual(receivedData, 'hello'); |
| 81 | + } |
| 82 | + |
| 83 | + @test |
| 84 | + 'MustacheStatement with explicit scope'(assert: Assert) { |
| 85 | + let receivedData: Record<string, unknown> | undefined; |
| 86 | + |
| 87 | + let capture = (data: Record<string, unknown>) => { |
| 88 | + receivedData = data; |
| 89 | + assert.step('captured'); |
| 90 | + }; |
| 91 | + |
| 92 | + const Child = template('{{capture @data}}', { |
| 93 | + strictMode: true, |
| 94 | + scope: () => ({ capture }), |
| 95 | + }); |
| 96 | + |
| 97 | + const compiled = template('<Child @data={{hash greeting="hello"}} />', { |
| 98 | + strictMode: true, |
| 99 | + scope: () => ({ |
| 100 | + Child, |
| 101 | + }), |
| 102 | + }); |
| 103 | + |
| 104 | + this.renderComponent(compiled); |
| 105 | + |
| 106 | + assert.verifySteps(['captured']); |
| 107 | + assert.strictEqual(receivedData?.['greeting'], 'hello'); |
| 108 | + } |
| 109 | + |
| 110 | + @test |
| 111 | + 'no eval and no scope'(assert: Assert) { |
| 112 | + let receivedData: Record<string, unknown> | undefined; |
| 113 | + |
| 114 | + class Foo extends GlimmerishComponent { |
| 115 | + static { |
| 116 | + template( |
| 117 | + '<button {{on "click" (fn this.capture (hash greeting="hello"))}}>Click</button>', |
| 118 | + { |
| 119 | + strictMode: true, |
| 120 | + component: this, |
| 121 | + } |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + capture = (data: Record<string, unknown>) => { |
| 126 | + receivedData = data; |
| 127 | + assert.step('captured'); |
| 128 | + }; |
| 129 | + } |
| 130 | + |
| 131 | + this.renderComponent(Foo); |
| 132 | + |
| 133 | + castToBrowser(this.element, 'div').querySelector('button')!.click(); |
| 134 | + assert.verifySteps(['captured']); |
| 135 | + assert.strictEqual(receivedData?.['greeting'], 'hello'); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +jitSuite(KeywordHashRuntime); |
| 140 | + |
| 141 | +/** |
| 142 | + * This function is used to hide a variable from the transpiler, so that it |
| 143 | + * doesn't get removed as "unused". It does not actually do anything with the |
| 144 | + * variable, it just makes it be part of an expression that the transpiler |
| 145 | + * won't remove. |
| 146 | + * |
| 147 | + * It's a bit of a hack, but it's necessary for testing. |
| 148 | + * |
| 149 | + * @param variable The variable to hide. |
| 150 | + */ |
| 151 | +const hide = (variable: unknown) => { |
| 152 | + new Function(`return (${JSON.stringify(variable)});`); |
| 153 | +}; |
0 commit comments