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 */
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");
});
});
});

View file

@ -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();
});
});

View file

@ -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);
});
});

View file

@ -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');
});
});
});
});

View file

@ -2,20 +2,14 @@
describe("sanitize", function(){
it("strips all script tags", function(){
var sanitized = sanitizeHtml("<div><script>alert('hi');</script></div>");
expect(sanitized)
.toBe("<div></div>");
expect(sanitized).toBe("<div></div>");
});
it("strips disallowed attributes", function(){
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,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);
});
});
});

View file

@ -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("<p>hello</p>");
});
it("automatically handles trivial newlines", function() {
return expect(cook("1\n2\n3")).toBe("<p>1 <br>\n2 <br>\n3</p>");
});
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("<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>");
});
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("<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>");
});
describe("Links", function() {
it("allows links to contain query params", function() {
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>');
});
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>');
});
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>');
});
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>');
});
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>');
});
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>');
});
return it("autolinks a URL with parentheses (like Wikipedia)", function() {
return 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() {
it("translates mentions to links", function() {
return expect(cook("Hello @sam", {
mentionLookup: (function() {
return true;
})
})).toBe("<p>Hello <a href='/users/sam' class='mention'>@sam</a></p>");
});
it("adds a mention class", function() {
return expect(cook("Hello @EvilTrout")).toBe("<p>Hello <span class='mention'>@EvilTrout</span></p>");
});
it("won't add mention class to an email address", function() {
return 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() {
return 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>");
});
});
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("<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>");
});
});
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("<p>hello</p>");
});
it("automatically handles trivial newlines", function() {
expect(cook("1\n2\n3")).toBe("<p>1 <br>\n2 <br>\n3</p>");
});
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("<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>");
});
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("<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>");
});
describe("Links", function() {
it("allows links to contain query params", function() {
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>');
});
it("escapes double underscores in URLs", function() {
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() {
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() {
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() {
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() {
expect(cook("here is [an example](http://twitter.com)")).toBe('<p>here is <a href="http://twitter.com">an example</a></p>');
});
it("autolinks a URL with parentheses (like Wikipedia)", function() {
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() {
it("translates mentions to links", function() {
expect(cook("Hello @sam", { mentionLookup: (function() { return true; }) })).toBe("<p>Hello <a href='/users/sam' class='mention'>@sam</a></p>");
});
it("adds a mention class", function() {
expect(cook("Hello @EvilTrout")).toBe("<p>Hello <span class='mention'>@EvilTrout</span></p>");
});
it("won't add mention class to an email address", function() {
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() {
expect(cook("hanzo55@yahoo.com")).toBe("<p>hanzo55@yahoo.com</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>");
});
});
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("<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>");
});
});
});
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);
});
});
});