Skip to content

Commit

Permalink
Merge pull request #11466 from stefanpenner/fix-should-display
Browse files Browse the repository at this point in the history
[Bugfix beta] ensure isTruthy is only attempted if the predicate is a…
  • Loading branch information
stefanpenner committed Jun 19, 2015
2 parents d8b1bb1 + 1167636 commit 0df7beb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/ember-metal/lib/computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ ComputedPropertyPrototype.teardown = function(obj, keyName) {
@return {Ember.ComputedProperty} property descriptor instance
@public
*/
function computed(func) {
export default function computed(func) {
var args;

if (arguments.length > 1) {
Expand Down
12 changes: 10 additions & 2 deletions packages/ember-views/lib/streams/should_display.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ export default function shouldDisplay(predicate) {
return new ShouldDisplayStream(predicate);
}

var truthy = predicate && get(predicate, 'isTruthy');
if (typeof truthy === 'boolean') { return truthy; }
var type = typeof predicate;

if (type === 'boolean') { return predicate; }

if (type && type === 'object') {
var isTruthy = get(predicate, 'isTruthy');
if (typeof isTruthy === 'boolean') {
return isTruthy;
}
}

if (isArray(predicate)) {
return get(predicate, 'length') !== 0;
Expand Down
55 changes: 55 additions & 0 deletions packages/ember-views/tests/streams/streams-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import shouldDisplay from 'ember-views/streams/should_display';
import { defineProperty } from 'ember-metal/properties';
import computed from 'ember-metal/computed';

QUnit.module('shouldDisplay');

QUnit.test('predicate permutations', function() {
equal(shouldDisplay(0), false, 'shouldDisplay(0)');
equal(shouldDisplay(-1), true, 'shouldDisplay(-1)');
equal(shouldDisplay(1), true, 'shouldDisplay(1)');
equal(shouldDisplay(Number(1)), true, 'shouldDisplay(Number(1))');
equal(shouldDisplay(Number(0)), false, 'shouldDisplay(Number(0))');
equal(shouldDisplay(Number(-1)), true, 'shouldDisplay(Number(-1))');
equal(shouldDisplay(Boolean(true)), true, 'shouldDisplay(Boolean(true))');
equal(shouldDisplay(Boolean(false)), false, 'shouldDisplay(Boolean(false))');
equal(shouldDisplay(NaN), false, 'shouldDisplay(NaN)');
equal(shouldDisplay('string'), true, 'shouldDisplay("string")');
equal(shouldDisplay(String('string')), true, 'shouldDisplay(String("string"))');
equal(shouldDisplay(Infinity), true, 'shouldDisplay(Infinity)');
equal(shouldDisplay(-Infinity), true, 'shouldDisplay(-Infinity)');
equal(shouldDisplay([]), false, 'shouldDisplay([])');
equal(shouldDisplay([1]), true, 'shouldDisplay([1])');
equal(shouldDisplay({}), true, 'shouldDisplay({})');
equal(shouldDisplay(true), true, 'shouldDisplay(true)');
equal(shouldDisplay(false), false, 'shouldDisplay(false)');
equal(shouldDisplay({ isTruthy: true }), true, 'shouldDisplay({ isTruthy: true })');
equal(shouldDisplay({ isTruthy: false }), false, 'shouldDisplay({ isTruthy: false })');

equal(shouldDisplay(function foo() {}), true, 'shouldDisplay(function (){})');

function falseFunction() { }
falseFunction.isTruthy = false;

equal(shouldDisplay(falseFunction), true, 'shouldDisplay(function.isTruthy = false)');

function trueFunction() { }
falseFunction.isTruthy = true;
equal(shouldDisplay(trueFunction), true, 'shouldDisplay(function.isTruthy = true)');

var truthyObj = { };
defineProperty(truthyObj, 'isTruthy', computed(() => true));
equal(shouldDisplay(truthyObj), true, 'shouldDisplay(obj.get("isTruthy") === true)');

var falseyObj = { };
defineProperty(falseyObj, 'isTruthy', computed(() => false));
equal(shouldDisplay(falseyObj), false, 'shouldDisplay(obj.get("isFalsey") === false)');

var falsyArray = [1];
falsyArray.isTruthy = false;
equal(shouldDisplay(falsyArray), false, '[1].isTruthy = false');

var falseyCPArray = [1];
defineProperty(falseyCPArray, 'isTruthy', computed(() => false));
equal(shouldDisplay(falseyCPArray), false, 'shouldDisplay([1].get("isFalsey") === true');
});

0 comments on commit 0df7beb

Please sign in to comment.