Skip to content

Commit 9ffed5d

Browse files
committed
more test coverage
1 parent 93f91b3 commit 9ffed5d

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

__tests__/tests.ts

+130
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,136 @@ describe('htmlbars-inline-precompile', function () {
19891989
`);
19901990
});
19911991

1992+
it('captures lexical "this" in mustache when template is used as an expression', function () {
1993+
plugins = [
1994+
[
1995+
HTMLBarsInlinePrecompile,
1996+
{
1997+
compiler,
1998+
targetFormat: 'hbs',
1999+
},
2000+
],
2001+
];
2002+
2003+
let transformed = transform(
2004+
`import { template } from '@ember/template-compiler';
2005+
function upper(s) { return s.toUpperCase() }
2006+
export function exampleTest() {
2007+
this.message = "hello";
2008+
render(template('{{upper this.message}}', { eval: function() { return eval(arguments[0]) } }))
2009+
}
2010+
`
2011+
);
2012+
2013+
expect(transformed).toEqualCode(`
2014+
import { precompileTemplate } from "@ember/template-compilation";
2015+
import { setComponentTemplate } from "@ember/component";
2016+
import templateOnly from "@ember/component/template-only";
2017+
function upper(s) {
2018+
return s.toUpperCase();
2019+
}
2020+
export function exampleTest() {
2021+
this.message = "hello";
2022+
render(
2023+
setComponentTemplate(
2024+
precompileTemplate("{{upper this.message}}", {
2025+
strictMode: true,
2026+
scope: () => ({
2027+
upper,
2028+
this: this,
2029+
}),
2030+
}),
2031+
templateOnly()
2032+
)
2033+
);
2034+
}
2035+
`);
2036+
});
2037+
2038+
it('captures lexical "this" in Element when template is used as an expression', function () {
2039+
plugins = [
2040+
[
2041+
HTMLBarsInlinePrecompile,
2042+
{
2043+
compiler,
2044+
targetFormat: 'hbs',
2045+
},
2046+
],
2047+
];
2048+
2049+
let transformed = transform(
2050+
`import { template } from '@ember/template-compiler';
2051+
import SomeComponent from './elsewhere.js';
2052+
export function exampleTest() {
2053+
this.message = SomeComponent;
2054+
render(template('<this.message />', { eval: function() { return eval(arguments[0]) } }))
2055+
}
2056+
`
2057+
);
2058+
2059+
expect(transformed).toEqualCode(`
2060+
import SomeComponent from './elsewhere.js';
2061+
import { precompileTemplate } from "@ember/template-compilation";
2062+
import { setComponentTemplate } from "@ember/component";
2063+
import templateOnly from "@ember/component/template-only";
2064+
export function exampleTest() {
2065+
this.message = SomeComponent;
2066+
render(
2067+
setComponentTemplate(
2068+
precompileTemplate("<this.message />", {
2069+
strictMode: true,
2070+
scope: () => ({
2071+
this: this,
2072+
}),
2073+
}),
2074+
templateOnly()
2075+
)
2076+
);
2077+
}
2078+
`);
2079+
});
2080+
2081+
it('does not captures lexical "this" when template is used in class body', function () {
2082+
plugins = [
2083+
[
2084+
HTMLBarsInlinePrecompile,
2085+
{
2086+
compiler,
2087+
targetFormat: 'hbs',
2088+
},
2089+
],
2090+
];
2091+
2092+
let transformed = transform(
2093+
`import { template } from '@ember/template-compiler';
2094+
import Component from '@glimmer/component';
2095+
export class Example extends Component {
2096+
upper(s) { return s.toUpperCase() }
2097+
message = "hi";
2098+
static {
2099+
template('{{this.upper this.message}}', { component: this, eval: function() { return eval(arguments[0]) } })
2100+
}
2101+
}
2102+
`
2103+
);
2104+
2105+
expect(transformed).toEqualCode(`
2106+
import Component from '@glimmer/component';
2107+
import { precompileTemplate } from "@ember/template-compilation";
2108+
import { setComponentTemplate } from "@ember/component";
2109+
export class Example extends Component {
2110+
upper(s) { return s.toUpperCase() }
2111+
message = "hi";
2112+
static {
2113+
setComponentTemplate(
2114+
precompileTemplate("{{this.upper this.message}}", {
2115+
strictMode: true,
2116+
}), this)
2117+
}
2118+
}
2119+
`);
2120+
});
2121+
19922122
it('leaves ember keywords alone when no local is defined', function () {
19932123
plugins = [
19942124
[

0 commit comments

Comments
 (0)