From 949f51ffe02d01bbbe2961178671f75a39d5db70 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 1 Dec 2015 16:04:13 -0500 Subject: [PATCH] Remove ajax stubbing from post-stream tests --- .../discourse/models/post-stream.js.es6 | 14 ++++----- .../helpers/create-pretender.js.es6 | 10 +++++++ .../models/post-stream-test.js.es6 | 29 ++++--------------- 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/app/assets/javascripts/discourse/models/post-stream.js.es6 b/app/assets/javascripts/discourse/models/post-stream.js.es6 index 0368b2c2ec1..02e0b49febc 100644 --- a/app/assets/javascripts/discourse/models/post-stream.js.es6 +++ b/app/assets/javascripts/discourse/models/post-stream.js.es6 @@ -731,20 +731,16 @@ const PostStream = RestModel.extend({ loadIntoIdentityMap(postIds) { // If we don't want any posts, return a promise that resolves right away if (Em.isEmpty(postIds)) { - return Ember.RSVP.resolve(); + return Ember.RSVP.resolve([]); } - const url = "/t/" + this.get('topic.id') + "/posts.json", - data = { post_ids: postIds }, - postStream = this; - + const url = "/t/" + this.get('topic.id') + "/posts.json"; + const data = { post_ids: postIds }; const store = this.store; - return Discourse.ajax(url, {data: data}).then(function(result) { + return Discourse.ajax(url, {data: data}).then(result => { const posts = Em.get(result, "post_stream.posts"); if (posts) { - posts.forEach(function (p) { - postStream.storePost(store.createRecord('post', p)); - }); + posts.forEach(p => this.storePost(store.createRecord('post', p))); } }); }, diff --git a/test/javascripts/helpers/create-pretender.js.es6 b/test/javascripts/helpers/create-pretender.js.es6 index 9754b390819..fca63e935bf 100644 --- a/test/javascripts/helpers/create-pretender.js.es6 +++ b/test/javascripts/helpers/create-pretender.js.es6 @@ -153,6 +153,16 @@ export default function() { slug: request.params.slug } }); }); + this.get('/t/:topic_id/posts.json', request => { + const postIds = request.queryParams.post_ids; + const posts = postIds.map(p => ({id: parseInt(p), post_number: parseInt(p) })); + return response(200, { post_stream: { posts } }); + }); + + this.get('/posts/:post_id/reply-history.json', () => { + return response(200, [ { id: 2222, post_number: 2222 } ]); + }); + this.post('/posts', function(request) { const data = parsePostData(request.requestBody); diff --git a/test/javascripts/models/post-stream-test.js.es6 b/test/javascripts/models/post-stream-test.js.es6 index 6feef3763d8..13ae84a961b 100644 --- a/test/javascripts/models/post-stream-test.js.es6 +++ b/test/javascripts/models/post-stream-test.js.es6 @@ -302,47 +302,28 @@ test("identity map", function() { deepEqual(postStream.listUnloadedIds([1, 2, 3, 4]), [2, 4], "it only returns unloaded posts"); }); -asyncTestDiscourse("loadIntoIdentityMap with no data", function() { - const postStream = buildStream(1234); - expect(1); - - sandbox.stub(Discourse, "ajax"); - postStream.loadIntoIdentityMap([]).then(function() { - ok(!Discourse.ajax.calledOnce, "an empty array returned a promise yet performed no ajax request"); - start(); +test("loadIntoIdentityMap with no data", () => { + buildStream(1234).loadIntoIdentityMap([]).then(result => { + equal(result.length, 0, 'requesting no posts produces no posts'); }); }); -asyncTestDiscourse("loadIntoIdentityMap with post ids", function() { +test("loadIntoIdentityMap with post ids", function() { const postStream = buildStream(1234); - expect(1); - - sandbox.stub(Discourse, "ajax").returns(Ember.RSVP.resolve({ - post_stream: { - posts: [{id: 10, post_number: 10}] - } - })); postStream.loadIntoIdentityMap([10]).then(function() { present(postStream.findLoadedPost(10), "it adds the returned post to the store"); - start(); }); }); -asyncTestDiscourse("loading a post's history", function() { +test("loading a post's history", function() { const postStream = buildStream(1234); const store = postStream.store; - expect(3); - const post = store.createRecord('post', {id: 4321}); - const secondPost = store.createRecord('post', {id: 2222}); - sandbox.stub(Discourse, "ajax").returns(Ember.RSVP.resolve([secondPost])); postStream.findReplyHistory(post).then(function() { - ok(Discourse.ajax.calledOnce, "it made the ajax request"); present(postStream.findLoadedPost(2222), "it stores the returned post in the identity map"); present(post.get('replyHistory'), "it sets the replyHistory attribute for the post"); - start(); }); });