Skip to content

Commit

Permalink
Merge pull request #12922 from chadhietala/failing-isTruthy-test
Browse files Browse the repository at this point in the history
[BUGFIX Release] Special case `{{#with}}` for `isTruthy`
  • Loading branch information
rwjblue committed Feb 7, 2016
2 parents 838434a + 31a5295 commit 9dd4db5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/ember-htmlbars/lib/hooks/link-render-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export default function linkRenderNode(renderNode, env, scope, path, params, has
switch (path) {
case 'unbound': return true;
case 'unless':
case 'if': params[0] = shouldDisplay(params[0]); break;
case 'if': params[0] = shouldDisplay(params[0], toBool); break;
case 'each': params[0] = eachParam(params[0]); break;
case 'with': params[0] = shouldDisplay(params[0], identity); break;
}
}

Expand Down Expand Up @@ -76,7 +77,7 @@ function eachParam(list) {
return stream;
}

function shouldDisplay(predicate) {
function shouldDisplay(predicate, coercer) {
let length = getKey(predicate, 'length');
let isTruthy = getKey(predicate, 'isTruthy');

Expand All @@ -93,7 +94,7 @@ function shouldDisplay(predicate) {
return isTruthyVal;
}

return !!predicateVal;
return coercer(predicateVal);
}, 'ShouldDisplay');

addDependency(stream, length);
Expand All @@ -102,6 +103,14 @@ function shouldDisplay(predicate) {
return stream;
}

function toBool(value) {
return !!value;
}

function identity(value) {
return value;
}

function getKey(obj, key) {
if (isStream(obj)) {
return obj.getKey(key);
Expand Down
73 changes: 73 additions & 0 deletions packages/ember-htmlbars/tests/helpers/with_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,79 @@ QUnit.test('re-using the same variable with different #with blocks does not over
equal(view.$().text(), 'Admin: Tom Dale User: Yehuda Katz', 'should be properly scoped');
});

QUnit.test('should respect `isTruthy` field on a view', function() {
view = EmberView.create({
template: compile('{{#with view}}True{{else}}False{{/with}}'),
isTruthy: true
});
runAppend(view);

equal(view.$().text(), 'True');

run(function() {
set(view, 'isTruthy', false);
});

equal(view.$().text(), 'False');

run(function() {
set(view, 'isTruthy', true);
});

equal(view.$().text(), 'True');
});

QUnit.test('should respect `isTruthy` field on an object', function() {
view = EmberView.create({
template: compile('{{#with view.foo}}True{{else}}False{{/with}}'),
foo: {
isTruthy: true
}
});
runAppend(view);

equal(view.$().text(), 'True');

run(function() {
set(view, 'foo.isTruthy', false);
});

equal(view.$().text(), 'False');

run(function() {
set(view, 'foo.isTruthy', true);
});

equal(view.$().text(), 'True');
});

QUnit.test('should respect `isTruthy` field on the context object', function() {
view = EmberView.create({
template: compile('{{#with foo}}True{{else}}False{{/with}}'),
context: {
foo: {
isTruthy: true
}
}
});

runAppend(view);

equal(view.$().text(), 'True');

run(function() {
set(view, 'context.foo.isTruthy', false);
});

equal(view.$().text(), 'False');

run(function() {
set(view, 'context.foo.isTruthy', true);
});

equal(view.$().text(), 'True');
});

QUnit.test('the scoped variable is not available outside the {{with}} block.', function() {
view = EmberView.create({
template: compile('{{name}}-{{#with other as |name|}}{{name}}{{/with}}-{{name}}'),
Expand Down

0 comments on commit 9dd4db5

Please sign in to comment.