|
| 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 KeywordArrayRuntime extends RenderTest { |
| 12 | + static suiteName = 'keyword helper: array (runtime)'; |
| 13 | + |
| 14 | + @test |
| 15 | + 'it works'() { |
| 16 | + const compiled = template('{{JSON.stringify (array "hello" "goodbye")}}', { |
| 17 | + strictMode: true, |
| 18 | + scope: () => ({ JSON }), |
| 19 | + }); |
| 20 | + |
| 21 | + this.renderComponent(compiled); |
| 22 | + this.assertHTML('["hello","goodbye"]'); |
| 23 | + } |
| 24 | + |
| 25 | + @test |
| 26 | + 'it works (shadowed)'() { |
| 27 | + const array = (x: string) => x.toUpperCase(); |
| 28 | + const compiled = template('{{array "hello"}}', { |
| 29 | + strictMode: true, |
| 30 | + scope: () => ({ JSON, array }), |
| 31 | + }); |
| 32 | + |
| 33 | + this.renderComponent(compiled); |
| 34 | + this.assertHTML('HELLO'); |
| 35 | + } |
| 36 | + |
| 37 | + @test |
| 38 | + 'implicit scope'() { |
| 39 | + const compiled = template('{{JSON.stringify (array "hello" "goodbye")}}', { |
| 40 | + strictMode: true, |
| 41 | + eval() { |
| 42 | + return eval(arguments[0]); |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + this.renderComponent(compiled); |
| 47 | + this.assertHTML('["hello","goodbye"]'); |
| 48 | + } |
| 49 | + |
| 50 | + @test |
| 51 | + 'implicit scope (shadowed)'() { |
| 52 | + const array = (...data: string[]) => data.reverse(); |
| 53 | + |
| 54 | + hide(array); |
| 55 | + |
| 56 | + const compiled = template('{{JSON.stringify (array "hello" "goodbye")}}', { |
| 57 | + strictMode: true, |
| 58 | + eval() { |
| 59 | + return eval(arguments[0]); |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + this.renderComponent(compiled); |
| 64 | + this.assertHTML('["goodbye","hello"]'); |
| 65 | + } |
| 66 | + |
| 67 | + @test |
| 68 | + 'no eval and no scope'(assert: Assert) { |
| 69 | + let receivedData: unknown[] | undefined; |
| 70 | + |
| 71 | + class Foo extends GlimmerishComponent { |
| 72 | + static { |
| 73 | + template( |
| 74 | + '<button {{on "click" (fn this.capture (array "hello" "goodbye"))}}>Click</button>', |
| 75 | + { |
| 76 | + strictMode: true, |
| 77 | + component: this, |
| 78 | + } |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + capture = (data: unknown[]) => { |
| 83 | + receivedData = data; |
| 84 | + assert.step('captured'); |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + this.renderComponent(Foo); |
| 89 | + |
| 90 | + castToBrowser(this.element, 'div').querySelector('button')!.click(); |
| 91 | + assert.verifySteps(['captured']); |
| 92 | + assert.deepEqual(receivedData, ['hello', 'goodbye']); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +jitSuite(KeywordArrayRuntime); |
| 97 | + |
| 98 | +/** |
| 99 | + * This function is used to hide a variable from the transpiler, so that it |
| 100 | + * doesn't get removed as "unused". It does not actually do anything with the |
| 101 | + * variable, it just makes it be part of an expression that the transpiler |
| 102 | + * won't remove. |
| 103 | + * |
| 104 | + * It's a bit of a hack, but it's necessary for testing. |
| 105 | + * |
| 106 | + * @param variable The variable to hide. |
| 107 | + */ |
| 108 | +const hide = (variable: unknown) => { |
| 109 | + new Function(`return (${JSON.stringify(variable)});`); |
| 110 | +}; |
0 commit comments