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 beta] ensure isTruthy is only attempted if the predicate is a… #11466

Merged
merged 1 commit into from
Jun 19, 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
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');
});