2020-03-07 06:49:28 +08:00
|
|
|
import ArrayProxy from "@ember/array/proxy";
|
2019-11-09 02:13:13 +08:00
|
|
|
import Post from "discourse/models/post";
|
2018-06-15 23:03:24 +08:00
|
|
|
import createStore from "helpers/create-store";
|
2019-11-14 04:55:32 +08:00
|
|
|
import User from "discourse/models/user";
|
2019-11-21 04:18:37 +08:00
|
|
|
import { Promise } from "rsvp";
|
2020-03-03 03:24:31 +08:00
|
|
|
import pretender from "helpers/create-pretender";
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2019-11-09 02:13:13 +08:00
|
|
|
QUnit.module("model:post-stream");
|
|
|
|
|
2015-04-09 02:44:44 +08:00
|
|
|
const buildStream = function(id, stream) {
|
|
|
|
const store = createStore();
|
2018-06-15 23:03:24 +08:00
|
|
|
const topic = store.createRecord("topic", { id, chunk_size: 5 });
|
|
|
|
const ps = topic.get("postStream");
|
2013-06-21 05:20:08 +08:00
|
|
|
if (stream) {
|
2018-06-15 23:03:24 +08:00
|
|
|
ps.set("stream", stream);
|
2013-06-21 05:20:08 +08:00
|
|
|
}
|
|
|
|
return ps;
|
|
|
|
};
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const participant = { username: "eviltrout" };
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
QUnit.test("create", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const store = createStore();
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.ok(
|
|
|
|
store.createRecord("postStream"),
|
|
|
|
"it can be created with no parameters"
|
|
|
|
);
|
2013-10-30 01:01:42 +08:00
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
QUnit.test("defaults", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(1234);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.blank(
|
|
|
|
postStream.get("posts"),
|
|
|
|
"there are no posts in a stream by default"
|
|
|
|
);
|
|
|
|
assert.ok(!postStream.get("loaded"), "it has never loaded");
|
|
|
|
assert.present(postStream.get("topic"));
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
QUnit.test("appending posts", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(4567, [1, 3, 4]);
|
|
|
|
const store = postStream.store;
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(postStream.get("firstPostId"), 1);
|
|
|
|
assert.equal(postStream.get("lastPostId"), 4, "the last post id is 4");
|
|
|
|
|
|
|
|
assert.ok(!postStream.get("hasPosts"), "there are no posts by default");
|
|
|
|
assert.ok(
|
|
|
|
!postStream.get("firstPostPresent"),
|
|
|
|
"the first post is not loaded"
|
|
|
|
);
|
|
|
|
assert.ok(!postStream.get("loadedAllPosts"), "the last post is not loaded");
|
|
|
|
assert.equal(postStream.get("posts.length"), 0, "it has no posts initially");
|
|
|
|
|
|
|
|
postStream.appendPost(store.createRecord("post", { id: 2, post_number: 2 }));
|
|
|
|
assert.ok(
|
|
|
|
!postStream.get("firstPostPresent"),
|
|
|
|
"the first post is still not loaded"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
postStream.get("posts.length"),
|
|
|
|
1,
|
|
|
|
"it has one post in the stream"
|
|
|
|
);
|
|
|
|
|
|
|
|
postStream.appendPost(store.createRecord("post", { id: 4, post_number: 4 }));
|
|
|
|
assert.ok(
|
|
|
|
!postStream.get("firstPostPresent"),
|
|
|
|
"the first post is still loaded"
|
|
|
|
);
|
|
|
|
assert.ok(postStream.get("loadedAllPosts"), "the last post is now loaded");
|
|
|
|
assert.equal(
|
|
|
|
postStream.get("posts.length"),
|
|
|
|
2,
|
|
|
|
"it has two posts in the stream"
|
|
|
|
);
|
|
|
|
|
|
|
|
postStream.appendPost(store.createRecord("post", { id: 4, post_number: 4 }));
|
|
|
|
assert.equal(
|
|
|
|
postStream.get("posts.length"),
|
|
|
|
2,
|
|
|
|
"it will not add the same post with id twice"
|
|
|
|
);
|
|
|
|
|
|
|
|
const stagedPost = store.createRecord("post", { raw: "incomplete post" });
|
2013-06-21 05:20:08 +08:00
|
|
|
postStream.appendPost(stagedPost);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
postStream.get("posts.length"),
|
|
|
|
3,
|
|
|
|
"it can handle posts without ids"
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
postStream.appendPost(stagedPost);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
postStream.get("posts.length"),
|
|
|
|
3,
|
|
|
|
"it won't add the same post without an id twice"
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
|
|
|
|
// change the stream
|
2018-06-15 23:03:24 +08:00
|
|
|
postStream.set("stream", [1, 2, 4]);
|
|
|
|
assert.ok(
|
|
|
|
!postStream.get("firstPostPresent"),
|
|
|
|
"the first post no longer loaded since the stream changed."
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
postStream.get("loadedAllPosts"),
|
|
|
|
"the last post is still the last post in the new stream"
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
QUnit.test("closestPostNumberFor", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(1231);
|
|
|
|
const store = postStream.store;
|
2013-07-04 00:30:22 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.blank(
|
|
|
|
postStream.closestPostNumberFor(1),
|
|
|
|
"there is no closest post when nothing is loaded"
|
|
|
|
);
|
|
|
|
|
|
|
|
postStream.appendPost(store.createRecord("post", { id: 1, post_number: 2 }));
|
|
|
|
postStream.appendPost(store.createRecord("post", { id: 2, post_number: 3 }));
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
postStream.closestPostNumberFor(2),
|
|
|
|
2,
|
|
|
|
"If a post is in the stream it returns its post number"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
postStream.closestPostNumberFor(3),
|
|
|
|
3,
|
|
|
|
"If a post is in the stream it returns its post number"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
postStream.closestPostNumberFor(10),
|
|
|
|
3,
|
|
|
|
"it clips to the upper bound of the stream"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
postStream.closestPostNumberFor(0),
|
|
|
|
2,
|
|
|
|
"it clips to the lower bound of the stream"
|
|
|
|
);
|
2013-07-04 00:30:22 +08:00
|
|
|
});
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
QUnit.test("closestDaysAgoFor", assert => {
|
2016-05-18 01:03:08 +08:00
|
|
|
const postStream = buildStream(1231);
|
2019-11-15 23:34:26 +08:00
|
|
|
postStream.set("timelineLookup", [
|
|
|
|
[1, 10],
|
|
|
|
[3, 8],
|
|
|
|
[5, 1]
|
|
|
|
]);
|
2016-05-18 01:03:08 +08:00
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(postStream.closestDaysAgoFor(1), 10);
|
|
|
|
assert.equal(postStream.closestDaysAgoFor(2), 10);
|
|
|
|
assert.equal(postStream.closestDaysAgoFor(3), 8);
|
|
|
|
assert.equal(postStream.closestDaysAgoFor(4), 8);
|
|
|
|
assert.equal(postStream.closestDaysAgoFor(5), 1);
|
2016-05-18 01:03:08 +08:00
|
|
|
|
|
|
|
// Out of bounds
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(postStream.closestDaysAgoFor(-1), 10);
|
|
|
|
assert.equal(postStream.closestDaysAgoFor(0), 10);
|
|
|
|
assert.equal(postStream.closestDaysAgoFor(10), 1);
|
2018-07-10 16:43:20 +08:00
|
|
|
|
|
|
|
postStream.set("timelineLookup", []);
|
|
|
|
assert.equal(postStream.closestDaysAgoFor(1), undefined);
|
2016-05-18 01:03:08 +08:00
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
QUnit.test("closestDaysAgoFor - empty", assert => {
|
2016-05-18 01:03:08 +08:00
|
|
|
const postStream = buildStream(1231);
|
2018-06-15 23:03:24 +08:00
|
|
|
postStream.set("timelineLookup", []);
|
2016-05-18 01:03:08 +08:00
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(postStream.closestDaysAgoFor(1), null);
|
2016-05-18 01:03:08 +08:00
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
QUnit.test("updateFromJson", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(1231);
|
2013-06-21 05:20:08 +08:00
|
|
|
|
|
|
|
postStream.updateFromJson({
|
2018-06-15 23:03:24 +08:00
|
|
|
posts: [{ id: 1 }],
|
2013-06-21 05:20:08 +08:00
|
|
|
stream: [1],
|
|
|
|
extra_property: 12
|
|
|
|
});
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(postStream.get("posts.length"), 1, "it loaded the posts");
|
2019-11-09 02:13:13 +08:00
|
|
|
assert.containsInstance(postStream.get("posts"), Post);
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(postStream.get("extra_property"), 12);
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("removePosts", assert => {
|
2018-06-15 23:03:24 +08:00
|
|
|
const postStream = buildStream(10000001, [1, 2, 3]);
|
2015-04-09 02:44:44 +08:00
|
|
|
const store = postStream.store;
|
2013-07-24 02:59:05 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const p1 = store.createRecord("post", { id: 1, post_number: 2 }),
|
|
|
|
p2 = store.createRecord("post", { id: 2, post_number: 3 }),
|
|
|
|
p3 = store.createRecord("post", { id: 3, post_number: 4 });
|
2013-07-24 02:59:05 +08:00
|
|
|
|
|
|
|
postStream.appendPost(p1);
|
|
|
|
postStream.appendPost(p2);
|
|
|
|
postStream.appendPost(p3);
|
|
|
|
|
|
|
|
// Removing nothing does nothing
|
|
|
|
postStream.removePosts();
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(postStream.get("posts.length"), 3);
|
2013-07-24 02:59:05 +08:00
|
|
|
|
|
|
|
postStream.removePosts([p1, p3]);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(postStream.get("posts.length"), 1);
|
|
|
|
assert.deepEqual(postStream.get("stream"), [2]);
|
2013-07-24 02:59:05 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("cancelFilter", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(1235);
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2019-11-21 04:18:37 +08:00
|
|
|
sandbox.stub(postStream, "refresh").returns(Promise.resolve());
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
postStream.set("summary", true);
|
2013-06-21 05:20:08 +08:00
|
|
|
postStream.cancelFilter();
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.ok(!postStream.get("summary"), "summary is cancelled");
|
2013-06-21 05:20:08 +08:00
|
|
|
|
|
|
|
postStream.toggleParticipant(participant);
|
|
|
|
postStream.cancelFilter();
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.blank(
|
|
|
|
postStream.get("userFilters"),
|
|
|
|
"cancelling the filters clears the userFilters"
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("findPostIdForPostNumber", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(1234, [10, 20, 30, 40, 50, 60, 70]);
|
2018-06-15 23:03:24 +08:00
|
|
|
postStream.set("gaps", { before: { 60: [55, 58] } });
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
postStream.findPostIdForPostNumber(500),
|
|
|
|
null,
|
|
|
|
"it returns null when the post cannot be found"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
postStream.findPostIdForPostNumber(1),
|
|
|
|
10,
|
|
|
|
"it finds the postId at the beginning"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
postStream.findPostIdForPostNumber(5),
|
|
|
|
50,
|
|
|
|
"it finds the postId in the middle"
|
|
|
|
);
|
|
|
|
assert.equal(postStream.findPostIdForPostNumber(8), 60, "it respects gaps");
|
2015-01-13 07:10:16 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("toggleParticipant", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(1236);
|
2019-11-21 04:18:37 +08:00
|
|
|
sandbox.stub(postStream, "refresh").returns(Promise.resolve());
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
postStream.get("userFilters.length"),
|
|
|
|
0,
|
|
|
|
"by default no participants are toggled"
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
|
|
|
|
postStream.toggleParticipant(participant.username);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.ok(
|
|
|
|
postStream.get("userFilters").includes("eviltrout"),
|
|
|
|
"eviltrout is in the filters"
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
|
|
|
|
postStream.toggleParticipant(participant.username);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.blank(
|
|
|
|
postStream.get("userFilters"),
|
|
|
|
"toggling the participant again removes them"
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("streamFilters", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(1237);
|
2019-11-21 04:18:37 +08:00
|
|
|
sandbox.stub(postStream, "refresh").returns(Promise.resolve());
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.deepEqual(
|
|
|
|
postStream.get("streamFilters"),
|
|
|
|
{},
|
|
|
|
"there are no postFilters by default"
|
|
|
|
);
|
|
|
|
assert.ok(postStream.get("hasNoFilters"), "there are no filters by default");
|
|
|
|
|
|
|
|
postStream.set("summary", true);
|
|
|
|
assert.deepEqual(
|
|
|
|
postStream.get("streamFilters"),
|
|
|
|
{ filter: "summary" },
|
|
|
|
"postFilters contains the summary flag"
|
|
|
|
);
|
|
|
|
assert.ok(!postStream.get("hasNoFilters"), "now there are filters present");
|
2013-06-21 05:20:08 +08:00
|
|
|
|
|
|
|
postStream.toggleParticipant(participant.username);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.deepEqual(
|
|
|
|
postStream.get("streamFilters"),
|
|
|
|
{
|
|
|
|
username_filters: "eviltrout"
|
|
|
|
},
|
|
|
|
"streamFilters contains the username we filtered"
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("loading", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
let postStream = buildStream(1234);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.ok(!postStream.get("loading"), "we're not loading by default");
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
postStream.set("loadingAbove", true);
|
|
|
|
assert.ok(postStream.get("loading"), "we're loading if loading above");
|
2013-06-21 05:20:08 +08:00
|
|
|
|
|
|
|
postStream = buildStream(1234);
|
2018-06-15 23:03:24 +08:00
|
|
|
postStream.set("loadingBelow", true);
|
|
|
|
assert.ok(postStream.get("loading"), "we're loading if loading below");
|
2013-06-21 05:20:08 +08:00
|
|
|
|
|
|
|
postStream = buildStream(1234);
|
2018-06-15 23:03:24 +08:00
|
|
|
postStream.set("loadingFilter", true);
|
|
|
|
assert.ok(postStream.get("loading"), "we're loading if loading a filter");
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("nextWindow", assert => {
|
2018-06-15 23:03:24 +08:00
|
|
|
const postStream = buildStream(1234, [
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
5,
|
|
|
|
8,
|
|
|
|
9,
|
|
|
|
10,
|
|
|
|
11,
|
|
|
|
13,
|
|
|
|
14,
|
|
|
|
15,
|
|
|
|
16
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.blank(
|
|
|
|
postStream.get("nextWindow"),
|
|
|
|
"With no posts loaded, the window is blank"
|
|
|
|
);
|
|
|
|
|
|
|
|
postStream.updateFromJson({ posts: [{ id: 1 }, { id: 2 }] });
|
|
|
|
assert.deepEqual(
|
|
|
|
postStream.get("nextWindow"),
|
|
|
|
[3, 5, 8, 9, 10],
|
|
|
|
"If we've loaded the first 2 posts, the window should be the 5 after that"
|
|
|
|
);
|
|
|
|
|
|
|
|
postStream.updateFromJson({ posts: [{ id: 13 }] });
|
|
|
|
assert.deepEqual(
|
|
|
|
postStream.get("nextWindow"),
|
|
|
|
[14, 15, 16],
|
|
|
|
"Boundary check: stop at the end."
|
|
|
|
);
|
|
|
|
|
|
|
|
postStream.updateFromJson({ posts: [{ id: 16 }] });
|
|
|
|
assert.blank(
|
|
|
|
postStream.get("nextWindow"),
|
|
|
|
"Once we've seen everything there's nothing to load."
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("previousWindow", assert => {
|
2018-06-15 23:03:24 +08:00
|
|
|
const postStream = buildStream(1234, [
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
5,
|
|
|
|
8,
|
|
|
|
9,
|
|
|
|
10,
|
|
|
|
11,
|
|
|
|
13,
|
|
|
|
14,
|
|
|
|
15,
|
|
|
|
16
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert.blank(
|
|
|
|
postStream.get("previousWindow"),
|
|
|
|
"With no posts loaded, the window is blank"
|
|
|
|
);
|
|
|
|
|
|
|
|
postStream.updateFromJson({ posts: [{ id: 11 }, { id: 13 }] });
|
|
|
|
assert.deepEqual(
|
|
|
|
postStream.get("previousWindow"),
|
|
|
|
[3, 5, 8, 9, 10],
|
|
|
|
"If we've loaded in the middle, it's the previous 5 posts"
|
|
|
|
);
|
|
|
|
|
|
|
|
postStream.updateFromJson({ posts: [{ id: 3 }] });
|
|
|
|
assert.deepEqual(
|
|
|
|
postStream.get("previousWindow"),
|
|
|
|
[1, 2],
|
|
|
|
"Boundary check: stop at the beginning."
|
|
|
|
);
|
|
|
|
|
|
|
|
postStream.updateFromJson({ posts: [{ id: 1 }] });
|
|
|
|
assert.blank(
|
|
|
|
postStream.get("previousWindow"),
|
|
|
|
"Once we've seen everything there's nothing to load."
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("storePost", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(1234),
|
2018-06-15 23:03:24 +08:00
|
|
|
store = postStream.store,
|
|
|
|
post = store.createRecord("post", {
|
|
|
|
id: 1,
|
|
|
|
post_number: 100,
|
|
|
|
raw: "initial value"
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.blank(
|
|
|
|
postStream.get("topic.highest_post_number"),
|
|
|
|
"it has no highest post number yet"
|
|
|
|
);
|
2015-04-09 02:44:44 +08:00
|
|
|
let stored = postStream.storePost(post);
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(post, stored, "it returns the post it stored");
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
post.get("topic"),
|
|
|
|
postStream.get("topic"),
|
|
|
|
"it creates the topic reference properly"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
postStream.get("topic.highest_post_number"),
|
|
|
|
100,
|
|
|
|
"it set the highest post number"
|
|
|
|
);
|
|
|
|
|
|
|
|
const dupePost = store.createRecord("post", {
|
|
|
|
id: 1,
|
|
|
|
post_number: 100,
|
|
|
|
raw: "updated value"
|
|
|
|
});
|
2015-04-09 02:44:44 +08:00
|
|
|
const storedDupe = postStream.storePost(dupePost);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
storedDupe,
|
|
|
|
post,
|
|
|
|
"it returns the previously stored post instead to avoid dupes"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
storedDupe.get("raw"),
|
|
|
|
"updated value",
|
|
|
|
"it updates the previously stored post"
|
|
|
|
);
|
|
|
|
|
|
|
|
const postWithoutId = store.createRecord("post", { raw: "hello world" });
|
2013-06-21 05:20:08 +08:00
|
|
|
stored = postStream.storePost(postWithoutId);
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(stored, postWithoutId, "it returns the same post back");
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
QUnit.test("identity map", async assert => {
|
2015-12-02 06:27:32 +08:00
|
|
|
const postStream = buildStream(1234);
|
|
|
|
const store = postStream.store;
|
2015-04-09 02:44:44 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const p1 = postStream.appendPost(
|
|
|
|
store.createRecord("post", { id: 1, post_number: 1 })
|
|
|
|
);
|
|
|
|
const p3 = postStream.appendPost(
|
|
|
|
store.createRecord("post", { id: 3, post_number: 4 })
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
postStream.findLoadedPost(1),
|
|
|
|
p1,
|
|
|
|
"it can return cached posts by id"
|
|
|
|
);
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.blank(postStream.findLoadedPost(4), "it can't find uncached posts");
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2015-12-02 06:27:32 +08:00
|
|
|
// Find posts by ids uses the identity map
|
2020-03-03 04:20:19 +08:00
|
|
|
const result = await postStream.findPostsByIds([1, 2, 3]);
|
|
|
|
assert.equal(result.length, 3);
|
|
|
|
assert.equal(result.objectAt(0), p1);
|
|
|
|
assert.equal(result.objectAt(1).get("post_number"), 2);
|
|
|
|
assert.equal(result.objectAt(2), p3);
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
QUnit.test("loadIntoIdentityMap with no data", async assert => {
|
|
|
|
const result = await buildStream(1234).loadIntoIdentityMap([]);
|
|
|
|
assert.equal(result.length, 0, "requesting no posts produces no posts");
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
QUnit.test("loadIntoIdentityMap with post ids", async assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(1234);
|
2020-03-03 04:20:19 +08:00
|
|
|
await postStream.loadIntoIdentityMap([10]);
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
assert.present(
|
|
|
|
postStream.findLoadedPost(10),
|
|
|
|
"it adds the returned post to the store"
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
QUnit.test("appendMore for megatopic", async assert => {
|
2018-07-11 15:41:26 +08:00
|
|
|
const postStream = buildStream(1234);
|
|
|
|
const store = createStore();
|
|
|
|
const post = store.createRecord("post", { id: 1, post_number: 1 });
|
|
|
|
|
|
|
|
postStream.setProperties({
|
|
|
|
isMegaTopic: true,
|
|
|
|
posts: [post]
|
|
|
|
});
|
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
await postStream.appendMore();
|
|
|
|
assert.present(
|
|
|
|
postStream.findLoadedPost(2),
|
|
|
|
"it adds the returned post to the store"
|
|
|
|
);
|
2018-07-11 15:41:26 +08:00
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
assert.equal(
|
|
|
|
postStream.get("posts").length,
|
|
|
|
6,
|
|
|
|
"it adds the right posts into the stream"
|
|
|
|
);
|
2018-07-11 15:41:26 +08:00
|
|
|
});
|
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
QUnit.test("prependMore for megatopic", async assert => {
|
2018-07-11 15:41:26 +08:00
|
|
|
const postStream = buildStream(1234);
|
|
|
|
const store = createStore();
|
|
|
|
const post = store.createRecord("post", { id: 6, post_number: 6 });
|
|
|
|
|
|
|
|
postStream.setProperties({
|
|
|
|
isMegaTopic: true,
|
|
|
|
posts: [post]
|
|
|
|
});
|
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
await postStream.prependMore();
|
|
|
|
assert.present(
|
|
|
|
postStream.findLoadedPost(5),
|
|
|
|
"it adds the returned post to the store"
|
|
|
|
);
|
2018-07-11 15:41:26 +08:00
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
assert.equal(
|
|
|
|
postStream.get("posts").length,
|
|
|
|
6,
|
|
|
|
"it adds the right posts into the stream"
|
|
|
|
);
|
2018-07-11 15:41:26 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("staging and undoing a new post", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(10101, [1]);
|
|
|
|
const store = postStream.store;
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const original = store.createRecord("post", {
|
|
|
|
id: 1,
|
|
|
|
post_number: 1,
|
|
|
|
topic_id: 10101
|
|
|
|
});
|
2015-06-19 22:32:58 +08:00
|
|
|
postStream.appendPost(original);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.ok(
|
|
|
|
postStream.get("lastAppended"),
|
|
|
|
original,
|
|
|
|
"the original post is lastAppended"
|
|
|
|
);
|
|
|
|
|
2019-11-14 04:55:32 +08:00
|
|
|
const user = User.create({
|
2018-06-15 23:03:24 +08:00
|
|
|
username: "eviltrout",
|
|
|
|
name: "eviltrout",
|
|
|
|
id: 321
|
|
|
|
});
|
|
|
|
const stagedPost = store.createRecord("post", {
|
|
|
|
raw: "hello world this is my new post",
|
|
|
|
topic_id: 10101
|
|
|
|
});
|
2015-04-09 02:44:44 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const topic = postStream.get("topic");
|
2013-06-21 05:20:08 +08:00
|
|
|
topic.setProperties({
|
|
|
|
posts_count: 1,
|
|
|
|
highest_post_number: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
// Stage the new post in the stream
|
2015-04-09 02:44:44 +08:00
|
|
|
const result = postStream.stagePost(stagedPost, user);
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(result, "staged", "it returns staged");
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
topic.get("highest_post_number"),
|
|
|
|
2,
|
|
|
|
"it updates the highest_post_number"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
postStream.get("loading"),
|
|
|
|
"it is loading while the post is being staged"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
postStream.get("lastAppended"),
|
|
|
|
original,
|
|
|
|
"it doesn't consider staged posts as the lastAppended"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(topic.get("posts_count"), 2, "it increases the post count");
|
|
|
|
assert.present(topic.get("last_posted_at"), "it updates last_posted_at");
|
|
|
|
assert.equal(
|
|
|
|
topic.get("details.last_poster"),
|
|
|
|
user,
|
|
|
|
"it changes the last poster"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
stagedPost.get("topic"),
|
|
|
|
topic,
|
|
|
|
"it assigns the topic reference"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
stagedPost.get("post_number"),
|
|
|
|
2,
|
|
|
|
"it is assigned the probable post_number"
|
|
|
|
);
|
|
|
|
assert.present(stagedPost.get("created_at"), "it is assigned a created date");
|
|
|
|
assert.ok(
|
|
|
|
postStream.get("posts").includes(stagedPost),
|
|
|
|
"the post is added to the stream"
|
|
|
|
);
|
|
|
|
assert.equal(stagedPost.get("id"), -1, "the post has a magical -1 id");
|
2013-06-21 05:20:08 +08:00
|
|
|
|
|
|
|
// Undoing a created post (there was an error)
|
|
|
|
postStream.undoPost(stagedPost);
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.ok(!postStream.get("loading"), "it is no longer loading");
|
|
|
|
assert.equal(
|
|
|
|
topic.get("highest_post_number"),
|
|
|
|
1,
|
|
|
|
"it reverts the highest_post_number"
|
|
|
|
);
|
|
|
|
assert.equal(topic.get("posts_count"), 1, "it reverts the post count");
|
|
|
|
assert.equal(
|
|
|
|
postStream.get("filteredPostsCount"),
|
|
|
|
1,
|
|
|
|
"it retains the filteredPostsCount"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
!postStream.get("posts").includes(stagedPost),
|
|
|
|
"the post is removed from the stream"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
postStream.get("lastAppended"),
|
|
|
|
original,
|
|
|
|
"it doesn't consider undid post lastAppended"
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("staging and committing a post", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(10101, [1]);
|
|
|
|
const store = postStream.store;
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const original = store.createRecord("post", {
|
|
|
|
id: 1,
|
|
|
|
post_number: 1,
|
|
|
|
topic_id: 10101
|
|
|
|
});
|
2015-06-19 22:32:58 +08:00
|
|
|
postStream.appendPost(original);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.ok(
|
|
|
|
postStream.get("lastAppended"),
|
|
|
|
original,
|
|
|
|
"the original post is lastAppended"
|
|
|
|
);
|
|
|
|
|
2019-11-14 04:55:32 +08:00
|
|
|
const user = User.create({
|
2018-06-15 23:03:24 +08:00
|
|
|
username: "eviltrout",
|
|
|
|
name: "eviltrout",
|
|
|
|
id: 321
|
|
|
|
});
|
|
|
|
const stagedPost = store.createRecord("post", {
|
|
|
|
raw: "hello world this is my new post",
|
|
|
|
topic_id: 10101
|
|
|
|
});
|
2013-06-21 05:20:08 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
const topic = postStream.get("topic");
|
|
|
|
topic.set("posts_count", 1);
|
2013-06-21 05:20:08 +08:00
|
|
|
|
|
|
|
// Stage the new post in the stream
|
2015-04-09 02:44:44 +08:00
|
|
|
let result = postStream.stagePost(stagedPost, user);
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(result, "staged", "it returns staged");
|
2013-07-19 04:09:03 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.ok(
|
|
|
|
postStream.get("loading"),
|
|
|
|
"it is loading while the post is being staged"
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
stagedPost.setProperties({ id: 1234, raw: "different raw value" });
|
|
|
|
|
2013-07-10 00:34:28 +08:00
|
|
|
result = postStream.stagePost(stagedPost, user);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
result,
|
|
|
|
"alreadyStaging",
|
|
|
|
"you can't stage a post while it is currently staging"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
postStream.get("lastAppended"),
|
|
|
|
original,
|
|
|
|
"staging a post doesn't change the lastAppended"
|
|
|
|
);
|
2013-07-10 00:34:28 +08:00
|
|
|
|
2013-06-21 05:20:08 +08:00
|
|
|
postStream.commitPost(stagedPost);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.ok(
|
|
|
|
postStream.get("posts").includes(stagedPost),
|
|
|
|
"the post is still in the stream"
|
|
|
|
);
|
|
|
|
assert.ok(!postStream.get("loading"), "it is no longer loading");
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
postStream.get("filteredPostsCount"),
|
|
|
|
2,
|
|
|
|
"it increases the filteredPostsCount"
|
|
|
|
);
|
|
|
|
|
|
|
|
const found = postStream.findLoadedPost(stagedPost.get("id"));
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.present(found, "the post is in the identity map");
|
|
|
|
assert.ok(postStream.indexOf(stagedPost) > -1, "the post is in the stream");
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
found.get("raw"),
|
|
|
|
"different raw value",
|
|
|
|
"it also updated the value in the stream"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
postStream.get("lastAppended"),
|
|
|
|
found,
|
|
|
|
"comitting a post changes lastAppended"
|
|
|
|
);
|
2013-06-21 05:20:08 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("loadedAllPosts when the id changes", assert => {
|
2013-07-19 04:09:03 +08:00
|
|
|
// This can happen in a race condition between staging a post and it coming through on the
|
2013-11-21 05:33:36 +08:00
|
|
|
// message bus. If the id of a post changes we should reconsider the loadedAllPosts property.
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(10101, [1, 2]);
|
|
|
|
const store = postStream.store;
|
2018-06-15 23:03:24 +08:00
|
|
|
const postWithoutId = store.createRecord("post", {
|
|
|
|
raw: "hello world this is my new post"
|
|
|
|
});
|
2013-07-19 04:09:03 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
postStream.appendPost(store.createRecord("post", { id: 1, post_number: 1 }));
|
2013-07-19 04:09:03 +08:00
|
|
|
postStream.appendPost(postWithoutId);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.ok(!postStream.get("loadedAllPosts"), "the last post is not loaded");
|
2013-07-19 04:09:03 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
postWithoutId.set("id", 2);
|
|
|
|
assert.ok(
|
|
|
|
postStream.get("loadedAllPosts"),
|
|
|
|
"the last post is loaded now that the post has an id"
|
|
|
|
);
|
2013-07-19 04:09:03 +08:00
|
|
|
});
|
|
|
|
|
2018-06-29 11:41:44 +08:00
|
|
|
QUnit.test("triggerRecoveredPost", async assert => {
|
|
|
|
const postStream = buildStream(4567);
|
|
|
|
const store = postStream.store;
|
|
|
|
|
|
|
|
[1, 2, 3, 5].forEach(id => {
|
|
|
|
postStream.appendPost(
|
|
|
|
store.createRecord("post", { id: id, post_number: id })
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const response = object => {
|
|
|
|
return [200, { "Content-Type": "application/json" }, object];
|
|
|
|
};
|
|
|
|
|
2020-03-03 03:24:31 +08:00
|
|
|
pretender.get("/posts/4", () => {
|
2018-06-29 11:41:44 +08:00
|
|
|
return response({ id: 4, post_number: 4 });
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
postStream.get("postsWithPlaceholders.length"),
|
|
|
|
4,
|
|
|
|
"it should return the right length"
|
|
|
|
);
|
|
|
|
|
|
|
|
await postStream.triggerRecoveredPost(4);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
postStream.get("postsWithPlaceholders.length"),
|
|
|
|
5,
|
|
|
|
"it should return the right length"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("comitting and triggerNewPostInStream race condition", assert => {
|
2015-04-09 02:44:44 +08:00
|
|
|
const postStream = buildStream(4964);
|
|
|
|
const store = postStream.store;
|
2013-07-18 04:50:20 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
postStream.appendPost(store.createRecord("post", { id: 1, post_number: 1 }));
|
2019-11-14 04:55:32 +08:00
|
|
|
const user = User.create({
|
2018-06-15 23:03:24 +08:00
|
|
|
username: "eviltrout",
|
|
|
|
name: "eviltrout",
|
|
|
|
id: 321
|
|
|
|
});
|
|
|
|
const stagedPost = store.createRecord("post", {
|
|
|
|
raw: "hello world this is my new post"
|
|
|
|
});
|
2013-07-18 04:50:20 +08:00
|
|
|
|
2013-12-31 02:29:52 +08:00
|
|
|
postStream.stagePost(stagedPost, user);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
postStream.get("filteredPostsCount"),
|
|
|
|
0,
|
|
|
|
"it has no filteredPostsCount yet"
|
|
|
|
);
|
|
|
|
stagedPost.set("id", 123);
|
|
|
|
|
|
|
|
sandbox.stub(postStream, "appendMore");
|
2013-07-18 04:50:20 +08:00
|
|
|
postStream.triggerNewPostInStream(123);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(postStream.get("filteredPostsCount"), 1, "it added the post");
|
2013-07-18 04:50:20 +08:00
|
|
|
|
|
|
|
postStream.commitPost(stagedPost);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
postStream.get("filteredPostsCount"),
|
|
|
|
1,
|
|
|
|
"it does not add the same post twice"
|
|
|
|
);
|
2013-07-19 04:09:03 +08:00
|
|
|
});
|
|
|
|
|
2019-06-11 19:07:14 +08:00
|
|
|
QUnit.test("triggerNewPostInStream for ignored posts", async assert => {
|
|
|
|
const postStream = buildStream(280, [1]);
|
|
|
|
const store = postStream.store;
|
2019-11-14 04:55:32 +08:00
|
|
|
User.resetCurrent(
|
|
|
|
User.create({
|
2019-07-25 18:29:10 +08:00
|
|
|
username: "eviltrout",
|
|
|
|
name: "eviltrout",
|
|
|
|
id: 321,
|
|
|
|
ignored_users: ["ignoreduser"]
|
|
|
|
})
|
|
|
|
);
|
2019-06-11 19:07:14 +08:00
|
|
|
|
|
|
|
postStream.appendPost(store.createRecord("post", { id: 1, post_number: 1 }));
|
|
|
|
|
|
|
|
const post2 = store.createRecord("post", {
|
|
|
|
id: 101,
|
|
|
|
post_number: 2,
|
|
|
|
username: "regularuser"
|
|
|
|
});
|
|
|
|
|
|
|
|
const post3 = store.createRecord("post", {
|
|
|
|
id: 102,
|
|
|
|
post_number: 3,
|
|
|
|
username: "ignoreduser"
|
|
|
|
});
|
|
|
|
|
|
|
|
var stub = sandbox
|
|
|
|
.stub(postStream, "findPostsByIds")
|
|
|
|
.returns(Promise.resolve([post2]));
|
|
|
|
|
|
|
|
await postStream.triggerNewPostInStream(101);
|
2019-07-25 18:29:10 +08:00
|
|
|
assert.equal(
|
|
|
|
postStream.posts.length,
|
|
|
|
2,
|
|
|
|
"it added the regular post to the posts"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
postStream.get("stream.length"),
|
|
|
|
2,
|
|
|
|
"it added the regular post to the stream"
|
|
|
|
);
|
2019-06-11 19:07:14 +08:00
|
|
|
|
|
|
|
stub.restore();
|
|
|
|
sandbox.stub(postStream, "findPostsByIds").returns(Promise.resolve([post3]));
|
|
|
|
|
2019-07-25 18:29:10 +08:00
|
|
|
await postStream.triggerNewPostInStream(102);
|
|
|
|
assert.equal(
|
|
|
|
postStream.posts.length,
|
|
|
|
2,
|
|
|
|
"it does not add the ignored post to the posts"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
postStream.stream.length,
|
|
|
|
2,
|
|
|
|
"it does not add the ignored post to the stream"
|
|
|
|
);
|
2019-06-11 19:07:14 +08:00
|
|
|
});
|
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
QUnit.test("postsWithPlaceholders", async assert => {
|
2015-12-02 06:27:32 +08:00
|
|
|
const postStream = buildStream(4964, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
2018-06-15 23:03:24 +08:00
|
|
|
const postsWithPlaceholders = postStream.get("postsWithPlaceholders");
|
2015-12-02 06:27:32 +08:00
|
|
|
const store = postStream.store;
|
|
|
|
|
2020-03-07 06:49:28 +08:00
|
|
|
const testProxy = ArrayProxy.create({ content: postsWithPlaceholders });
|
2015-12-05 04:59:46 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
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 });
|
2015-12-02 06:27:32 +08:00
|
|
|
|
|
|
|
postStream.appendPost(p1);
|
|
|
|
postStream.appendPost(p2);
|
|
|
|
postStream.appendPost(p3);
|
|
|
|
|
|
|
|
// Test enumerable and array access
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(postsWithPlaceholders.get("length"), 3);
|
|
|
|
assert.equal(testProxy.get("length"), 3);
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(postsWithPlaceholders.nextObject(0), p1);
|
|
|
|
assert.equal(postsWithPlaceholders.objectAt(0), p1);
|
|
|
|
assert.equal(postsWithPlaceholders.nextObject(1, p1), p2);
|
|
|
|
assert.equal(postsWithPlaceholders.objectAt(1), p2);
|
|
|
|
assert.equal(postsWithPlaceholders.nextObject(2, p2), p3);
|
|
|
|
assert.equal(postsWithPlaceholders.objectAt(2), p3);
|
2015-12-02 06:27:32 +08:00
|
|
|
|
|
|
|
const promise = postStream.appendMore();
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
postsWithPlaceholders.get("length"),
|
|
|
|
8,
|
|
|
|
"we immediately have a larger placeholder window"
|
|
|
|
);
|
|
|
|
assert.equal(testProxy.get("length"), 8);
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.ok(!!postsWithPlaceholders.nextObject(3, p3));
|
|
|
|
assert.ok(!!postsWithPlaceholders.objectAt(4));
|
|
|
|
assert.ok(postsWithPlaceholders.objectAt(3) !== p4);
|
|
|
|
assert.ok(testProxy.objectAt(3) !== p4);
|
2015-12-02 06:27:32 +08:00
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
await promise;
|
|
|
|
assert.equal(postsWithPlaceholders.objectAt(3), p4);
|
|
|
|
assert.equal(
|
|
|
|
postsWithPlaceholders.get("length"),
|
|
|
|
8,
|
|
|
|
"have a larger placeholder window when loaded"
|
|
|
|
);
|
|
|
|
assert.equal(testProxy.get("length"), 8);
|
|
|
|
assert.equal(testProxy.objectAt(3), p4);
|
2015-12-02 06:27:32 +08:00
|
|
|
});
|
2018-07-11 15:41:26 +08:00
|
|
|
|
|
|
|
QUnit.test("filteredPostsCount", assert => {
|
|
|
|
const postStream = buildStream(4567, [1, 3, 4]);
|
|
|
|
|
|
|
|
assert.equal(postStream.get("filteredPostsCount"), 3);
|
|
|
|
|
|
|
|
// Megatopic
|
|
|
|
postStream.set("isMegaTopic", true);
|
|
|
|
postStream.set("topic.highest_post_number", 4);
|
|
|
|
|
|
|
|
assert.equal(postStream.get("filteredPostsCount"), 4);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("firstPostId", assert => {
|
|
|
|
const postStream = buildStream(4567, [1, 3, 4]);
|
|
|
|
|
|
|
|
assert.equal(postStream.get("firstPostId"), 1);
|
|
|
|
|
|
|
|
postStream.setProperties({
|
|
|
|
isMegaTopic: true,
|
|
|
|
firstId: 2
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(postStream.get("firstPostId"), 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("lastPostId", assert => {
|
|
|
|
const postStream = buildStream(4567, [1, 3, 4]);
|
|
|
|
|
|
|
|
assert.equal(postStream.get("lastPostId"), 4);
|
|
|
|
|
|
|
|
postStream.setProperties({
|
|
|
|
isMegaTopic: true,
|
|
|
|
lastId: 2
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(postStream.get("lastPostId"), 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("progressIndexOfPostId", assert => {
|
|
|
|
const postStream = buildStream(4567, [1, 3, 4]);
|
|
|
|
const store = createStore();
|
|
|
|
const post = store.createRecord("post", { id: 1, post_number: 5 });
|
|
|
|
|
|
|
|
assert.equal(postStream.progressIndexOfPostId(post), 1);
|
|
|
|
|
|
|
|
postStream.set("isMegaTopic", true);
|
|
|
|
|
|
|
|
assert.equal(postStream.progressIndexOfPostId(post), 5);
|
|
|
|
});
|