From 8e96299653750828593a208b28790639f4b2bc83 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 18 Jun 2013 11:35:51 -0400 Subject: [PATCH] FIX: `Discourse.Presence` was broken on arrays. The flag button was showing for anonymous users. --- .../javascripts/discourse/mixins/presence.js | 11 +------- test/javascripts/components/bbcode_test.js | 22 +++++++-------- test/javascripts/mixins/presence_test.js | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+), 22 deletions(-) create mode 100644 test/javascripts/mixins/presence_test.js diff --git a/app/assets/javascripts/discourse/mixins/presence.js b/app/assets/javascripts/discourse/mixins/presence.js index 3c8ac4b0c..a09bea3d6 100644 --- a/app/assets/javascripts/discourse/mixins/presence.js +++ b/app/assets/javascripts/discourse/mixins/presence.js @@ -18,16 +18,7 @@ Discourse.Presence = Em.Mixin.create({ @return {Boolean} */ blank: function(name) { - var prop = this[name] || this.get(name); - if (!prop) return true; - - switch (typeof prop) { - case "string": - return prop.trim().length === 0; - case "object": - return $.isEmptyObject(prop); - } - return false; + return Ember.isEmpty(this[name] || this.get(name)); }, /** diff --git a/test/javascripts/components/bbcode_test.js b/test/javascripts/components/bbcode_test.js index b3855cebf..e0fef9c33 100644 --- a/test/javascripts/components/bbcode_test.js +++ b/test/javascripts/components/bbcode_test.js @@ -2,40 +2,38 @@ module("Discourse.BBCode"); -test('format', function(){ - - var format = function(input, expected, text) { - equal(Discourse.BBCode.format(input), expected, text); - } +var format = function(input, expected, text) { + equal(Discourse.BBCode.format(input), expected, text); +} +test('basic bbcode', function() { format("[b]strong[/b]", "strong", "bolds text"); format("[i]emphasis[/i]", "emphasis", "italics text"); format("[u]underlined[/u]", "underlined", "underlines text"); format("[s]strikethrough[/s]", "strikethrough", "strikes-through text"); format("[code]\nx++\n[/code]", "
\nx++\n
", "makes code into pre"); format("[spoiler]it's a sled[/spoiler]", "it's a sled", "supports spoiler tags"); - format("[img]http://eviltrout.com/eviltrout.png[/img]", "", "links images"); format("[url]http://bettercallsaul.com[/url]", "http://bettercallsaul.com", "supports [url] without a title"); format("[email]eviltrout@mailinator.com[/email]", "eviltrout@mailinator.com", "supports [email] without a title"); +}); - // Lists +test('lists', function() { format("[ul][li]option one[/li][/ul]", "", "creates an ul"); format("[ol][li]option one[/li][/ol]", "
  1. option one
", "creates an ol"); +}); - // Color +test('color', function() { format("[color=#00f]blue[/color]", "blue", "supports [color=] with a short hex value"); format("[color=#ffff00]yellow[/color]", "yellow", "supports [color=] with a long hex value"); format("[color=red]red[/color]", "red", "supports [color=] with an html color"); format("[color=javascript:alert('wat')]noop[/color]", "noop", "it performs a noop on invalid input"); +}); - // Tags with arguments +test('tags with arguments', function() { format("[size=35]BIG[/size]", "BIG", "supports [size=]"); - format("[url=http://bettercallsaul.com]better call![/url]", "better call!", "supports [url] with a title"); format("[email=eviltrout@mailinator.com]evil trout[/email]", "evil trout", "supports [email] with a title"); format("[u][i]abc[/i][/u]", "abc", "can nest tags"); format("[b]first[/b] [b]second[/b]", "first second", "can bold two things on the same line"); - - }); \ No newline at end of file diff --git a/test/javascripts/mixins/presence_test.js b/test/javascripts/mixins/presence_test.js new file mode 100644 index 000000000..f6e0816b0 --- /dev/null +++ b/test/javascripts/mixins/presence_test.js @@ -0,0 +1,28 @@ +/*global module:true test:true ok:true visit:true expect:true exists:true count:true */ + +module("Discourse.Presence"); + +var testObj = Em.Object.createWithMixins(Discourse.Presence, { + emptyString: "", + nonEmptyString: "Evil Trout", + emptyArray: [], + nonEmptyArray: [1, 2, 3], + age: 34, +}); + +test("present", function() { + ok(testObj.present('nonEmptyString'), "Non empty strings are present"); + ok(!testObj.present('emptyString'), "Empty strings are not present"); + ok(testObj.present('nonEmptyArray'), "Non Empty Arrays are present"); + ok(!testObj.present('emptyArray'), "Empty arrays are not present"); + ok(testObj.present('age'), "integers are present"); +}); + + +test("blank", function() { + ok(testObj.blank('emptyString'), "Empty strings are blank"); + ok(!testObj.blank('nonEmptyString'), "Non empty strings are not blank"); + ok(testObj.blank('emptyArray'), "Empty arrays are blank"); + ok(!testObj.blank('nonEmptyArray'), "Non empty arrays are not blank"); + ok(testObj.blank('missing'), "Missing properties are blank"); +}); \ No newline at end of file