Skip to content

Commit

Permalink
fix(static-view-strategy): correctly handles invalid resources
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon authored and EisenbergEffect committed Oct 30, 2018
1 parent c0e6204 commit 4149e73
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/view-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ export class StaticViewStrategy {
let exported = dep[key];
if (typeof exported === 'function') {
resource = viewResources.autoRegister(container, exported);
}
if (resource.elementName !== null) {
elDeps.push(resource);
if (resource.elementName !== null) {
elDeps.push(resource);
}
}
}
} else {
Expand Down
58 changes: 58 additions & 0 deletions test/view-strategy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ describe('ViewLocator', () => {
const strategy = new StaticViewStrategy('<template><input value.bind="value"></template>');
expect(strategy.moduleId).toBeDefined();
});

describe('Invalid dependencies', () => {
const dependencies = [null, undefined, 'a valid dep, NOT', 42, Symbol()];
for (const dep of dependencies) {
it('throws when one of dependencies is not a function or a module. Actual: "' + String(dep) + '"', done => {
class El {}
let strategy = new StaticViewStrategy({
template: '<template></template>',
dependencies: () => [dep]
});
strategy
.loadViewFactory(viewEngine, ViewCompileInstruction.normal, new ResourceLoadContext(), El)
.then(
() => {
done.fail(new Error('It should have failed with dep: ' + String(dep)));
},
(ex) => {
expect(ex.toString()).toContain('dependency neither function nor object');
done();
}
);
});
}
});
});

it('loads dependencies', (done) => {
Expand Down Expand Up @@ -173,6 +197,40 @@ describe('ViewLocator', () => {
}).then(done);
});

it('ignore dependencies that are not function', (done) => {
class Ekko {}
function mockEsmImport(path) {
// Note: export name was intenionally made one character to demonstrate static dependencies declaration relies on
// the exported value (class), not the export name. This introduces inconsistency with the rest
return Promise.resolve({
b: null,
c: undefined,
d: 42,
e: 'a valid dep, NOT',
f: Symbol()
});
}
let strategy = new StaticViewStrategy({
template: '<template></template>',
dependencies: () => [mockEsmImport()]
});
let spy = spyOn(ViewResources.prototype, 'autoRegister').and.callThrough();
strategy
.loadViewFactory(viewEngine, ViewCompileInstruction.normal, new ResourceLoadContext(), Ekko)
.then(
(factory) => {
let resources = factory.resources;
expect(resources.getElement('ekko').target).toBe(Ekko);
expect(spy).toHaveBeenCalledTimes(1);
done();
},
ex => {
expect(false).toBe(true, 'It should not have failled');
done.fail(ex);
}
);
});

describe('with custom elements', () => {
it('loads when mixing multiple custom elements and other resource types', done => {
let loadCount = 0;
Expand Down

0 comments on commit 4149e73

Please sign in to comment.