Skip to content

Commit

Permalink
feat(alias): Added additional test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonseydel committed Sep 11, 2019
1 parent 19399af commit 4a45a5c
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export default template;
export const dependencies = [ ];
export const containerless = true;
export const bindables = {"age":{"mode":2},"firstName":{},"lastName":{}};
export const aliases = ['test','test2']
export const aliases = ["test","test2"];
let _e;
export function register(container) {
if (!_e) {
Expand All @@ -364,4 +364,140 @@ export function register(container) {
assert.equal(result.code, expected);
});

it('processes template with containerless, bindables, and aliases (node)', function () {
const html = '<alias name="test, test2"><bindable name="age" mode="one-way"><containerless><template bindable="firstName, lastName"></template>';
const expected = `import { CustomElement } from '@aurelia/runtime';
export const name = "foo";
export const template = "<template></template>";
export default template;
export const dependencies = [ ];
export const containerless = true;
export const bindables = {"age":{"mode":2},"firstName":{},"lastName":{}};
export const aliases = ["test","test2"];
let _e;
export function register(container) {
if (!_e) {
_e = CustomElement.define({ name, template, dependencies, containerless, bindables, aliases });
}
container.register(_e);
}
`;
const result = preprocessHtmlTemplate(
{ path: path.join('lo', 'foo.html'), contents: html },
preprocessOptions({
stringModuleWrap: (id: string) => `!!raw-loader!${id}`
})
);
assert.equal(result.code, expected);
});


it('processes template with containerless, bindables, and aliases (noth)', function () {
const html = '<alias name="test, test2"><bindable name="age" mode="one-way"><containerless><template alias="test3, test4" bindable="firstName, lastName"></template>';
const expected = `import { CustomElement } from '@aurelia/runtime';
export const name = "foo";
export const template = "<template></template>";
export default template;
export const dependencies = [ ];
export const containerless = true;
export const bindables = {"age":{"mode":2},"firstName":{},"lastName":{}};
export const aliases = ["test","test2","test3","test4"];
let _e;
export function register(container) {
if (!_e) {
_e = CustomElement.define({ name, template, dependencies, containerless, bindables, aliases });
}
container.register(_e);
}
`;
const result = preprocessHtmlTemplate(
{ path: path.join('lo', 'foo.html'), contents: html },
preprocessOptions({
stringModuleWrap: (id: string) => `!!raw-loader!${id}`
})
);
assert.equal(result.code, expected);
});

it('processes template with containerless, bindables, and aliases (empty node) (noth)', function () {
const html = '<alias><bindable name="age" mode="one-way"><containerless><template alias="test3, test4" bindable="firstName, lastName"></template>';
const expected = `import { CustomElement } from '@aurelia/runtime';
export const name = "foo";
export const template = "<template></template>";
export default template;
export const dependencies = [ ];
export const containerless = true;
export const bindables = {"age":{"mode":2},"firstName":{},"lastName":{}};
export const aliases = ["test3","test4"];
let _e;
export function register(container) {
if (!_e) {
_e = CustomElement.define({ name, template, dependencies, containerless, bindables, aliases });
}
container.register(_e);
}
`;
const result = preprocessHtmlTemplate(
{ path: path.join('lo', 'foo.html'), contents: html },
preprocessOptions({
stringModuleWrap: (id: string) => `!!raw-loader!${id}`
})
);
assert.equal(result.code, expected);
});

it('processes template with containerless, bindables, and aliases (empty attr) (noth)', function () {
const html = '<alias name="test, test2"><bindable name="age" mode="one-way"><containerless><template alias="" bindable="firstName, lastName"></template>';
const expected = `import { CustomElement } from '@aurelia/runtime';
export const name = "foo";
export const template = "<template></template>";
export default template;
export const dependencies = [ ];
export const containerless = true;
export const bindables = {"age":{"mode":2},"firstName":{},"lastName":{}};
export const aliases = ["test","test2"];
let _e;
export function register(container) {
if (!_e) {
_e = CustomElement.define({ name, template, dependencies, containerless, bindables, aliases });
}
container.register(_e);
}
`;
const result = preprocessHtmlTemplate(
{ path: path.join('lo', 'foo.html'), contents: html },
preprocessOptions({
stringModuleWrap: (id: string) => `!!raw-loader!${id}`
})
);
assert.equal(result.code, expected);
});

it('processes template with containerless, bindables, and aliases (empty attr+node) (noth)', function () {
const html = '<alias><bindable name="age" mode="one-way"><containerless><template alias="" bindable="firstName, lastName"></template>';
const expected = `import { CustomElement } from '@aurelia/runtime';
export const name = "foo";
export const template = "<template></template>";
export default template;
export const dependencies = [ ];
export const containerless = true;
export const bindables = {"age":{"mode":2},"firstName":{},"lastName":{}};
let _e;
export function register(container) {
if (!_e) {
_e = CustomElement.define({ name, template, dependencies, containerless, bindables });
}
container.register(_e);
}
`;
const result = preprocessHtmlTemplate(
{ path: path.join('lo', 'foo.html'), contents: html },
preprocessOptions({
stringModuleWrap: (id: string) => `!!raw-loader!${id}`
})
);
assert.equal(result.code, expected);
});


});
124 changes: 124 additions & 0 deletions packages/__tests__/plugin-conventions/strip-meta-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,128 @@ ${' ' /* leading space is untouched */}
bindables: { firstName: {}, lastName: {}, age: {} }
});
});

it('strips alias attribute with multiple names with spaces', function () {
const html = `<template alias="firstName,
alias">
</template>
`;
const expected = `<template>
</template>
`;
assert.deepEqual(stripMetaData(html), {
aliases: ['firstName', 'alias'],
html: expected,
shadowMode: null,
deps: [],
containerless: false,
bindables: {}
});
});

it('strips alias attribute with multiple names', function () {
const html = `<template alias="firstName, alias">
</template>
`;
const expected = `<template>
</template>
`;
assert.deepEqual(stripMetaData(html), {
aliases: ['firstName', 'alias'],
html: expected,
shadowMode: null,
deps: [],
containerless: false,
bindables: {}
});
});

it('strips alias attribute with single name', function () {
const html = `<template alias="firstName">
</template>
`;
const expected = `<template>
</template>
`;
assert.deepEqual(stripMetaData(html), {
aliases: ['firstName'],
html: expected,
shadowMode: null,
deps: [],
containerless: false,
bindables: {}
});
});

it('strips alias node with single name', function () {
const html = `<alias name="firstName"><template>
</template>
`;
const expected = `<template>
</template>
`;
assert.deepEqual(stripMetaData(html), {
aliases: ['firstName'],
html: expected,
shadowMode: null,
deps: [],
containerless: false,
bindables: {}
});
});

it('strips alias node with multiple name', function () {
const html = `<alias name="firstName,alias"><template>
</template>
`;
const expected = `<template>
</template>
`;
assert.deepEqual(stripMetaData(html), {
aliases: ['firstName', 'alias'],
html: expected,
shadowMode: null,
deps: [],
containerless: false,
bindables: {}
});
});


it('strips alias node and attr combo', function () {
const html = `<alias name="firstName,alias"><template alias="firstName2, alias2">
</template>
`;
const expected = `<template>
</template>
`;
assert.deepEqual(stripMetaData(html), {
aliases: ['firstName', "alias", "firstName2", "alias2"],
html: expected,
shadowMode: null,
deps: [],
containerless: false,
bindables: {}
});
});

it('strips alias node with no name', function () {
const html = `<alias><template>
</template>
`;
const expected = `<template>
</template>
`;
assert.deepEqual(stripMetaData(html), {
aliases: [],
html: expected,
shadowMode: null,
deps: [],
containerless: false,
bindables: {}
});
});



});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { stripMetaData } from './strip-meta-data';
export function preprocessHtmlTemplate(unit: IFileUnit, options: IPreprocessOptions): ModifyCodeResult {
const name = kebabCase(path.basename(unit.path, path.extname(unit.path)));
const stripped = stripMetaData(unit.contents);
console.log(stripped);
const { html, deps, containerless, bindables, aliases } = stripped;
let { shadowMode } = stripped;

Expand Down
7 changes: 4 additions & 3 deletions packages/plugin-conventions/src/strip-meta-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ function stripContainerlesss(node: DefaultTreeElement, cb: (ranges: [number, num
function stripAlias(node: DefaultTreeElement, cb: (bindables: string[], ranges: [number, number][]) => void) {
return stripTag(node, 'alias', (attrs, ranges) => {
const { name } = attrs;
if (!name) {
const aliases: string[] = name.split(',').map(s => s.trim()).filter(s => s);
cb(aliases, ranges);
let aliases: string[] = [];
if (name) {
aliases = name.split(',').map(s => s.trim()).filter(s => s);
}
cb(aliases, ranges);
}) || stripAttribute(node, 'template', 'alias', (value, ranges) => {
const aliases: string[] = value.split(',').map(s => s.trim()).filter(s => s);
cb(aliases, ranges);
Expand Down

0 comments on commit 4a45a5c

Please sign in to comment.