FIX: Setting computed properties in tests

This commit is contained in:
Robin Ward 2020-07-16 11:57:50 -04:00
parent b630fccbd7
commit 46e5372c06
2 changed files with 33 additions and 48 deletions

View File

@ -208,12 +208,12 @@ export default Controller.extend(bufferedProperty("model"), {
_smallActionPostIds() { _smallActionPostIds() {
const smallActionsPostIds = new Set(); const smallActionsPostIds = new Set();
const posts = this.get("model.postStream.posts"); const posts = this.get("model.postStream.posts");
if (posts) { if (posts && this.site) {
const small_action = this.site.get("post_types.small_action"); const smallAction = this.site.get("post_types.small_action");
const whisper = this.site.get("post_types.whisper"); const whisper = this.site.get("post_types.whisper");
posts.forEach(post => { posts.forEach(post => {
if ( if (
post.post_type === small_action || post.post_type === smallAction ||
(!post.cooked && post.post_type === whisper) (!post.cooked && post.post_type === whisper)
) { ) {
smallActionsPostIds.add(post.id); smallActionsPostIds.add(post.id);

View File

@ -1,7 +1,6 @@
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import { next } from "@ember/runloop"; import { next } from "@ember/runloop";
import Topic from "discourse/models/topic"; import Topic from "discourse/models/topic";
import PostStream from "discourse/models/post-stream";
import { Placeholder } from "discourse/lib/posts-with-placeholders"; import { Placeholder } from "discourse/lib/posts-with-placeholders";
import User from "discourse/models/user"; import User from "discourse/models/user";
import { Promise } from "rsvp"; import { Promise } from "rsvp";
@ -18,6 +17,12 @@ moduleFor("controller:topic", "controller:topic", {
} }
}); });
function topicWithStream(streamDetails) {
let topic = Topic.create();
topic.get("postStream").setProperties(streamDetails);
return topic;
}
QUnit.test("editTopic", function(assert) { QUnit.test("editTopic", function(assert) {
const model = Topic.create(); const model = Topic.create();
const controller = this.subject({ model }); const controller = this.subject({ model });
@ -94,8 +99,7 @@ QUnit.test("toggleMultiSelect", function(assert) {
}); });
QUnit.test("selectedPosts", function(assert) { QUnit.test("selectedPosts", function(assert) {
const postStream = { posts: [{ id: 1 }, { id: 2 }, { id: 3 }] }; let model = topicWithStream({ posts: [{ id: 1 }, { id: 2 }, { id: 3 }] });
const model = Topic.create({ postStream });
const controller = this.subject({ model }); const controller = this.subject({ model });
controller.set("selectedPostIds", [1, 2, 42]); controller.set("selectedPostIds", [1, 2, 42]);
@ -112,8 +116,7 @@ QUnit.test("selectedPosts", function(assert) {
}); });
QUnit.test("selectedAllPosts", function(assert) { QUnit.test("selectedAllPosts", function(assert) {
const postStream = { stream: [1, 2, 3] }; let model = topicWithStream({ stream: [1, 2, 3] });
const model = Topic.create({ postStream });
const controller = this.subject({ model }); const controller = this.subject({ model });
controller.set("selectedPostIds", [1, 2]); controller.set("selectedPostIds", [1, 2]);
@ -143,16 +146,14 @@ QUnit.test("selectedAllPosts", function(assert) {
}); });
QUnit.test("selectedPostsUsername", function(assert) { QUnit.test("selectedPostsUsername", function(assert) {
const postStream = { let model = topicWithStream({
posts: [ posts: [
{ id: 1, username: "gary" }, { id: 1, username: "gary" },
{ id: 2, username: "gary" }, { id: 2, username: "gary" },
{ id: 3, username: "lili" } { id: 3, username: "lili" }
], ],
stream: [1, 2, 3] stream: [1, 2, 3]
}; });
const model = Topic.create({ postStream });
const controller = this.subject({ model }); const controller = this.subject({ model });
const selectedPostIds = controller.get("selectedPostIds"); const selectedPostIds = controller.get("selectedPostIds");
@ -218,20 +219,19 @@ QUnit.test("showSelectedPostsAtBottom", function(assert) {
}); });
QUnit.test("canDeleteSelected", function(assert) { QUnit.test("canDeleteSelected", function(assert) {
const postStream = { const currentUser = User.create({ admin: false });
this.registry.register("current-user:main", currentUser, {
instantiate: false
});
this.registry.injection("controller", "currentUser", "current-user:main");
let model = topicWithStream({
posts: [ posts: [
{ id: 1, can_delete: false }, { id: 1, can_delete: false },
{ id: 2, can_delete: true }, { id: 2, can_delete: true },
{ id: 3, can_delete: true } { id: 3, can_delete: true }
], ],
stream: [1, 2, 3] stream: [1, 2, 3]
};
const currentUser = User.create({ admin: false });
this.registry.register("current-user:main", currentUser, {
instantiate: false
}); });
this.registry.injection("controller", "currentUser", "current-user:main");
const model = Topic.create({ postStream });
const controller = this.subject({ model }); const controller = this.subject({ model });
const selectedPostIds = controller.get("selectedPostIds"); const selectedPostIds = controller.get("selectedPostIds");
@ -270,19 +270,15 @@ QUnit.test("canDeleteSelected", function(assert) {
}); });
QUnit.test("Can split/merge topic", function(assert) { QUnit.test("Can split/merge topic", function(assert) {
const postStream = { let model = topicWithStream({
posts: [ posts: [
{ id: 1, post_number: 1, post_type: 1 }, { id: 1, post_number: 1, post_type: 1 },
{ id: 2, post_number: 2, post_type: 4 }, { id: 2, post_number: 2, post_type: 4 },
{ id: 3, post_number: 3, post_type: 1 } { id: 3, post_number: 3, post_type: 1 }
], ],
stream: [1, 2, 3] stream: [1, 2, 3]
};
const model = Topic.create({
postStream,
details: { can_move_posts: false }
}); });
model.set("details.can_move_posts", false);
const controller = this.subject({ model }); const controller = this.subject({ model });
const selectedPostIds = controller.get("selectedPostIds"); const selectedPostIds = controller.get("selectedPostIds");
@ -325,15 +321,14 @@ QUnit.test("canChangeOwner", function(assert) {
}); });
this.registry.injection("controller", "currentUser", "current-user:main"); this.registry.injection("controller", "currentUser", "current-user:main");
const postStream = { let model = topicWithStream({
posts: [ posts: [
{ id: 1, username: "gary" }, { id: 1, username: "gary" },
{ id: 2, username: "lili" } { id: 2, username: "lili" }
], ],
stream: [1, 2] stream: [1, 2]
}; });
model.set("currentUser", { admin: false });
const model = Topic.create({ postStream, currentUser: { admin: false } });
const controller = this.subject({ model }); const controller = this.subject({ model });
const selectedPostIds = controller.get("selectedPostIds"); const selectedPostIds = controller.get("selectedPostIds");
@ -362,7 +357,7 @@ QUnit.test("canChangeOwner", function(assert) {
}); });
QUnit.test("canMergePosts", function(assert) { QUnit.test("canMergePosts", function(assert) {
const postStream = { let model = topicWithStream({
posts: [ posts: [
{ id: 1, username: "gary", can_delete: true }, { id: 1, username: "gary", can_delete: true },
{ id: 2, username: "lili", can_delete: true }, { id: 2, username: "lili", can_delete: true },
@ -370,9 +365,7 @@ QUnit.test("canMergePosts", function(assert) {
{ id: 4, username: "gary", can_delete: true } { id: 4, username: "gary", can_delete: true }
], ],
stream: [1, 2, 3] stream: [1, 2, 3]
}; });
const model = Topic.create({ postStream });
const controller = this.subject({ model }); const controller = this.subject({ model });
const selectedPostIds = controller.get("selectedPostIds"); const selectedPostIds = controller.get("selectedPostIds");
@ -411,8 +404,7 @@ QUnit.test("canMergePosts", function(assert) {
}); });
QUnit.test("Select/deselect all", function(assert) { QUnit.test("Select/deselect all", function(assert) {
const postStream = { stream: [1, 2, 3] }; let model = topicWithStream({ stream: [1, 2, 3] });
const model = Topic.create({ postStream });
const controller = this.subject({ model }); const controller = this.subject({ model });
assert.equal( assert.equal(
@ -425,7 +417,7 @@ QUnit.test("Select/deselect all", function(assert) {
assert.equal( assert.equal(
controller.get("selectedPostsCount"), controller.get("selectedPostsCount"),
postStream.stream.length, 3,
"calling 'selectAll' selects all posts" "calling 'selectAll' selects all posts"
); );
@ -478,17 +470,14 @@ QUnit.test("selectBelow", function(assert) {
const site = EmberObject.create({ const site = EmberObject.create({
post_types: { small_action: 3, whisper: 4 } post_types: { small_action: 3, whisper: 4 }
}); });
let model = topicWithStream({
const postStream = {
stream: [1, 2, 3, 4, 5, 6, 7, 8], stream: [1, 2, 3, 4, 5, 6, 7, 8],
posts: [ posts: [
{ id: 5, cooked: "whisper post", post_type: 4 }, { id: 5, cooked: "whisper post", post_type: 4 },
{ id: 6, cooked: "a small action", post_type: 3 }, { id: 6, cooked: "a small action", post_type: 3 },
{ id: 7, cooked: "", post_type: 4 } { id: 7, cooked: "", post_type: 4 }
] ]
}; });
const model = Topic.create({ postStream });
const controller = this.subject({ site, model }); const controller = this.subject({ site, model });
let selectedPostIds = controller.get("selectedPostIds"); let selectedPostIds = controller.get("selectedPostIds");
@ -503,11 +492,9 @@ QUnit.test("selectBelow", function(assert) {
}); });
QUnit.test("topVisibleChanged", function(assert) { QUnit.test("topVisibleChanged", function(assert) {
const postStream = PostStream.create({ let model = topicWithStream({
posts: [{ id: 1 }] posts: [{ id: 1 }]
}); });
const model = Topic.create({ postStream });
const controller = this.subject({ model }); const controller = this.subject({ model });
const placeholder = new Placeholder("post-placeholder"); const placeholder = new Placeholder("post-placeholder");
@ -539,13 +526,11 @@ QUnit.test(
} }
}); });
const postStream = EmberObject.create({ const currentUser = EmberObject.create({ moderator: true });
let model = topicWithStream({
stream: [2, 3, 4], stream: [2, 3, 4],
posts: [post, { id: 3 }, { id: 4 }] posts: [post, { id: 3 }, { id: 4 }]
}); });
const currentUser = EmberObject.create({ moderator: true });
const model = Topic.create({ postStream });
const controller = this.subject({ model, currentUser }); const controller = this.subject({ model, currentUser });
const done = assert.async(); const done = assert.async();