Permalink
Browse files

fix(StaticViewStrategy): check for null template before compiling

when ".template" is null act as NoViewStrategy - process the dependencies and return null for factory
  • Loading branch information...
StrahilKazlachev authored and EisenbergEffect committed Sep 21, 2018
1 parent f9fb639 commit 37d23ea666d0fa199a9d2537172406ec64b5f8bb
Showing with 29 additions and 6 deletions.
  1. +5 −3 src/view-strategy.js
  2. +24 −3 test/view-strategy.spec.js
@@ -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 || '';
}
@@ -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 || '';
@@ -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;
@@ -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} */
@@ -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) => {
@@ -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();

0 comments on commit 37d23ea

Please sign in to comment.