Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Apr 12, 2024
1 parent 4269d2f commit 14d9409
Showing 1 changed file with 115 additions and 2 deletions.
117 changes: 115 additions & 2 deletions __tests__/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,13 +997,104 @@ describe('htmlbars-inline-precompile', function () {
});

it('can emit side-effectful import', function () {
let compatTransform: ExtendedPluginBuilder = (env) => {
let compatTransform: ExtendedPluginBuilder = () => {
return {
name: 'compat-transform',
visitor: {
ElementNode(node) {
if (node.tag === 'Thing') {
node.tag = 'NewThing';
}
},
},
};
};

plugins = [[HTMLBarsInlinePrecompile, { targetFormat: 'hbs', transforms: [compatTransform] }]];

let transformed = transform(stripIndent`
import { precompileTemplate } from '@ember/template-compilation';
let NewThing = '';
export default function() {
const template = precompileTemplate('<Thing />');
}
`);

expect(transformed).toEqualCode(`
import { precompileTemplate } from '@ember/template-compilation';
let NewThing = '';
export default function () {
const template = precompileTemplate("<NewThing />", {
scope: () => ({
NewThing
})
});
}`);
});

it('updates scope correctly when renamed', function () {
let renameTransform: ExtendedPluginBuilder = () => {
return {
name: 'compat-transform',
visitor: {
ElementNode(node) {
if (node.tag === 'Thing') {
node.tag = 'NewThing';
}
},
},
};
};

plugins = [[HTMLBarsInlinePrecompile, { targetFormat: 'hbs', transforms: [] }]];

let transformed = transform(stripIndent`
import { precompileTemplate } from '@ember/template-compilation';
let Thing = '';
let NewThing = '';
export default function() {
const template = precompileTemplate('<Thing />');
}
`);

expect(transformed).toEqualCode(`
import { precompileTemplate } from '@ember/template-compilation';
let Thing = '';
let NewThing = '';
export default function () {
const template = precompileTemplate("<Thing />", {
scope: () => ({
Thing
})
});
}`);

plugins = [[HTMLBarsInlinePrecompile, { targetFormat: 'hbs', transforms: [renameTransform] }]];

transformed = transform(transformed);

expect(transformed).toEqualCode(`
import { precompileTemplate } from '@ember/template-compilation';
let Thing = '';
let NewThing = '';
export default function () {
const template = precompileTemplate("<NewThing />", {
scope: () => ({
NewThing
})
});
}`);
});

it('updates scope correctly when renamed', function () {
let compatTransform: ExtendedPluginBuilder = (env) => {
return {
name: 'compat-transform',
visitor: {
ElementNode(node, path) {
if (node.tag === 'Thing') {
env.meta.jsutils.importForSideEffect('setup-the-things');
node.tag = env.meta.jsutils.bindExpression('Thing', path, { nameHint: 'NewThing' });
}
},
},
Expand All @@ -1014,12 +1105,24 @@ describe('htmlbars-inline-precompile', function () {

let transformed = transform(stripIndent`
import { precompileTemplate } from '@ember/template-compilation';
let Thing = '';
export default function() {
const template = precompileTemplate('<Thing />');
}
`);

expect(transformed).toContain(`import "setup-the-things"`);
expect(transformed).toEqualCode(`
import { precompileTemplate } from '@ember/template-compilation';
let NewThing = Thing;
import "setup-the-things";
let Thing = '';
export default function () {
const template = precompileTemplate("<NewThing />", {
scope: () => ({
NewThing
})
});
}`);
});

describe('source-to-source', function () {
Expand Down Expand Up @@ -1627,6 +1730,16 @@ describe('htmlbars-inline-precompile', function () {
/Scope objects for `precompileTemplate` may only contain direct references to in-scope values, e.g. { bar } or { bar: bar }/
);
});

it('correctly removes not used scope', function () {
let source = '<foo /><bar/>';
let spy = sinon.spy(compiler, 'precompile');

transform(
`import { precompileTemplate } from '@ember/template-compilation';\nvar compiled = precompileTemplate('${source}', { scope: () => ({ foo, bar, baz }) });`
);
expect(spy.firstCall.lastArg).toHaveProperty('locals', ['foo', 'bar']);
});
});

describe('implicit-scope-form', function () {
Expand Down

0 comments on commit 14d9409

Please sign in to comment.