From 37d23ea666d0fa199a9d2537172406ec64b5f8bb Mon Sep 17 00:00:00 2001 From: StrahilKazlachev Date: Fri, 21 Sep 2018 21:41:14 +0300 Subject: [PATCH] fix(StaticViewStrategy): check for null template before compiling when ".template" is null act as NoViewStrategy - process the dependencies and return null for factory --- src/view-strategy.js | 8 +++++--- test/view-strategy.spec.js | 27 ++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/view-strategy.js b/src/view-strategy.js index 527f89cb..09b59b59 100644 --- a/src/view-strategy.js +++ b/src/view-strategy.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, dependencyBaseUrl?: string) { + constructor(dependencies?: Array, 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, dependencyBaseUrl?: string) { + constructor(markup: string, dependencies?: Array, 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; diff --git a/test/view-strategy.spec.js b/test/view-strategy.spec.js index 11864876..12752116 100644 --- a/test/view-strategy.spec.js +++ b/test/view-strategy.spec.js @@ -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: '', 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(''); expect(strategy.moduleId).toBeDefined();