cleaned up all javascript specs for better readability

This commit is contained in:
Régis Hanol 2013-02-28 03:26:20 +01:00
parent 416f981f92
commit 9a40789f4d
7 changed files with 333 additions and 326 deletions

View File

@ -1,28 +1,24 @@
/*global waitsFor:true expect:true describe:true beforeEach:true it:true */ /*global waitsFor:true expect:true describe:true beforeEach:true it:true */
describe("Discourse.KeyValueStore", function() { describe("Discourse.KeyValueStore", function() {
return describe("Setting values", function() {
var store; describe("Setting values", function() {
store = Discourse.KeyValueStore; var store = Discourse.KeyValueStore;
store.init("test"); store.init("test");
it("able to get the value back from the store", function() {
store.set({ it("is able to get the value back from the store", function() {
key: "bob", store.set({ key: "bob", value: "uncle" });
value: "uncle" expect(store.get("bob")).toBe("uncle");
});
return expect(store.get("bob")).toBe("uncle");
});
return it("able to nuke the store", function() {
store.set({
key: "bob1",
value: "uncle"
}); });
it("is able to nuke the store", function() {
store.set({ key: "bob1", value: "uncle" });
store.abandonLocal(); store.abandonLocal();
localStorage.a = 1; localStorage.a = 1;
expect(store.get("bob1")).toBe(void 0); expect(store.get("bob1")).toBe(void 0);
return expect(localStorage.a).toBe("1"); expect(localStorage.a).toBe("1");
});
});
}); });
});
});

View File

@ -1,11 +1,9 @@
/*global waitsFor:true expect:true describe:true beforeEach:true it:true */ /*global waitsFor:true expect:true describe:true beforeEach:true it:true */
describe("Discourse.MessageBus", function() { describe("Discourse.MessageBus", function() {
return describe("Long polling", function() {
var bus; describe("Long polling", function() {
bus = Discourse.MessageBus; Discourse.MessageBus.start();
return bus.start();
});
}); });
});

View File

@ -1,26 +1,21 @@
/*global waitsFor:true expect:true describe:true beforeEach:true it:true spyOn:true */ /*global waitsFor:true expect:true describe:true beforeEach:true it:true spyOn:true */
describe("Discourse.Onebox", function() {
describe("Discourse.Onebox", function() {
beforeEach(function() { beforeEach(function() {
return spyOn(jQuery, 'ajax').andCallThrough(); spyOn(jQuery, 'ajax').andCallThrough();
}); });
it("Stops rapid calls with cache true", function() { it("Stops rapid calls with cache true", function() {
Discourse.Onebox.lookup('http://bla.com', true, function(c) { Discourse.Onebox.lookup('http://bla.com', true, function(c) { return c; });
return c; Discourse.Onebox.lookup('http://bla.com', true, function(c) { return c; });
}); expect(jQuery.ajax.calls.length).toBe(1);
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);
});
}); });
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);
});
});

View File

@ -1,30 +1,42 @@
/*global waitsFor:true expect:true describe:true beforeEach:true it:true runs:true */ /*global waitsFor:true expect:true describe:true beforeEach:true it:true runs:true */
describe("PreloadStore", function() { describe("PreloadStore", function() {
beforeEach(function() { beforeEach(function() {
return PreloadStore.store('bane', 'evil'); PreloadStore.store('bane', 'evil');
}); });
describe("contains", function() { describe("contains", function() {
it("returns false for a key that doesn't exist", function() { it("returns false for a key that doesn't exist", function() {
return expect(PreloadStore.contains('joker')).toBe(false); expect(PreloadStore.contains('joker')).toBe(false);
}); });
return it("returns true for a stored key", function() {
return expect(PreloadStore.contains('bane')).toBe(true); it("returns true for a stored key", function() {
expect(PreloadStore.contains('bane')).toBe(true);
}); });
}); });
describe('getStatic', function() { describe('getStatic', function() {
it("returns undefined if the key doesn't exist", function() { it("returns undefined if the key doesn't exist", function() {
return expect(PreloadStore.getStatic('joker')).toBe(void 0); expect(PreloadStore.getStatic('joker')).toBe(void 0);
}); });
it("returns the the key if it exists", function() { it("returns the the key if it exists", function() {
return expect(PreloadStore.getStatic('bane')).toBe('evil'); expect(PreloadStore.getStatic('bane')).toBe('evil');
}); });
return it("removes the key after being called", function() {
it("removes the key after being called", function() {
PreloadStore.getStatic('bane'); PreloadStore.getStatic('bane');
return expect(PreloadStore.getStatic('bane')).toBe(void 0); expect(PreloadStore.getStatic('bane')).toBe(void 0);
}); });
}); });
return describe('get', function() {
describe('get', function() {
it("returns a promise that resolves to undefined", function() { it("returns a promise that resolves to undefined", function() {
var done, storeResult; var done, storeResult;
done = storeResult = null; done = storeResult = null;
@ -32,36 +44,31 @@
done = true; done = true;
storeResult = result; storeResult = result;
}); });
waitsFor((function() { waitsFor((function() { return done; }), "Promise never resolved", 1000);
return done; runs(function() {
}), "Promise never resolved", 1000); expect(storeResult).toBe(void 0);
return runs(function() {
return expect(storeResult).toBe(void 0);
}); });
}); });
it("returns a promise that resolves to the result of the finder", function() { it("returns a promise that resolves to the result of the finder", function() {
var done, finder, storeResult; var done, finder, storeResult;
done = storeResult = null; done = storeResult = null;
finder = function() { finder = function() { return 'evil'; };
return 'evil';
};
PreloadStore.get('joker', finder).then(function(result) { PreloadStore.get('joker', finder).then(function(result) {
done = true; done = true;
storeResult = result; storeResult = result;
}); });
waitsFor((function() { waitsFor((function() { return done; }), "Promise never resolved", 1000);
return done; runs(function() {
}), "Promise never resolved", 1000); expect(storeResult).toBe('evil');
return runs(function() {
return expect(storeResult).toBe('evil');
}); });
}); });
it("returns a promise that resolves to the result of the finder's promise", function() { it("returns a promise that resolves to the result of the finder's promise", function() {
var done, finder, storeResult; var done, finder, storeResult;
done = storeResult = null; done = storeResult = null;
finder = function() { finder = function() {
var promise; var promise = new RSVP.Promise();
promise = new RSVP.Promise();
promise.resolve('evil'); promise.resolve('evil');
return promise; return promise;
}; };
@ -69,19 +76,17 @@
done = true; done = true;
storeResult = result; storeResult = result;
}); });
waitsFor((function() { waitsFor((function() { return done; }), "Promise never resolved", 1000);
return done; runs(function() {
}), "Promise never resolved", 1000); expect(storeResult).toBe('evil');
return runs(function() {
return expect(storeResult).toBe('evil');
}); });
}); });
it("returns a promise that resolves to the result of the finder's rejected promise", function() { it("returns a promise that resolves to the result of the finder's rejected promise", function() {
var done, finder, storeResult; var done, finder, storeResult;
done = storeResult = null; done = storeResult = null;
finder = function() { finder = function() {
var promise; var promise = new RSVP.Promise();
promise = new RSVP.Promise();
promise.reject('evil'); promise.reject('evil');
return promise; return promise;
}; };
@ -89,28 +94,25 @@
done = true; done = true;
storeResult = rejectedResult; storeResult = rejectedResult;
}); });
waitsFor((function() { waitsFor((function() { return done; }), "Promise never rejected", 1000);
return done; runs(function() {
}), "Promise never rejected", 1000); expect(storeResult).toBe('evil');
return runs(function() {
return expect(storeResult).toBe('evil');
}); });
}); });
return it("returns a promise that resolves to 'evil'", function() {
it("returns a promise that resolves to 'evil'", function() {
var done, storeResult; var done, storeResult;
done = storeResult = null; done = storeResult = null;
PreloadStore.get('bane').then(function(result) { PreloadStore.get('bane').then(function(result) {
done = true; done = true;
storeResult = result; storeResult = result;
}); });
waitsFor((function() { waitsFor((function() { return done; }), "Promise never resolved", 1000);
return done; runs(function() {
}), "Promise never resolved", 1000); expect(storeResult).toBe('evil');
return runs(function() {
return expect(storeResult).toBe('evil');
});
});
}); });
}); });
});
});

View File

@ -2,20 +2,14 @@
describe("sanitize", function(){ describe("sanitize", function(){
it("strips all script tags", function(){ it("strips all script tags", function(){
var sanitized = sanitizeHtml("<div><script>alert('hi');</script></div>"); var sanitized = sanitizeHtml("<div><script>alert('hi');</script></div>");
expect(sanitized).toBe("<div></div>");
expect(sanitized)
.toBe("<div></div>");
}); });
it("strips disallowed attributes", function(){ it("strips disallowed attributes", function(){
var sanitized = sanitizeHtml("<div><p class=\"funky\" wrong='1'>hello</p></div>"); var sanitized = sanitizeHtml("<div><p class=\"funky\" wrong='1'>hello</p></div>");
expect(sanitized).toBe("<div><p class=\"funky\">hello</p></div>");
expect(sanitized)
.toBe("<div><p class=\"funky\">hello</p></div>");
}); });
}); });

View File

@ -1,9 +1,11 @@
/*global waitsFor:true expect:true describe:true beforeEach:true it:true */ /*global waitsFor:true expect:true describe:true beforeEach:true it:true */
describe("Discourse.UserAction", function() {
return describe("collapseStream", function() { describe("Discourse.UserAction", function() {
return it("collapses all likes", function() {
var actions; describe("collapseStream", function() {
actions = [
it("collapses all likes", function() {
var actions = [
Discourse.UserAction.create({ Discourse.UserAction.create({
action_type: Discourse.UserAction.LIKE, action_type: Discourse.UserAction.LIKE,
topic_id: 1, topic_id: 1,
@ -21,12 +23,14 @@
post_number: 1 post_number: 1
}) })
]; ];
actions = Discourse.UserAction.collapseStream(actions); actions = Discourse.UserAction.collapseStream(actions);
expect(actions.length).toBe(2); expect(actions.length).toBe(2);
expect(actions[0].get("children").length).toBe(1); expect(actions[0].get("children").length).toBe(1);
return expect(actions[0].get("children")[0].items.length).toBe(2); expect(actions[0].get("children")[0].items.length).toBe(2);
});
});
}); });
});
});

View File

@ -1,134 +1,152 @@
/*global waitsFor:true expect:true describe:true beforeEach:true it:true */ /*global waitsFor:true expect:true describe:true beforeEach:true it:true */
describe("Discourse.Utilities", function() { describe("Discourse.Utilities", function() {
describe("categoryUrlId", function() { describe("categoryUrlId", function() {
it("returns the slug when it exists", function() { it("returns the slug when it exists", function() {
return expect(Discourse.Utilities.categoryUrlId({ expect(Discourse.Utilities.categoryUrlId({ slug: 'hello' })).toBe("hello");
slug: 'hello'
})).toBe("hello");
}); });
it("returns id-category when slug is an empty string", function() { it("returns id-category when slug is an empty string", function() {
return expect(Discourse.Utilities.categoryUrlId({ expect(Discourse.Utilities.categoryUrlId({ id: 123, slug: '' })).toBe("123-category");
id: 123,
slug: ''
})).toBe("123-category");
}); });
return it("returns id-category without a slug", function() {
return expect(Discourse.Utilities.categoryUrlId({ it("returns id-category without a slug", function() {
id: 456 expect(Discourse.Utilities.categoryUrlId({ id: 456 })).toBe("456-category");
})).toBe("456-category");
}); });
}); });
describe("Cooking", function() { describe("Cooking", function() {
var cook;
cook = function(contents, opts) { var cook = function(contents, opts) {
opts = opts || {}; opts = opts || {};
opts.mentionLookup = opts.mentionLookup || (function() { opts.mentionLookup = opts.mentionLookup || false;
return false;
});
return Discourse.Utilities.cook(contents, opts); return Discourse.Utilities.cook(contents, opts);
}; };
it("surrounds text with paragraphs", function() { it("surrounds text with paragraphs", function() {
return expect(cook("hello")).toBe("<p>hello</p>"); expect(cook("hello")).toBe("<p>hello</p>");
}); });
it("automatically handles trivial newlines", function() { it("automatically handles trivial newlines", function() {
return expect(cook("1\n2\n3")).toBe("<p>1 <br>\n2 <br>\n3</p>"); expect(cook("1\n2\n3")).toBe("<p>1 <br>\n2 <br>\n3</p>");
}); });
it("handles quotes properly", function() { it("handles quotes properly", function() {
var cooked; var cooked = cook("1[quote=\"bob, post:1\"]my quote[/quote]2", {
cooked = cook("1[quote=\"bob, post:1\"]my quote[/quote]2", {
topicId: 2, topicId: 2,
lookupAvatar: function(name) { lookupAvatar: function(name) { return "" + name; }
return "" + name;
}
}); });
return expect(cooked).toBe("<p>1</p><aside class='quote' data-post=\"1\" >\n <div class='title'>\n <div class='quote-controls'></div>\n" + expect(cooked).toBe("<p>1</p><aside class='quote' data-post=\"1\" >\n <div class='title'>\n <div class='quote-controls'></div>\n" +
" bob\n bob\n said:\n </div>\n <blockquote>my quote</blockquote>\n</aside>\n<p>2</p>"); " bob\n bob\n said:\n </div>\n <blockquote>my quote</blockquote>\n</aside>\n<p>2</p>");
}); });
it("includes no avatar if none is found", function() { it("includes no avatar if none is found", function() {
var cooked; var cooked = cook("1[quote=\"bob, post:1\"]my quote[/quote]2", {
cooked = cook("1[quote=\"bob, post:1\"]my quote[/quote]2", {
topicId: 2, topicId: 2,
lookupAvatar: function(name) { lookupAvatar: function(name) { return null; }
return null;
}
}); });
return expect(cooked).toBe("<p>1</p><aside class='quote' data-post=\"1\" >\n <div class='title'>\n <div class='quote-controls'></div>\n" + expect(cooked).toBe("<p>1</p><aside class='quote' data-post=\"1\" >\n <div class='title'>\n <div class='quote-controls'></div>\n" +
" \n bob\n said:\n </div>\n <blockquote>my quote</blockquote>\n</aside>\n<p>2</p>"); " \n bob\n said:\n </div>\n <blockquote>my quote</blockquote>\n</aside>\n<p>2</p>");
}); });
describe("Links", function() { describe("Links", function() {
it("allows links to contain query params", function() { it("allows links to contain query params", function() {
expect(cook("Youtube: http://www.youtube.com/watch?v=1MrpeBRkM5A")). expect(cook("Youtube: http://www.youtube.com/watch?v=1MrpeBRkM5A")).
toBe('<p>Youtube: <a href="http://www.youtube.com/watch?v=1MrpeBRkM5A">http://www.youtube.com/watch?v=1MrpeBRkM5A</a></p>'); toBe('<p>Youtube: <a href="http://www.youtube.com/watch?v=1MrpeBRkM5A">http://www.youtube.com/watch?v=1MrpeBRkM5A</a></p>');
}); });
it("escapes double underscores in URLs", function() { it("escapes double underscores in URLs", function() {
return expect(cook("Derpy: http://derp.com?__test=1")).toBe('<p>Derpy: <a href="http://derp.com?%5F%5Ftest=1">http://derp.com?__test=1</a></p>'); expect(cook("Derpy: http://derp.com?__test=1")).toBe('<p>Derpy: <a href="http://derp.com?%5F%5Ftest=1">http://derp.com?__test=1</a></p>');
}); });
it("autolinks something that begins with www", function() { it("autolinks something that begins with www", function() {
return expect(cook("Atwood: www.codinghorror.com")).toBe('<p>Atwood: <a href="http://www.codinghorror.com">www.codinghorror.com</a></p>'); expect(cook("Atwood: www.codinghorror.com")).toBe('<p>Atwood: <a href="http://www.codinghorror.com">www.codinghorror.com</a></p>');
}); });
it("autolinks a URL with http://www", function() { it("autolinks a URL with http://www", function() {
return expect(cook("Atwood: http://www.codinghorror.com")).toBe('<p>Atwood: <a href="http://www.codinghorror.com">http://www.codinghorror.com</a></p>'); expect(cook("Atwood: http://www.codinghorror.com")).toBe('<p>Atwood: <a href="http://www.codinghorror.com">http://www.codinghorror.com</a></p>');
}); });
it("autolinks a URL", function() { it("autolinks a URL", function() {
return expect(cook("EvilTrout: http://eviltrout.com")).toBe('<p>EvilTrout: <a href="http://eviltrout.com">http://eviltrout.com</a></p>'); expect(cook("EvilTrout: http://eviltrout.com")).toBe('<p>EvilTrout: <a href="http://eviltrout.com">http://eviltrout.com</a></p>');
}); });
it("supports markdown style links", function() { it("supports markdown style links", function() {
return expect(cook("here is [an example](http://twitter.com)")).toBe('<p>here is <a href="http://twitter.com">an example</a></p>'); expect(cook("here is [an example](http://twitter.com)")).toBe('<p>here is <a href="http://twitter.com">an example</a></p>');
}); });
return it("autolinks a URL with parentheses (like Wikipedia)", function() {
return expect(cook("Batman: http://en.wikipedia.org/wiki/The_Dark_Knight_(film)")) it("autolinks a URL with parentheses (like Wikipedia)", function() {
.toBe('<p>Batman: <a href="http://en.wikipedia.org/wiki/The_Dark_Knight_(film)">http://en.wikipedia.org/wiki/The_Dark_Knight_(film)</a></p>'); expect(cook("Batman: http://en.wikipedia.org/wiki/The_Dark_Knight_(film)")).
toBe('<p>Batman: <a href="http://en.wikipedia.org/wiki/The_Dark_Knight_(film)">http://en.wikipedia.org/wiki/The_Dark_Knight_(film)</a></p>');
}); });
}); });
describe("Mentioning", function() { describe("Mentioning", function() {
it("translates mentions to links", function() { it("translates mentions to links", function() {
return expect(cook("Hello @sam", { expect(cook("Hello @sam", { mentionLookup: (function() { return true; }) })).toBe("<p>Hello <a href='/users/sam' class='mention'>@sam</a></p>");
mentionLookup: (function() {
return true;
})
})).toBe("<p>Hello <a href='/users/sam' class='mention'>@sam</a></p>");
}); });
it("adds a mention class", function() { it("adds a mention class", function() {
return expect(cook("Hello @EvilTrout")).toBe("<p>Hello <span class='mention'>@EvilTrout</span></p>"); expect(cook("Hello @EvilTrout")).toBe("<p>Hello <span class='mention'>@EvilTrout</span></p>");
}); });
it("won't add mention class to an email address", function() { it("won't add mention class to an email address", function() {
return expect(cook("robin@email.host")).toBe("<p>robin@email.host</p>"); expect(cook("robin@email.host")).toBe("<p>robin@email.host</p>");
}); });
it("won't be affected by email addresses that have a number before the @ symbol", function() { it("won't be affected by email addresses that have a number before the @ symbol", function() {
return expect(cook("hanzo55@yahoo.com")).toBe("<p>hanzo55@yahoo.com</p>"); expect(cook("hanzo55@yahoo.com")).toBe("<p>hanzo55@yahoo.com</p>");
}); });
return it("supports a @mention at the beginning of a post", function() {
return expect(cook("@EvilTrout yo")).toBe("<p><span class='mention'>@EvilTrout</span> yo</p>"); it("supports a @mention at the beginning of a post", function() {
expect(cook("@EvilTrout yo")).toBe("<p><span class='mention'>@EvilTrout</span> yo</p>");
}); });
}); });
return describe("Oneboxing", function() {
describe("Oneboxing", function() {
it("doesn't onebox a link within a list", 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/); 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() { it("adds a onebox class to a link on its own line", function() {
return expect(cook("http://test.com")).toMatch(/onebox/); expect(cook("http://test.com")).toMatch(/onebox/);
}); });
it("supports multiple links", function() { it("supports multiple links", function() {
return expect(cook("http://test.com\nhttp://test2.com")).toMatch(/onebox[\s\S]+onebox/m); expect(cook("http://test.com\nhttp://test2.com")).toMatch(/onebox[\s\S]+onebox/m);
}); });
it("doesn't onebox links that have trailing text", function() { it("doesn't onebox links that have trailing text", function() {
return expect(cook("http://test.com bob")).not.toMatch(/onebox/); 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")). it("works with links that have underscores in them", function() {
expect(cook("http://en.wikipedia.org/wiki/Homicide:_Life_on_the_Street")).
toBe("<p><a href=\"http://en.wikipedia.org/wiki/Homicide:_Life_on_the_Street\" class=\"onebox\" target=\"_blank\">http://en.wikipedia.org/wiki/Homicide:_Life_on_the_Street</a></p>"); toBe("<p><a href=\"http://en.wikipedia.org/wiki/Homicide:_Life_on_the_Street\" class=\"onebox\" target=\"_blank\">http://en.wikipedia.org/wiki/Homicide:_Life_on_the_Street</a></p>");
}); });
}); });
}); });
return describe("emailValid", function() {
describe("emailValid", function() {
it("allows upper case in first part of emails", function() { it("allows upper case in first part of emails", function() {
return expect(Discourse.Utilities.emailValid('Bob@example.com')).toBe(true); 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("allows upper case in domain of emails", function() {
expect(Discourse.Utilities.emailValid('bob@EXAMPLE.com')).toBe(true);
});
});
});