FEATURE: Reply Placeholders in Stream

This commit is contained in:
Robin Ward
2015-12-01 17:27:32 -05:00
parent 1987a35daf
commit 40c8d39137
14 changed files with 312 additions and 185 deletions

View File

@ -65,6 +65,7 @@ test('appending posts', function() {
const postStream = buildStream(4567, [1, 3, 4]);
const store = postStream.store;
equal(postStream.get('firstPostId'), 1);
equal(postStream.get('lastPostId'), 4, "the last post id is 4");
ok(!postStream.get('hasPosts'), "there are no posts by default");
@ -283,23 +284,26 @@ test("storePost", function() {
const postWithoutId = store.createRecord('post', {raw: 'hello world'});
stored = postStream.storePost(postWithoutId);
equal(stored, postWithoutId, "it returns the same post back");
equal(postStream.get('postIdentityMap.size'), 1, "it does not add a new entry into the identity map");
});
test("identity map", function() {
const postStream = buildStream(1234),
store = postStream.store;
const postStream = buildStream(1234);
const store = postStream.store;
const p1 = postStream.appendPost(store.createRecord('post', {id: 1, post_number: 1}));
postStream.appendPost(store.createRecord('post', {id: 3, post_number: 4}));
const p3 = postStream.appendPost(store.createRecord('post', {id: 3, post_number: 4}));
equal(postStream.findLoadedPost(1), p1, "it can return cached posts by id");
blank(postStream.findLoadedPost(4), "it can't find uncached posts");
deepEqual(postStream.listUnloadedIds([10, 11, 12]), [10, 11, 12], "it returns a list of all unloaded ids");
blank(postStream.listUnloadedIds([1, 3]), "if we have loaded all posts it's blank");
deepEqual(postStream.listUnloadedIds([1, 2, 3, 4]), [2, 4], "it only returns unloaded posts");
// Find posts by ids uses the identity map
postStream.findPostsByIds([1, 2, 3]).then(result => {
equal(result.length, 3);
equal(result.objectAt(0), p1);
equal(result.objectAt(1).get('post_number'), 2);
equal(result.objectAt(2), p3);
});
});
test("loadIntoIdentityMap with no data", () => {
@ -439,7 +443,6 @@ test('triggerNewPostInStream', function() {
ok(postStream.appendMore.calledOnce, "delegates to appendMore because the last post is loaded");
});
test("loadedAllPosts when the id changes", function() {
// This can happen in a race condition between staging a post and it coming through on the
// message bus. If the id of a post changes we should reconsider the loadedAllPosts property.
@ -475,3 +478,45 @@ test("comitting and triggerNewPostInStream race condition", function() {
equal(postStream.get('filteredPostsCount'), 1, "it does not add the same post twice");
});
test("postsWithPlaceholders", () => {
const postStream = buildStream(4964, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
const postsWithPlaceholders = postStream.get('postsWithPlaceholders');
const store = postStream.store;
const testProxy = Ember.ArrayProxy.create({ content: postsWithPlaceholders });
const p1 = store.createRecord('post', {id: 1, post_number: 1});
const p2 = store.createRecord('post', {id: 2, post_number: 2});
const p3 = store.createRecord('post', {id: 3, post_number: 3});
const p4 = store.createRecord('post', {id: 4, post_number: 4});
postStream.appendPost(p1);
postStream.appendPost(p2);
postStream.appendPost(p3);
// Test enumerable and array access
equal(postsWithPlaceholders.get('length'), 3);
equal(testProxy.get('length'), 3);
equal(postsWithPlaceholders.nextObject(0), p1);
equal(postsWithPlaceholders.objectAt(0), p1);
equal(postsWithPlaceholders.nextObject(1, p1), p2);
equal(postsWithPlaceholders.objectAt(1), p2);
equal(postsWithPlaceholders.nextObject(2, p2), p3);
equal(postsWithPlaceholders.objectAt(2), p3);
const promise = postStream.appendMore();
equal(postsWithPlaceholders.get('length'), 8, 'we immediately have a larger placeholder window');
equal(testProxy.get('length'), 8);
ok(!!postsWithPlaceholders.nextObject(3, p3));
ok(!!postsWithPlaceholders.objectAt(4));
ok(postsWithPlaceholders.objectAt(3) !== p4);
ok(testProxy.objectAt(3) !== p4);
return promise.then(() => {
equal(postsWithPlaceholders.objectAt(3), p4);
equal(postsWithPlaceholders.get('length'), 8, 'have a larger placeholder window when loaded');
equal(testProxy.get('length'), 8);
equal(testProxy.objectAt(3), p4);
});
});