DEV: Modernize model tests (#19104)

Uses `module()` instead of `discourseModule()`, native getters instead of `.get()`, fixes some assertions, uses the store instead of creating models directly
This commit is contained in:
Jarek Radosz 2022-11-18 20:36:32 +01:00 committed by GitHub
parent 44e27ddab8
commit 4f12fd0339
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 763 additions and 716 deletions

View File

@ -1,12 +1,18 @@
import { module, test } from "qunit";
import Badge from "discourse/models/badge";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Model | badge", function (hooks) {
setupTest(hooks);
module("Unit | Model | badge", function () {
test("newBadge", function (assert) {
const badge1 = Badge.create({ name: "New Badge" }),
badge2 = Badge.create({ id: 1, name: "Old Badge" });
assert.ok(badge1.get("newBadge"), "badges without ids are new");
assert.ok(!badge2.get("newBadge"), "badges with ids are not new");
const store = getOwner(this).lookup("service:store");
const badge1 = store.createRecord("badge", { name: "New Badge" });
const badge2 = store.createRecord("badge", { id: 1, name: "Old Badge" });
assert.ok(badge1.newBadge, "badges without ids are new");
assert.ok(!badge2.newBadge, "badges with ids are not new");
});
test("createFromJson array", function (assert) {
@ -20,13 +26,9 @@ module("Unit | Model | badge", function () {
const badges = Badge.createFromJson(badgesJson);
assert.ok(Array.isArray(badges), "returns an array");
assert.strictEqual(badges[0].name, "Badge 1", "badge details are set");
assert.strictEqual(
badges[0].get("name"),
"Badge 1",
"badge details are set"
);
assert.strictEqual(
badges[0].get("badge_type.name"),
badges[0].badge_type.name,
"Silver 1",
"badge_type reference is set"
);
@ -44,15 +46,16 @@ module("Unit | Model | badge", function () {
});
test("updateFromJson", function (assert) {
const badgeJson = {
const store = getOwner(this).lookup("service:store");
const badge = store.createRecord("badge", { name: "Badge 1" });
badge.updateFromJson({
badge_types: [{ id: 6, name: "Silver 1" }],
badge: { id: 1126, name: "Badge 1", description: null, badge_type_id: 6 },
};
const badge = Badge.create({ name: "Badge 1" });
badge.updateFromJson(badgeJson);
assert.strictEqual(badge.get("id"), 1126, "id is set");
});
assert.strictEqual(badge.id, 1126, "id is set");
assert.strictEqual(
badge.get("badge_type.name"),
badge.badge_type.name,
"Silver 1",
"badge_type reference is set"
);
@ -60,23 +63,25 @@ module("Unit | Model | badge", function () {
test("save", function (assert) {
assert.expect(0);
const badge = Badge.create({
const store = getOwner(this).lookup("service:store");
const badge = store.createRecord("badge", {
name: "New Badge",
description: "This is a new badge.",
badge_type_id: 1,
});
return badge.save(["name", "description", "badge_type_id"]);
badge.save(["name", "description", "badge_type_id"]);
});
test("destroy", function (assert) {
assert.expect(0);
const badge = Badge.create({
const store = getOwner(this).lookup("service:store");
const badge = store.createRecord("badge", {
name: "New Badge",
description: "This is a new badge.",
badge_type_id: 1,
});
badge.destroy();
badge.set("id", 3);
return badge.destroy();
badge.destroy();
});
});

View File

@ -1,9 +1,13 @@
import { module, test } from "qunit";
import Bookmark from "discourse/models/bookmark";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Model | bookmark", function (hooks) {
setupTest(hooks);
module("Unit | Model | bookmark", function () {
test("topicForList - Topic bookmarkable", function (assert) {
let bookmark = Bookmark.create({
const store = getOwner(this).lookup("service:store");
const bookmark = store.createRecord("bookmark", {
id: 1,
bookmarkable_type: "Topic",
bookmarkable_id: 999,
@ -22,7 +26,8 @@ module("Unit | Model | bookmark", function () {
});
test("topicForList - Post bookmarkable", function (assert) {
let bookmark = Bookmark.create({
const store = getOwner(this).lookup("service:store");
const bookmark = store.createRecord("bookmark", {
id: 1,
bookmarkable_type: "Post",
bookmarkable_id: 999,

View File

@ -225,44 +225,44 @@ module("Unit | Model | category", function (hooks) {
test("minimumRequiredTags", function (assert) {
const store = getOwner(this).lookup("service:store");
let foo = store.createRecord("category", {
const foo = store.createRecord("category", {
id: 1,
slug: "foo",
required_tag_groups: [{ name: "bar", min_count: 2 }],
});
assert.equal(foo.minimumRequiredTags, 2);
assert.strictEqual(foo.minimumRequiredTags, 2);
foo = store.createRecord("category", {
const bar = store.createRecord("category", {
id: 2,
slug: "foo",
slug: "bar",
});
assert.equal(foo.minimumRequiredTags, null);
assert.strictEqual(bar.minimumRequiredTags, null);
foo = store.createRecord("category", {
const baz = store.createRecord("category", {
id: 3,
slug: "foo",
slug: "baz",
minimum_required_tags: 0,
});
assert.equal(foo.minimumRequiredTags, null);
assert.strictEqual(baz.minimumRequiredTags, null);
foo = store.createRecord("category", {
const qux = store.createRecord("category", {
id: 4,
slug: "foo",
slug: "qux",
minimum_required_tags: 2,
});
assert.equal(foo.minimumRequiredTags, 2);
assert.strictEqual(qux.minimumRequiredTags, 2);
foo = store.createRecord("category", {
const quux = store.createRecord("category", {
id: 5,
slug: "foo",
slug: "quux",
required_tag_groups: [],
});
assert.equal(foo.minimumRequiredTags, null);
assert.strictEqual(quux.minimumRequiredTags, null);
});
test("search with category name", function (assert) {
@ -329,7 +329,7 @@ module("Unit | Model | category", function (hooks) {
const child_category1 = store.createRecord("category", {
id: 3,
name: "term start",
parent_category_id: category1.get("id"),
parent_category_id: category1.id,
}),
read_restricted_category = store.createRecord("category", {
id: 4,

View File

@ -6,7 +6,6 @@ import {
} from "discourse/models/composer";
import { currentUser } from "discourse/tests/helpers/qunit-helpers";
import AppEvents from "discourse/services/app-events";
import EmberObject from "@ember/object";
import { module, test } from "qunit";
import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
@ -34,7 +33,7 @@ module("Unit | Model | composer", function (hooks) {
test("replyLength", function (assert) {
const replyLength = function (val, expectedLength) {
const composer = createComposer.call(this, { reply: val });
assert.strictEqual(composer.get("replyLength"), expectedLength);
assert.strictEqual(composer.replyLength, expectedLength);
};
replyLength("basic reply", 11, "basic reply length");
@ -67,27 +66,21 @@ module("Unit | Model | composer", function (hooks) {
test("missingReplyCharacters", function (assert) {
this.siteSettings.min_first_post_length = 40;
const missingReplyCharacters = function (
val,
isPM,
isFirstPost,
expected,
message
) {
let action = REPLY;
if (isPM) {
action = PRIVATE_MESSAGE;
}
function missingReplyCharacters(val, isPM, isFirstPost, expected, message) {
let action;
if (isFirstPost) {
action = CREATE_TOPIC;
} else if (isPM) {
action = PRIVATE_MESSAGE;
} else {
action = REPLY;
}
const composer = createComposer.call(this, { reply: val, action });
assert.strictEqual(
composer.get("missingReplyCharacters"),
expected,
message
);
};
assert.strictEqual(composer.missingReplyCharacters, expected, message);
}
missingReplyCharacters(
"hi",
@ -123,7 +116,7 @@ module("Unit | Model | composer", function (hooks) {
});
assert.strictEqual(
composer.get("missingReplyCharacters"),
composer.missingReplyCharacters,
0,
"don't require any post content"
);
@ -135,11 +128,7 @@ module("Unit | Model | composer", function (hooks) {
title: val,
action: isPM ? PRIVATE_MESSAGE : REPLY,
});
assert.strictEqual(
composer.get("missingTitleCharacters"),
expected,
message
);
assert.strictEqual(composer.missingTitleCharacters, expected, message);
};
missingTitleCharacters(
@ -158,7 +147,7 @@ module("Unit | Model | composer", function (hooks) {
test("replyDirty", function (assert) {
const composer = createComposer();
assert.ok(!composer.get("replyDirty"), "by default it's false");
assert.ok(!composer.replyDirty, "by default it's false");
composer.setProperties({
originalText: "hello",
@ -166,27 +155,23 @@ module("Unit | Model | composer", function (hooks) {
});
assert.ok(
!composer.get("replyDirty"),
!composer.replyDirty,
"it's false when the originalText is the same as the reply"
);
composer.set("reply", "hello world");
assert.ok(composer.get("replyDirty"), "it's true when the reply changes");
assert.ok(composer.replyDirty, "it's true when the reply changes");
});
test("appendText", function (assert) {
const composer = createComposer();
assert.blank(composer.get("reply"), "the reply is blank by default");
assert.blank(composer.reply, "the reply is blank by default");
composer.appendText("hello");
assert.strictEqual(
composer.get("reply"),
"hello",
"it appends text to nothing"
);
assert.strictEqual(composer.reply, "hello", "it appends text to nothing");
composer.appendText(" world");
assert.strictEqual(
composer.get("reply"),
composer.reply,
"hello world",
"it appends text to existing text"
);
@ -195,43 +180,39 @@ module("Unit | Model | composer", function (hooks) {
composer.appendText("a\n\n\n\nb");
composer.appendText("c", 3, { block: true });
assert.strictEqual(composer.get("reply"), "a\n\nc\n\nb");
assert.strictEqual(composer.reply, "a\n\nc\n\nb");
composer.clearState();
composer.appendText("ab");
composer.appendText("c", 1, { block: true });
assert.strictEqual(composer.get("reply"), "a\n\nc\n\nb");
assert.strictEqual(composer.reply, "a\n\nc\n\nb");
composer.clearState();
composer.appendText("\nab");
composer.appendText("c", 0, { block: true });
assert.strictEqual(composer.get("reply"), "c\n\nab");
assert.strictEqual(composer.reply, "c\n\nab");
});
test("prependText", function (assert) {
const composer = createComposer();
assert.blank(composer.get("reply"), "the reply is blank by default");
assert.blank(composer.reply, "the reply is blank by default");
composer.prependText("hello");
assert.strictEqual(
composer.get("reply"),
"hello",
"it prepends text to nothing"
);
assert.strictEqual(composer.reply, "hello", "it prepends text to nothing");
composer.prependText("world ");
assert.strictEqual(
composer.get("reply"),
composer.reply,
"world hello",
"it prepends text to existing text"
);
composer.prependText("before new line", { new_line: true });
assert.strictEqual(
composer.get("reply"),
composer.reply,
"before new line\n\nworld hello",
"it prepends text with new line to existing text"
);
@ -243,13 +224,13 @@ module("Unit | Model | composer", function (hooks) {
const composer = createComposer();
composer.set("title", "asdf");
assert.ok(!composer.get("titleLengthValid"), "short titles are not valid");
assert.ok(!composer.titleLengthValid, "short titles are not valid");
composer.set("title", "this is a long title");
assert.ok(!composer.get("titleLengthValid"), "long titles are not valid");
assert.ok(!composer.titleLengthValid, "long titles are not valid");
composer.set("title", "just right");
assert.ok(composer.get("titleLengthValid"), "in the range is okay");
assert.ok(composer.titleLengthValid, "in the range is okay");
});
test("Title length for private messages", function (assert) {
@ -258,38 +239,40 @@ module("Unit | Model | composer", function (hooks) {
const composer = createComposer.call(this, { action: PRIVATE_MESSAGE });
composer.set("title", "asdf");
assert.ok(!composer.get("titleLengthValid"), "short titles are not valid");
assert.ok(!composer.titleLengthValid, "short titles are not valid");
composer.set("title", "this is a long title");
assert.ok(!composer.get("titleLengthValid"), "long titles are not valid");
assert.ok(!composer.titleLengthValid, "long titles are not valid");
composer.set("title", "just right");
assert.ok(composer.get("titleLengthValid"), "in the range is okay");
assert.ok(composer.titleLengthValid, "in the range is okay");
});
test("Post length for private messages with non human users", function (assert) {
const store = getOwner(this).lookup("service:store");
const topic = store.createRecord("topic", { pm_with_non_human_user: true });
const composer = createComposer.call(this, {
topic: EmberObject.create({ pm_with_non_human_user: true }),
topic,
});
assert.strictEqual(composer.get("minimumPostLength"), 1);
assert.strictEqual(composer.minimumPostLength, 1);
});
test("editingFirstPost", function (assert) {
const composer = createComposer();
assert.ok(!composer.get("editingFirstPost"), "it's false by default");
assert.ok(!composer.editingFirstPost, "it's false by default");
const store = getOwner(this).lookup("service:store");
const post = store.createRecord("post", { id: 123, post_number: 2 });
composer.setProperties({ post, action: EDIT });
assert.ok(
!composer.get("editingFirstPost"),
!composer.editingFirstPost,
"it's false when not editing the first post"
);
post.set("post_number", 1);
assert.ok(
composer.get("editingFirstPost"),
composer.editingFirstPost,
"it's true when editing the first post"
);
});
@ -305,31 +288,31 @@ module("Unit | Model | composer", function (hooks) {
composer.clearState();
assert.blank(composer.get("originalText"));
assert.blank(composer.get("reply"));
assert.blank(composer.get("post"));
assert.blank(composer.get("title"));
assert.blank(composer.originalText);
assert.blank(composer.reply);
assert.blank(composer.post);
assert.blank(composer.title);
});
test("initial category when uncategorized is allowed", function (assert) {
this.siteSettings.allow_uncategorized_topics = true;
const composer = openComposer.call(this, {
action: CREATE_TOPIC,
draftKey: "asfd",
draftKey: "abcd",
draftSequence: 1,
});
assert.ok(!composer.get("categoryId"), "Uncategorized by default");
assert.ok(!composer.categoryId, "Uncategorized by default");
});
test("initial category when uncategorized is not allowed", function (assert) {
this.siteSettings.allow_uncategorized_topics = false;
const composer = openComposer.call(this, {
action: CREATE_TOPIC,
draftKey: "asfd",
draftKey: "abcd",
draftSequence: 1,
});
assert.ok(
!composer.get("categoryId"),
!composer.categoryId,
"Uncategorized by default. Must choose a category."
);
});
@ -340,19 +323,19 @@ module("Unit | Model | composer", function (hooks) {
const newComposer = function () {
return openComposer.call(this, {
action: REPLY,
draftKey: "asfd",
draftKey: "abcd",
draftSequence: 1,
quote,
});
};
assert.strictEqual(
newComposer().get("originalText"),
newComposer().originalText,
quote,
"originalText is the quote"
);
assert.strictEqual(
newComposer().get("replyDirty"),
newComposer().replyDirty,
false,
"replyDirty is initially false with a quote"
);
@ -372,17 +355,17 @@ module("Unit | Model | composer", function (hooks) {
composer.setProperties({ post, action: EDIT });
composer.set("title", "asdf");
assert.ok(composer.get("titleLengthValid"), "admins can use short titles");
assert.ok(composer.titleLengthValid, "admins can use short titles");
composer.set("title", "this is a long title");
assert.ok(composer.get("titleLengthValid"), "admins can use long titles");
assert.ok(composer.titleLengthValid, "admins can use long titles");
composer.set("title", "just right");
assert.ok(composer.get("titleLengthValid"), "in the range is okay");
assert.ok(composer.titleLengthValid, "in the range is okay");
composer.set("title", "");
assert.ok(
!composer.get("titleLengthValid"),
!composer.titleLengthValid,
"admins must set title to at least 1 character"
);
});
@ -391,14 +374,14 @@ module("Unit | Model | composer", function (hooks) {
this.siteSettings.topic_featured_link_enabled = false;
let composer = createComposer.call(this, { action: CREATE_TOPIC });
assert.strictEqual(
composer.get("titlePlaceholder"),
composer.titlePlaceholder,
"composer.title_placeholder",
"placeholder for normal topic"
);
composer = createComposer.call(this, { action: PRIVATE_MESSAGE });
assert.strictEqual(
composer.get("titlePlaceholder"),
composer.titlePlaceholder,
"composer.title_placeholder",
"placeholder for private message"
);
@ -407,14 +390,14 @@ module("Unit | Model | composer", function (hooks) {
composer = createComposer.call(this, { action: CREATE_TOPIC });
assert.strictEqual(
composer.get("titlePlaceholder"),
composer.titlePlaceholder,
"composer.title_or_link_placeholder",
"placeholder invites you to paste a link"
);
composer = createComposer.call(this, { action: PRIVATE_MESSAGE });
assert.strictEqual(
composer.get("titlePlaceholder"),
composer.titlePlaceholder,
"composer.title_placeholder",
"placeholder for private message with topic links enabled"
);
@ -423,24 +406,24 @@ module("Unit | Model | composer", function (hooks) {
test("allows featured link before choosing a category", function (assert) {
this.siteSettings.topic_featured_link_enabled = true;
this.siteSettings.allow_uncategorized_topics = false;
let composer = createComposer.call(this, { action: CREATE_TOPIC });
const composer = createComposer.call(this, { action: CREATE_TOPIC });
assert.strictEqual(
composer.get("titlePlaceholder"),
composer.titlePlaceholder,
"composer.title_or_link_placeholder",
"placeholder invites you to paste a link"
);
assert.ok(composer.get("canEditTopicFeaturedLink"), "can paste link");
assert.ok(composer.canEditTopicFeaturedLink, "can paste link");
});
test("targetRecipientsArray contains types", function (assert) {
let composer = createComposer.call(this, {
const composer = createComposer.call(this, {
targetRecipients: "test,codinghorror,staff,foo@bar.com",
});
assert.ok(composer.targetRecipientsArray, [
{ type: "group", name: "test" },
{ type: "user", name: "codinghorror" },
{ type: "group", name: "staff" },
{ type: "email", name: "foo@bar.com" },
assert.deepEqual(composer.targetRecipientsArray, [
{ name: "test", type: "user" },
{ name: "codinghorror", type: "user" },
{ name: "staff", type: "group" },
{ name: "foo@bar.com", type: "email" },
]);
});
});

View File

@ -1,10 +1,17 @@
import { module, test } from "qunit";
import EmailLog from "admin/models/email-log";
import { setPrefix } from "discourse-common/lib/get-url";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Model | email-log", function (hooks) {
setupTest(hooks);
module("Unit | Model | email-log", function () {
test("create", function (assert) {
assert.ok(EmailLog.create(), "it can be created without arguments");
const store = getOwner(this).lookup("service:store");
assert.ok(
store.createRecord("email-log"),
"it can be created without arguments"
);
});
test("subfolder support", function (assert) {
@ -25,9 +32,11 @@ module("Unit | Model | email-log", function () {
"/forum/letter_avatar_proxy/v2/letter/w/dfb087/{size}.png",
},
};
const emailLog = EmailLog.create(attrs);
const store = getOwner(this).lookup("service:store");
const emailLog = store.createRecord("email-log", attrs);
assert.strictEqual(
emailLog.get("post_url"),
emailLog.post_url,
"/forum/t/some-pro-tips-for-you/41/5",
"includes the subfolder in the post url"
);

View File

@ -1,12 +1,19 @@
import { module, test } from "qunit";
import Group from "discourse/models/group";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Model | group", function (hooks) {
setupTest(hooks);
module("Unit | Model | group", function () {
test("displayName", function (assert) {
const group = Group.create({ name: "test", display_name: "donkey" });
const store = getOwner(this).lookup("service:store");
const group = store.createRecord("group", {
name: "test",
display_name: "donkey",
});
assert.strictEqual(
group.get("displayName"),
group.displayName,
"donkey",
"it should return the display name"
);
@ -14,7 +21,7 @@ module("Unit | Model | group", function () {
group.set("display_name", null);
assert.strictEqual(
group.get("displayName"),
group.displayName,
"test",
"it should return the group's name"
);

View File

@ -1,8 +1,15 @@
import { module, test } from "qunit";
import Invite from "discourse/models/invite";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Model | invite", function (hooks) {
setupTest(hooks);
module("Unit | Model | invite", function () {
test("create", function (assert) {
assert.ok(Invite.create(), "it can be created without arguments");
const store = getOwner(this).lookup("service:store");
assert.ok(
store.createRecord("invite"),
"it can be created without arguments"
);
});
});

View File

@ -24,11 +24,7 @@ module("Unit | Model | nav-item", function (hooks) {
assert.expect(4);
function href(text, opts, expected, label) {
assert.strictEqual(
NavItem.fromText(text, opts).get("href"),
expected,
label
);
assert.strictEqual(NavItem.fromText(text, opts).href, expected, label);
}
href("latest", {}, "/latest", "latest");
@ -46,18 +42,17 @@ module("Unit | Model | nav-item", function (hooks) {
const store = getOwner(this).lookup("service:store");
const navItem = store.createRecord("nav-item", { name: "new" });
assert.strictEqual(navItem.get("count"), 0, "it has no count by default");
assert.strictEqual(navItem.count, 0, "it has no count by default");
const tracker = navItem.get("topicTrackingState");
tracker.modifyState("t1", {
navItem.topicTrackingState.modifyState("t1", {
topic_id: 1,
last_read_post_number: null,
created_in_new_period: true,
});
tracker.incrementMessageCount();
navItem.topicTrackingState.incrementMessageCount();
assert.strictEqual(
navItem.get("count"),
navItem.count,
1,
"it updates when a new message arrives"
);

View File

@ -19,9 +19,13 @@ module("Unit | Model | pending-post", function (hooks) {
// pending-post initializer performs async operations
await settled();
assert.equal(post.postUrl, "topic-url", "topic_url is aliased to postUrl");
assert.equal(post.truncated, false, "truncated is always false");
assert.equal(
assert.strictEqual(
post.postUrl,
"topic-url",
"topic_url is aliased to postUrl"
);
assert.false(post.truncated, "truncated is always false");
assert.strictEqual(
post.userUrl,
"/u/username",
"it returns user URL from the username"
@ -42,7 +46,7 @@ module("Unit | Model | pending-post", function (hooks) {
// pending-post initializer performs async operations
await settled();
assert.equal(
assert.strictEqual(
post.expandedExcerpt.string,
"<p><strong>bold text</strong></p>"
);

View File

@ -34,41 +34,30 @@ module("Unit | Model | post-stream", function (hooks) {
test("defaults", function (assert) {
const postStream = buildStream.call(this, 1234);
assert.blank(postStream.posts, "there are no posts in a stream by default");
assert.ok(!postStream.get("loaded"), "it has never loaded");
assert.present(postStream.get("topic"));
assert.ok(!postStream.loaded, "it has never loaded");
assert.present(postStream.topic);
});
test("appending posts", function (assert) {
const postStream = buildStream.call(this, 4567, [1, 3, 4]);
const store = getOwner(this).lookup("service:store");
assert.strictEqual(
postStream.get("lastPostId"),
4,
"the last post id is 4"
);
assert.strictEqual(postStream.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.strictEqual(
postStream.get("posts.length"),
0,
"it has no posts initially"
);
assert.ok(!postStream.hasPosts, "there are no posts by default");
assert.ok(!postStream.firstPostPresent, "the first post is not loaded");
assert.ok(!postStream.loadedAllPosts, "the last post is not loaded");
assert.strictEqual(postStream.posts.length, 0, "it has no posts initially");
postStream.appendPost(
store.createRecord("post", { id: 2, post_number: 2 })
);
assert.ok(
!postStream.get("firstPostPresent"),
!postStream.firstPostPresent,
"the first post is still not loaded"
);
assert.strictEqual(
postStream.get("posts.length"),
postStream.posts.length,
1,
"it has one post in the stream"
);
@ -76,13 +65,10 @@ module("Unit | Model | post-stream", function (hooks) {
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.ok(!postStream.firstPostPresent, "the first post is still loaded");
assert.ok(postStream.loadedAllPosts, "the last post is now loaded");
assert.strictEqual(
postStream.get("posts.length"),
postStream.posts.length,
2,
"it has two posts in the stream"
);
@ -91,7 +77,7 @@ module("Unit | Model | post-stream", function (hooks) {
store.createRecord("post", { id: 4, post_number: 4 })
);
assert.strictEqual(
postStream.get("posts.length"),
postStream.posts.length,
2,
"it will not add the same post with id twice"
);
@ -99,13 +85,13 @@ module("Unit | Model | post-stream", function (hooks) {
const stagedPost = store.createRecord("post", { raw: "incomplete post" });
postStream.appendPost(stagedPost);
assert.strictEqual(
postStream.get("posts.length"),
postStream.posts.length,
3,
"it can handle posts without ids"
);
postStream.appendPost(stagedPost);
assert.strictEqual(
postStream.get("posts.length"),
postStream.posts.length,
3,
"it won't add the same post without an id twice"
);
@ -113,11 +99,11 @@ module("Unit | Model | post-stream", function (hooks) {
// change the stream
postStream.set("stream", [1, 2, 4]);
assert.ok(
!postStream.get("firstPostPresent"),
!postStream.firstPostPresent,
"the first post no longer loaded since the stream changed."
);
assert.ok(
postStream.get("loadedAllPosts"),
postStream.loadedAllPosts,
"the last post is still the last post in the new stream"
);
});
@ -199,14 +185,10 @@ module("Unit | Model | post-stream", function (hooks) {
extra_property: 12,
});
assert.strictEqual(
postStream.get("posts.length"),
1,
"it loaded the posts"
);
assert.strictEqual(postStream.posts.length, 1, "it loaded the posts");
assert.containsInstance(postStream.posts, Post);
assert.strictEqual(postStream.get("extra_property"), 12);
assert.strictEqual(postStream.extra_property, 12);
});
test("removePosts", function (assert) {
@ -223,11 +205,11 @@ module("Unit | Model | post-stream", function (hooks) {
// Removing nothing does nothing
postStream.removePosts();
assert.strictEqual(postStream.get("posts.length"), 3);
assert.strictEqual(postStream.posts.length, 3);
postStream.removePosts([p1, p3]);
assert.strictEqual(postStream.get("posts.length"), 1);
assert.deepEqual(postStream.get("stream"), [2]);
assert.strictEqual(postStream.posts.length, 1);
assert.deepEqual(postStream.stream, [2]);
});
test("cancelFilter", function (assert) {
@ -237,12 +219,12 @@ module("Unit | Model | post-stream", function (hooks) {
postStream.set("filter", "summary");
postStream.cancelFilter();
assert.ok(!postStream.get("summary"), "summary is cancelled");
assert.ok(!postStream.summary, "summary is cancelled");
postStream.filterParticipant(participant);
postStream.cancelFilter();
assert.blank(
postStream.get("userFilters"),
postStream.userFilters,
"cancelling the filters clears the userFilters"
);
});
@ -280,7 +262,7 @@ module("Unit | Model | post-stream", function (hooks) {
test("fillGapBefore", function (assert) {
const postStream = buildStream.call(this, 1234, [60]);
sinon.stub(postStream, "findPostsByIds").resolves([]);
let post = postStream.store.createRecord("post", {
const post = postStream.store.createRecord("post", {
id: 60,
post_number: 60,
});
@ -302,19 +284,19 @@ module("Unit | Model | post-stream", function (hooks) {
sinon.stub(postStream, "refresh").resolves();
assert.strictEqual(
postStream.get("userFilters.length"),
postStream.userFilters.length,
0,
"by default no participants are toggled"
);
postStream.filterParticipant(participant.username);
assert.ok(
postStream.get("userFilters").includes("eviltrout"),
postStream.userFilters.includes("eviltrout"),
"eviltrout is in the filters"
);
postStream.cancelFilter();
assert.blank(postStream.get("userFilters"), "cancelFilter clears");
assert.blank(postStream.userFilters, "cancelFilter clears");
});
test("filterReplies", function (assert) {
@ -328,21 +310,21 @@ module("Unit | Model | post-stream", function (hooks) {
sinon.stub(postStream, "refresh").resolves();
assert.strictEqual(
postStream.get("filterRepliesToPostNumber"),
postStream.filterRepliesToPostNumber,
false,
"by default no replies are filtered"
);
postStream.filterReplies(3, 2);
assert.strictEqual(
postStream.get("filterRepliesToPostNumber"),
postStream.filterRepliesToPostNumber,
3,
"postNumber is in the filters"
);
postStream.cancelFilter();
assert.strictEqual(
postStream.get("filterRepliesToPostNumber"),
postStream.filterRepliesToPostNumber,
false,
"cancelFilter clears"
);
@ -359,24 +341,16 @@ module("Unit | Model | post-stream", function (hooks) {
sinon.stub(postStream, "refresh").resolves();
assert.strictEqual(
postStream.get("filterUpwardsPostID"),
postStream.filterUpwardsPostID,
false,
"by default filter is false"
);
postStream.filterUpwards(2);
assert.strictEqual(
postStream.get("filterUpwardsPostID"),
2,
"filter is set"
);
assert.strictEqual(postStream.filterUpwardsPostID, 2, "filter is set");
postStream.cancelFilter();
assert.strictEqual(
postStream.get("filterUpwardsPostID"),
false,
"filter cleared"
);
assert.strictEqual(postStream.filterUpwardsPostID, false, "filter cleared");
});
test("streamFilters", function (assert) {
@ -384,26 +358,23 @@ module("Unit | Model | post-stream", function (hooks) {
sinon.stub(postStream, "refresh").resolves();
assert.deepEqual(
postStream.get("streamFilters"),
postStream.streamFilters,
{},
"there are no postFilters by default"
);
assert.ok(
postStream.get("hasNoFilters"),
"there are no filters by default"
);
assert.ok(postStream.hasNoFilters, "there are no filters by default");
postStream.set("filter", "summary");
assert.deepEqual(
postStream.get("streamFilters"),
postStream.streamFilters,
{ filter: "summary" },
"postFilters contains the summary flag"
);
assert.ok(!postStream.get("hasNoFilters"), "now there are filters present");
assert.ok(!postStream.hasNoFilters, "now there are filters present");
postStream.filterParticipant(participant.username);
assert.deepEqual(
postStream.get("streamFilters"),
postStream.streamFilters,
{
username_filters: "eviltrout",
},
@ -412,7 +383,7 @@ module("Unit | Model | post-stream", function (hooks) {
postStream.filterUpwards(2);
assert.deepEqual(
postStream.get("streamFilters"),
postStream.streamFilters,
{
filter_upwards_post_id: 2,
},
@ -421,7 +392,7 @@ module("Unit | Model | post-stream", function (hooks) {
postStream.filterReplies(1);
assert.deepEqual(
postStream.get("streamFilters"),
postStream.streamFilters,
{
replies_to_post_number: 1,
},
@ -430,19 +401,19 @@ module("Unit | Model | post-stream", function (hooks) {
});
test("loading", function (assert) {
let postStream = buildStream.call(this, 1234);
assert.ok(!postStream.get("loading"), "we're not loading by default");
const postStream = buildStream.call(this, 1234);
assert.ok(!postStream.loading, "we're not loading by default");
postStream.set("loadingAbove", true);
assert.ok(postStream.get("loading"), "we're loading if loading above");
assert.ok(postStream.loading, "we're loading if loading above");
postStream = buildStream.call(this, 1234);
postStream.set("loadingBelow", true);
assert.ok(postStream.get("loading"), "we're loading if loading below");
const postStream2 = buildStream.call(this, 1234);
postStream2.set("loadingBelow", true);
assert.ok(postStream2.loading, "we're loading if loading below");
postStream = buildStream.call(this, 1234);
postStream.set("loadingFilter", true);
assert.ok(postStream.get("loading"), "we're loading if loading a filter");
const postStream3 = buildStream.call(this, 1234);
postStream3.set("loadingFilter", true);
assert.ok(postStream3.loading, "we're loading if loading a filter");
});
test("nextWindow", function (assert) {
@ -453,27 +424,27 @@ module("Unit | Model | post-stream", function (hooks) {
);
assert.blank(
postStream.get("nextWindow"),
postStream.nextWindow,
"With no posts loaded, the window is blank"
);
postStream.updateFromJson({ posts: [{ id: 1 }, { id: 2 }] });
assert.deepEqual(
postStream.get("nextWindow"),
postStream.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"),
postStream.nextWindow,
[14, 15, 16],
"Boundary check: stop at the end."
);
postStream.updateFromJson({ posts: [{ id: 16 }] });
assert.blank(
postStream.get("nextWindow"),
postStream.nextWindow,
"Once we've seen everything there's nothing to load."
);
});
@ -486,27 +457,27 @@ module("Unit | Model | post-stream", function (hooks) {
);
assert.blank(
postStream.get("previousWindow"),
postStream.previousWindow,
"With no posts loaded, the window is blank"
);
postStream.updateFromJson({ posts: [{ id: 11 }, { id: 13 }] });
assert.deepEqual(
postStream.get("previousWindow"),
postStream.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"),
postStream.previousWindow,
[1, 2],
"Boundary check: stop at the beginning."
);
postStream.updateFromJson({ posts: [{ id: 1 }] });
assert.blank(
postStream.get("previousWindow"),
postStream.previousWindow,
"Once we've seen everything there's nothing to load."
);
});
@ -521,18 +492,18 @@ module("Unit | Model | post-stream", function (hooks) {
});
assert.blank(
postStream.get("topic.highest_post_number"),
postStream.topic.highest_post_number,
"it has no highest post number yet"
);
let stored = postStream.storePost(post);
const stored = postStream.storePost(post);
assert.strictEqual(post, stored, "it returns the post it stored");
assert.strictEqual(
post.get("topic"),
postStream.get("topic"),
post.topic,
postStream.topic,
"it creates the topic reference properly"
);
assert.strictEqual(
postStream.get("topic.highest_post_number"),
postStream.topic.highest_post_number,
100,
"it set the highest post number"
);
@ -549,14 +520,14 @@ module("Unit | Model | post-stream", function (hooks) {
"it returns the previously stored post instead to avoid dupes"
);
assert.strictEqual(
storedDupe.get("raw"),
storedDupe.raw,
"updated value",
"it updates the previously stored post"
);
const postWithoutId = store.createRecord("post", { raw: "hello world" });
stored = postStream.storePost(postWithoutId);
assert.strictEqual(stored, postWithoutId, "it returns the same post back");
const stored2 = postStream.storePost(postWithoutId);
assert.strictEqual(stored2, postWithoutId, "it returns the same post back");
});
test("identity map", async function (assert) {
@ -581,7 +552,7 @@ module("Unit | Model | post-stream", function (hooks) {
const result = await postStream.findPostsByIds([1, 2, 3]);
assert.strictEqual(result.length, 3);
assert.strictEqual(result.objectAt(0), p1);
assert.strictEqual(result.objectAt(1).get("post_number"), 2);
assert.strictEqual(result.objectAt(1).post_number, 2);
assert.strictEqual(result.objectAt(2), p3);
});
@ -661,7 +632,7 @@ module("Unit | Model | post-stream", function (hooks) {
});
postStream.appendPost(original);
assert.strictEqual(
postStream.get("lastAppended"),
postStream.lastAppended,
original,
"the original post is lastAppended"
);
@ -676,7 +647,7 @@ module("Unit | Model | post-stream", function (hooks) {
topic_id: 10101,
});
const topic = postStream.get("topic");
const topic = postStream.topic;
topic.setProperties({
posts_count: 1,
highest_post_number: 1,
@ -686,72 +657,57 @@ module("Unit | Model | post-stream", function (hooks) {
const result = postStream.stagePost(stagedPost, user);
assert.strictEqual(result, "staged", "it returns staged");
assert.strictEqual(
topic.get("highest_post_number"),
topic.highest_post_number,
2,
"it updates the highest_post_number"
);
assert.ok(
postStream.get("loading"),
postStream.loading,
"it is loading while the post is being staged"
);
assert.strictEqual(
postStream.get("lastAppended"),
postStream.lastAppended,
original,
"it doesn't consider staged posts as the lastAppended"
);
assert.strictEqual(topic.posts_count, 2, "it increases the post count");
assert.present(topic.last_posted_at, "it updates last_posted_at");
assert.strictEqual(
topic.get("posts_count"),
2,
"it increases the post count"
);
assert.present(topic.get("last_posted_at"), "it updates last_posted_at");
assert.strictEqual(
topic.get("details.last_poster"),
topic.details.last_poster,
user,
"it changes the last poster"
);
assert.strictEqual(
stagedPost.get("topic"),
stagedPost.topic,
topic,
"it assigns the topic reference"
);
assert.strictEqual(
stagedPost.get("post_number"),
stagedPost.post_number,
2,
"it is assigned the probable post_number"
);
assert.present(
stagedPost.get("created_at"),
"it is assigned a created date"
);
assert.present(stagedPost.created_at, "it is assigned a created date");
assert.ok(
postStream.posts.includes(stagedPost),
"the post is added to the stream"
);
assert.strictEqual(
stagedPost.get("id"),
-1,
"the post has a magical -1 id"
);
assert.strictEqual(stagedPost.id, -1, "the post has a magical -1 id");
// Undoing a created post (there was an error)
postStream.undoPost(stagedPost);
assert.ok(!postStream.get("loading"), "it is no longer loading");
assert.ok(!postStream.loading, "it is no longer loading");
assert.strictEqual(
topic.get("highest_post_number"),
topic.highest_post_number,
1,
"it reverts the highest_post_number"
);
assert.strictEqual(topic.posts_count, 1, "it reverts the post count");
assert.strictEqual(
topic.get("posts_count"),
1,
"it reverts the post count"
);
assert.strictEqual(
postStream.get("filteredPostsCount"),
postStream.filteredPostsCount,
1,
"it retains the filteredPostsCount"
);
@ -760,7 +716,7 @@ module("Unit | Model | post-stream", function (hooks) {
"the post is removed from the stream"
);
assert.strictEqual(
postStream.get("lastAppended"),
postStream.lastAppended,
original,
"it doesn't consider undid post lastAppended"
);
@ -777,7 +733,7 @@ module("Unit | Model | post-stream", function (hooks) {
});
postStream.appendPost(original);
assert.strictEqual(
postStream.get("lastAppended"),
postStream.lastAppended,
original,
"the original post is lastAppended"
);
@ -792,27 +748,27 @@ module("Unit | Model | post-stream", function (hooks) {
topic_id: 10101,
});
const topic = postStream.get("topic");
const topic = postStream.topic;
topic.set("posts_count", 1);
// Stage the new post in the stream
let result = postStream.stagePost(stagedPost, user);
const result = postStream.stagePost(stagedPost, user);
assert.strictEqual(result, "staged", "it returns staged");
assert.ok(
postStream.get("loading"),
postStream.loading,
"it is loading while the post is being staged"
);
stagedPost.setProperties({ id: 1234, raw: "different raw value" });
result = postStream.stagePost(stagedPost, user);
const result2 = postStream.stagePost(stagedPost, user);
assert.strictEqual(
result,
result2,
"alreadyStaging",
"you can't stage a post while it is currently staging"
);
assert.strictEqual(
postStream.get("lastAppended"),
postStream.lastAppended,
original,
"staging a post doesn't change the lastAppended"
);
@ -822,27 +778,27 @@ module("Unit | Model | post-stream", function (hooks) {
postStream.posts.includes(stagedPost),
"the post is still in the stream"
);
assert.ok(!postStream.get("loading"), "it is no longer loading");
assert.ok(!postStream.loading, "it is no longer loading");
assert.strictEqual(
postStream.get("filteredPostsCount"),
postStream.filteredPostsCount,
2,
"it increases the filteredPostsCount"
);
const found = postStream.findLoadedPost(stagedPost.get("id"));
const found = postStream.findLoadedPost(stagedPost.id);
assert.present(found, "the post is in the identity map");
assert.ok(
postStream.posts.includes(stagedPost),
"the post is in the stream"
);
assert.strictEqual(
found.get("raw"),
found.raw,
"different raw value",
"it also updated the value in the stream"
);
assert.strictEqual(
postStream.get("lastAppended"),
postStream.lastAppended,
found,
"committing a post changes lastAppended"
);
@ -861,11 +817,11 @@ module("Unit | Model | post-stream", function (hooks) {
store.createRecord("post", { id: 1, post_number: 1 })
);
postStream.appendPost(postWithoutId);
assert.ok(!postStream.get("loadedAllPosts"), "the last post is not loaded");
assert.ok(!postStream.loadedAllPosts, "the last post is not loaded");
postWithoutId.set("id", 2);
assert.ok(
postStream.get("loadedAllPosts"),
postStream.loadedAllPosts,
"the last post is loaded now that the post has an id"
);
});
@ -885,7 +841,7 @@ module("Unit | Model | post-stream", function (hooks) {
});
assert.strictEqual(
postStream.get("postsWithPlaceholders.length"),
postStream.postsWithPlaceholders.length,
4,
"it should return the right length"
);
@ -893,7 +849,7 @@ module("Unit | Model | post-stream", function (hooks) {
await postStream.triggerRecoveredPost(4);
assert.strictEqual(
postStream.get("postsWithPlaceholders.length"),
postStream.postsWithPlaceholders.length,
5,
"it should return the right length"
);
@ -917,7 +873,7 @@ module("Unit | Model | post-stream", function (hooks) {
postStream.stagePost(stagedPost, user);
assert.strictEqual(
postStream.get("filteredPostsCount"),
postStream.filteredPostsCount,
0,
"it has no filteredPostsCount yet"
);
@ -925,15 +881,11 @@ module("Unit | Model | post-stream", function (hooks) {
sinon.stub(postStream, "appendMore");
postStream.triggerNewPostsInStream([123]);
assert.strictEqual(
postStream.get("filteredPostsCount"),
1,
"it added the post"
);
assert.strictEqual(postStream.filteredPostsCount, 1, "it added the post");
postStream.commitPost(stagedPost);
assert.strictEqual(
postStream.get("filteredPostsCount"),
postStream.filteredPostsCount,
1,
"it does not add the same post twice"
);
@ -967,7 +919,7 @@ module("Unit | Model | post-stream", function (hooks) {
username: "ignored-user",
});
let stub = sinon.stub(postStream, "findPostsByIds").resolves([post2]);
const stub = sinon.stub(postStream, "findPostsByIds").resolves([post2]);
await postStream.triggerNewPostsInStream([101]);
assert.strictEqual(
@ -976,7 +928,7 @@ module("Unit | Model | post-stream", function (hooks) {
"it added the regular post to the posts"
);
assert.strictEqual(
postStream.get("stream.length"),
postStream.stream.length,
2,
"it added the regular post to the stream"
);
@ -1003,7 +955,7 @@ module("Unit | Model | post-stream", function (hooks) {
4964,
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
);
const postsWithPlaceholders = postStream.get("postsWithPlaceholders");
const postsWithPlaceholders = postStream.postsWithPlaceholders;
const store = getOwner(this).lookup("service:store");
const testProxy = ArrayProxy.create({ content: postsWithPlaceholders });
@ -1018,8 +970,8 @@ module("Unit | Model | post-stream", function (hooks) {
postStream.appendPost(p3);
// Test enumerable and array access
assert.strictEqual(postsWithPlaceholders.get("length"), 3);
assert.strictEqual(testProxy.get("length"), 3);
assert.strictEqual(postsWithPlaceholders.length, 3);
assert.strictEqual(testProxy.length, 3);
assert.strictEqual(postsWithPlaceholders.nextObject(0), p1);
assert.strictEqual(postsWithPlaceholders.objectAt(0), p1);
assert.strictEqual(postsWithPlaceholders.nextObject(1, p1), p2);
@ -1029,11 +981,11 @@ module("Unit | Model | post-stream", function (hooks) {
const promise = postStream.appendMore();
assert.strictEqual(
postsWithPlaceholders.get("length"),
postsWithPlaceholders.length,
8,
"we immediately have a larger placeholder window"
);
assert.strictEqual(testProxy.get("length"), 8);
assert.strictEqual(testProxy.length, 8);
assert.ok(!!postsWithPlaceholders.nextObject(3, p3));
assert.ok(!!postsWithPlaceholders.objectAt(4));
assert.ok(postsWithPlaceholders.objectAt(3) !== p4);
@ -1042,37 +994,37 @@ module("Unit | Model | post-stream", function (hooks) {
await promise;
assert.strictEqual(postsWithPlaceholders.objectAt(3), p4);
assert.strictEqual(
postsWithPlaceholders.get("length"),
postsWithPlaceholders.length,
8,
"have a larger placeholder window when loaded"
);
assert.strictEqual(testProxy.get("length"), 8);
assert.strictEqual(testProxy.length, 8);
assert.strictEqual(testProxy.objectAt(3), p4);
});
test("filteredPostsCount", function (assert) {
const postStream = buildStream.call(this, 4567, [1, 3, 4]);
assert.strictEqual(postStream.get("filteredPostsCount"), 3);
assert.strictEqual(postStream.filteredPostsCount, 3);
// Megatopic
postStream.set("isMegaTopic", true);
postStream.set("topic.highest_post_number", 4);
assert.strictEqual(postStream.get("filteredPostsCount"), 4);
assert.strictEqual(postStream.filteredPostsCount, 4);
});
test("lastPostId", function (assert) {
const postStream = buildStream.call(this, 4567, [1, 3, 4]);
assert.strictEqual(postStream.get("lastPostId"), 4);
assert.strictEqual(postStream.lastPostId, 4);
postStream.setProperties({
isMegaTopic: true,
lastId: 2,
});
assert.strictEqual(postStream.get("lastPostId"), 2);
assert.strictEqual(postStream.lastPostId, 2);
});
test("progressIndexOfPostId", function (assert) {

View File

@ -1,32 +1,34 @@
import { module, test } from "qunit";
import User from "discourse/models/user";
import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
module("Unit | Model | post", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
this.store = getOwner(this).lookup("service:store");
});
test("defaults", function (assert) {
const post = this.store.createRecord("post", { id: 1 });
assert.blank(post.get("deleted_at"), "it has no deleted_at by default");
assert.blank(post.get("deleted_by"), "there is no deleted_by by default");
assert.blank(post.deleted_at, "it has no deleted_at by default");
assert.blank(post.deleted_by, "there is no deleted_by by default");
});
test("new_user", function (assert) {
const post = this.store.createRecord("post", { trust_level: 0 });
assert.ok(post.get("new_user"), "post is from a new user");
assert.ok(post.new_user, "post is from a new user");
post.set("trust_level", 1);
assert.ok(!post.get("new_user"), "post is no longer from a new user");
assert.ok(!post.new_user, "post is no longer from a new user");
});
test("firstPost", function (assert) {
const post = this.store.createRecord("post", { post_number: 1 });
assert.ok(post.get("firstPost"), "it's the first post");
assert.ok(post.firstPost, "it's the first post");
post.set("post_number", 10);
assert.ok(!post.get("firstPost"), "post is no longer the first post");
assert.ok(!post.firstPost, "post is no longer the first post");
});
test("updateFromPost", function (assert) {
@ -41,11 +43,14 @@ module("Unit | Model | post", function (hooks) {
})
);
assert.strictEqual(post.get("raw"), "different raw", "raw field updated");
assert.strictEqual(post.raw, "different raw", "raw field updated");
});
test("destroy by staff", async function (assert) {
const user = User.create({ username: "staff", moderator: true });
const user = this.store.createRecord("user", {
username: "staff",
moderator: true,
});
const post = this.store.createRecord("post", {
id: 1,
can_delete: true,
@ -55,28 +60,22 @@ module("Unit | Model | post", function (hooks) {
await post.destroy(user);
assert.present(post.get("deleted_at"), "it has a `deleted_at` field.");
assert.present(post.deleted_at, "it has a `deleted_at` field.");
assert.strictEqual(
post.get("deleted_by"),
post.deleted_by,
user,
"it has the user in the `deleted_by` field"
);
await post.recover();
assert.blank(
post.get("deleted_at"),
"it clears `deleted_at` when recovering"
);
assert.blank(
post.get("deleted_by"),
"it clears `deleted_by` when recovering"
);
assert.blank(post.deleted_at, "it clears `deleted_at` when recovering");
assert.blank(post.deleted_by, "it clears `deleted_by` when recovering");
});
test("destroy by non-staff", async function (assert) {
const originalCooked = "this is the original cooked value";
const user = User.create({ username: "evil trout" });
const user = this.store.createRecord("user", { username: "evil trout" });
const post = this.store.createRecord("post", {
id: 1,
can_delete: true,
@ -88,13 +87,10 @@ module("Unit | Model | post", function (hooks) {
await post.destroy(user);
assert.ok(
!post.get("can_delete"),
!post.can_delete,
"the post can't be deleted again in this session"
);
assert.ok(
post.get("cooked") !== originalCooked,
"the cooked content changed"
);
assert.strictEqual(post.get("version"), 2, "the version number increased");
assert.ok(post.cooked !== originalCooked, "the cooked content changed");
assert.strictEqual(post.version, 2, "the version number increased");
});
});

View File

@ -1,12 +1,10 @@
import { test } from "qunit";
import { module, test } from "qunit";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
import {
discourseModule,
publishToMessageBus,
} from "discourse/tests/helpers/qunit-helpers";
import { publishToMessageBus } from "discourse/tests/helpers/qunit-helpers";
import MessageBus from "message-bus-client";
import PrivateMessageTopicTrackingState from "discourse/services/pm-topic-tracking-state";
import User from "discourse/models/user";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
function setupPretender() {
pretender.get(`/u/test/private-message-topic-tracking-state`, () => {
@ -22,49 +20,47 @@ function setupPretender() {
});
}
discourseModule(
"Unit | Model | private-message-topic-tracking-state",
function (hooks) {
let pmTopicTrackingState;
module("Unit | Model | private-message-topic-tracking-state", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
pmTopicTrackingState = PrivateMessageTopicTrackingState.create({
messageBus: MessageBus,
currentUser: User.create({ id: 77889, username: "test" }),
});
test("modifying state calls onStateChange callbacks", function (assert) {
const store = getOwner(this).lookup("service:store");
const pmTopicTrackingState = PrivateMessageTopicTrackingState.create({
messageBus: MessageBus,
currentUser: store.createRecord("user", { id: 77889, username: "test" }),
});
test("modifying state calls onStateChange callbacks", function (assert) {
let callbackCalled = false;
let callbackCalled = false;
pmTopicTrackingState.onStateChange("testing", () => {
callbackCalled = true;
});
pmTopicTrackingState.set("isTracking", true);
pmTopicTrackingState.removeTopics([]);
assert.ok(callbackCalled);
pmTopicTrackingState.onStateChange("testing", () => {
callbackCalled = true;
});
}
);
discourseModule(
pmTopicTrackingState.set("isTracking", true);
pmTopicTrackingState.removeTopics([]);
assert.ok(callbackCalled);
});
});
module(
"Unit | Model | private-message-topic-tracking-state | processing new_topic message",
function (hooks) {
let pmTopicTrackingState;
hooks.beforeEach(async function () {
setupPretender();
pmTopicTrackingState = PrivateMessageTopicTrackingState.create({
messageBus: MessageBus,
currentUser: User.create({ id: 77889, username: "test" }),
});
await pmTopicTrackingState.startTracking();
});
setupTest(hooks);
test("modifies the topic state only if the topic was not created by the current user", async function (assert) {
let payload = {
setupPretender();
const store = getOwner(this).lookup("service:store");
const pmTopicTrackingState = PrivateMessageTopicTrackingState.create({
messageBus: MessageBus,
currentUser: store.createRecord("user", {
id: 77889,
username: "test",
}),
});
await pmTopicTrackingState.startTracking();
const payload = {
last_read_post_number: null,
highest_post_number: 1,
group_ids: [],
@ -84,7 +80,7 @@ discourseModule(
"the new topic created by a different user is loaded into state"
);
payload = {
const payload2 = {
last_read_post_number: null,
highest_post_number: 1,
group_ids: [],
@ -95,10 +91,10 @@ discourseModule(
{
message_type: "new_topic",
topic_id: 4400,
payload,
payload: payload2,
}
);
assert.deepEqual(
assert.strictEqual(
pmTopicTrackingState.findState(4400),
undefined,
"the new topic created by the current user is not loaded into state"
@ -107,22 +103,24 @@ discourseModule(
}
);
discourseModule(
module(
"Unit | Model | private-message-topic-tracking-state | processing unread message",
function (hooks) {
let pmTopicTrackingState;
hooks.beforeEach(async function () {
setupPretender();
pmTopicTrackingState = PrivateMessageTopicTrackingState.create({
messageBus: MessageBus,
currentUser: User.create({ id: 77889, username: "test" }),
});
await pmTopicTrackingState.startTracking();
});
setupTest(hooks);
test("modifies the last_read_post_number and highest_post_number", async function (assert) {
let payload = {
setupPretender();
const store = getOwner(this).lookup("service:store");
const pmTopicTrackingState = PrivateMessageTopicTrackingState.create({
messageBus: MessageBus,
currentUser: store.createRecord("user", {
id: 77889,
username: "test",
}),
});
await pmTopicTrackingState.startTracking();
const payload = {
last_read_post_number: 12,
highest_post_number: 13,
notification_level: 3,
@ -138,19 +136,19 @@ discourseModule(
}
);
let state = pmTopicTrackingState.findState(123);
assert.deepEqual(
const state = pmTopicTrackingState.findState(123);
assert.strictEqual(
state.highest_post_number,
13,
"the unread payload triggered by a different user creating a new post updates the state with the correct highest_post_number"
);
assert.deepEqual(
assert.strictEqual(
state.last_read_post_number,
12,
"the unread payload triggered by a different user creating a new post updates the state with the correct last_read_post_number"
);
payload = {
const payload2 = {
last_read_post_number: 14,
highest_post_number: 14,
notification_level: 3,
@ -162,18 +160,18 @@ discourseModule(
{
message_type: "unread",
topic_id: 123,
payload,
payload: payload2,
}
);
state = pmTopicTrackingState.findState(123);
assert.deepEqual(
state.highest_post_number,
const state2 = pmTopicTrackingState.findState(123);
assert.strictEqual(
state2.highest_post_number,
14,
"the unread payload triggered by the current user creating a new post updates the state with the correct highest_post_number"
);
assert.deepEqual(
state.last_read_post_number,
assert.strictEqual(
state2.last_read_post_number,
14,
"the unread payload triggered by the current user creating a new post updates the state with the correct last_read_post_number"
);

View File

@ -1,9 +1,12 @@
import { module, test } from "qunit";
import Report from "admin/models/report";
import { setPrefix } from "discourse-common/lib/get-url";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
function reportWithData(data) {
return Report.create({
const store = getOwner(this).lookup("service:store");
return store.createRecord("report", {
type: "topics",
data: data.map((val, index) => {
return {
@ -14,19 +17,24 @@ function reportWithData(data) {
});
}
module("Unit | Model | report", function () {
test("counts", function (assert) {
const report = reportWithData([5, 4, 3, 2, 1, 100, 99, 98, 1000]);
module("Unit | Model | report", function (hooks) {
setupTest(hooks);
assert.strictEqual(report.get("todayCount"), 5);
assert.strictEqual(report.get("yesterdayCount"), 4);
test("counts", function (assert) {
const report = reportWithData.call(
this,
[5, 4, 3, 2, 1, 100, 99, 98, 1000]
);
assert.strictEqual(report.todayCount, 5);
assert.strictEqual(report.yesterdayCount, 4);
assert.strictEqual(
report.valueFor(2, 4),
6,
"adds the values for the given range of days, inclusive"
);
assert.strictEqual(
report.get("lastSevenDaysCount"),
report.lastSevenDaysCount,
307,
"sums 7 days excluding today"
);
@ -40,7 +48,7 @@ module("Unit | Model | report", function () {
});
test("percentChangeString", function (assert) {
const report = reportWithData([]);
const report = reportWithData.call(this, []);
assert.strictEqual(
report.percentChangeString(5, 8),
@ -73,184 +81,225 @@ module("Unit | Model | report", function () {
});
test("yesterdayCountTitle with valid values", function (assert) {
const title = reportWithData([6, 8, 5, 2, 1]).get("yesterdayCountTitle");
const title = reportWithData.call(
this,
[6, 8, 5, 2, 1]
).yesterdayCountTitle;
assert.ok(title.includes("+60%"));
assert.ok(title.match(/Was 5/));
});
test("yesterdayCountTitle when two days ago was 0", function (assert) {
const title = reportWithData([6, 8, 0, 2, 1]).get("yesterdayCountTitle");
const title = reportWithData.call(
this,
[6, 8, 0, 2, 1]
).yesterdayCountTitle;
assert.ok(!title.includes("%"));
assert.ok(title.match(/Was 0/));
});
test("sevenDaysCountTitle", function (assert) {
const title = reportWithData([
100, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 100, 100,
]).get("sevenDaysCountTitle");
const title = reportWithData.call(
this,
[100, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 100, 100]
).sevenDaysCountTitle;
assert.ok(title.match(/-50%/));
assert.ok(title.match(/Was 14/));
});
test("thirtyDaysCountTitle", function (assert) {
let report = reportWithData([5, 5, 5, 5]);
const report = reportWithData.call(this, [5, 5, 5, 5]);
report.set("prev30Days", 10);
let title = report.get("thirtyDaysCountTitle");
assert.ok(title.includes("+50%"));
assert.ok(title.match(/Was 10/));
assert.ok(report.thirtyDaysCountTitle.includes("+50%"));
assert.ok(report.thirtyDaysCountTitle.match(/Was 10/));
report = reportWithData([5, 5, 5, 5]);
report.set("prev_period", 20);
title = report.get("thirtyDaysCountTitle");
const report2 = reportWithData.call(this, [5, 5, 5, 5]);
report2.set("prev_period", 20);
assert.ok(title.includes("-25%"));
assert.ok(title.match(/Was 20/));
assert.ok(report2.thirtyDaysCountTitle.includes("-25%"));
assert.ok(report2.thirtyDaysCountTitle.match(/Was 20/));
});
test("sevenDaysTrend", function (assert) {
let report;
let trend;
report = reportWithData([0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
trend = report.get("sevenDaysTrend");
assert.ok(trend === "no-change");
report = reportWithData.call(
this,
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
);
trend = report.sevenDaysTrend;
assert.strictEqual(trend, "no-change");
report = reportWithData([0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]);
trend = report.get("sevenDaysTrend");
assert.ok(trend === "high-trending-up");
report = reportWithData.call(
this,
[0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
);
trend = report.sevenDaysTrend;
assert.strictEqual(trend, "high-trending-up");
report = reportWithData([0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]);
trend = report.get("sevenDaysTrend");
assert.ok(trend === "trending-up");
report = reportWithData.call(
this,
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]
);
trend = report.sevenDaysTrend;
assert.strictEqual(trend, "trending-up");
report = reportWithData([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]);
trend = report.get("sevenDaysTrend");
assert.ok(trend === "high-trending-down");
report = reportWithData.call(
this,
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]
);
trend = report.sevenDaysTrend;
assert.strictEqual(trend, "high-trending-down");
report = reportWithData([0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]);
trend = report.get("sevenDaysTrend");
assert.ok(trend === "trending-down");
report = reportWithData.call(
this,
[0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]
);
trend = report.sevenDaysTrend;
assert.strictEqual(trend, "trending-down");
});
test("yesterdayTrend", function (assert) {
let report;
let trend;
report = reportWithData([0, 1, 1]);
trend = report.get("yesterdayTrend");
assert.ok(trend === "no-change");
report = reportWithData.call(this, [0, 1, 1]);
trend = report.yesterdayTrend;
assert.strictEqual(trend, "no-change");
report = reportWithData([0, 1, 0]);
trend = report.get("yesterdayTrend");
assert.ok(trend === "high-trending-up");
report = reportWithData.call(this, [0, 1, 0]);
trend = report.yesterdayTrend;
assert.strictEqual(trend, "high-trending-up");
report = reportWithData([0, 1.1, 1]);
trend = report.get("yesterdayTrend");
assert.ok(trend === "trending-up");
report = reportWithData.call(this, [0, 1.1, 1]);
trend = report.yesterdayTrend;
assert.strictEqual(trend, "trending-up");
report = reportWithData([0, 0, 1]);
trend = report.get("yesterdayTrend");
assert.ok(trend === "high-trending-down");
report = reportWithData.call(this, [0, 0, 1]);
trend = report.yesterdayTrend;
assert.strictEqual(trend, "high-trending-down");
report = reportWithData([0, 1, 1.1]);
trend = report.get("yesterdayTrend");
assert.ok(trend === "trending-down");
report = reportWithData.call(this, [0, 1, 1.1]);
trend = report.yesterdayTrend;
assert.strictEqual(trend, "trending-down");
});
test("thirtyDaysTrend", function (assert) {
let report;
let trend;
report = reportWithData([
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1,
]);
report = reportWithData.call(
this,
[
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
]
);
report.set("prev30Days", 30);
trend = report.get("thirtyDaysTrend");
assert.ok(trend === "no-change");
trend = report.thirtyDaysTrend;
assert.strictEqual(trend, "no-change");
report = reportWithData([
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1,
]);
report = reportWithData.call(
this,
[
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
]
);
report.set("prev30Days", 0);
trend = report.get("thirtyDaysTrend");
assert.ok(trend === "high-trending-up");
trend = report.thirtyDaysTrend;
assert.strictEqual(trend, "high-trending-up");
report = reportWithData([
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1,
]);
report = reportWithData.call(
this,
[
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
]
);
report.set("prev30Days", 25);
trend = report.get("thirtyDaysTrend");
assert.ok(trend === "trending-up");
trend = report.thirtyDaysTrend;
assert.strictEqual(trend, "trending-up");
report = reportWithData([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
]);
report = reportWithData.call(
this,
[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
]
);
report.set("prev30Days", 60);
trend = report.get("thirtyDaysTrend");
assert.ok(trend === "high-trending-down");
trend = report.thirtyDaysTrend;
assert.strictEqual(trend, "high-trending-down");
report = reportWithData([
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0,
]);
report = reportWithData.call(
this,
[
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0,
]
);
report.set("prev30Days", 35);
trend = report.get("thirtyDaysTrend");
assert.ok(trend === "trending-down");
trend = report.thirtyDaysTrend;
assert.strictEqual(trend, "trending-down");
});
test("higher is better false", function (assert) {
let report;
let trend;
report = reportWithData([0, 1, 0]);
report = reportWithData.call(this, [0, 1, 0]);
report.set("higher_is_better", false);
trend = report.get("yesterdayTrend");
assert.ok(trend === "high-trending-down");
trend = report.yesterdayTrend;
assert.strictEqual(trend, "high-trending-down");
report = reportWithData([0, 1.1, 1]);
report = reportWithData.call(this, [0, 1.1, 1]);
report.set("higher_is_better", false);
trend = report.get("yesterdayTrend");
assert.ok(trend === "trending-down");
trend = report.yesterdayTrend;
assert.strictEqual(trend, "trending-down");
report = reportWithData([0, 0, 1]);
report = reportWithData.call(this, [0, 0, 1]);
report.set("higher_is_better", false);
trend = report.get("yesterdayTrend");
assert.ok(trend === "high-trending-up");
trend = report.yesterdayTrend;
assert.strictEqual(trend, "high-trending-up");
report = reportWithData([0, 1, 1.1]);
report = reportWithData.call(this, [0, 1, 1.1]);
report.set("higher_is_better", false);
trend = report.get("yesterdayTrend");
assert.ok(trend === "trending-up");
trend = report.yesterdayTrend;
assert.strictEqual(trend, "trending-up");
});
test("small variation (-2/+2% change) is no-change", function (assert) {
let report;
let trend;
report = reportWithData([0, 1, 1, 1, 1, 1, 1, 0.9, 1, 1, 1, 1, 1, 1, 1]);
trend = report.get("sevenDaysTrend");
assert.ok(trend === "no-change");
report = reportWithData.call(
this,
[0, 1, 1, 1, 1, 1, 1, 0.9, 1, 1, 1, 1, 1, 1, 1]
);
trend = report.sevenDaysTrend;
assert.strictEqual(trend, "no-change");
report = reportWithData([0, 1, 1, 1, 1, 1, 1, 1.1, 1, 1, 1, 1, 1, 1, 1]);
trend = report.get("sevenDaysTrend");
assert.ok(trend === "no-change");
report = reportWithData.call(
this,
[0, 1, 1, 1, 1, 1, 1, 1.1, 1, 1, 1, 1, 1, 1, 1]
);
trend = report.sevenDaysTrend;
assert.strictEqual(trend, "no-change");
});
test("average", function (assert) {
let report;
report = reportWithData([5, 5, 5, 5, 5, 5, 5, 5]);
report = reportWithData.call(this, [5, 5, 5, 5, 5, 5, 5, 5]);
report.set("average", true);
assert.ok(report.get("lastSevenDaysCount") === 5);
assert.strictEqual(report.lastSevenDaysCount, 5);
report.set("average", false);
assert.ok(report.get("lastSevenDaysCount") === 35);
assert.strictEqual(report.lastSevenDaysCount, 35);
});
test("computed labels", function (assert) {
@ -303,14 +352,15 @@ module("Unit | Model | report", function () {
{ type: "bytes", property: "filesize", title: "Filesize" },
];
const report = Report.create({
const store = getOwner(this).lookup("service:store");
const report = store.createRecord("report", {
type: "topics",
labels,
data,
});
const row = report.get("data.0");
const computedLabels = report.get("computedLabels");
const row = report.data[0];
const computedLabels = report.computedLabels;
const usernameLabel = computedLabels[0];
assert.strictEqual(usernameLabel.mainProperty, "username");

View File

@ -10,7 +10,7 @@ module("Unit | Model | rest-model", function (hooks) {
test("munging", function (assert) {
const store = getOwner(this).lookup("service:store");
const Grape = RestModel.extend();
class Grape extends RestModel {}
Grape.reopenClass({
munge: function (json) {
json.inverse = 1 - json.percent;
@ -18,29 +18,30 @@ module("Unit | Model | rest-model", function (hooks) {
},
});
let g = Grape.create({ store, percent: 0.4 });
assert.strictEqual(g.get("inverse"), 0.6, "it runs `munge` on `create`");
getOwner(this).register("model:grape", Grape);
const g = store.createRecord("grape", { store, percent: 0.4 });
assert.strictEqual(g.inverse, 0.6, "it runs `munge` on `create`");
});
test("update", async function (assert) {
const store = getOwner(this).lookup("service:store");
const widget = await store.find("widget", 123);
assert.strictEqual(widget.get("name"), "Trout Lure");
assert.ok(!widget.get("isSaving"), "it is not saving");
assert.strictEqual(widget.name, "Trout Lure");
assert.ok(!widget.isSaving, "it is not saving");
const spyBeforeUpdate = sinon.spy(widget, "beforeUpdate");
const spyAfterUpdate = sinon.spy(widget, "afterUpdate");
const promise = widget.update({ name: "new name" });
assert.ok(widget.get("isSaving"), "it is saving");
assert.ok(widget.isSaving, "it is saving");
assert.ok(spyBeforeUpdate.calledOn(widget));
const result = await promise;
assert.ok(spyAfterUpdate.calledOn(widget));
assert.ok(!widget.get("isSaving"), "it is no longer saving");
assert.strictEqual(widget.get("name"), "new name");
assert.ok(!widget.isSaving, "it is no longer saving");
assert.strictEqual(widget.name, "new name");
assert.ok(result.target, "it has a reference to the record");
assert.strictEqual(result.target.name, widget.get("name"));
assert.strictEqual(result.target.name, widget.name);
});
test("updating simultaneously", async function (assert) {
@ -65,26 +66,26 @@ module("Unit | Model | rest-model", function (hooks) {
const store = getOwner(this).lookup("service:store");
const widget = store.createRecord("widget");
assert.ok(widget.get("isNew"), "it is a new record");
assert.ok(!widget.get("isCreated"), "it is not created");
assert.ok(!widget.get("isSaving"), "it is not saving");
assert.ok(widget.isNew, "it is a new record");
assert.ok(!widget.isCreated, "it is not created");
assert.ok(!widget.isSaving, "it is not saving");
const spyBeforeCreate = sinon.spy(widget, "beforeCreate");
const spyAfterCreate = sinon.spy(widget, "afterCreate");
const promise = widget.save({ name: "Evil Widget" });
assert.ok(widget.get("isSaving"), "it is not saving");
assert.ok(widget.isSaving, "it is not saving");
assert.ok(spyBeforeCreate.calledOn(widget));
const result = await promise;
assert.ok(spyAfterCreate.calledOn(widget));
assert.ok(!widget.get("isSaving"), "it is no longer saving");
assert.ok(widget.get("id"), "it has an id");
assert.ok(widget.get("name"), "Evil Widget");
assert.ok(widget.get("isCreated"), "it is created");
assert.ok(!widget.get("isNew"), "it is no longer new");
assert.ok(!widget.isSaving, "it is no longer saving");
assert.ok(widget.id, "it has an id");
assert.ok(widget.name, "Evil Widget");
assert.ok(widget.isCreated, "it is created");
assert.ok(!widget.isNew, "it is no longer new");
assert.ok(result.target, "it has a reference to the record");
assert.strictEqual(result.target.name, widget.get("name"));
assert.strictEqual(result.target.name, widget.name);
});
test("creating simultaneously", function (assert) {
@ -127,15 +128,15 @@ module("Unit | Model | rest-model", function (hooks) {
// The pretenders only respond to requests for `widget`
// If these basic tests pass, the name override worked correctly
//Create
// Create
const widget = store.createRecord("my-widget");
await widget.save({ name: "Evil Widget" });
assert.strictEqual(widget.id, 100, "it saved a new record successfully");
assert.strictEqual(widget.get("name"), "Evil Widget");
assert.strictEqual(widget.name, "Evil Widget");
// Update
await widget.update({ name: "new name" });
assert.strictEqual(widget.get("name"), "new name");
assert.strictEqual(widget.name, "new name");
// Destroy
await widget.destroyRecord();

View File

@ -1,5 +1,4 @@
import { module, test } from "qunit";
import ResultSet from "discourse/models/result-set";
import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
@ -7,47 +6,48 @@ module("Unit | Model | result-set", function (hooks) {
setupTest(hooks);
test("defaults", function (assert) {
const resultSet = ResultSet.create({ content: [] });
assert.strictEqual(resultSet.get("length"), 0);
assert.strictEqual(resultSet.get("totalRows"), 0);
assert.ok(!resultSet.get("loadMoreUrl"));
assert.ok(!resultSet.get("loading"));
assert.ok(!resultSet.get("loadingMore"));
assert.ok(!resultSet.get("refreshing"));
const store = getOwner(this).lookup("service:store");
const resultSet = store.createRecord("result-set", { content: [] });
assert.strictEqual(resultSet.length, 0);
assert.strictEqual(resultSet.totalRows, 0);
assert.ok(!resultSet.loadMoreUrl);
assert.ok(!resultSet.loading);
assert.ok(!resultSet.loadingMore);
assert.ok(!resultSet.refreshing);
});
test("pagination support", async function (assert) {
const store = getOwner(this).lookup("service:store");
const resultSet = await store.findAll("widget");
assert.strictEqual(resultSet.get("length"), 2);
assert.strictEqual(resultSet.get("totalRows"), 4);
assert.ok(resultSet.get("loadMoreUrl"), "has a url to load more");
assert.ok(!resultSet.get("loadingMore"), "it is not loading more");
assert.ok(resultSet.get("canLoadMore"));
assert.strictEqual(resultSet.length, 2);
assert.strictEqual(resultSet.totalRows, 4);
assert.ok(resultSet.loadMoreUrl, "has a url to load more");
assert.ok(!resultSet.loadingMore, "it is not loading more");
assert.ok(resultSet.canLoadMore);
const promise = resultSet.loadMore();
assert.ok(resultSet.get("loadingMore"), "it is loading more");
assert.ok(resultSet.loadingMore, "it is loading more");
await promise;
assert.ok(!resultSet.get("loadingMore"), "it finished loading more");
assert.strictEqual(resultSet.get("length"), 4);
assert.ok(!resultSet.get("loadMoreUrl"));
assert.ok(!resultSet.get("canLoadMore"));
assert.ok(!resultSet.loadingMore, "it finished loading more");
assert.strictEqual(resultSet.length, 4);
assert.ok(!resultSet.loadMoreUrl);
assert.ok(!resultSet.canLoadMore);
});
test("refresh support", async function (assert) {
const store = getOwner(this).lookup("service:store");
const resultSet = await store.findAll("widget");
assert.strictEqual(
resultSet.get("refreshUrl"),
resultSet.refreshUrl,
"/widgets?refresh=true",
"it has the refresh url"
);
const promise = resultSet.refresh();
assert.ok(resultSet.get("refreshing"), "it is refreshing");
assert.ok(resultSet.refreshing, "it is refreshing");
await promise;
assert.ok(!resultSet.get("refreshing"), "it is finished refreshing");
assert.ok(!resultSet.refreshing, "it is finished refreshing");
});
});

View File

@ -1,7 +1,10 @@
import { module, test } from "qunit";
import Session from "discourse/models/session";
import { setupTest } from "ember-qunit";
module("Unit | Model | session", function (hooks) {
setupTest(hooks);
module("Unit | Model | session", function () {
test("highestSeenByTopic", function (assert) {
const session = Session.current();
assert.deepEqual(

View File

@ -7,25 +7,17 @@ module("Unit | Model | site", function (hooks) {
setupTest(hooks);
test("create", function (assert) {
assert.ok(Site.create(), "it can create with no parameters");
const store = getOwner(this).lookup("service:store");
assert.ok(store.createRecord("site"), "it can create with no parameters");
});
test("instance", function (assert) {
const site = Site.current();
assert.present(site, "We have a current site singleton");
assert.present(
site.get("categories"),
"The instance has a list of categories"
);
assert.present(
site.get("flagTypes"),
"The instance has a list of flag types"
);
assert.present(
site.get("trustLevels"),
"The instance has a list of trust levels"
);
assert.present(site.categories, "The instance has a list of categories");
assert.present(site.flagTypes, "The instance has a list of flag types");
assert.present(site.trustLevels, "The instance has a list of trust levels");
});
test("create categories", function (assert) {

View File

@ -1,8 +1,15 @@
import { module, test } from "qunit";
import StaffActionLog from "admin/models/staff-action-log";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Model | staff-action-log", function (hooks) {
setupTest(hooks);
module("Unit | Model | staff-action-log", function () {
test("create", function (assert) {
assert.ok(StaffActionLog.create(), "it can be created without arguments");
const store = getOwner(this).lookup("service:store");
assert.ok(
store.createRecord("staff-action-log"),
"it can be created without arguments"
);
});
});

View File

@ -1,18 +1,22 @@
import { module, test } from "qunit";
import Theme from "admin/models/theme";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Model | theme", function (hooks) {
setupTest(hooks);
module("Unit | Model | theme", function () {
test("can add an upload correctly", function (assert) {
let theme = Theme.create();
const store = getOwner(this).lookup("service:store");
const theme = store.createRecord("theme");
assert.strictEqual(
theme.get("uploads.length"),
theme.uploads.length,
0,
"uploads should be an empty array"
);
theme.setField("common", "bob", "", 999, 2);
let fields = theme.get("theme_fields");
let fields = theme.theme_fields;
assert.strictEqual(fields.length, 1, "expecting 1 theme field");
assert.strictEqual(
fields[0].upload_id,
@ -21,6 +25,6 @@ module("Unit | Model | theme", function () {
);
assert.strictEqual(fields[0].type_id, 2, "expecting type id to be set");
assert.strictEqual(theme.get("uploads.length"), 1, "expecting an upload");
assert.strictEqual(theme.uploads.length, 1, "expecting an upload");
});
});

View File

@ -1,15 +1,18 @@
import { module, test } from "qunit";
import User from "discourse/models/user";
import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
module("Unit | Model | topic-details", function (hooks) {
setupTest(hooks);
module("Unit | Model | topic-details", function () {
test("defaults", function (assert) {
const store = getOwner(this).lookup("service:store");
const topic = store.createRecord("topic", { id: 1234 });
const details = topic.details;
assert.present(details, "the details are present by default");
assert.ok(!details.get("loaded"), "details are not loaded by default");
assert.ok(!details.loaded, "details are not loaded by default");
});
test("updateFromJson", function (assert) {
@ -22,10 +25,10 @@ module("Unit | Model | topic-details", function () {
});
assert.strictEqual(
details.get("allowed_users.length"),
details.allowed_users.length,
1,
"it loaded the allowed users"
);
assert.containsInstance(details.get("allowed_users"), User);
assert.containsInstance(details.allowed_users, User);
});
});

View File

@ -15,8 +15,8 @@ module("Unit | Model | topic", function (hooks) {
test("defaults", function (assert) {
const topic = this.store.createRecord("topic", { id: 1234 });
assert.blank(topic.get("deleted_at"), "deleted_at defaults to blank");
assert.blank(topic.get("deleted_by"), "deleted_by defaults to blank");
assert.blank(topic.deleted_at, "deleted_at defaults to blank");
assert.blank(topic.deleted_by, "deleted_by defaults to blank");
});
test("visited", function (assert) {
@ -25,17 +25,14 @@ module("Unit | Model | topic", function (hooks) {
last_read_post_number: 1,
});
assert.notOk(
topic.get("visited"),
"not visited unless we've read all the posts"
);
assert.notOk(topic.visited, "not visited unless we've read all the posts");
topic.set("last_read_post_number", 2);
assert.ok(topic.get("visited"), "is visited once we've read all the posts");
assert.ok(topic.visited, "is visited once we've read all the posts");
topic.set("last_read_post_number", 3);
assert.ok(
topic.get("visited"),
topic.visited,
"is visited if we've read all the posts and some are deleted at the end"
);
});
@ -109,11 +106,11 @@ module("Unit | Model | topic", function (hooks) {
test("has details", function (assert) {
const topic = this.store.createRecord("topic", { id: 1234 });
const topicDetails = topic.get("details");
const topicDetails = topic.details;
assert.present(topicDetails, "a topic has topicDetails after we create it");
assert.strictEqual(
topicDetails.get("topic"),
topicDetails.topic,
topic,
"the topicDetails has a reference back to the topic"
);
@ -121,11 +118,11 @@ module("Unit | Model | topic", function (hooks) {
test("has a postStream", function (assert) {
const topic = this.store.createRecord("topic", { id: 1234 });
const postStream = topic.get("postStream");
const postStream = topic.postStream;
assert.present(postStream, "a topic has a postStream after we create it");
assert.strictEqual(
postStream.get("topic"),
postStream.topic,
topic,
"the postStream has a reference back to the topic"
);
@ -135,7 +132,7 @@ module("Unit | Model | topic", function (hooks) {
const topic = this.store.createRecord("topic", {
suggested_topics: [{ id: 1 }, { id: 2 }],
});
const suggestedTopics = topic.get("suggestedTopics");
const suggestedTopics = topic.suggestedTopics;
assert.strictEqual(
suggestedTopics.length,
@ -150,10 +147,10 @@ module("Unit | Model | topic", function (hooks) {
const category = Category.list()[0];
const topic = this.store.createRecord("topic", {
id: 1111,
category_id: category.get("id"),
category_id: category.id,
});
assert.strictEqual(topic.get("category"), category);
assert.strictEqual(topic.category, category);
});
test("updateFromJson", function (assert) {
@ -164,21 +161,13 @@ module("Unit | Model | topic", function (hooks) {
post_stream: [1, 2, 3],
details: { hello: "world" },
cool: "property",
category_id: category.get("id"),
category_id: category.id,
});
assert.blank(topic.get("post_stream"), "it does not update post_stream");
assert.strictEqual(
topic.get("details.hello"),
"world",
"it updates the details"
);
assert.strictEqual(
topic.get("cool"),
"property",
"it updates other properties"
);
assert.strictEqual(topic.get("category"), category);
assert.blank(topic.post_stream, "it does not update post_stream");
assert.strictEqual(topic.details.hello, "world", "it updates the details");
assert.strictEqual(topic.cool, "property", "it updates other properties");
assert.strictEqual(topic.category, category);
});
test("recover", async function (assert) {
@ -191,8 +180,8 @@ module("Unit | Model | topic", function (hooks) {
await topic.recover();
assert.blank(topic.get("deleted_at"), "it clears deleted_at");
assert.blank(topic.get("deleted_by"), "it clears deleted_by");
assert.blank(topic.deleted_at, "it clears deleted_at");
assert.blank(topic.deleted_by, "it clears deleted_by");
});
test("fancyTitle", function (assert) {
@ -201,7 +190,7 @@ module("Unit | Model | topic", function (hooks) {
});
assert.strictEqual(
topic.get("fancyTitle"),
topic.fancyTitle,
`<img width=\"20\" height=\"20\" src='/images/emoji/twitter/smile.png?v=${v}' title='smile' alt='smile' class='emoji'> with all <img width=\"20\" height=\"20\" src='/images/emoji/twitter/slight_smile.png?v=${v}' title='slight_smile' alt='slight_smile' class='emoji'> the emojis <img width=\"20\" height=\"20\" src='/images/emoji/twitter/pear.png?v=${v}' title='pear' alt='pear' class='emoji'><img width=\"20\" height=\"20\" src='/images/emoji/twitter/peach.png?v=${v}' title='peach' alt='peach' class='emoji'>`,
"supports emojis"
);
@ -219,12 +208,12 @@ module("Unit | Model | topic", function (hooks) {
siteSettings.support_mixed_text_direction = true;
assert.strictEqual(
rtlTopic.get("fancyTitle"),
rtlTopic.fancyTitle,
`<span dir="rtl">هذا اختبار</span>`,
"sets the dir-span to rtl"
);
assert.strictEqual(
ltrTopic.get("fancyTitle"),
ltrTopic.fancyTitle,
`<span dir="ltr">This is a test</span>`,
"sets the dir-span to ltr"
);
@ -237,7 +226,7 @@ module("Unit | Model | topic", function (hooks) {
});
assert.strictEqual(
topic.get("escapedExcerpt"),
topic.escapedExcerpt,
`This is a test topic <img width=\"20\" height=\"20\" src='/images/emoji/twitter/smile.png?v=${v}' title='smile' alt='smile' class='emoji'>`,
"supports emojis"
);

View File

@ -244,10 +244,10 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) {
},
]);
let randomUnread = 0,
randomNew = 0,
sevenUnread = 0,
sevenNew = 0;
let randomUnread = 0;
let randomNew = 0;
let sevenUnread = 0;
let sevenNew = 0;
trackingState.forEachTracked((topic, isNew, isUnread) => {
if (topic.category_id === 7) {

View File

@ -1,22 +1,27 @@
import { module, test } from "qunit";
import UserAction from "discourse/models/user-action";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Model | user-action", function (hooks) {
setupTest(hooks);
module("Unit | Model | user-action", function () {
test("collapsing likes", function (assert) {
let actions = UserAction.collapseStream([
UserAction.create({
const store = getOwner(this).lookup("service:store");
const actions = UserAction.collapseStream([
store.createRecord("user-action", {
action_type: UserAction.TYPES.likes_given,
topic_id: 1,
user_id: 1,
post_number: 1,
}),
UserAction.create({
store.createRecord("user-action", {
action_type: UserAction.TYPES.edits,
topic_id: 2,
user_id: 1,
post_number: 1,
}),
UserAction.create({
store.createRecord("user-action", {
action_type: UserAction.TYPES.likes_given,
topic_id: 1,
user_id: 2,
@ -25,7 +30,7 @@ module("Unit | Model | user-action", function () {
]);
assert.strictEqual(actions.length, 2);
assert.strictEqual(actions[0].get("children.length"), 1);
assert.strictEqual(actions[0].get("children")[0].items.length, 2);
assert.strictEqual(actions[0].children.length, 1);
assert.strictEqual(actions[0].children[0].items.length, 2);
});
});

View File

@ -2,25 +2,29 @@ import { module, test } from "qunit";
import UserBadge from "discourse/models/user-badge";
import badgeFixtures from "discourse/tests/fixtures/user-badges";
import { cloneJSON } from "discourse-common/lib/object";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Model | user-badge", function (hooks) {
setupTest(hooks);
module("Unit | Model | user-badge", function () {
test("createFromJson single", function (assert) {
const userBadge = UserBadge.createFromJson(
cloneJSON(badgeFixtures["/user_badges"])
);
assert.ok(!Array.isArray(userBadge), "does not return an array");
assert.strictEqual(
userBadge.get("badge.name"),
userBadge.badge.name,
"Badge 2",
"badge reference is set"
);
assert.strictEqual(
userBadge.get("badge.badge_type.name"),
userBadge.badge.badge_type.name,
"Silver 2",
"badge.badge_type reference is set"
);
assert.strictEqual(
userBadge.get("granted_by.username"),
userBadge.granted_by.username,
"anne3",
"granted_by reference is set"
);
@ -32,7 +36,7 @@ module("Unit | Model | user-badge", function () {
);
assert.ok(Array.isArray(userBadges), "returns an array");
assert.strictEqual(
userBadges[0].get("granted_by"),
userBadges[0].granted_by,
undefined,
"granted_by reference is not set when null"
);
@ -55,12 +59,14 @@ module("Unit | Model | user-badge", function () {
test("revoke", async function (assert) {
assert.expect(0);
const userBadge = UserBadge.create({ id: 1 });
const store = getOwner(this).lookup("service:store");
const userBadge = store.createRecord("user-badge", { id: 1 });
await userBadge.revoke();
});
test("favorite", async function (assert) {
const userBadge = UserBadge.create({ id: 1 });
const store = getOwner(this).lookup("service:store");
const userBadge = store.createRecord("user-badge", { id: 1 });
assert.notOk(userBadge.is_favorite);
await userBadge.favorite();

View File

@ -1,13 +1,17 @@
import { module, test } from "qunit";
import I18n from "I18n";
import { NEW_TOPIC_KEY } from "discourse/models/composer";
import User from "discourse/models/user";
import UserDraft from "discourse/models/user-draft";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Model | user-draft", function (hooks) {
setupTest(hooks);
module("Unit | Model | user-draft", function () {
test("stream", function (assert) {
const user = User.create({ id: 1, username: "eviltrout" });
const store = getOwner(this).lookup("service:store");
const user = store.createRecord("user", { id: 1, username: "eviltrout" });
const stream = user.userDraftsStream;
assert.present(stream, "a user has a drafts stream by default");
assert.strictEqual(
stream.content.length,
@ -18,12 +22,13 @@ module("Unit | Model | user-draft", function () {
});
test("draft", function (assert) {
const store = getOwner(this).lookup("service:store");
const drafts = [
UserDraft.create({
store.createRecord("user-draft", {
draft_key: "topic_1",
post_number: "10",
}),
UserDraft.create({
store.createRecord("user-draft", {
draft_key: NEW_TOPIC_KEY,
}),
];

View File

@ -1,43 +1,38 @@
import { module, test } from "qunit";
import User from "discourse/models/user";
import UserAction from "discourse/models/user-action";
import { setupTest } from "ember-qunit";
module("Unit | Model | user-stream", function (hooks) {
setupTest(hooks);
module("Unit | Model | user-stream", function () {
test("basics", function (assert) {
let user = User.create({ id: 1, username: "eviltrout" });
let stream = user.get("stream");
const user = User.create({ id: 1, username: "eviltrout" });
const stream = user.stream;
assert.present(stream, "a user has a stream by default");
assert.strictEqual(
stream.get("user"),
user,
"the stream points back to the user"
);
assert.strictEqual(stream.user, user, "the stream points back to the user");
assert.strictEqual(
stream.get("itemsLoaded"),
0,
"no items are loaded by default"
);
assert.blank(stream.get("content"), "no content by default");
assert.blank(stream.get("filter"), "no filter by default");
assert.strictEqual(stream.itemsLoaded, 0, "no items are loaded by default");
assert.blank(stream.content, "no content by default");
assert.blank(stream.filter, "no filter by default");
assert.ok(!stream.get("loaded"), "the stream is not loaded by default");
assert.ok(!stream.loaded, "the stream is not loaded by default");
});
test("filterParam", function (assert) {
let user = User.create({ id: 1, username: "eviltrout" });
let stream = user.get("stream");
const user = User.create({ id: 1, username: "eviltrout" });
const stream = user.stream;
// defaults to posts/topics
assert.strictEqual(stream.get("filterParam"), "4,5");
assert.strictEqual(stream.filterParam, "4,5");
stream.set("filter", UserAction.TYPES.topics);
assert.strictEqual(stream.get("filterParam"), 4);
assert.strictEqual(stream.filterParam, 4);
stream.set("filter", UserAction.TYPES.likes_given);
assert.strictEqual(stream.get("filterParam"), UserAction.TYPES.likes_given);
assert.strictEqual(stream.filterParam, UserAction.TYPES.likes_given);
stream.set("filter", UserAction.TYPES.replies);
assert.strictEqual(stream.get("filterParam"), "6,9");
assert.strictEqual(stream.filterParam, "6,9");
});
});

View File

@ -1,37 +1,42 @@
import { module, test } from "qunit";
import Group from "discourse/models/group";
import User from "discourse/models/user";
import { setupTest } from "ember-qunit";
import PreloadStore from "discourse/lib/preload-store";
import sinon from "sinon";
import { settled } from "@ember/test-helpers";
import Site from "discourse/models/site";
import User from "discourse/models/user";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Model | user", function (hooks) {
setupTest(hooks);
module("Unit | Model | user", function () {
test("staff", function (assert) {
let user = User.create({ id: 1, username: "eviltrout" });
const store = getOwner(this).lookup("service:store");
const user = store.createRecord("user", { id: 1, username: "eviltrout" });
assert.ok(!user.get("staff"), "user is not staff");
assert.ok(!user.staff, "user is not staff");
user.toggleProperty("moderator");
assert.ok(user.get("staff"), "moderators are staff");
assert.ok(user.staff, "moderators are staff");
user.setProperties({ moderator: false, admin: true });
assert.ok(user.get("staff"), "admins are staff");
assert.ok(user.staff, "admins are staff");
});
test("searchContext", function (assert) {
let user = User.create({ id: 1, username: "EvilTrout" });
const store = getOwner(this).lookup("service:store");
const user = store.createRecord("user", { id: 1, username: "EvilTrout" });
assert.deepEqual(
user.get("searchContext"),
user.searchContext,
{ type: "user", id: "eviltrout", user },
"has a search context"
);
});
test("isAllowedToUploadAFile", function (assert) {
let user = User.create({ trust_level: 0, admin: true });
const store = getOwner(this).lookup("service:store");
const user = store.createRecord("user", { trust_level: 0, admin: true });
assert.ok(
user.isAllowedToUploadAFile("image"),
"admin can always upload a file"
@ -45,8 +50,9 @@ module("Unit | Model | user", function () {
});
test("canMangeGroup", function (assert) {
let user = User.create({ admin: true });
let group = Group.create({ automatic: true });
const store = getOwner(this).lookup("service:store");
const user = store.createRecord("user", { admin: true });
const group = store.createRecord("group", { automatic: true });
assert.strictEqual(
user.canManageGroup(group),
@ -74,7 +80,11 @@ module("Unit | Model | user", function () {
});
test("muted ids", function (assert) {
let user = User.create({ username: "chuck", muted_category_ids: [] });
const store = getOwner(this).lookup("service:store");
const user = store.createRecord("user", {
username: "chuck",
muted_category_ids: [],
});
assert.deepEqual(user.calculateMutedIds(0, 1, "muted_category_ids"), [1]);
assert.deepEqual(user.calculateMutedIds(1, 1, "muted_category_ids"), []);
@ -109,24 +119,26 @@ module("Unit | Model | user", function () {
});
test("subsequent calls to trackStatus and stopTrackingStatus increase and decrease subscribers counter", function (assert) {
const user = User.create();
assert.equal(user._subscribersCount, 0);
const store = getOwner(this).lookup("service:store");
const user = store.createRecord("user");
assert.strictEqual(user._subscribersCount, 0);
user.trackStatus();
assert.equal(user._subscribersCount, 1);
assert.strictEqual(user._subscribersCount, 1);
user.trackStatus();
assert.equal(user._subscribersCount, 2);
assert.strictEqual(user._subscribersCount, 2);
user.stopTrackingStatus();
assert.equal(user._subscribersCount, 1);
assert.strictEqual(user._subscribersCount, 1);
user.stopTrackingStatus();
assert.equal(user._subscribersCount, 0);
assert.strictEqual(user._subscribersCount, 0);
});
test("attempt to stop tracking status if status wasn't tracked doesn't throw", function (assert) {
const user = User.create();
const store = getOwner(this).lookup("service:store");
const user = store.createRecord("user");
user.stopTrackingStatus();
assert.ok(true);
});
@ -140,26 +152,27 @@ module("Unit | Model | user", function () {
description: "user2 status",
emoji: "speech_balloon",
};
const user1 = User.create({
const store = getOwner(this).lookup("service:store");
const user1 = store.createRecord("user", {
id: 1,
status: status1,
});
const user2 = User.create({ id: 2, status: status2 });
const user2 = store.createRecord("user", { id: 2, status: status2 });
const appEvents = user1.appEvents;
try {
user1.trackStatus();
user2.trackStatus();
assert.equal(user1.status, status1);
assert.equal(user2.status, status2);
assert.strictEqual(user1.status, status1);
assert.strictEqual(user2.status, status2);
appEvents.trigger("user-status:changed", { [user1.id]: null });
assert.equal(user1.status, null);
assert.equal(user2.status, status2);
assert.strictEqual(user1.status, null);
assert.strictEqual(user2.status, status2);
appEvents.trigger("user-status:changed", { [user2.id]: null });
assert.equal(user1.status, null);
assert.equal(user2.status, null);
assert.strictEqual(user1.status, null);
assert.strictEqual(user2.status, null);
} finally {
user1.stopTrackingStatus();
user2.stopTrackingStatus();
@ -167,7 +180,8 @@ module("Unit | Model | user", function () {
});
test("create() doesn't set internal status tracking fields", function (assert) {
const user = User.create({
const store = getOwner(this).lookup("service:store");
const user = store.createRecord("user", {
_subscribersCount: 10,
_clearStatusTimerId: 100,
});
@ -183,8 +197,11 @@ module("Unit | Model | user", function () {
});
test("hideUserTipForever() makes a single request", async function (assert) {
Site.current().set("user_tips", { first_notification: 1 });
const user = User.create({ username: "test" });
const site = getOwner(this).lookup("service:site");
site.set("user_tips", { first_notification: 1 });
const store = getOwner(this).lookup("service:store");
const user = store.createRecord("user", { username: "test" });
let requestsCount = 0;
pretender.put("/u/test.json", () => {

View File

@ -1,35 +1,44 @@
import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import WizardField from "wizard/models/wizard-field";
module("Unit | Model | Wizard | wizard-field", function () {
module("Unit | Model | Wizard | wizard-field", function (hooks) {
setupTest(hooks);
test("basic state", function (assert) {
const w = WizardField.create({ type: "text" });
assert.ok(w.unchecked);
assert.ok(!w.valid);
assert.ok(!w.invalid);
const store = getOwner(this).lookup("service:store");
const field = store.createRecord("wizard-field", { type: "text" });
assert.ok(field.unchecked);
assert.ok(!field.valid);
assert.ok(!field.invalid);
});
test("text - required - validation", function (assert) {
const w = WizardField.create({ type: "text", required: true });
assert.ok(w.unchecked);
const store = getOwner(this).lookup("service:store");
const field = store.createRecord("wizard-field", {
type: "text",
required: true,
});
assert.ok(field.unchecked);
w.check();
assert.ok(!w.unchecked);
assert.ok(!w.valid);
assert.ok(w.invalid);
field.check();
assert.ok(!field.unchecked);
assert.ok(!field.valid);
assert.ok(field.invalid);
w.set("value", "a value");
w.check();
assert.ok(!w.unchecked);
assert.ok(w.valid);
assert.ok(!w.invalid);
field.set("value", "a value");
field.check();
assert.ok(!field.unchecked);
assert.ok(field.valid);
assert.ok(!field.invalid);
});
test("text - optional - validation", function (assert) {
const f = WizardField.create({ type: "text" });
assert.ok(f.unchecked);
const store = getOwner(this).lookup("service:store");
const field = store.createRecord("wizard-field", { type: "text" });
assert.ok(field.unchecked);
f.check();
assert.ok(f.valid);
field.check();
assert.ok(field.valid);
});
});