DEV: Use store to create Post records in tests (#18920)

This commit is contained in:
Jarek Radosz 2022-11-08 18:13:45 +01:00 committed by GitHub
parent 21570410ab
commit a5099c72a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 54 deletions

View File

@ -3,15 +3,11 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { render } from "@ember/test-helpers";
import { count } from "discourse/tests/helpers/qunit-helpers";
import { hbs } from "ember-cli-htmlbars";
import Post from "discourse/models/post";
import { getOwner } from "discourse-common/lib/get-owner";
function postStreamTest(name, attrs) {
test(name, async function (assert) {
const site = this.container.lookup("service:site");
let posts = attrs.posts.call(this);
posts.forEach((p) => p.set("site", site));
this.set("posts", posts);
this.set("posts", attrs.posts.call(this));
await render(
hbs`<MountWidget @widget="post-stream" @args={{hash posts=this.posts}} />`
@ -26,13 +22,13 @@ module("Integration | Component | Widget | post-stream", function (hooks) {
postStreamTest("basics", {
posts() {
const site = this.container.lookup("service:site");
const site = getOwner(this).lookup("service:site");
const store = getOwner(this).lookup("service:store");
const topic = store.createRecord("topic");
topic.set("details.created_by", { id: 123 });
return [
Post.create({
store.createRecord("post", {
topic,
id: 1,
post_number: 1,
@ -40,27 +36,32 @@ module("Integration | Component | Widget | post-stream", function (hooks) {
primary_group_name: "trout",
avatar_template: "/images/avatar.png",
}),
Post.create({
store.createRecord("post", {
topic,
id: 2,
post_number: 2,
post_type: site.get("post_types.moderator_action"),
}),
Post.create({ topic, id: 3, post_number: 3, hidden: true }),
Post.create({
store.createRecord("post", {
topic,
id: 3,
post_number: 3,
hidden: true,
}),
store.createRecord("post", {
topic,
id: 4,
post_number: 4,
post_type: site.get("post_types.whisper"),
}),
Post.create({
store.createRecord("post", {
topic,
id: 5,
post_number: 5,
wiki: true,
via_email: true,
}),
Post.create({
store.createRecord("post", {
topic,
id: 6,
post_number: 6,
@ -133,7 +134,7 @@ module("Integration | Component | Widget | post-stream", function (hooks) {
topic.set("details.created_by", { id: 123 });
return [
Post.create({
store.createRecord("post", {
topic,
id: 1,
post_number: 1,

View File

@ -1,10 +1,11 @@
import LinkLookup from "discourse/lib/link-lookup";
import { module, test } from "qunit";
import Post from "discourse/models/post";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Utility | link-lookup", function (hooks) {
hooks.beforeEach(function () {
this.post = Post.create();
const store = getOwner(this).lookup("service:store");
this.post = store.createRecord("post");
this.linkLookup = new LinkLookup({
"en.wikipedia.org/wiki/handheld_game_console": {
post_number: 1,

View File

@ -4,12 +4,12 @@ import {
deleteCachedInlineOnebox,
} from "pretty-text/inline-oneboxer";
import QUnit, { module, test } from "qunit";
import Post from "discourse/models/post";
import { buildQuote } from "discourse/lib/quote";
import { deepMerge } from "discourse-common/lib/object";
import { extractDataAttribute } from "pretty-text/engines/discourse-markdown-it";
import { registerEmoji } from "pretty-text/emoji";
import { IMAGE_VERSION as v } from "pretty-text/emoji/version";
import { getOwner } from "discourse-common/lib/get-owner";
const rawOpts = {
siteSettings: {
@ -1274,7 +1274,8 @@ eviltrout</p>
});
test("quotes", function (assert) {
const post = Post.create({
const store = getOwner(this).lookup("service:store");
const post = store.createRecord("post", {
cooked: "<p><b>lorem</b> ipsum</p>",
username: "eviltrout",
post_number: 1,
@ -1334,7 +1335,8 @@ eviltrout</p>
});
test("quoting a quote", function (assert) {
const post = Post.create({
const store = getOwner(this).lookup("service:store");
const post = store.createRecord("post", {
cooked: new PrettyText(defaultOpts).cook(
'[quote="sam, post:1, topic:1, full:true"]\nhello\n[/quote]\n*Test*'
),

View File

@ -10,9 +10,9 @@ import {
} from "discourse/tests/helpers/qunit-helpers";
import AppEvents from "discourse/services/app-events";
import EmberObject from "@ember/object";
import Post from "discourse/models/post";
import createStore from "discourse/tests/helpers/create-store";
import { test } from "qunit";
import { getOwner } from "discourse-common/lib/get-owner";
function createComposer(opts) {
opts = opts || {};
@ -276,7 +276,8 @@ discourseModule("Unit | Model | composer", function () {
const composer = createComposer();
assert.ok(!composer.get("editingFirstPost"), "it's false by default");
const post = Post.create({ id: 123, post_number: 2 });
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"),
@ -291,10 +292,11 @@ discourseModule("Unit | Model | composer", function () {
});
test("clearState", function (assert) {
const store = getOwner(this).lookup("service:store");
const composer = createComposer({
originalText: "asdf",
reply: "asdf2",
post: Post.create({ id: 1 }),
post: store.createRecord("post", { id: 1 }),
title: "wat",
});
@ -358,7 +360,8 @@ discourseModule("Unit | Model | composer", function () {
this.siteSettings.max_topic_title_length = 10;
const composer = createComposer();
const post = Post.create({
const store = getOwner(this).lookup("service:store");
const post = store.createRecord("post", {
id: 123,
post_number: 2,
static_doc: true,

View File

@ -1,18 +1,22 @@
import { module, test } from "qunit";
import PendingPost from "discourse/models/pending-post";
import createStore from "discourse/tests/helpers/create-store";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
import { settled } from "@ember/test-helpers";
module("Unit | Model | pending-post", function () {
module("Unit | Model | pending-post", function (hooks) {
setupTest(hooks);
test("Properties", async function (assert) {
const store = createStore();
const store = getOwner(this).lookup("service:store");
const category = store.createRecord("category", { id: 2 });
const post = PendingPost.create({
const post = store.createRecord("pending-post", {
id: 1,
topic_url: "topic-url",
username: "USERNAME",
category_id: 2,
});
// pending-post initializer performs async operations
await settled();
assert.equal(post.postUrl, "topic-url", "topic_url is aliased to postUrl");
@ -30,7 +34,12 @@ module("Unit | Model | pending-post", function () {
});
test("it cooks raw_text", async function (assert) {
const post = PendingPost.create({ raw_text: "**bold text**" });
const store = getOwner(this).lookup("service:store");
const post = store.createRecord("pending-post", {
raw_text: "**bold text**",
});
// pending-post initializer performs async operations
await settled();
assert.equal(

View File

@ -1,30 +1,20 @@
import { module, test } from "qunit";
import Post from "discourse/models/post";
import User from "discourse/models/user";
import { deepMerge } from "discourse-common/lib/object";
import { getOwner } from "discourse-common/lib/get-owner";
function buildPost(args) {
return Post.create(
deepMerge(
{
id: 1,
can_delete: true,
version: 1,
},
args || {}
)
);
}
module("Unit | Model | post", function (hooks) {
hooks.beforeEach(function () {
this.store = getOwner(this).lookup("service:store");
});
module("Unit | Model | post", function () {
test("defaults", function (assert) {
let post = Post.create({ id: 1 });
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");
});
test("new_user", function (assert) {
let post = Post.create({ trust_level: 0 });
const post = this.store.createRecord("post", { trust_level: 0 });
assert.ok(post.get("new_user"), "post is from a new user");
post.set("trust_level", 1);
@ -32,7 +22,7 @@ module("Unit | Model | post", function () {
});
test("firstPost", function (assert) {
let post = Post.create({ post_number: 1 });
const post = this.store.createRecord("post", { post_number: 1 });
assert.ok(post.get("firstPost"), "it's the first post");
post.set("post_number", 10);
@ -40,17 +30,14 @@ module("Unit | Model | post", function () {
});
test("updateFromPost", function (assert) {
let post = Post.create({
const post = this.store.createRecord("post", {
post_number: 1,
raw: "hello world",
});
post.updateFromPost(
Post.create({
this.store.createRecord("post", {
raw: "different raw",
wat: function () {
return 123;
},
})
);
@ -58,8 +45,13 @@ module("Unit | Model | post", function () {
});
test("destroy by staff", async function (assert) {
let user = User.create({ username: "staff", moderator: true });
let post = buildPost({ user });
const user = User.create({ username: "staff", moderator: true });
const post = this.store.createRecord("post", {
id: 1,
can_delete: true,
version: 1,
user,
});
await post.destroy(user);
@ -85,7 +77,13 @@ module("Unit | Model | post", function () {
test("destroy by non-staff", async function (assert) {
const originalCooked = "this is the original cooked value";
const user = User.create({ username: "evil trout" });
const post = buildPost({ user, cooked: originalCooked });
const post = this.store.createRecord("post", {
id: 1,
can_delete: true,
version: 1,
user,
cooked: originalCooked,
});
await post.destroy(user);