2015-09-03 16:55:55 -04:00
|
|
|
import TopicTrackingState from "discourse/models/topic-tracking-state";
|
2015-09-20 20:36:20 -04:00
|
|
|
import createStore from "helpers/create-store";
|
2019-11-08 13:30:41 -05:00
|
|
|
import Category from "discourse/models/category";
|
2020-01-17 05:02:57 -05:00
|
|
|
import { NotificationLevels } from "discourse/lib/notification-levels";
|
2014-02-10 23:28:05 -05:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.module("model:topic-tracking-state");
|
2014-02-10 23:28:05 -05:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("sync", function(assert) {
|
2015-09-03 16:55:55 -04:00
|
|
|
const state = TopicTrackingState.create();
|
2014-02-10 23:28:05 -05:00
|
|
|
state.states["t111"] = { last_read_post_number: null };
|
|
|
|
|
|
|
|
state.updateSeen(111, 7);
|
2015-09-03 16:55:55 -04:00
|
|
|
const list = {
|
|
|
|
topics: [
|
2018-06-15 11:03:24 -04:00
|
|
|
{
|
2014-02-10 23:28:05 -05:00
|
|
|
highest_post_number: null,
|
|
|
|
id: 111,
|
|
|
|
unread: 10,
|
|
|
|
new_posts: 10
|
2018-06-15 11:03:24 -04:00
|
|
|
}
|
|
|
|
]
|
2015-09-03 16:55:55 -04:00
|
|
|
};
|
2014-02-10 23:28:05 -05:00
|
|
|
|
|
|
|
state.sync(list, "new");
|
2015-09-03 16:55:55 -04:00
|
|
|
assert.equal(
|
|
|
|
list.topics.length,
|
|
|
|
0,
|
|
|
|
"expect new topic to be removed as it was seen"
|
|
|
|
);
|
2014-02-10 23:28:05 -05:00
|
|
|
});
|
2015-09-20 20:36:20 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("subscribe to category", function(assert) {
|
2015-09-20 20:36:20 -04:00
|
|
|
const store = createStore();
|
|
|
|
const darth = store.createRecord("category", { id: 1, slug: "darth" }),
|
|
|
|
luke = store.createRecord("category", {
|
|
|
|
id: 2,
|
|
|
|
slug: "luke",
|
|
|
|
parentCategory: darth
|
|
|
|
}),
|
|
|
|
categoryList = [darth, luke];
|
|
|
|
|
2019-11-08 13:30:41 -05:00
|
|
|
sandbox.stub(Category, "list").returns(categoryList);
|
2015-09-20 20:36:20 -04:00
|
|
|
|
|
|
|
const state = TopicTrackingState.create();
|
|
|
|
|
2019-12-16 16:33:38 -05:00
|
|
|
state.trackIncoming("c/darth/1/l/latest");
|
2015-09-20 20:36:20 -04:00
|
|
|
|
|
|
|
state.notify({
|
|
|
|
message_type: "new_topic",
|
|
|
|
topic_id: 1,
|
|
|
|
payload: { category_id: 2, topic_id: 1 }
|
|
|
|
});
|
|
|
|
state.notify({
|
|
|
|
message_type: "new_topic",
|
|
|
|
topic_id: 2,
|
|
|
|
payload: { category_id: 3, topic_id: 2 }
|
|
|
|
});
|
|
|
|
state.notify({
|
|
|
|
message_type: "new_topic",
|
|
|
|
topic_id: 3,
|
|
|
|
payload: { category_id: 1, topic_id: 3 }
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
state.get("incomingCount"),
|
|
|
|
2,
|
|
|
|
"expect to properly track incoming for category"
|
|
|
|
);
|
|
|
|
|
|
|
|
state.resetTracking();
|
2019-12-16 16:33:38 -05:00
|
|
|
state.trackIncoming("c/darth/luke/2/l/latest");
|
2015-09-20 20:36:20 -04:00
|
|
|
|
|
|
|
state.notify({
|
|
|
|
message_type: "new_topic",
|
|
|
|
topic_id: 1,
|
|
|
|
payload: { category_id: 2, topic_id: 1 }
|
|
|
|
});
|
|
|
|
state.notify({
|
|
|
|
message_type: "new_topic",
|
|
|
|
topic_id: 2,
|
|
|
|
payload: { category_id: 3, topic_id: 2 }
|
|
|
|
});
|
|
|
|
state.notify({
|
|
|
|
message_type: "new_topic",
|
|
|
|
topic_id: 3,
|
|
|
|
payload: { category_id: 1, topic_id: 3 }
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
state.get("incomingCount"),
|
|
|
|
1,
|
|
|
|
"expect to properly track incoming for subcategory"
|
|
|
|
);
|
2017-06-14 13:57:58 -04:00
|
|
|
});
|
2020-01-17 05:02:57 -05:00
|
|
|
|
|
|
|
QUnit.test("getSubCategoryIds", assert => {
|
|
|
|
const store = createStore();
|
|
|
|
const foo = store.createRecord("category", { id: 1, slug: "foo" });
|
|
|
|
const bar = store.createRecord("category", {
|
|
|
|
id: 2,
|
|
|
|
slug: "bar",
|
|
|
|
parent_category_id: foo.id
|
|
|
|
});
|
|
|
|
const baz = store.createRecord("category", {
|
|
|
|
id: 3,
|
|
|
|
slug: "baz",
|
|
|
|
parent_category_id: bar.id
|
|
|
|
});
|
|
|
|
sandbox.stub(Category, "list").returns([foo, bar, baz]);
|
|
|
|
|
|
|
|
const state = TopicTrackingState.create();
|
|
|
|
assert.deepEqual(Array.from(state.getSubCategoryIds(1)), [1, 2, 3]);
|
|
|
|
assert.deepEqual(Array.from(state.getSubCategoryIds(2)), [2, 3]);
|
|
|
|
assert.deepEqual(Array.from(state.getSubCategoryIds(3)), [3]);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("countNew", assert => {
|
|
|
|
const store = createStore();
|
|
|
|
const foo = store.createRecord("category", { id: 1, slug: "foo" });
|
|
|
|
const bar = store.createRecord("category", {
|
|
|
|
id: 2,
|
|
|
|
slug: "bar",
|
|
|
|
parent_category_id: foo.id
|
|
|
|
});
|
|
|
|
const baz = store.createRecord("category", {
|
|
|
|
id: 3,
|
|
|
|
slug: "baz",
|
|
|
|
parent_category_id: bar.id
|
|
|
|
});
|
|
|
|
sandbox.stub(Category, "list").returns([foo, bar, baz]);
|
|
|
|
|
|
|
|
const state = TopicTrackingState.create();
|
|
|
|
|
|
|
|
assert.equal(state.countNew(1), 0);
|
|
|
|
assert.equal(state.countNew(2), 0);
|
|
|
|
assert.equal(state.countNew(3), 0);
|
|
|
|
|
|
|
|
state.states["t112"] = {
|
|
|
|
last_read_post_number: null,
|
|
|
|
id: 112,
|
|
|
|
notification_level: NotificationLevels.TRACKING,
|
|
|
|
category_id: 2
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.equal(state.countNew(1), 1);
|
|
|
|
assert.equal(state.countNew(2), 1);
|
|
|
|
assert.equal(state.countNew(3), 0);
|
|
|
|
|
|
|
|
state.states["t113"] = {
|
|
|
|
last_read_post_number: null,
|
|
|
|
id: 113,
|
|
|
|
notification_level: NotificationLevels.TRACKING,
|
|
|
|
category_id: 3
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.equal(state.countNew(1), 2);
|
|
|
|
assert.equal(state.countNew(2), 2);
|
|
|
|
assert.equal(state.countNew(3), 1);
|
|
|
|
|
|
|
|
state.states["t111"] = {
|
|
|
|
last_read_post_number: null,
|
|
|
|
id: 111,
|
|
|
|
notification_level: NotificationLevels.TRACKING,
|
|
|
|
category_id: 1
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.equal(state.countNew(1), 3);
|
|
|
|
assert.equal(state.countNew(2), 2);
|
|
|
|
assert.equal(state.countNew(3), 1);
|
|
|
|
});
|