Skip to content

Commit

Permalink
fix(StaticViewStrategy): check for null template before compiling
Browse files Browse the repository at this point in the history
when ".template" is null act as NoViewStrategy - process the dependencies and return null for factory
  • Loading branch information
StrahilKazlachev authored and EisenbergEffect committed Sep 22, 2018
1 parent f9fb639 commit 37d23ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/view-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class NoViewStrategy {
* @param dependencies A list of view resource dependencies of this view.
* @param dependencyBaseUrl The base url for the view dependencies.
*/
constructor(dependencies?: Array<string|Function|Object>, dependencyBaseUrl?: string) {
constructor(dependencies?: Array<string | Function | Object>, dependencyBaseUrl?: string) {
this.dependencies = dependencies || null;
this.dependencyBaseUrl = dependencyBaseUrl || '';
}
Expand Down Expand Up @@ -213,7 +213,7 @@ export class InlineViewStrategy {
* @param dependencies A list of view resource dependencies of this view.
* @param dependencyBaseUrl The base url for the view dependencies.
*/
constructor(markup: string, dependencies?: Array<string|Function|Object>, dependencyBaseUrl?: string) {
constructor(markup: string, dependencies?: Array<string | Function | Object>, dependencyBaseUrl?: string) {
this.markup = markup;
this.dependencies = dependencies || null;
this.dependencyBaseUrl = dependencyBaseUrl || '';
Expand Down Expand Up @@ -335,7 +335,9 @@ export class StaticViewStrategy {
}
// only load custom element as first step.
return Promise.all(elDeps.map(el => el.load(container, el.target))).then(() => {
const factory = viewCompiler.compile(this.template, viewResources, compileInstruction);
const factory = this.template !== null
? viewCompiler.compile(this.template, viewResources, compileInstruction)
: null;
this.factoryIsReady = true;
this.factory = factory;
return factory;
Expand Down
27 changes: 24 additions & 3 deletions test/view-strategy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ViewEngine } from '../src/view-engine';
import { ViewResources } from '../src/view-resources';
import { StaticViewStrategy } from '../src/view-strategy';
import './setup';
import { ViewEngineHooksResource } from '../src/view-engine-hooks-resource';

describe('ViewLocator', () => {
/**@type {ViewEngine} */
Expand Down Expand Up @@ -36,9 +37,7 @@ describe('ViewLocator', () => {
template: '<template><input value.bind="value" /></template>',
dependencies: []
});
class El {

}
class El {}
strategy
.loadViewFactory(viewEngine, ViewCompileInstruction.normal, new ResourceLoadContext(), El)
.then((factory) => {
Expand All @@ -48,6 +47,28 @@ describe('ViewLocator', () => {
}).then(done);
});

it('loads dependencies when template is "null"', (done) => {
class HooksViewEngineHooks {}
let strategy = new StaticViewStrategy({
template: null,
dependencies: [HooksViewEngineHooks]
});
class El {}
spyOn(ViewEngineHooksResource.prototype, 'initialize').and.callThrough();
spyOn(ViewEngineHooksResource.prototype, 'load').and.callThrough();
strategy
.loadViewFactory(viewEngine, ViewCompileInstruction.normal, new ResourceLoadContext(), El)
.then((factory) => {
expect(ViewEngineHooksResource.prototype.initialize)
.toHaveBeenCalledWith(viewEngine.container, HooksViewEngineHooks);
expect(ViewEngineHooksResource.prototype.load)
.toHaveBeenCalledTimes(1);
expect(factory).toBe(null);
done();
})
.catch(done.fail);
});

it('sets formal "moduleId"', () => {
const strategy = new StaticViewStrategy('<template><input value.bind="value" /></template>');
expect(strategy.moduleId).toBeDefined();
Expand Down

0 comments on commit 37d23ea

Please sign in to comment.