diff --git a/src/App.ts b/src/App.ts index 9e74b0ff..d812ec87 100644 --- a/src/App.ts +++ b/src/App.ts @@ -163,9 +163,13 @@ export abstract class AppBase extends EventEmitter { this.addViews(viewSource, viewName); view = this.views.find(viewName); - } else if (name) { + } else if (viewName) { + // Look for a previously registered view matching the component name. view = this.views.find(viewName); - + // If no match, register a new empty view, for backwards compatibility. + if (!view) { + view = this.views.register(viewName, ''); + } } else { view = this.views.register(viewName, ''); } diff --git a/test/all/ComponentHarness.mocha.js b/test/all/ComponentHarness.mocha.js index bb9237bd..cf9254c7 100644 --- a/test/all/ComponentHarness.mocha.js +++ b/test/all/ComponentHarness.mocha.js @@ -258,8 +258,6 @@ describe('ComponentHarness', function() { }); it('gets overridden without error', function() { - function ConflictingClown() {} - ConflictingClown.view = {is: 'clown'}; function Clown() {} Clown.view = { is: 'clown', @@ -277,6 +275,8 @@ describe('ComponentHarness', function() { '', dependencies: [Clown] }; + function ConflictingClown() {} + ConflictingClown.view = {is: 'clown', source: ''}; var html = new ComponentHarness( '', Box, ConflictingClown ).renderHtml().html; diff --git a/test/dom/components.mocha.js b/test/dom/components.mocha.js new file mode 100644 index 00000000..94913db5 --- /dev/null +++ b/test/dom/components.mocha.js @@ -0,0 +1,58 @@ +var expect = require('chai').expect; +var pathLib = require('node:path'); +var domTestRunner = require('../../test-utils/domTestRunner'); + +describe('components', function() { + var runner = domTestRunner.install(); + + describe('app.component registration', function() { + describe('passing just component class', function() { + describe('with static view prop', function() { + it('external view file', function() { + var harness = runner.createHarness(); + + function SimpleBox() {} + SimpleBox.view = { + is: 'simple-box', + // Static `view.file` property, defining path of view file + file: pathLib.resolve(__dirname, '../fixtures/simple-box') + }; + harness.app.component(SimpleBox); + + harness.setup(''); + expect(harness.renderHtml().html).to.equal('
'); + }); + + it('inlined view.source', function() { + var harness = runner.createHarness(); + + function SimpleBox() {} + SimpleBox.view = { + is: 'simple-box', + source: '
Inlined source
' + }; + harness.app.component(SimpleBox); + + harness.setup(''); + expect(harness.renderHtml().html).to.equal('
Inlined source
'); + }); + + it('inferred view file from view name', function() { + var harness = runner.createHarness(); + + // Pre-load view with same name as the component's static `view.is` + harness.app.loadViews(pathLib.resolve(__dirname, '../fixtures/simple-box'), 'simple-box'); + + function SimpleBox() {} + SimpleBox.view = { + is: 'simple-box' + }; + harness.app.component(SimpleBox); + + harness.setup(''); + expect(harness.renderHtml().html).to.equal('
'); + }); + }); + }); + }); +});