REFACTOR: Remove Discourse.Ajax

This commit is contained in:
Robin Ward
2016-06-30 13:55:44 -04:00
parent 56f07529bb
commit b8125b3512
111 changed files with 567 additions and 549 deletions
@@ -1,13 +0,0 @@
module("adapter:topic-list");
import { finderFor } from 'discourse/adapters/topic-list';
test("finderFor", function() {
// Mocking instead of using a pretender which decodes the path and thus does
// not reflect the behavior of an actual web server.
var mock = sandbox.mock(Discourse);
mock.expects("ajax").withArgs("/search.json?q=test%25%25");
var finderForFunction = finderFor('search', { q: "test%%" });
finderForFunction();
mock.verify();
});
@@ -2,32 +2,23 @@ import { blank, present } from 'helpers/qunit-helpers';
import AdminUser from 'admin/models/admin-user';
import ApiKey from 'admin/models/api-key';
module("Discourse.AdminUser");
asyncTestDiscourse('generate key', function() {
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve({api_key: {id: 1234, key: 'asdfasdf'}}));
module("model:admin-user");
test('generate key', function() {
var adminUser = AdminUser.create({id: 333});
blank(adminUser.get('api_key'), 'it has no api key by default');
adminUser.generateApiKey().then(function() {
start();
ok(Discourse.ajax.calledWith("/admin/users/333/generate_api_key", { type: 'POST' }), "it POSTed to the url");
present(adminUser.get('api_key'), 'it has an api_key now');
});
});
asyncTestDiscourse('revoke key', function() {
test('revoke key', function() {
var apiKey = ApiKey.create({id: 1234, key: 'asdfasdf'}),
adminUser = AdminUser.create({id: 333, api_key: apiKey});
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve());
equal(adminUser.get('api_key'), apiKey, 'it has the api key in the beginning');
adminUser.revokeApiKey().then(function() {
start();
ok(Discourse.ajax.calledWith("/admin/users/333/revoke_api_key", { type: 'DELETE' }), "it DELETEd to the url");
blank(adminUser.get('api_key'), 'it cleared the api_key');
});
});
@@ -1,48 +0,0 @@
import { present } from 'helpers/qunit-helpers';
import ApiKey from 'admin/models/api-key';
module("Discourse.ApiKey");
test('create', function() {
var apiKey = ApiKey.create({id: 123, user: {id: 345}});
present(apiKey, 'it creates the api key');
present(apiKey.get('user'), 'it creates the user inside');
});
asyncTestDiscourse('find', function() {
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve([]));
ApiKey.find().then(function() {
start();
ok(Discourse.ajax.calledWith("/admin/api"), "it GETs the keys");
});
});
asyncTestDiscourse('generateMasterKey', function() {
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve({api_key: {}}));
ApiKey.generateMasterKey().then(function() {
start();
ok(Discourse.ajax.calledWith("/admin/api/key", {type: 'POST'}), "it POSTs to create a master key");
});
});
asyncTestDiscourse('regenerate', function() {
var apiKey = ApiKey.create({id: 3456});
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve({api_key: {id: 3456}}));
apiKey.regenerate().then(function() {
start();
ok(Discourse.ajax.calledWith("/admin/api/key", {type: 'PUT', data: {id: 3456}}), "it PUTs the key");
});
});
asyncTestDiscourse('revoke', function() {
var apiKey = ApiKey.create({id: 3456});
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve([]));
apiKey.revoke().then(function() {
start();
ok(Discourse.ajax.calledWith("/admin/api/key", {type: 'DELETE', data: {id: 3456}}), "it DELETES the key");
});
});
@@ -1,21 +0,0 @@
import FlaggedPost from 'admin/models/flagged-post';
module("Discourse.FlaggedPost");
test('delete first post', function() {
sandbox.stub(Discourse, 'ajax');
FlaggedPost.create({ id: 1, topic_id: 2, post_number: 1 })
.deletePost();
ok(Discourse.ajax.calledWith("/t/2", { type: 'DELETE', cache: false }), "it deleted the topic");
});
test('delete second post', function() {
sandbox.stub(Discourse, 'ajax');
FlaggedPost.create({ id: 1, topic_id: 2, post_number: 2 })
.deletePost();
ok(Discourse.ajax.calledWith("/posts/1", { type: 'DELETE', cache: false }), "it deleted the post");
});
@@ -86,7 +86,7 @@ export default {
"/groups/discourse/posts.json":[
{
"id":94607,
"cooked":"<p>Right now we have two entirely different styles for new topics and new posts within a topic... we can probably fix that pretty easily. </p>\n\n<p><img src=\"//meta-discourse.global.ssl.fastly.net/uploads/default/38088/55cb4f3595adf4d8.png\" width=\"460\" height=\"147\"> </p>\n\n<p>So the simple change would be:</p>\n\n<p><img src=\"//meta-discourse.global.ssl.fastly.net/uploads/default/38090/a6c1bc4d470c8b9b.png\" width=\"454\" height=\"144\"> </p>\n\n<p>but... while the dot makes the \"• new\" stand out more... it doesn't communicate any information other than \"look at me\" — can we add more context without adding more noise? </p>\n\n<p><img src=\"//meta-discourse.global.ssl.fastly.net/uploads/default/38089/4e910bbbc03c77d5.png\" width=\"458\" height=\"144\"></p>",
"cooked":"<p>I don't know how to pronounce that in English, but this makes me think of the French word \"disquette\" (floppy disk)</p>",
"created_at":"2015-01-23T15:13:01.935Z",
"title":"Consistent new indicator",
"url":"/t/consistent-new-indicator/24355/1",
@@ -186,7 +186,7 @@ export default {
},
{
"id":94601,
"cooked":"<p>Yeah I think this category arrangement is the way to go at the very least - much easier to scan two columns...</p>\n\n<p>Also, maybe square off the bars? </p>\n\n<p><img src=\"//meta-discourse.global.ssl.fastly.net/uploads/default/38086/0931384a1feb4467.png\" width=\"382\" height=\"489\"> </p>\n\n<p><div class=\"lightbox-wrapper\"><a data-download-href=\"//meta-discourse.global.ssl.fastly.net/uploads/default/0eb77e51ba5428f28c6b4899da1b4e7483607f3e\" href=\"//meta-discourse.global.ssl.fastly.net/uploads/default/38085/4bf843ad43673f3e.png\" class=\"lightbox\" title=\"Screenshot 2015-01-23 09.44.48.png\"><img src=\"//meta-discourse.global.ssl.fastly.net/uploads/default/_optimized/1c1/959/751ac06f4d_690x376.png\" width=\"690\" height=\"376\"><div class=\"meta\">\n<span class=\"filename\">Screenshot 2015-01-23 09.44.48.png</span><span class=\"informations\">1211x661 180 KB</span><span class=\"expand\"></span>\n</div></a></div></p>",
"cooked":"<p>Agree that the markup isn't ideal - it's kind of hacked together at the moment; especially because we have two different styles. I think once we settle on the specifics it can be re-written entirely.</p>",
"created_at":"2015-01-23T14:51:55.497Z",
"title":"The end of Clown Vomit, or, simplified category styles",
"url":"/t/the-end-of-clown-vomit-or-simplified-category-styles/24249/62",
@@ -236,7 +236,7 @@ export default {
},
{
"id":94577,
"cooked":"<p>Yup, that's the latest version <img src=\"//meta-discourse.global.ssl.fastly.net/images/emoji/twitter/wink.png?v=1\" title=\":wink:\" class=\"emoji\" alt=\"wink\" width=\"20\" height=\"20\"></p>\n\n<p><div class=\"lightbox-wrapper\"><a data-download-href=\"//meta-discourse.global.ssl.fastly.net/uploads/default/aa5ecd62e1730fccf29aac1fd082e3a4ca07cae4\" href=\"//meta-discourse.global.ssl.fastly.net/uploads/default/38076/ad330f8cff1b7982.gif\" class=\"lightbox\" title=\"quote reply.gif\"><img src=\"//meta-discourse.global.ssl.fastly.net/uploads/default/_optimized/df1/df0/99b737a0e4_690x385.gif\" width=\"690\" height=\"385\"><div class=\"meta\">\n<span class=\"filename\">quote reply.gif</span><span class=\"informations\">1307x731 426 KB</span><span class=\"expand\"></span>\n</div></a></div></p>\n\n<p>(<em>click to view animated version</em>)</p>",
"cooked":"<p>Agree that the markup isn't ideal - it's kind of hacked together at the moment; especially because we have two different styles. I think once we settle on the specifics it can be re-written entirely.</p>",
"created_at":"2015-01-23T10:50:55.846Z",
"title":"Quote reply insertion at cursor position",
"url":"/t/quote-reply-insertion-at-cursor-position/24344/4",
@@ -286,7 +286,7 @@ export default {
},
{
"id":94574,
"cooked":"<aside class=\"quote\" data-post=\"1\" data-topic=\"24344\"><div class=\"title\">\n<div class=\"quote-controls\"></div>\n<img width=\"20\" height=\"20\" src=\"/user_avatar/meta.discourse.org/tonninseteli/40/37409.png\" class=\"avatar\">tonninseteli:</div>\n<blockquote><p>I've noticed that quoted text is always inserted at the end of the message</p></blockquote></aside>\n\n<p>It used to be that but that was fixed a while ago. Are you running a recent version?</p>",
"cooked":"<p>Agree that the markup isn't ideal - it's kind of hacked together at the moment; especially because we have two different styles. I think once we settle on the specifics it can be re-written entirely.</p>",
"created_at":"2015-01-23T10:31:29.222Z",
"title":"Quote reply insertion at cursor position",
"url":"/t/quote-reply-insertion-at-cursor-position/24344/2",
@@ -336,7 +336,7 @@ export default {
},
{
"id":94572,
"cooked":"<aside class=\"quote\" data-post=\"26\" data-topic=\"22546\"><div class=\"title\">\n<div class=\"quote-controls\"></div>\n<img width=\"20\" height=\"20\" src=\"/user_avatar/meta.discourse.org/etewiah/40/2527.png\" class=\"avatar\">etewiah:</div>\n<blockquote><p>I'm not sure exactly which update introduced that change.</p></blockquote></aside>\n\n<p>That's an Ember update that introduced this change.</p>",
"cooked":"<p>Agree that the markup isn't ideal - it's kind of hacked together at the moment; especially because we have two different styles. I think once we settle on the specifics it can be re-written entirely.</p>",
"created_at":"2015-01-23T09:46:00.901Z",
"title":"Translations frequently broken",
"url":"/t/translations-frequently-broken/22546/27",
@@ -386,7 +386,7 @@ export default {
},
{
"id":94555,
"cooked":"<p>I don't know how to pronounce that in English, but this makes me think of the French word \"disquette\" (floppy disk) <img src=\"//meta-discourse.global.ssl.fastly.net/images/emoji/twitter/smile.png?v=1\" title=\":smile:\" class=\"emoji\" alt=\"smile\" width=\"72\" height=\"72\"></p>",
"cooked":"<p>I don't know how to pronounce that in English, but this makes me think of the French word \"disquette\" (floppy disk)</p>",
"created_at":"2015-01-23T08:17:31.700Z",
"title":"Introducing Discette - a minimal ember-cli front end to Discourse",
"url":"/t/introducing-discette-a-minimal-ember-cli-front-end-to-discourse/24321/3",
@@ -636,7 +636,7 @@ export default {
},
{
"id":94521,
"cooked":"<aside class=\"quote\" data-post=\"46\" data-topic=\"24249\"><div class=\"title\">\n<div class=\"quote-controls\"></div>\n<img width=\"20\" height=\"20\" src=\"/user_avatar/meta.discourse.org/clay/40/5265.png\" class=\"avatar\">clay:</div>\n<blockquote><p>the only thing that really stands out to me is the green used for the post counter widget navigator thing:</p></blockquote></aside>\n\n<p>Yeah probably.</p>\n\n<aside class=\"quote\" data-post=\"55\" data-topic=\"24249\"><div class=\"title\">\n<div class=\"quote-controls\"></div>\n<img width=\"20\" height=\"20\" src=\"/user_avatar/meta.discourse.org/geek/40/5255.png\" class=\"avatar\">geek:</div>\n<blockquote><p>So why not just make all the subcategory colors a lighter version of the parent category?</p></blockquote></aside>\n\n<p>Definitely a good idea. We have seen some eye melting color schemes people have picked for categories.. Much less subcategories.</p>\n\n<aside class=\"quote\" data-post=\"47\" data-topic=\"24249\"><div class=\"title\">\n<div class=\"quote-controls\"></div>\n<img width=\"20\" height=\"20\" src=\"/user_avatar/meta.discourse.org/abarker/40/33014.png\" class=\"avatar\">abarker:</div>\n<blockquote><p>Black is still a strong color. Have you tried with more muted colors? (Think along the lines of pastels, for example.)</p></blockquote></aside>\n\n<p>Sure try <a href=\"http://talk.folksy.com\">http://talk.folksy.com</a> -- it's still too much color in boxes. Particularly anywhere a bunch of categories are displayed together, which is a lot of places considering the topic list is the main form of nav, both on the homepage default of latest and in suggested topics at the bottom of every topic...</p>\n\n<p><div class=\"lightbox-wrapper\"><a data-download-href=\"//meta-discourse.global.ssl.fastly.net/uploads/default/a6482c9da01bdc4d88e989f1d26a8c197b862d7f\" href=\"//meta-discourse.global.ssl.fastly.net/uploads/default/38068/fb86421cedd9ff0c.jpg\" class=\"lightbox\" title=\"image.jpg\"><img src=\"//meta-discourse.global.ssl.fastly.net/uploads/default/_optimized/282/0db/b355bd85ba_666x500.jpg\" width=\"666\" height=\"500\"><div class=\"meta\">\n<span class=\"filename\">image.jpg</span><span class=\"informations\">2048x1536 377 KB</span><span class=\"expand\"></span>\n</div></a></div></p>",
"cooked":"<p><a class=\"mention\" href=\"/users/techapj\">@techapj</a> fixed this for 1.2.</p>",
"created_at":"2015-01-23T02:58:27.451Z",
"title":"The end of Clown Vomit, or, simplified category styles",
"url":"/t/the-end-of-clown-vomit-or-simplified-category-styles/24249/57",
@@ -886,7 +886,7 @@ export default {
},
{
"id":94515,
"cooked":"<p>Liked just for the word \"Discettes\" which is adorable <img src=\"//meta-discourse.global.ssl.fastly.net/images/emoji/twitter/heart_eyes.png?v=1\" title=\":heart_eyes:\" class=\"emoji\" alt=\"heart_eyes\" width=\"72\" height=\"72\"></p>",
"cooked":"<p>I would worry about getting your expenses down to $5 per month, that seems more likely over time as hosting for Docker compliant sites gets cheaper.</p>",
"created_at":"2015-01-23T02:38:29.185Z",
"title":"Introducing Discette - a minimal ember-cli front end to Discourse",
"url":"/t/introducing-discette-a-minimal-ember-cli-front-end-to-discourse/24321/2",
@@ -936,7 +936,7 @@ export default {
},
{
"id":94514,
"cooked":"<aside class=\"quote\" data-post=\"1\" data-topic=\"24328\"><div class=\"title\">\n<div class=\"quote-controls\"></div>\n<img width=\"20\" height=\"20\" src=\"/user_avatar/meta.discourse.org/riordan/40/12270.png\" class=\"avatar\">riordan:</div>\n<blockquote><p>building a custom Onebox for it seems like a great way to bring the content into the discussion.</p></blockquote></aside>\n\n<p>This is a good idea, are the documents public web URLs? Perhaps we could help build this onebox if so.</p>\n\n<aside class=\"quote\" data-post=\"1\" data-topic=\"24328\"><div class=\"title\">\n<div class=\"quote-controls\"></div>\n<img width=\"20\" height=\"20\" src=\"/user_avatar/meta.discourse.org/riordan/40/12270.png\" class=\"avatar\">riordan:</div>\n<blockquote><p>Would there be a relatively simple way to have a \"reverse onebox\", so that if a link to one of those whitelisted assets is found in a post, the other server can be notified of what threads its in?</p></blockquote></aside>\n\n<p>Hmm. I suspect this could be done via the API. Query all new topics (assuming older topics are already synced), and for those with a certain URL within the topic (first post only? All posts?) ping those URLs.</p>\n\n<p>This could potentially be done with a webhook on save on the Discourse side.</p>\n\n<p>Let us know how we can help, very interested in public projects like this.</p>",
"cooked":"<p>I would worry about getting your expenses down to $5 per month, that seems more likely over time as hosting for Docker compliant sites gets cheaper.</p>",
"created_at":"2015-01-23T02:37:39.518Z",
"title":"How to do \"Object Oriented Discussion\" through Oneboxes?",
"url":"/t/how-to-do-object-oriented-discussion-through-oneboxes/24328/2",
File diff suppressed because one or more lines are too long
@@ -0,0 +1,57 @@
export default {
'/user_badges':{
"badges":[
{
"id":874,
"name":"Badge 2",
"description":null,
"badge_type_id":7
}
],
"badge_types":[
{
"id":7,
"name":"Silver 2"
}
],
"users":[
{
"id":13470,
"username":"anne3",
"avatar_template":"//www.gravatar.com/avatar/a4151b1fd72089c54e2374565a87da7f.png?s={size}\u0026r=pg\u0026d=identicon"
}
],
"user_badge":{
"id":665,
"granted_at":"2014-03-09T20:30:01.190-04:00",
"badge_id":874,
"granted_by_id":13470
}
},
'/user-badges/:username':{
"badges":[
{
"id":880,
"name":"Badge 8",
"description":null,
"badge_type_id":13
}
],
"badge_types":[
{
"id":13,
"name":"Silver 8"
}
],
"users":[
],
"user_badges":[
{
"id":668,
"granted_at":"2014-03-09T20:30:01.420-04:00",
"badge_id":880,
"granted_by_id":null
}
]
}
};
File diff suppressed because one or more lines are too long
@@ -64,18 +64,21 @@ export default function() {
return response(json);
});
this.get('/clicks/track', success);
this.put('/users/eviltrout', () => response({ user: {} }));
this.get("/t/280.json", () => response(fixturesByUrl['/t/280/1.json']));
this.get("/t/28830.json", () => response(fixturesByUrl['/t/28830/1.json']));
this.get("/t/9.json", () => response(fixturesByUrl['/t/9/1.json']));
this.get("/t/id_for/:slug", () => {
return response({id: 280, slug: "internationalization-localization", url: "/t/internationalization-localization/280"});
});
this.delete('/t/:id', success);
this.put('/t/:id/recover', success);
this.get("/404-body", () => {
return [200, {"Content-Type": "text/html"}, "<div class='page-not-found'>not found</div>"];
});
@@ -196,6 +199,9 @@ export default function() {
return response(200, [ { id: 2222, post_number: 2222 } ]);
});
this.post('/user_badges', () => response(200, fixturesByUrl['/user_badges']));
this.delete('/user_badges/:badge_id', success);
this.post('/posts', function(request) {
const data = parsePostData(request.requestBody);
@@ -238,6 +244,10 @@ export default function() {
});
this.get('/tag_groups', () => response(200, {tag_groups: []}));
this.post('/admin/users/:user_id/generate_api_key', success);
this.delete('/admin/users/:user_id/revoke_api_key', success);
this.post('/admin/badges', success);
this.delete('/admin/badges/:id', success);
});
server.prepareBody = function(body){
@@ -12,7 +12,6 @@ module("lib:click-track-edit-history", {
// Prevent any of these tests from navigating away
win = {focus: function() { } };
redirectTo = sandbox.stub(DiscourseURL, "redirectTo");
sandbox.stub(Discourse, "ajax");
windowOpen = sandbox.stub(window, "open").returns(win);
sandbox.stub(win, "focus");
@@ -141,7 +140,6 @@ var testOpenInANewTab = function(description, clickEventModifier) {
clickEventModifier(clickEvent);
sandbox.stub(clickEvent, "preventDefault");
ok(track(clickEvent));
ok(Discourse.ajax.calledOnce);
ok(!clickEvent.preventDefault.calledOnce);
});
};
@@ -167,7 +165,6 @@ test("tracks via AJAX if we're on the same site", function() {
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
ok(!track(generateClickEventOn('#same-site')));
ok(Discourse.ajax.calledOnce);
ok(DiscourseURL.routeTo.calledOnce);
});
@@ -12,7 +12,6 @@ module("lib:click-track-profile-page", {
// Prevent any of these tests from navigating away
win = {focus: function() { } };
redirectTo = sandbox.stub(DiscourseURL, "redirectTo");
sandbox.stub(Discourse, "ajax");
windowOpen = sandbox.stub(window, "open").returns(win);
sandbox.stub(win, "focus");
@@ -141,7 +140,6 @@ var testOpenInANewTab = function(description, clickEventModifier) {
clickEventModifier(clickEvent);
sandbox.stub(clickEvent, "preventDefault");
ok(track(clickEvent));
ok(Discourse.ajax.calledOnce);
ok(!clickEvent.preventDefault.calledOnce);
});
};
@@ -167,7 +165,6 @@ test("tracks via AJAX if we're on the same site", function() {
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
ok(!track(generateClickEventOn('#same-site')));
ok(Discourse.ajax.calledOnce);
ok(DiscourseURL.routeTo.calledOnce);
});
@@ -12,7 +12,6 @@ module("lib:click-track", {
// Prevent any of these tests from navigating away
win = {focus: function() { } };
redirectTo = sandbox.stub(DiscourseURL, "redirectTo");
sandbox.stub(Discourse, "ajax");
windowOpen = sandbox.stub(window, "open").returns(win);
sandbox.stub(win, "focus");
@@ -161,7 +160,6 @@ var testOpenInANewTab = function(description, clickEventModifier) {
clickEventModifier(clickEvent);
sandbox.stub(clickEvent, "preventDefault");
ok(track(clickEvent));
ok(Discourse.ajax.calledOnce);
ok(!clickEvent.preventDefault.calledOnce);
});
};
@@ -187,7 +185,6 @@ test("tracks via AJAX if we're on the same site", function() {
sandbox.stub(DiscourseURL, "origin").returns("http://discuss.domain.com");
ok(!track(generateClickEventOn('#same-site')));
ok(Discourse.ajax.calledOnce);
ok(DiscourseURL.routeTo.calledOnce);
});
+4 -8
View File
@@ -37,19 +37,15 @@ test('updateFromJson', function() {
});
test('save', function() {
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve({}));
expect(0);
const badge = Badge.create({name: "New Badge", description: "This is a new badge.", badge_type_id: 1});
// TODO: clean API
badge.save(["name", "description", "badge_type_id"]);
ok(Discourse.ajax.calledOnce, "saved badge");
return badge.save(["name", "description", "badge_type_id"]);
});
test('destroy', function() {
sandbox.stub(Discourse, 'ajax');
expect(0);
const badge = Badge.create({name: "New Badge", description: "This is a new badge.", badge_type_id: 1});
badge.destroy();
ok(!Discourse.ajax.calledOnce, "no AJAX call for a new badge");
badge.set('id', 3);
badge.destroy();
ok(Discourse.ajax.calledOnce, "AJAX call was made");
return badge.destroy();
});
@@ -55,24 +55,18 @@ test("destroy", function() {
var user = Discourse.User.create({username: 'eviltrout'});
var topic = Topic.create({id: 1234});
sandbox.stub(Discourse, 'ajax');
topic.destroy(user);
present(topic.get('deleted_at'), 'deleted at is set');
equal(topic.get('deleted_by'), user, 'deleted by is set');
//ok(Discourse.ajax.calledOnce, "it called delete over the wire");
});
test("recover", function() {
var user = Discourse.User.create({username: 'eviltrout'});
var topic = Topic.create({id: 1234, deleted_at: new Date(), deleted_by: user});
sandbox.stub(Discourse, 'ajax');
topic.recover();
blank(topic.get('deleted_at'), "it clears deleted_at");
blank(topic.get('deleted_by'), "it clears deleted_by");
//ok(Discourse.ajax.calledOnce, "it called recover over the wire");
});
test('fancyTitle', function() {
+11 -26
View File
@@ -1,12 +1,10 @@
import UserBadge from 'discourse/models/user-badge';
import badgeFixtures from 'fixtures/user-badges';
module("model:user-badge");
const singleBadgeJson = {"badges":[{"id":874,"name":"Badge 2","description":null,"badge_type_id":7}],"badge_types":[{"id":7,"name":"Silver 2"}],"users":[{"id":13470,"username":"anne3","avatar_template":"//www.gravatar.com/avatar/a4151b1fd72089c54e2374565a87da7f.png?s={size}\u0026r=pg\u0026d=identicon"}],"user_badge":{"id":665,"granted_at":"2014-03-09T20:30:01.190-04:00","badge_id":874,"granted_by_id":13470}},
multipleBadgesJson = {"badges":[{"id":880,"name":"Badge 8","description":null,"badge_type_id":13}],"badge_types":[{"id":13,"name":"Silver 8"}],"users":[],"user_badges":[{"id":668,"granted_at":"2014-03-09T20:30:01.420-04:00","badge_id":880,"granted_by_id":null}]};
test('createFromJson single', function() {
const userBadge = UserBadge.createFromJson(singleBadgeJson);
const userBadge = UserBadge.createFromJson(badgeFixtures['/user_badges']);
ok(!Array.isArray(userBadge), "does not return an array");
equal(userBadge.get('badge.name'), "Badge 2", "badge reference is set");
equal(userBadge.get('badge.badge_type.name'), "Silver 2", "badge.badge_type reference is set");
@@ -14,44 +12,31 @@ test('createFromJson single', function() {
});
test('createFromJson array', function() {
const userBadges = UserBadge.createFromJson(multipleBadgesJson);
const userBadges = UserBadge.createFromJson(badgeFixtures['/user-badges/:username']);
ok(Array.isArray(userBadges), "returns an array");
equal(userBadges[0].get('granted_by'), null, "granted_by reference is not set when null");
});
asyncTestDiscourse('findByUsername', function() {
expect(2);
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve(multipleBadgesJson));
UserBadge.findByUsername("anne3").then(function(badges) {
test('findByUsername', function() {
return UserBadge.findByUsername("anne3").then(function(badges) {
ok(Array.isArray(badges), "returns an array");
start();
});
ok(Discourse.ajax.calledOnce, "makes an AJAX call");
});
asyncTestDiscourse('findByBadgeId', function() {
expect(2);
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve(multipleBadgesJson));
UserBadge.findByBadgeId(880).then(function(badges) {
test('findByBadgeId', function() {
return UserBadge.findByBadgeId(880).then(function(badges) {
ok(Array.isArray(badges), "returns an array");
start();
});
ok(Discourse.ajax.calledOnce, "makes an AJAX call");
});
asyncTestDiscourse('grant', function() {
expect(2);
sandbox.stub(Discourse, 'ajax').returns(Ember.RSVP.resolve(singleBadgeJson));
UserBadge.grant(1, "username").then(function(userBadge) {
test('grant', function() {
return UserBadge.grant(1, "username").then(function(userBadge) {
ok(!Array.isArray(userBadge), "does not return an array");
start();
});
ok(Discourse.ajax.calledOnce, "makes an AJAX call");
});
test('revoke', function() {
sandbox.stub(Discourse, 'ajax');
expect(0);
const userBadge = UserBadge.create({id: 1});
userBadge.revoke();
ok(Discourse.ajax.calledOnce, "makes an AJAX call");
return userBadge.revoke();
});