Skip to content

Commit

Permalink
Merge pull request #627 from derbyjs/fix-component-inferred-view
Browse files Browse the repository at this point in the history
Fix bug registering component with static `view.is` prop but no `view.file` prop
  • Loading branch information
ericyhwang authored Jan 30, 2024
2 parents 8eedad2 + 0d24009 commit 006e372
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,13 @@ export abstract class AppBase<T = object> 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, '');
}
Expand Down
4 changes: 2 additions & 2 deletions test/all/ComponentHarness.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -277,6 +275,8 @@ describe('ComponentHarness', function() {
'</div>',
dependencies: [Clown]
};
function ConflictingClown() {}
ConflictingClown.view = {is: 'clown', source: '<index:>'};
var html = new ComponentHarness(
'<view is="box" />', Box, ConflictingClown
).renderHtml().html;
Expand Down
58 changes: 58 additions & 0 deletions test/dom/components.mocha.js
Original file line number Diff line number Diff line change
@@ -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('<view is="simple-box"/>');
expect(harness.renderHtml().html).to.equal('<div class="simple-box"></div>');
});

it('inlined view.source', function() {
var harness = runner.createHarness();

function SimpleBox() {}
SimpleBox.view = {
is: 'simple-box',
source: '<index:><div>Inlined source</div>'
};
harness.app.component(SimpleBox);

harness.setup('<view is="simple-box"/>');
expect(harness.renderHtml().html).to.equal('<div>Inlined source</div>');
});

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('<view is="simple-box"/>');
expect(harness.renderHtml().html).to.equal('<div class="simple-box"></div>');
});
});
});
});
});

0 comments on commit 006e372

Please sign in to comment.