Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Fix contextual components handling invalid component paths #12422

Merged
merged 1 commit into from
Oct 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions packages/ember-htmlbars/lib/keywords/closure-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
@submodule ember-templates
*/

import { assert } from 'ember-metal/debug';
import isNone from 'ember-metal/is_none';
import { symbol } from 'ember-metal/utils';
import BasicStream from 'ember-metal/streams/stream';
import { read } from 'ember-metal/streams/utils';
Expand All @@ -27,7 +29,7 @@ let ClosureComponentStream = BasicStream.extend({
this[COMPONENT_REFERENCE] = true;
},
compute() {
return createClosureComponentCell(this._env, this._path, this._params, this._hash);
return createClosureComponentCell(this._env, this._path, this._params, this._hash, this.label);
}
});

Expand All @@ -46,16 +48,27 @@ export default function closureComponent(env, [path, ...params], hash) {
return s;
}

function createClosureComponentCell(env, originalComponentPath, params, hash) {
function createClosureComponentCell(env, originalComponentPath, params, hash, label) {
let componentPath = read(originalComponentPath);

assert(`Component path cannot be null in ${label}`,
!isNone(componentPath));

if (isComponentCell(componentPath)) {
return createNestedClosureComponentCell(componentPath, params, hash);
} else {
assert(`The component helper cannot be used without a valid component name. You used "${componentPath}" via ${label}`,
isValidComponentPath(env, componentPath));
return createNewClosureComponentCell(env, componentPath, params, hash);
}
}

function isValidComponentPath(env, path) {
const result = lookupComponent(env.container, path);

return !!(result.component || result.layout);
}

export function isComponentCell(component) {
return component && component[COMPONENT_CELL];
}
Expand Down
40 changes: 35 additions & 5 deletions packages/ember-htmlbars/tests/helpers/closure_component_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,14 @@ if (isEnabled('ember-contextual-components')) {
);

let template = compile('{{component (component lookupComponent)}}');
component = Component.extend({ container, template }).create();
component = Component.extend({
container,
template,
lookupComponent: '-mandarin'
}).create();

runAppend(component);
equal(component.$().text(), ``, 'undefined lookupComponent does not render');
run(() => {
component.set('lookupComponent', '-mandarin');
});

equal(component.$().text(), `ni hao`,
'mandarin lookupComponent renders greeting');
run(() => {
Expand Down Expand Up @@ -309,4 +310,33 @@ if (isEnabled('ember-contextual-components')) {
runAppend(component);
equal(component.$().text(), 'Hodi Sergio', 'component is rendered');
});

QUnit.test('raises an assertion when component path is null', function() {
let template = compile(`{{component (component lookupComponent)}}`);
component = Component.extend({ container, template }).create();

expectAssertion(() => {
runAppend(component);
});
});

QUnit.test('raises an assertion when component path is not a component name', function() {
let template = compile(`{{component (component "not-a-component")}}`);
component = Component.extend({ container, template }).create();

expectAssertion(() => {
runAppend(component);
}, `The component helper cannot be used without a valid component name. You used "not-a-component" via (component "not-a-component")`);

template = compile(`{{component (component compName)}}`);
component = Component.extend({
container,
template,
compName: 'not-a-component'
}).create();

expectAssertion(() => {
runAppend(component);
}, `The component helper cannot be used without a valid component name. You used "not-a-component" via (component compName)`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 awesome!!

});
}