From 9a40789f4d0302a61640b3feb7e807aae1e83d60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Thu, 28 Feb 2013 03:26:20 +0100 Subject: [PATCH] cleaned up all javascript specs for better readability --- spec/javascripts/key_value_store_spec.js | 42 ++-- spec/javascripts/message_bus_spec.js | 12 +- spec/javascripts/onebox_spec.js | 39 ++-- spec/javascripts/preload_store_spec.js | 224 +++++++++---------- spec/javascripts/sanitize_spec.js | 12 +- spec/javascripts/user_action_spec.js | 60 ++--- spec/javascripts/utilities_spec.js | 270 ++++++++++++----------- 7 files changed, 333 insertions(+), 326 deletions(-) diff --git a/spec/javascripts/key_value_store_spec.js b/spec/javascripts/key_value_store_spec.js index 9469d7a35..7adcd119f 100644 --- a/spec/javascripts/key_value_store_spec.js +++ b/spec/javascripts/key_value_store_spec.js @@ -1,28 +1,24 @@ /*global waitsFor:true expect:true describe:true beforeEach:true it:true */ - describe("Discourse.KeyValueStore", function() { - return describe("Setting values", function() { - var store; - store = Discourse.KeyValueStore; - store.init("test"); - it("able to get the value back from the store", function() { - store.set({ - key: "bob", - value: "uncle" - }); - return expect(store.get("bob")).toBe("uncle"); - }); - return it("able to nuke the store", function() { - store.set({ - key: "bob1", - value: "uncle" - }); - store.abandonLocal(); - localStorage.a = 1; - expect(store.get("bob1")).toBe(void 0); - return expect(localStorage.a).toBe("1"); - }); +describe("Discourse.KeyValueStore", function() { + + describe("Setting values", function() { + var store = Discourse.KeyValueStore; + store.init("test"); + + it("is able to get the value back from the store", function() { + store.set({ key: "bob", value: "uncle" }); + expect(store.get("bob")).toBe("uncle"); }); + + it("is able to nuke the store", function() { + store.set({ key: "bob1", value: "uncle" }); + store.abandonLocal(); + localStorage.a = 1; + expect(store.get("bob1")).toBe(void 0); + expect(localStorage.a).toBe("1"); + }); + }); - +}); diff --git a/spec/javascripts/message_bus_spec.js b/spec/javascripts/message_bus_spec.js index 8d9e10ffe..98da9eecf 100644 --- a/spec/javascripts/message_bus_spec.js +++ b/spec/javascripts/message_bus_spec.js @@ -1,11 +1,9 @@ /*global waitsFor:true expect:true describe:true beforeEach:true it:true */ - describe("Discourse.MessageBus", function() { - return describe("Long polling", function() { - var bus; - bus = Discourse.MessageBus; - return bus.start(); - }); +describe("Discourse.MessageBus", function() { + + describe("Long polling", function() { + Discourse.MessageBus.start(); }); - +}); diff --git a/spec/javascripts/onebox_spec.js b/spec/javascripts/onebox_spec.js index 1d942cca9..6eaa48b90 100644 --- a/spec/javascripts/onebox_spec.js +++ b/spec/javascripts/onebox_spec.js @@ -1,26 +1,21 @@ /*global waitsFor:true expect:true describe:true beforeEach:true it:true spyOn:true */ - describe("Discourse.Onebox", function() { - beforeEach(function() { - return spyOn(jQuery, 'ajax').andCallThrough(); - }); - it("Stops rapid calls with cache true", function() { - Discourse.Onebox.lookup('http://bla.com', true, function(c) { - return c; - }); - Discourse.Onebox.lookup('http://bla.com', true, function(c) { - return c; - }); - return expect(jQuery.ajax.calls.length).toBe(1); - }); - return it("Stops rapid calls with cache false", function() { - Discourse.Onebox.lookup('http://bla.com/a', false, function(c) { - return c; - }); - Discourse.Onebox.lookup('http://bla.com/a', false, function(c) { - return c; - }); - return expect(jQuery.ajax.calls.length).toBe(1); - }); + +describe("Discourse.Onebox", function() { + + beforeEach(function() { + spyOn(jQuery, 'ajax').andCallThrough(); }); + it("Stops rapid calls with cache true", function() { + Discourse.Onebox.lookup('http://bla.com', true, function(c) { return c; }); + Discourse.Onebox.lookup('http://bla.com', true, function(c) { return c; }); + expect(jQuery.ajax.calls.length).toBe(1); + }); + it("Stops rapid calls with cache false", function() { + Discourse.Onebox.lookup('http://bla.com/a', false, function(c) { return c; }); + Discourse.Onebox.lookup('http://bla.com/a', false, function(c) { return c; }); + expect(jQuery.ajax.calls.length).toBe(1); + }); + +}); diff --git a/spec/javascripts/preload_store_spec.js b/spec/javascripts/preload_store_spec.js index 4e0dde3d6..5d95ade38 100644 --- a/spec/javascripts/preload_store_spec.js +++ b/spec/javascripts/preload_store_spec.js @@ -1,116 +1,118 @@ /*global waitsFor:true expect:true describe:true beforeEach:true it:true runs:true */ - describe("PreloadStore", function() { - beforeEach(function() { - return PreloadStore.store('bane', 'evil'); - }); - describe("contains", function() { - it("returns false for a key that doesn't exist", function() { - return expect(PreloadStore.contains('joker')).toBe(false); - }); - return it("returns true for a stored key", function() { - return expect(PreloadStore.contains('bane')).toBe(true); - }); - }); - describe('getStatic', function() { - it("returns undefined if the key doesn't exist", function() { - return expect(PreloadStore.getStatic('joker')).toBe(void 0); - }); - it("returns the the key if it exists", function() { - return expect(PreloadStore.getStatic('bane')).toBe('evil'); - }); - return it("removes the key after being called", function() { - PreloadStore.getStatic('bane'); - return expect(PreloadStore.getStatic('bane')).toBe(void 0); - }); - }); - return describe('get', function() { - it("returns a promise that resolves to undefined", function() { - var done, storeResult; - done = storeResult = null; - PreloadStore.get('joker').then(function(result) { - done = true; - storeResult = result; - }); - waitsFor((function() { - return done; - }), "Promise never resolved", 1000); - return runs(function() { - return expect(storeResult).toBe(void 0); - }); - }); - it("returns a promise that resolves to the result of the finder", function() { - var done, finder, storeResult; - done = storeResult = null; - finder = function() { - return 'evil'; - }; - PreloadStore.get('joker', finder).then(function(result) { - done = true; - storeResult = result; - }); - waitsFor((function() { - return done; - }), "Promise never resolved", 1000); - return runs(function() { - return expect(storeResult).toBe('evil'); - }); - }); - it("returns a promise that resolves to the result of the finder's promise", function() { - var done, finder, storeResult; - done = storeResult = null; - finder = function() { - var promise; - promise = new RSVP.Promise(); - promise.resolve('evil'); - return promise; - }; - PreloadStore.get('joker', finder).then(function(result) { - done = true; - storeResult = result; - }); - waitsFor((function() { - return done; - }), "Promise never resolved", 1000); - return runs(function() { - return expect(storeResult).toBe('evil'); - }); - }); - it("returns a promise that resolves to the result of the finder's rejected promise", function() { - var done, finder, storeResult; - done = storeResult = null; - finder = function() { - var promise; - promise = new RSVP.Promise(); - promise.reject('evil'); - return promise; - }; - PreloadStore.get('joker', finder).then(null, function(rejectedResult) { - done = true; - storeResult = rejectedResult; - }); - waitsFor((function() { - return done; - }), "Promise never rejected", 1000); - return runs(function() { - return expect(storeResult).toBe('evil'); - }); - }); - return it("returns a promise that resolves to 'evil'", function() { - var done, storeResult; - done = storeResult = null; - PreloadStore.get('bane').then(function(result) { - done = true; - storeResult = result; - }); - waitsFor((function() { - return done; - }), "Promise never resolved", 1000); - return runs(function() { - return expect(storeResult).toBe('evil'); - }); - }); - }); +describe("PreloadStore", function() { + + beforeEach(function() { + PreloadStore.store('bane', 'evil'); }); + describe("contains", function() { + it("returns false for a key that doesn't exist", function() { + expect(PreloadStore.contains('joker')).toBe(false); + }); + + it("returns true for a stored key", function() { + expect(PreloadStore.contains('bane')).toBe(true); + }); + + }); + + describe('getStatic', function() { + + it("returns undefined if the key doesn't exist", function() { + expect(PreloadStore.getStatic('joker')).toBe(void 0); + }); + + it("returns the the key if it exists", function() { + expect(PreloadStore.getStatic('bane')).toBe('evil'); + }); + + it("removes the key after being called", function() { + PreloadStore.getStatic('bane'); + expect(PreloadStore.getStatic('bane')).toBe(void 0); + }); + + }); + + describe('get', function() { + + it("returns a promise that resolves to undefined", function() { + var done, storeResult; + done = storeResult = null; + PreloadStore.get('joker').then(function(result) { + done = true; + storeResult = result; + }); + waitsFor((function() { return done; }), "Promise never resolved", 1000); + runs(function() { + expect(storeResult).toBe(void 0); + }); + }); + + it("returns a promise that resolves to the result of the finder", function() { + var done, finder, storeResult; + done = storeResult = null; + finder = function() { return 'evil'; }; + PreloadStore.get('joker', finder).then(function(result) { + done = true; + storeResult = result; + }); + waitsFor((function() { return done; }), "Promise never resolved", 1000); + runs(function() { + expect(storeResult).toBe('evil'); + }); + }); + + it("returns a promise that resolves to the result of the finder's promise", function() { + var done, finder, storeResult; + done = storeResult = null; + finder = function() { + var promise = new RSVP.Promise(); + promise.resolve('evil'); + return promise; + }; + PreloadStore.get('joker', finder).then(function(result) { + done = true; + storeResult = result; + }); + waitsFor((function() { return done; }), "Promise never resolved", 1000); + runs(function() { + expect(storeResult).toBe('evil'); + }); + }); + + it("returns a promise that resolves to the result of the finder's rejected promise", function() { + var done, finder, storeResult; + done = storeResult = null; + finder = function() { + var promise = new RSVP.Promise(); + promise.reject('evil'); + return promise; + }; + PreloadStore.get('joker', finder).then(null, function(rejectedResult) { + done = true; + storeResult = rejectedResult; + }); + waitsFor((function() { return done; }), "Promise never rejected", 1000); + runs(function() { + expect(storeResult).toBe('evil'); + }); + }); + + it("returns a promise that resolves to 'evil'", function() { + var done, storeResult; + done = storeResult = null; + PreloadStore.get('bane').then(function(result) { + done = true; + storeResult = result; + }); + waitsFor((function() { return done; }), "Promise never resolved", 1000); + runs(function() { + expect(storeResult).toBe('evil'); + }); + }); + + }); + +}); \ No newline at end of file diff --git a/spec/javascripts/sanitize_spec.js b/spec/javascripts/sanitize_spec.js index ba8369d52..b5126ec5b 100644 --- a/spec/javascripts/sanitize_spec.js +++ b/spec/javascripts/sanitize_spec.js @@ -2,20 +2,14 @@ describe("sanitize", function(){ - it("strips all script tags", function(){ var sanitized = sanitizeHtml("
"); - - expect(sanitized) - .toBe("
"); + expect(sanitized).toBe("
"); }); it("strips disallowed attributes", function(){ var sanitized = sanitizeHtml("

hello

"); - - expect(sanitized) - .toBe("

hello

"); + expect(sanitized).toBe("

hello

"); }); + }); - - diff --git a/spec/javascripts/user_action_spec.js b/spec/javascripts/user_action_spec.js index fe709e798..fb6e3c630 100644 --- a/spec/javascripts/user_action_spec.js +++ b/spec/javascripts/user_action_spec.js @@ -1,32 +1,36 @@ /*global waitsFor:true expect:true describe:true beforeEach:true it:true */ - describe("Discourse.UserAction", function() { - return describe("collapseStream", function() { - return it("collapses all likes", function() { - var actions; - actions = [ - Discourse.UserAction.create({ - action_type: Discourse.UserAction.LIKE, - topic_id: 1, - user_id: 1, - post_number: 1 - }), Discourse.UserAction.create({ - action_type: Discourse.UserAction.EDIT, - topic_id: 2, - user_id: 1, - post_number: 1 - }), Discourse.UserAction.create({ - action_type: Discourse.UserAction.LIKE, - topic_id: 1, - user_id: 2, - post_number: 1 - }) - ]; - actions = Discourse.UserAction.collapseStream(actions); - expect(actions.length).toBe(2); - expect(actions[0].get("children").length).toBe(1); - return expect(actions[0].get("children")[0].items.length).toBe(2); - }); + +describe("Discourse.UserAction", function() { + + describe("collapseStream", function() { + + it("collapses all likes", function() { + var actions = [ + Discourse.UserAction.create({ + action_type: Discourse.UserAction.LIKE, + topic_id: 1, + user_id: 1, + post_number: 1 + }), Discourse.UserAction.create({ + action_type: Discourse.UserAction.EDIT, + topic_id: 2, + user_id: 1, + post_number: 1 + }), Discourse.UserAction.create({ + action_type: Discourse.UserAction.LIKE, + topic_id: 1, + user_id: 2, + post_number: 1 + }) + ]; + + actions = Discourse.UserAction.collapseStream(actions); + + expect(actions.length).toBe(2); + expect(actions[0].get("children").length).toBe(1); + expect(actions[0].get("children")[0].items.length).toBe(2); }); + }); - +}); diff --git a/spec/javascripts/utilities_spec.js b/spec/javascripts/utilities_spec.js index 068f696d7..4c53a05e3 100644 --- a/spec/javascripts/utilities_spec.js +++ b/spec/javascripts/utilities_spec.js @@ -1,134 +1,152 @@ /*global waitsFor:true expect:true describe:true beforeEach:true it:true */ - describe("Discourse.Utilities", function() { - describe("categoryUrlId", function() { - it("returns the slug when it exists", function() { - return expect(Discourse.Utilities.categoryUrlId({ - slug: 'hello' - })).toBe("hello"); - }); - it("returns id-category when slug is an empty string", function() { - return expect(Discourse.Utilities.categoryUrlId({ - id: 123, - slug: '' - })).toBe("123-category"); - }); - return it("returns id-category without a slug", function() { - return expect(Discourse.Utilities.categoryUrlId({ - id: 456 - })).toBe("456-category"); - }); +describe("Discourse.Utilities", function() { + + describe("categoryUrlId", function() { + + it("returns the slug when it exists", function() { + expect(Discourse.Utilities.categoryUrlId({ slug: 'hello' })).toBe("hello"); }); - describe("Cooking", function() { - var cook; - cook = function(contents, opts) { - opts = opts || {}; - opts.mentionLookup = opts.mentionLookup || (function() { - return false; - }); - return Discourse.Utilities.cook(contents, opts); - }; - it("surrounds text with paragraphs", function() { - return expect(cook("hello")).toBe("

hello

"); - }); - it("automatically handles trivial newlines", function() { - return expect(cook("1\n2\n3")).toBe("

1
\n2
\n3

"); - }); - it("handles quotes properly", function() { - var cooked; - cooked = cook("1[quote=\"bob, post:1\"]my quote[/quote]2", { - topicId: 2, - lookupAvatar: function(name) { - return "" + name; - } - }); - return expect(cooked).toBe("

1

\n

2

"); - }); - it("includes no avatar if none is found", function() { - var cooked; - cooked = cook("1[quote=\"bob, post:1\"]my quote[/quote]2", { - topicId: 2, - lookupAvatar: function(name) { - return null; - } - }); - return expect(cooked).toBe("

1

\n

2

"); - }); - describe("Links", function() { - it("allows links to contain query params", function() { - expect(cook("Youtube: http://www.youtube.com/watch?v=1MrpeBRkM5A")). - toBe('

Youtube: http://www.youtube.com/watch?v=1MrpeBRkM5A

'); - }); - it("escapes double underscores in URLs", function() { - return expect(cook("Derpy: http://derp.com?__test=1")).toBe('

Derpy: http://derp.com?__test=1

'); - }); - it("autolinks something that begins with www", function() { - return expect(cook("Atwood: www.codinghorror.com")).toBe('

Atwood: www.codinghorror.com

'); - }); - it("autolinks a URL with http://www", function() { - return expect(cook("Atwood: http://www.codinghorror.com")).toBe('

Atwood: http://www.codinghorror.com

'); - }); - it("autolinks a URL", function() { - return expect(cook("EvilTrout: http://eviltrout.com")).toBe('

EvilTrout: http://eviltrout.com

'); - }); - it("supports markdown style links", function() { - return expect(cook("here is [an example](http://twitter.com)")).toBe('

here is an example

'); - }); - return it("autolinks a URL with parentheses (like Wikipedia)", function() { - return expect(cook("Batman: http://en.wikipedia.org/wiki/The_Dark_Knight_(film)")) - .toBe('

Batman: http://en.wikipedia.org/wiki/The_Dark_Knight_(film)

'); - }); - }); - describe("Mentioning", function() { - it("translates mentions to links", function() { - return expect(cook("Hello @sam", { - mentionLookup: (function() { - return true; - }) - })).toBe("

Hello @sam

"); - }); - it("adds a mention class", function() { - return expect(cook("Hello @EvilTrout")).toBe("

Hello @EvilTrout

"); - }); - it("won't add mention class to an email address", function() { - return expect(cook("robin@email.host")).toBe("

robin@email.host

"); - }); - it("won't be affected by email addresses that have a number before the @ symbol", function() { - return expect(cook("hanzo55@yahoo.com")).toBe("

hanzo55@yahoo.com

"); - }); - return it("supports a @mention at the beginning of a post", function() { - return expect(cook("@EvilTrout yo")).toBe("

@EvilTrout yo

"); - }); - }); - return describe("Oneboxing", function() { - it("doesn't onebox a link within a list", function() { - return expect(cook("- http://www.textfiles.com/bbs/MINDVOX/FORUMS/ethics\n\n- http://drupal.org")).not.toMatch(/onebox/); - }); - it("adds a onebox class to a link on its own line", function() { - return expect(cook("http://test.com")).toMatch(/onebox/); - }); - it("supports multiple links", function() { - return expect(cook("http://test.com\nhttp://test2.com")).toMatch(/onebox[\s\S]+onebox/m); - }); - it("doesn't onebox links that have trailing text", function() { - return expect(cook("http://test.com bob")).not.toMatch(/onebox/); - }); - return it("works with links that have underscores in them", function() { - return expect(cook("http://en.wikipedia.org/wiki/Homicide:_Life_on_the_Street")). - toBe("

http://en.wikipedia.org/wiki/Homicide:_Life_on_the_Street

"); - }); - }); + + it("returns id-category when slug is an empty string", function() { + expect(Discourse.Utilities.categoryUrlId({ id: 123, slug: '' })).toBe("123-category"); }); - return describe("emailValid", function() { - it("allows upper case in first part of emails", function() { - return expect(Discourse.Utilities.emailValid('Bob@example.com')).toBe(true); - }); - return it("allows upper case in domain of emails", function() { - return expect(Discourse.Utilities.emailValid('bob@EXAMPLE.com')).toBe(true); - }); + + it("returns id-category without a slug", function() { + expect(Discourse.Utilities.categoryUrlId({ id: 456 })).toBe("456-category"); }); + }); + describe("Cooking", function() { + var cook = function(contents, opts) { + opts = opts || {}; + opts.mentionLookup = opts.mentionLookup || false; + return Discourse.Utilities.cook(contents, opts); + }; + + it("surrounds text with paragraphs", function() { + expect(cook("hello")).toBe("

hello

"); + }); + + it("automatically handles trivial newlines", function() { + expect(cook("1\n2\n3")).toBe("

1
\n2
\n3

"); + }); + + it("handles quotes properly", function() { + var cooked = cook("1[quote=\"bob, post:1\"]my quote[/quote]2", { + topicId: 2, + lookupAvatar: function(name) { return "" + name; } + }); + expect(cooked).toBe("

1

\n

2

"); + }); + + it("includes no avatar if none is found", function() { + var cooked = cook("1[quote=\"bob, post:1\"]my quote[/quote]2", { + topicId: 2, + lookupAvatar: function(name) { return null; } + }); + expect(cooked).toBe("

1

\n

2

"); + }); + + describe("Links", function() { + + it("allows links to contain query params", function() { + expect(cook("Youtube: http://www.youtube.com/watch?v=1MrpeBRkM5A")). + toBe('

Youtube: http://www.youtube.com/watch?v=1MrpeBRkM5A

'); + }); + + it("escapes double underscores in URLs", function() { + expect(cook("Derpy: http://derp.com?__test=1")).toBe('

Derpy: http://derp.com?__test=1

'); + }); + + it("autolinks something that begins with www", function() { + expect(cook("Atwood: www.codinghorror.com")).toBe('

Atwood: www.codinghorror.com

'); + }); + + it("autolinks a URL with http://www", function() { + expect(cook("Atwood: http://www.codinghorror.com")).toBe('

Atwood: http://www.codinghorror.com

'); + }); + + it("autolinks a URL", function() { + expect(cook("EvilTrout: http://eviltrout.com")).toBe('

EvilTrout: http://eviltrout.com

'); + }); + + it("supports markdown style links", function() { + expect(cook("here is [an example](http://twitter.com)")).toBe('

here is an example

'); + }); + + it("autolinks a URL with parentheses (like Wikipedia)", function() { + expect(cook("Batman: http://en.wikipedia.org/wiki/The_Dark_Knight_(film)")). + toBe('

Batman: http://en.wikipedia.org/wiki/The_Dark_Knight_(film)

'); + }); + + }); + + describe("Mentioning", function() { + + it("translates mentions to links", function() { + expect(cook("Hello @sam", { mentionLookup: (function() { return true; }) })).toBe("

Hello @sam

"); + }); + + it("adds a mention class", function() { + expect(cook("Hello @EvilTrout")).toBe("

Hello @EvilTrout

"); + }); + + it("won't add mention class to an email address", function() { + expect(cook("robin@email.host")).toBe("

robin@email.host

"); + }); + + it("won't be affected by email addresses that have a number before the @ symbol", function() { + expect(cook("hanzo55@yahoo.com")).toBe("

hanzo55@yahoo.com

"); + }); + + it("supports a @mention at the beginning of a post", function() { + expect(cook("@EvilTrout yo")).toBe("

@EvilTrout yo

"); + }); + + }); + + describe("Oneboxing", function() { + + it("doesn't onebox a link within a list", function() { + expect(cook("- http://www.textfiles.com/bbs/MINDVOX/FORUMS/ethics\n\n- http://drupal.org")).not.toMatch(/onebox/); + }); + + it("adds a onebox class to a link on its own line", function() { + expect(cook("http://test.com")).toMatch(/onebox/); + }); + + it("supports multiple links", function() { + expect(cook("http://test.com\nhttp://test2.com")).toMatch(/onebox[\s\S]+onebox/m); + }); + + it("doesn't onebox links that have trailing text", function() { + expect(cook("http://test.com bob")).not.toMatch(/onebox/); + }); + + it("works with links that have underscores in them", function() { + expect(cook("http://en.wikipedia.org/wiki/Homicide:_Life_on_the_Street")). + toBe("

http://en.wikipedia.org/wiki/Homicide:_Life_on_the_Street

"); + }); + + }); + + }); + + describe("emailValid", function() { + + it("allows upper case in first part of emails", function() { + expect(Discourse.Utilities.emailValid('Bob@example.com')).toBe(true); + }); + + it("allows upper case in domain of emails", function() { + expect(Discourse.Utilities.emailValid('bob@EXAMPLE.com')).toBe(true); + }); + + }); + +});