DEV: Deprecate `create-store` test helper (#19021)

Use the regular Store service instead.
This commit is contained in:
Jarek Radosz 2022-11-16 10:54:46 +01:00 committed by GitHub
parent 8e60c50f60
commit 25aa0bc10d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 338 additions and 262 deletions

View File

@ -7,6 +7,7 @@ import { buildResolver } from "discourse-common/resolver";
import { currentSettings } from "discourse/tests/helpers/site-settings"; import { currentSettings } from "discourse/tests/helpers/site-settings";
import Site from "discourse/models/site"; import Site from "discourse/models/site";
import RestModel from "discourse/models/rest"; import RestModel from "discourse/models/rest";
import deprecated from "discourse-common/lib/deprecated";
const CatAdapter = RestAdapter.extend({ const CatAdapter = RestAdapter.extend({
primaryKey: "cat_id", primaryKey: "cat_id",
@ -33,6 +34,17 @@ const CachedCat = RestModel.extend({
}); });
export default function (customLookup = () => {}) { export default function (customLookup = () => {}) {
deprecated(
`create-store helper is deprecated. Please use regular Store service instead, e.g.
\`getOwner(this).lookup("service:store")\`
`,
{
since: "2.9.0.beta12",
dropFrom: "3.1.0.beta1",
id: "discourse.create-store-helper",
}
);
const resolver = buildResolver("discourse").create({ const resolver = buildResolver("discourse").create({
namespace: { modulePrefix: "discourse" }, namespace: { modulePrefix: "discourse" },
}); });

View File

@ -3,13 +3,13 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { render } from "@ember/test-helpers"; import { render } from "@ember/test-helpers";
import { query } from "discourse/tests/helpers/qunit-helpers"; import { query } from "discourse/tests/helpers/qunit-helpers";
import { hbs } from "ember-cli-htmlbars"; import { hbs } from "ember-cli-htmlbars";
import createStore from "discourse/tests/helpers/create-store"; import { getOwner } from "discourse-common/lib/get-owner";
module("Integration | Component | pending-post", function (hooks) { module("Integration | Component | pending-post", function (hooks) {
setupRenderingTest(hooks); setupRenderingTest(hooks);
test("it renders", async function (assert) { test("it renders", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
store.createRecord("category", { id: 2 }); store.createRecord("category", { id: 2 });
const post = store.createRecord("pending-post", { const post = store.createRecord("pending-post", {
id: 1, id: 1,

View File

@ -2,9 +2,9 @@ import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { render } from "@ember/test-helpers"; import { render } from "@ember/test-helpers";
import I18n from "I18n"; import I18n from "I18n";
import createStore from "discourse/tests/helpers/create-store";
import { hbs } from "ember-cli-htmlbars"; import { hbs } from "ember-cli-htmlbars";
import selectKit from "discourse/tests/helpers/select-kit-helper"; import selectKit from "discourse/tests/helpers/select-kit-helper";
import { getOwner } from "discourse-common/lib/get-owner";
module( module(
"Integration | Component | select-kit/category-chooser", "Integration | Component | select-kit/category-chooser",
@ -267,7 +267,7 @@ module(
}); });
test("filter works with non english characters", async function (assert) { test("filter works with non english characters", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
store.createRecord("category", { store.createRecord("category", {
id: 1, id: 1,
name: "chữ Quốc ngữ", name: "chữ Quốc ngữ",
@ -287,7 +287,7 @@ module(
}); });
test("decodes entities in row title", async function (assert) { test("decodes entities in row title", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
store.createRecord("category", { store.createRecord("category", {
id: 1, id: 1,
name: "cat-with-entities", name: "cat-with-entities",

View File

@ -10,8 +10,8 @@ import {
import { hbs } from "ember-cli-htmlbars"; import { hbs } from "ember-cli-htmlbars";
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import I18n from "I18n"; import I18n from "I18n";
import createStore from "discourse/tests/helpers/create-store";
import User from "discourse/models/user"; import User from "discourse/models/user";
import { getOwner } from "discourse-common/lib/get-owner";
module("Integration | Component | Widget | post", function (hooks) { module("Integration | Component | Widget | post", function (hooks) {
setupRenderingTest(hooks); setupRenderingTest(hooks);
@ -163,7 +163,7 @@ module("Integration | Component | Widget | post", function (hooks) {
}); });
test("like count button", async function (assert) { test("like count button", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const topic = store.createRecord("topic", { id: 123 }); const topic = store.createRecord("topic", { id: 123 });
const post = store.createRecord("post", { const post = store.createRecord("post", {
id: 1, id: 1,
@ -507,7 +507,7 @@ module("Integration | Component | Widget | post", function (hooks) {
}); });
test("expand first post", async function (assert) { test("expand first post", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
this.set("args", { expandablePost: true }); this.set("args", { expandablePost: true });
this.set("post", store.createRecord("post", { id: 1234 })); this.set("post", store.createRecord("post", { id: 1234 }));

View File

@ -4,13 +4,13 @@ import { click, render } from "@ember/test-helpers";
import { exists } from "discourse/tests/helpers/qunit-helpers"; import { exists } from "discourse/tests/helpers/qunit-helpers";
import { hbs } from "ember-cli-htmlbars"; import { hbs } from "ember-cli-htmlbars";
import TopicStatusIcons from "discourse/helpers/topic-status-icons"; import TopicStatusIcons from "discourse/helpers/topic-status-icons";
import createStore from "discourse/tests/helpers/create-store"; import { getOwner } from "discourse-common/lib/get-owner";
module("Integration | Component | Widget | topic-status", function (hooks) { module("Integration | Component | Widget | topic-status", function (hooks) {
setupRenderingTest(hooks); setupRenderingTest(hooks);
test("basics", async function (assert) { test("basics", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
this.set("args", { this.set("args", {
topic: store.createRecord("topic", { closed: true }), topic: store.createRecord("topic", { closed: true }),
disableActions: true, disableActions: true,
@ -24,7 +24,7 @@ module("Integration | Component | Widget | topic-status", function (hooks) {
}); });
test("extendability", async function (assert) { test("extendability", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
TopicStatusIcons.addObject([ TopicStatusIcons.addObject([
"has_accepted_answer", "has_accepted_answer",
"far-check-square", "far-check-square",
@ -45,7 +45,7 @@ module("Integration | Component | Widget | topic-status", function (hooks) {
}); });
test("toggling pin status", async function (assert) { test("toggling pin status", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
this.set("args", { this.set("args", {
topic: store.createRecord("topic", { closed: true, pinned: true }), topic: store.createRecord("topic", { closed: true, pinned: true }),
}); });

View File

@ -1,15 +1,15 @@
import { module, test } from "qunit"; import { module, test } from "qunit";
import { setupTest } from "ember-qunit"; import { setupTest } from "ember-qunit";
import createStore from "discourse/tests/helpers/create-store"; import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Controller | reorder-categories", function (hooks) { module("Unit | Controller | reorder-categories", function (hooks) {
setupTest(hooks); setupTest(hooks);
test("reorder set unique position number", function (assert) { test("reorder set unique position number", function (assert) {
const controller = this.owner.lookup("controller:reorder-categories"); const controller = getOwner(this).lookup("controller:reorder-categories");
const store = createStore(); const store = getOwner(this).lookup("service:store");
const site = this.owner.lookup("service:site"); const site = getOwner(this).lookup("service:site");
site.set("categories", [ site.set("categories", [
store.createRecord("category", { id: 1, position: 0 }), store.createRecord("category", { id: 1, position: 0 }),
store.createRecord("category", { id: 2, position: 0 }), store.createRecord("category", { id: 2, position: 0 }),
@ -24,8 +24,8 @@ module("Unit | Controller | reorder-categories", function (hooks) {
}); });
test("reorder places subcategories after their parent categories, while maintaining the relative order", function (assert) { test("reorder places subcategories after their parent categories, while maintaining the relative order", function (assert) {
const controller = this.owner.lookup("controller:reorder-categories"); const controller = getOwner(this).lookup("controller:reorder-categories");
const store = createStore(); const store = getOwner(this).lookup("service:store");
const parent = store.createRecord("category", { const parent = store.createRecord("category", {
id: 1, id: 1,
@ -51,7 +51,7 @@ module("Unit | Controller | reorder-categories", function (hooks) {
}); });
const expectedOrderSlugs = ["parent", "child2", "child1", "other"]; const expectedOrderSlugs = ["parent", "child2", "child1", "other"];
const site = this.owner.lookup("service:site"); const site = getOwner(this).lookup("service:site");
site.set("categories", [child2, parent, other, child1]); site.set("categories", [child2, parent, other, child1]);
controller.reorder(); controller.reorder();
@ -63,8 +63,8 @@ module("Unit | Controller | reorder-categories", function (hooks) {
}); });
test("changing the position number of a category should place it at given position", function (assert) { test("changing the position number of a category should place it at given position", function (assert) {
const controller = this.owner.lookup("controller:reorder-categories"); const controller = getOwner(this).lookup("controller:reorder-categories");
const store = createStore(); const store = getOwner(this).lookup("service:store");
const elem1 = store.createRecord("category", { const elem1 = store.createRecord("category", {
id: 1, id: 1,
@ -84,7 +84,7 @@ module("Unit | Controller | reorder-categories", function (hooks) {
slug: "test", slug: "test",
}); });
const site = this.owner.lookup("service:site"); const site = getOwner(this).lookup("service:site");
site.set("categories", [elem1, elem2, elem3]); site.set("categories", [elem1, elem2, elem3]);
// Move category 'foo' from position 0 to position 2 // Move category 'foo' from position 0 to position 2
@ -98,8 +98,8 @@ module("Unit | Controller | reorder-categories", function (hooks) {
}); });
test("changing the position number of a category should place it at given position and respect children", function (assert) { test("changing the position number of a category should place it at given position and respect children", function (assert) {
const controller = this.owner.lookup("controller:reorder-categories"); const controller = getOwner(this).lookup("controller:reorder-categories");
const store = createStore(); const store = getOwner(this).lookup("service:store");
const elem1 = store.createRecord("category", { const elem1 = store.createRecord("category", {
id: 1, id: 1,
@ -126,7 +126,7 @@ module("Unit | Controller | reorder-categories", function (hooks) {
slug: "test", slug: "test",
}); });
const site = this.owner.lookup("service:site"); const site = getOwner(this).lookup("service:site");
site.set("categories", [elem1, child1, elem2, elem3]); site.set("categories", [elem1, child1, elem2, elem3]);
controller.send("change", elem1, { target: { value: 3 } }); controller.send("change", elem1, { target: { value: 3 } });
@ -140,8 +140,8 @@ module("Unit | Controller | reorder-categories", function (hooks) {
}); });
test("changing the position through click on arrow of a category should place it at given position and respect children", function (assert) { test("changing the position through click on arrow of a category should place it at given position and respect children", function (assert) {
const controller = this.owner.lookup("controller:reorder-categories"); const controller = getOwner(this).lookup("controller:reorder-categories");
const store = createStore(); const store = getOwner(this).lookup("service:store");
const child2 = store.createRecord("category", { const child2 = store.createRecord("category", {
id: 105, id: 105,
@ -177,7 +177,7 @@ module("Unit | Controller | reorder-categories", function (hooks) {
slug: "test", slug: "test",
}); });
const site = this.owner.lookup("service:site"); const site = getOwner(this).lookup("service:site");
site.set("categories", [elem1, child1, child2, elem2, elem3]); site.set("categories", [elem1, child1, child2, elem2, elem3]);
controller.reorder(); controller.reorder();

View File

@ -1,17 +1,19 @@
import { module, test } from "qunit";
import Site from "discourse/models/site"; import Site from "discourse/models/site";
import { categoryBadgeHTML } from "discourse/helpers/category-link"; import { categoryBadgeHTML } from "discourse/helpers/category-link";
import createStore from "discourse/tests/helpers/create-store";
import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
import sinon from "sinon"; import sinon from "sinon";
import { test } from "qunit"; import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
module("Unit | Utility | category-badge", function (hooks) {
setupTest(hooks);
discourseModule("Unit | Utility | category-badge", function () {
test("categoryBadge without a category", function (assert) { test("categoryBadge without a category", function (assert) {
assert.blank(categoryBadgeHTML(), "it returns no HTML"); assert.blank(categoryBadgeHTML(), "it returns no HTML");
}); });
test("Regular categoryBadge", function (assert) { test("Regular categoryBadge", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const category = store.createRecord("category", { const category = store.createRecord("category", {
name: "hello", name: "hello",
id: 123, id: 123,
@ -42,7 +44,7 @@ discourseModule("Unit | Utility | category-badge", function () {
}); });
test("undefined color", function (assert) { test("undefined color", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const noColor = store.createRecord("category", { name: "hello", id: 123 }); const noColor = store.createRecord("category", { name: "hello", id: 123 });
const tag = $.parseHTML(categoryBadgeHTML(noColor))[0]; const tag = $.parseHTML(categoryBadgeHTML(noColor))[0];
@ -53,7 +55,7 @@ discourseModule("Unit | Utility | category-badge", function () {
}); });
test("topic count", function (assert) { test("topic count", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const category = store.createRecord("category", { name: "hello", id: 123 }); const category = store.createRecord("category", { name: "hello", id: 123 });
assert.ok( assert.ok(
@ -68,7 +70,7 @@ discourseModule("Unit | Utility | category-badge", function () {
}); });
test("allowUncategorized", function (assert) { test("allowUncategorized", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const uncategorized = store.createRecord("category", { const uncategorized = store.createRecord("category", {
name: "uncategorized", name: "uncategorized",
id: 345, id: 345,
@ -90,8 +92,10 @@ discourseModule("Unit | Utility | category-badge", function () {
}); });
test("category names are wrapped in dir-spans", function (assert) { test("category names are wrapped in dir-spans", function (assert) {
this.siteSettings.support_mixed_text_direction = true; const siteSettings = getOwner(this).lookup("service:site-settings");
const store = createStore(); siteSettings.support_mixed_text_direction = true;
const store = getOwner(this).lookup("service:store");
const rtlCategory = store.createRecord("category", { const rtlCategory = store.createRecord("category", {
name: "תכנות עם Ruby", name: "תכנות עם Ruby",
id: 123, id: 123,
@ -115,7 +119,8 @@ discourseModule("Unit | Utility | category-badge", function () {
}); });
test("recursive", function (assert) { test("recursive", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const siteSettings = getOwner(this).lookup("service:site-settings");
const foo = store.createRecord("category", { const foo = store.createRecord("category", {
name: "foo", name: "foo",
@ -134,20 +139,20 @@ discourseModule("Unit | Utility | category-badge", function () {
parent_category_id: bar.id, parent_category_id: bar.id,
}); });
this.siteSettings.max_category_nesting = 0; siteSettings.max_category_nesting = 0;
assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("baz")); assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("baz"));
assert.ok(!categoryBadgeHTML(baz, { recursive: true }).includes("bar")); assert.ok(!categoryBadgeHTML(baz, { recursive: true }).includes("bar"));
this.siteSettings.max_category_nesting = 1; siteSettings.max_category_nesting = 1;
assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("baz")); assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("baz"));
assert.ok(!categoryBadgeHTML(baz, { recursive: true }).includes("bar")); assert.ok(!categoryBadgeHTML(baz, { recursive: true }).includes("bar"));
this.siteSettings.max_category_nesting = 2; siteSettings.max_category_nesting = 2;
assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("baz")); assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("baz"));
assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("bar")); assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("bar"));
assert.ok(!categoryBadgeHTML(baz, { recursive: true }).includes("foo")); assert.ok(!categoryBadgeHTML(baz, { recursive: true }).includes("foo"));
this.siteSettings.max_category_nesting = 3; siteSettings.max_category_nesting = 3;
assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("baz")); assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("baz"));
assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("bar")); assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("bar"));
assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("foo")); assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("foo"));

View File

@ -1,11 +1,14 @@
import { module, test } from "qunit"; import { module, test } from "qunit";
import Category from "discourse/models/category"; import Category from "discourse/models/category";
import createStore from "discourse/tests/helpers/create-store";
import sinon from "sinon"; import sinon from "sinon";
import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
module("Unit | Model | category", function (hooks) {
setupTest(hooks);
module("Unit | Model | category", function () {
test("slugFor", function (assert) { test("slugFor", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const slugFor = function (cat, val, text) { const slugFor = function (cat, val, text) {
assert.strictEqual(Category.slugFor(cat), val, text); assert.strictEqual(Category.slugFor(cat), val, text);
@ -68,7 +71,7 @@ module("Unit | Model | category", function () {
test("findBySlug", function (assert) { test("findBySlug", function (assert) {
assert.expect(6); assert.expect(6);
const store = createStore(); const store = getOwner(this).lookup("service:store");
const darth = store.createRecord("category", { id: 1, slug: "darth" }), const darth = store.createRecord("category", { id: 1, slug: "darth" }),
luke = store.createRecord("category", { luke = store.createRecord("category", {
id: 2, id: 2,
@ -133,7 +136,7 @@ module("Unit | Model | category", function () {
test("findSingleBySlug", function (assert) { test("findSingleBySlug", function (assert) {
assert.expect(6); assert.expect(6);
const store = createStore(); const store = getOwner(this).lookup("service:store");
const darth = store.createRecord("category", { id: 1, slug: "darth" }), const darth = store.createRecord("category", { id: 1, slug: "darth" }),
luke = store.createRecord("category", { luke = store.createRecord("category", {
id: 2, id: 2,
@ -196,7 +199,7 @@ module("Unit | Model | category", function () {
}); });
test("findBySlugPathWithID", function (assert) { test("findBySlugPathWithID", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const foo = store.createRecord("category", { id: 1, slug: "foo" }); const foo = store.createRecord("category", { id: 1, slug: "foo" });
const bar = store.createRecord("category", { const bar = store.createRecord("category", {
@ -220,7 +223,7 @@ module("Unit | Model | category", function () {
}); });
test("minimumRequiredTags", function (assert) { test("minimumRequiredTags", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
let foo = store.createRecord("category", { let foo = store.createRecord("category", {
id: 1, id: 1,
@ -263,23 +266,23 @@ module("Unit | Model | category", function () {
}); });
test("search with category name", function (assert) { test("search with category name", function (assert) {
const store = createStore(), const store = getOwner(this).lookup("service:store");
category1 = store.createRecord("category", { const category1 = store.createRecord("category", {
id: 1, id: 1,
name: "middle term", name: "middle term",
slug: "different-slug", slug: "different-slug",
}), });
category2 = store.createRecord("category", { const category2 = store.createRecord("category", {
id: 2, id: 2,
name: "middle term", name: "middle term",
slug: "another-different-slug", slug: "another-different-slug",
}), });
subcategory = store.createRecord("category", { const subcategory = store.createRecord("category", {
id: 3, id: 3,
name: "middle term", name: "middle term",
slug: "another-different-slug2", slug: "another-different-slug2",
parent_category_id: 2, parent_category_id: 2,
}); });
sinon sinon
.stub(Category, "listByActivity") .stub(Category, "listByActivity")
@ -369,17 +372,17 @@ module("Unit | Model | category", function () {
}); });
test("search with category slug", function (assert) { test("search with category slug", function (assert) {
const store = createStore(), const store = getOwner(this).lookup("service:store");
category1 = store.createRecord("category", { const category1 = store.createRecord("category", {
id: 1, id: 1,
name: "middle term", name: "middle term",
slug: "different-slug", slug: "different-slug",
}), });
category2 = store.createRecord("category", { const category2 = store.createRecord("category", {
id: 2, id: 2,
name: "middle term", name: "middle term",
slug: "another-different-slug", slug: "another-different-slug",
}); });
sinon.stub(Category, "listByActivity").returns([category1, category2]); sinon.stub(Category, "listByActivity").returns([category1, category2]);

View File

@ -4,33 +4,36 @@ import {
PRIVATE_MESSAGE, PRIVATE_MESSAGE,
REPLY, REPLY,
} from "discourse/models/composer"; } from "discourse/models/composer";
import { import { currentUser } from "discourse/tests/helpers/qunit-helpers";
currentUser,
discourseModule,
} from "discourse/tests/helpers/qunit-helpers";
import AppEvents from "discourse/services/app-events"; import AppEvents from "discourse/services/app-events";
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import createStore from "discourse/tests/helpers/create-store"; import { module, test } from "qunit";
import { test } from "qunit";
import { getOwner } from "discourse-common/lib/get-owner"; import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
function createComposer(opts) { function createComposer(opts = {}) {
opts = opts || {}; opts.user ??= currentUser();
opts.user = opts.user || currentUser();
opts.appEvents = AppEvents.create(); opts.appEvents = AppEvents.create();
return createStore().createRecord("composer", opts); const store = getOwner(this).lookup("service:store");
return store.createRecord("composer", opts);
} }
function openComposer(opts) { function openComposer(opts) {
const composer = createComposer(opts); const composer = createComposer.call(this, opts);
composer.open(opts); composer.open(opts);
return composer; return composer;
} }
discourseModule("Unit | Model | composer", function () { module("Unit | Model | composer", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
this.siteSettings = getOwner(this).lookup("service:site-settings");
});
test("replyLength", function (assert) { test("replyLength", function (assert) {
const replyLength = function (val, expectedLength) { const replyLength = function (val, expectedLength) {
const composer = createComposer({ reply: val }); const composer = createComposer.call(this, { reply: val });
assert.strictEqual(composer.get("replyLength"), expectedLength); assert.strictEqual(composer.get("replyLength"), expectedLength);
}; };
@ -78,7 +81,7 @@ discourseModule("Unit | Model | composer", function () {
if (isFirstPost) { if (isFirstPost) {
action = CREATE_TOPIC; action = CREATE_TOPIC;
} }
const composer = createComposer({ reply: val, action }); const composer = createComposer.call(this, { reply: val, action });
assert.strictEqual( assert.strictEqual(
composer.get("missingReplyCharacters"), composer.get("missingReplyCharacters"),
expected, expected,
@ -111,7 +114,7 @@ discourseModule("Unit | Model | composer", function () {
const link = "http://imgur.com/gallery/grxX8"; const link = "http://imgur.com/gallery/grxX8";
this.siteSettings.topic_featured_link_enabled = true; this.siteSettings.topic_featured_link_enabled = true;
this.siteSettings.topic_featured_link_allowed_category_ids = 12345; this.siteSettings.topic_featured_link_allowed_category_ids = 12345;
const composer = createComposer({ const composer = createComposer.call(this, {
title: link, title: link,
categoryId: 12345, categoryId: 12345,
featuredLink: link, featuredLink: link,
@ -128,7 +131,7 @@ discourseModule("Unit | Model | composer", function () {
test("missingTitleCharacters", function (assert) { test("missingTitleCharacters", function (assert) {
const missingTitleCharacters = function (val, isPM, expected, message) { const missingTitleCharacters = function (val, isPM, expected, message) {
const composer = createComposer({ const composer = createComposer.call(this, {
title: val, title: val,
action: isPM ? PRIVATE_MESSAGE : REPLY, action: isPM ? PRIVATE_MESSAGE : REPLY,
}); });
@ -252,7 +255,7 @@ discourseModule("Unit | Model | composer", function () {
test("Title length for private messages", function (assert) { test("Title length for private messages", function (assert) {
this.siteSettings.min_personal_message_title_length = 5; this.siteSettings.min_personal_message_title_length = 5;
this.siteSettings.max_topic_title_length = 10; this.siteSettings.max_topic_title_length = 10;
const composer = createComposer({ action: PRIVATE_MESSAGE }); const composer = createComposer.call(this, { action: PRIVATE_MESSAGE });
composer.set("title", "asdf"); composer.set("title", "asdf");
assert.ok(!composer.get("titleLengthValid"), "short titles are not valid"); assert.ok(!composer.get("titleLengthValid"), "short titles are not valid");
@ -265,7 +268,7 @@ discourseModule("Unit | Model | composer", function () {
}); });
test("Post length for private messages with non human users", function (assert) { test("Post length for private messages with non human users", function (assert) {
const composer = createComposer({ const composer = createComposer.call(this, {
topic: EmberObject.create({ pm_with_non_human_user: true }), topic: EmberObject.create({ pm_with_non_human_user: true }),
}); });
@ -293,7 +296,7 @@ discourseModule("Unit | Model | composer", function () {
test("clearState", function (assert) { test("clearState", function (assert) {
const store = getOwner(this).lookup("service:store"); const store = getOwner(this).lookup("service:store");
const composer = createComposer({ const composer = createComposer.call(this, {
originalText: "asdf", originalText: "asdf",
reply: "asdf2", reply: "asdf2",
post: store.createRecord("post", { id: 1 }), post: store.createRecord("post", { id: 1 }),
@ -310,7 +313,7 @@ discourseModule("Unit | Model | composer", function () {
test("initial category when uncategorized is allowed", function (assert) { test("initial category when uncategorized is allowed", function (assert) {
this.siteSettings.allow_uncategorized_topics = true; this.siteSettings.allow_uncategorized_topics = true;
const composer = openComposer({ const composer = openComposer.call(this, {
action: CREATE_TOPIC, action: CREATE_TOPIC,
draftKey: "asfd", draftKey: "asfd",
draftSequence: 1, draftSequence: 1,
@ -320,7 +323,7 @@ discourseModule("Unit | Model | composer", function () {
test("initial category when uncategorized is not allowed", function (assert) { test("initial category when uncategorized is not allowed", function (assert) {
this.siteSettings.allow_uncategorized_topics = false; this.siteSettings.allow_uncategorized_topics = false;
const composer = openComposer({ const composer = openComposer.call(this, {
action: CREATE_TOPIC, action: CREATE_TOPIC,
draftKey: "asfd", draftKey: "asfd",
draftSequence: 1, draftSequence: 1,
@ -335,7 +338,7 @@ discourseModule("Unit | Model | composer", function () {
const quote = const quote =
'[quote="neil, post:5, topic:413"]\nSimmer down you two.\n[/quote]'; '[quote="neil, post:5, topic:413"]\nSimmer down you two.\n[/quote]';
const newComposer = function () { const newComposer = function () {
return openComposer({ return openComposer.call(this, {
action: REPLY, action: REPLY,
draftKey: "asfd", draftKey: "asfd",
draftSequence: 1, draftSequence: 1,
@ -386,14 +389,14 @@ discourseModule("Unit | Model | composer", function () {
test("title placeholder depends on what you're doing", function (assert) { test("title placeholder depends on what you're doing", function (assert) {
this.siteSettings.topic_featured_link_enabled = false; this.siteSettings.topic_featured_link_enabled = false;
let composer = createComposer({ action: CREATE_TOPIC }); let composer = createComposer.call(this, { action: CREATE_TOPIC });
assert.strictEqual( assert.strictEqual(
composer.get("titlePlaceholder"), composer.get("titlePlaceholder"),
"composer.title_placeholder", "composer.title_placeholder",
"placeholder for normal topic" "placeholder for normal topic"
); );
composer = createComposer({ action: PRIVATE_MESSAGE }); composer = createComposer.call(this, { action: PRIVATE_MESSAGE });
assert.strictEqual( assert.strictEqual(
composer.get("titlePlaceholder"), composer.get("titlePlaceholder"),
"composer.title_placeholder", "composer.title_placeholder",
@ -402,14 +405,14 @@ discourseModule("Unit | Model | composer", function () {
this.siteSettings.topic_featured_link_enabled = true; this.siteSettings.topic_featured_link_enabled = true;
composer = createComposer({ action: CREATE_TOPIC }); composer = createComposer.call(this, { action: CREATE_TOPIC });
assert.strictEqual( assert.strictEqual(
composer.get("titlePlaceholder"), composer.get("titlePlaceholder"),
"composer.title_or_link_placeholder", "composer.title_or_link_placeholder",
"placeholder invites you to paste a link" "placeholder invites you to paste a link"
); );
composer = createComposer({ action: PRIVATE_MESSAGE }); composer = createComposer.call(this, { action: PRIVATE_MESSAGE });
assert.strictEqual( assert.strictEqual(
composer.get("titlePlaceholder"), composer.get("titlePlaceholder"),
"composer.title_placeholder", "composer.title_placeholder",
@ -420,7 +423,7 @@ discourseModule("Unit | Model | composer", function () {
test("allows featured link before choosing a category", function (assert) { test("allows featured link before choosing a category", function (assert) {
this.siteSettings.topic_featured_link_enabled = true; this.siteSettings.topic_featured_link_enabled = true;
this.siteSettings.allow_uncategorized_topics = false; this.siteSettings.allow_uncategorized_topics = false;
let composer = createComposer({ action: CREATE_TOPIC }); let composer = createComposer.call(this, { action: CREATE_TOPIC });
assert.strictEqual( assert.strictEqual(
composer.get("titlePlaceholder"), composer.get("titlePlaceholder"),
"composer.title_or_link_placeholder", "composer.title_or_link_placeholder",
@ -430,7 +433,7 @@ discourseModule("Unit | Model | composer", function () {
}); });
test("targetRecipientsArray contains types", function (assert) { test("targetRecipientsArray contains types", function (assert) {
let composer = createComposer({ let composer = createComposer.call(this, {
targetRecipients: "test,codinghorror,staff,foo@bar.com", targetRecipients: "test,codinghorror,staff,foo@bar.com",
}); });
assert.ok(composer.targetRecipientsArray, [ assert.ok(composer.targetRecipientsArray, [

View File

@ -2,13 +2,17 @@ import { module, test } from "qunit";
import Category from "discourse/models/category"; import Category from "discourse/models/category";
import NavItem from "discourse/models/nav-item"; import NavItem from "discourse/models/nav-item";
import Site from "discourse/models/site"; import Site from "discourse/models/site";
import createStore from "discourse/tests/helpers/create-store";
import { run } from "@ember/runloop"; import { run } from "@ember/runloop";
import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
module("Unit | Model | nav-item", function (hooks) { module("Unit | Model | nav-item", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () { hooks.beforeEach(function () {
run(function () { run(function () {
const fooCategory = Category.create({ const store = getOwner(this).lookup("service:store");
const fooCategory = store.createRecord("category", {
slug: "foo", slug: "foo",
id: 123, id: 123,
}); });
@ -39,7 +43,8 @@ module("Unit | Model | nav-item", function (hooks) {
}); });
test("count", function (assert) { test("count", function (assert) {
const navItem = createStore().createRecord("nav-item", { name: "new" }); 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.get("count"), 0, "it has no count by default");
@ -59,7 +64,8 @@ module("Unit | Model | nav-item", function (hooks) {
}); });
test("displayName", function (assert) { test("displayName", function (assert) {
const navItem = createStore().createRecord("nav-item", { const store = getOwner(this).lookup("service:store");
const navItem = store.createRecord("nav-item", {
name: "something", name: "something",
}); });
@ -73,7 +79,8 @@ module("Unit | Model | nav-item", function (hooks) {
}); });
test("title", function (assert) { test("title", function (assert) {
const navItem = createStore().createRecord("nav-item", { const store = getOwner(this).lookup("service:store");
const navItem = store.createRecord("nav-item", {
name: "something", name: "something",
}); });

View File

@ -1,28 +1,30 @@
import { module, test } from "qunit"; import { module, test } from "qunit";
import AppEvents from "discourse/services/app-events";
import ArrayProxy from "@ember/array/proxy"; import ArrayProxy from "@ember/array/proxy";
import Post from "discourse/models/post"; import Post from "discourse/models/post";
import User from "discourse/models/user"; import User from "discourse/models/user";
import createStore from "discourse/tests/helpers/create-store";
import pretender, { response } from "discourse/tests/helpers/create-pretender"; import pretender, { response } from "discourse/tests/helpers/create-pretender";
import sinon from "sinon"; import sinon from "sinon";
import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
function buildStream(id, stream) { function buildStream(id, stream) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const topic = store.createRecord("topic", { id, chunk_size: 5 }); const topic = store.createRecord("topic", { id, chunk_size: 5 });
const ps = topic.postStream;
if (stream) { if (stream) {
ps.set("stream", stream); topic.postStream.set("stream", stream);
} }
ps.appEvents = AppEvents.create();
return ps; return topic.postStream;
} }
const participant = { username: "eviltrout" }; const participant = { username: "eviltrout" };
module("Unit | Model | post-stream", function () { module("Unit | Model | post-stream", function (hooks) {
setupTest(hooks);
test("create", function (assert) { test("create", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
assert.ok( assert.ok(
store.createRecord("postStream"), store.createRecord("postStream"),
"it can be created with no parameters" "it can be created with no parameters"
@ -30,15 +32,15 @@ module("Unit | Model | post-stream", function () {
}); });
test("defaults", function (assert) { test("defaults", function (assert) {
const postStream = buildStream(1234); const postStream = buildStream.call(this, 1234);
assert.blank(postStream.posts, "there are no posts in a stream by default"); assert.blank(postStream.posts, "there are no posts in a stream by default");
assert.ok(!postStream.get("loaded"), "it has never loaded"); assert.ok(!postStream.get("loaded"), "it has never loaded");
assert.present(postStream.get("topic")); assert.present(postStream.get("topic"));
}); });
test("appending posts", function (assert) { test("appending posts", function (assert) {
const postStream = buildStream(4567, [1, 3, 4]); const postStream = buildStream.call(this, 4567, [1, 3, 4]);
const store = postStream.store; const store = getOwner(this).lookup("service:store");
assert.strictEqual( assert.strictEqual(
postStream.get("lastPostId"), postStream.get("lastPostId"),
@ -121,8 +123,8 @@ module("Unit | Model | post-stream", function () {
}); });
test("closestPostNumberFor", function (assert) { test("closestPostNumberFor", function (assert) {
const postStream = buildStream(1231); const postStream = buildStream.call(this, 1231);
const store = postStream.store; const store = getOwner(this).lookup("service:store");
assert.blank( assert.blank(
postStream.closestPostNumberFor(1), postStream.closestPostNumberFor(1),
@ -159,7 +161,7 @@ module("Unit | Model | post-stream", function () {
}); });
test("closestDaysAgoFor", function (assert) { test("closestDaysAgoFor", function (assert) {
const postStream = buildStream(1231); const postStream = buildStream.call(this, 1231);
postStream.set("timelineLookup", [ postStream.set("timelineLookup", [
[1, 10], [1, 10],
[3, 8], [3, 8],
@ -182,14 +184,14 @@ module("Unit | Model | post-stream", function () {
}); });
test("closestDaysAgoFor - empty", function (assert) { test("closestDaysAgoFor - empty", function (assert) {
const postStream = buildStream(1231); const postStream = buildStream.call(this, 1231);
postStream.set("timelineLookup", []); postStream.set("timelineLookup", []);
assert.strictEqual(postStream.closestDaysAgoFor(1), undefined); assert.strictEqual(postStream.closestDaysAgoFor(1), undefined);
}); });
test("updateFromJson", function (assert) { test("updateFromJson", function (assert) {
const postStream = buildStream(1231); const postStream = buildStream.call(this, 1231);
postStream.updateFromJson({ postStream.updateFromJson({
posts: [{ id: 1 }], posts: [{ id: 1 }],
@ -208,8 +210,8 @@ module("Unit | Model | post-stream", function () {
}); });
test("removePosts", function (assert) { test("removePosts", function (assert) {
const postStream = buildStream(10000001, [1, 2, 3]); const postStream = buildStream.call(this, 10000001, [1, 2, 3]);
const store = postStream.store; const store = getOwner(this).lookup("service:store");
const p1 = store.createRecord("post", { id: 1, post_number: 2 }), const p1 = store.createRecord("post", { id: 1, post_number: 2 }),
p2 = store.createRecord("post", { id: 2, post_number: 3 }), p2 = store.createRecord("post", { id: 2, post_number: 3 }),
@ -229,7 +231,7 @@ module("Unit | Model | post-stream", function () {
}); });
test("cancelFilter", function (assert) { test("cancelFilter", function (assert) {
const postStream = buildStream(1235); const postStream = buildStream.call(this, 1235);
sinon.stub(postStream, "refresh").resolves(); sinon.stub(postStream, "refresh").resolves();
@ -246,7 +248,11 @@ module("Unit | Model | post-stream", function () {
}); });
test("findPostIdForPostNumber", function (assert) { test("findPostIdForPostNumber", function (assert) {
const postStream = buildStream(1234, [10, 20, 30, 40, 50, 60, 70]); const postStream = buildStream.call(
this,
1234,
[10, 20, 30, 40, 50, 60, 70]
);
postStream.set("gaps", { before: { 60: [55, 58] } }); postStream.set("gaps", { before: { 60: [55, 58] } });
assert.strictEqual( assert.strictEqual(
@ -272,7 +278,7 @@ module("Unit | Model | post-stream", function () {
}); });
test("fillGapBefore", function (assert) { test("fillGapBefore", function (assert) {
const postStream = buildStream(1234, [60]); const postStream = buildStream.call(this, 1234, [60]);
sinon.stub(postStream, "findPostsByIds").resolves([]); sinon.stub(postStream, "findPostsByIds").resolves([]);
let post = postStream.store.createRecord("post", { let post = postStream.store.createRecord("post", {
id: 60, id: 60,
@ -292,7 +298,7 @@ module("Unit | Model | post-stream", function () {
}); });
test("filterParticipant", function (assert) { test("filterParticipant", function (assert) {
const postStream = buildStream(1236); const postStream = buildStream.call(this, 1236);
sinon.stub(postStream, "refresh").resolves(); sinon.stub(postStream, "refresh").resolves();
assert.strictEqual( assert.strictEqual(
@ -312,7 +318,7 @@ module("Unit | Model | post-stream", function () {
}); });
test("filterReplies", function (assert) { test("filterReplies", function (assert) {
const postStream = buildStream(1234), const postStream = buildStream.call(this, 1234),
store = postStream.store; store = postStream.store;
postStream.appendPost( postStream.appendPost(
@ -343,7 +349,7 @@ module("Unit | Model | post-stream", function () {
}); });
test("filterUpwards", function (assert) { test("filterUpwards", function (assert) {
const postStream = buildStream(1234), const postStream = buildStream.call(this, 1234),
store = postStream.store; store = postStream.store;
postStream.appendPost( postStream.appendPost(
@ -374,7 +380,7 @@ module("Unit | Model | post-stream", function () {
}); });
test("streamFilters", function (assert) { test("streamFilters", function (assert) {
const postStream = buildStream(1237); const postStream = buildStream.call(this, 1237);
sinon.stub(postStream, "refresh").resolves(); sinon.stub(postStream, "refresh").resolves();
assert.deepEqual( assert.deepEqual(
@ -424,23 +430,24 @@ module("Unit | Model | post-stream", function () {
}); });
test("loading", function (assert) { test("loading", function (assert) {
let postStream = buildStream(1234); let postStream = buildStream.call(this, 1234);
assert.ok(!postStream.get("loading"), "we're not loading by default"); assert.ok(!postStream.get("loading"), "we're not loading by default");
postStream.set("loadingAbove", true); postStream.set("loadingAbove", true);
assert.ok(postStream.get("loading"), "we're loading if loading above"); assert.ok(postStream.get("loading"), "we're loading if loading above");
postStream = buildStream(1234); postStream = buildStream.call(this, 1234);
postStream.set("loadingBelow", true); postStream.set("loadingBelow", true);
assert.ok(postStream.get("loading"), "we're loading if loading below"); assert.ok(postStream.get("loading"), "we're loading if loading below");
postStream = buildStream(1234); postStream = buildStream.call(this, 1234);
postStream.set("loadingFilter", true); postStream.set("loadingFilter", true);
assert.ok(postStream.get("loading"), "we're loading if loading a filter"); assert.ok(postStream.get("loading"), "we're loading if loading a filter");
}); });
test("nextWindow", function (assert) { test("nextWindow", function (assert) {
const postStream = buildStream( const postStream = buildStream.call(
this,
1234, 1234,
[1, 2, 3, 5, 8, 9, 10, 11, 13, 14, 15, 16] [1, 2, 3, 5, 8, 9, 10, 11, 13, 14, 15, 16]
); );
@ -472,7 +479,8 @@ module("Unit | Model | post-stream", function () {
}); });
test("previousWindow", function (assert) { test("previousWindow", function (assert) {
const postStream = buildStream( const postStream = buildStream.call(
this,
1234, 1234,
[1, 2, 3, 5, 8, 9, 10, 11, 13, 14, 15, 16] [1, 2, 3, 5, 8, 9, 10, 11, 13, 14, 15, 16]
); );
@ -504,7 +512,7 @@ module("Unit | Model | post-stream", function () {
}); });
test("storePost", function (assert) { test("storePost", function (assert) {
const postStream = buildStream(1234), const postStream = buildStream.call(this, 1234),
store = postStream.store, store = postStream.store,
post = store.createRecord("post", { post = store.createRecord("post", {
id: 1, id: 1,
@ -552,8 +560,8 @@ module("Unit | Model | post-stream", function () {
}); });
test("identity map", async function (assert) { test("identity map", async function (assert) {
const postStream = buildStream(1234); const postStream = buildStream.call(this, 1234);
const store = postStream.store; const store = getOwner(this).lookup("service:store");
const p1 = postStream.appendPost( const p1 = postStream.appendPost(
store.createRecord("post", { id: 1, post_number: 1 }) store.createRecord("post", { id: 1, post_number: 1 })
@ -578,7 +586,7 @@ module("Unit | Model | post-stream", function () {
}); });
test("loadIntoIdentityMap with no data", async function (assert) { test("loadIntoIdentityMap with no data", async function (assert) {
const result = await buildStream(1234).loadIntoIdentityMap([]); const result = await buildStream.call(this, 1234).loadIntoIdentityMap([]);
assert.strictEqual( assert.strictEqual(
result.length, result.length,
0, 0,
@ -587,7 +595,7 @@ module("Unit | Model | post-stream", function () {
}); });
test("loadIntoIdentityMap with post ids", async function (assert) { test("loadIntoIdentityMap with post ids", async function (assert) {
const postStream = buildStream(1234); const postStream = buildStream.call(this, 1234);
await postStream.loadIntoIdentityMap([10]); await postStream.loadIntoIdentityMap([10]);
assert.present( assert.present(
@ -597,8 +605,8 @@ module("Unit | Model | post-stream", function () {
}); });
test("appendMore for megatopic", async function (assert) { test("appendMore for megatopic", async function (assert) {
const postStream = buildStream(1234); const postStream = buildStream.call(this, 1234);
const store = createStore(); const store = getOwner(this).lookup("service:store");
const post = store.createRecord("post", { id: 1, post_number: 1 }); const post = store.createRecord("post", { id: 1, post_number: 1 });
postStream.setProperties({ postStream.setProperties({
@ -620,8 +628,8 @@ module("Unit | Model | post-stream", function () {
}); });
test("prependMore for megatopic", async function (assert) { test("prependMore for megatopic", async function (assert) {
const postStream = buildStream(1234); const postStream = buildStream.call(this, 1234);
const store = createStore(); const store = getOwner(this).lookup("service:store");
const post = store.createRecord("post", { id: 6, post_number: 6 }); const post = store.createRecord("post", { id: 6, post_number: 6 });
postStream.setProperties({ postStream.setProperties({
@ -643,8 +651,8 @@ module("Unit | Model | post-stream", function () {
}); });
test("staging and undoing a new post", function (assert) { test("staging and undoing a new post", function (assert) {
const postStream = buildStream(10101, [1]); const postStream = buildStream.call(this, 10101, [1]);
const store = postStream.store; const store = getOwner(this).lookup("service:store");
const original = store.createRecord("post", { const original = store.createRecord("post", {
id: 1, id: 1,
@ -658,7 +666,7 @@ module("Unit | Model | post-stream", function () {
"the original post is lastAppended" "the original post is lastAppended"
); );
const user = User.create({ const user = store.createRecord("user", {
username: "eviltrout", username: "eviltrout",
name: "eviltrout", name: "eviltrout",
id: 321, id: 321,
@ -759,8 +767,8 @@ module("Unit | Model | post-stream", function () {
}); });
test("staging and committing a post", function (assert) { test("staging and committing a post", function (assert) {
const postStream = buildStream(10101, [1]); const postStream = buildStream.call(this, 10101, [1]);
const store = postStream.store; const store = getOwner(this).lookup("service:store");
const original = store.createRecord("post", { const original = store.createRecord("post", {
id: 1, id: 1,
@ -774,7 +782,7 @@ module("Unit | Model | post-stream", function () {
"the original post is lastAppended" "the original post is lastAppended"
); );
const user = User.create({ const user = store.createRecord("user", {
username: "eviltrout", username: "eviltrout",
name: "eviltrout", name: "eviltrout",
id: 321, id: 321,
@ -843,8 +851,8 @@ module("Unit | Model | post-stream", function () {
test("loadedAllPosts when the id changes", function (assert) { test("loadedAllPosts when the id changes", function (assert) {
// This can happen in a race condition between staging a post and it coming through on the // This can happen in a race condition between staging a post and it coming through on the
// message bus. If the id of a post changes we should reconsider the loadedAllPosts property. // message bus. If the id of a post changes we should reconsider the loadedAllPosts property.
const postStream = buildStream(10101, [1, 2]); const postStream = buildStream.call(this, 10101, [1, 2]);
const store = postStream.store; const store = getOwner(this).lookup("service:store");
const postWithoutId = store.createRecord("post", { const postWithoutId = store.createRecord("post", {
raw: "hello world this is my new post", raw: "hello world this is my new post",
}); });
@ -863,8 +871,8 @@ module("Unit | Model | post-stream", function () {
}); });
test("triggerRecoveredPost", async function (assert) { test("triggerRecoveredPost", async function (assert) {
const postStream = buildStream(4567); const postStream = buildStream.call(this, 4567);
const store = postStream.store; const store = getOwner(this).lookup("service:store");
[1, 2, 3, 5].forEach((id) => { [1, 2, 3, 5].forEach((id) => {
postStream.appendPost( postStream.appendPost(
@ -892,13 +900,13 @@ module("Unit | Model | post-stream", function () {
}); });
test("committing and triggerNewPostsInStream race condition", function (assert) { test("committing and triggerNewPostsInStream race condition", function (assert) {
const postStream = buildStream(4964); const postStream = buildStream.call(this, 4964);
const store = postStream.store; const store = getOwner(this).lookup("service:store");
postStream.appendPost( postStream.appendPost(
store.createRecord("post", { id: 1, post_number: 1 }) store.createRecord("post", { id: 1, post_number: 1 })
); );
const user = User.create({ const user = store.createRecord("user", {
username: "eviltrout", username: "eviltrout",
name: "eviltrout", name: "eviltrout",
id: 321, id: 321,
@ -932,14 +940,14 @@ module("Unit | Model | post-stream", function () {
}); });
test("triggerNewPostInStream for ignored posts", async function (assert) { test("triggerNewPostInStream for ignored posts", async function (assert) {
const postStream = buildStream(280, [1]); const postStream = buildStream.call(this, 280, [1]);
const store = postStream.store; const store = getOwner(this).lookup("service:store");
User.resetCurrent( User.resetCurrent(
User.create({ store.createRecord("user", {
username: "eviltrout", username: "eviltrout",
name: "eviltrout", name: "eviltrout",
id: 321, id: 321,
ignored_users: ["ignoreduser"], ignored_users: ["ignored-user"],
}) })
); );
@ -950,13 +958,13 @@ module("Unit | Model | post-stream", function () {
const post2 = store.createRecord("post", { const post2 = store.createRecord("post", {
id: 101, id: 101,
post_number: 2, post_number: 2,
username: "regularuser", username: "regular-user",
}); });
const post3 = store.createRecord("post", { const post3 = store.createRecord("post", {
id: 102, id: 102,
post_number: 3, post_number: 3,
username: "ignoreduser", username: "ignored-user",
}); });
let stub = sinon.stub(postStream, "findPostsByIds").resolves([post2]); let stub = sinon.stub(postStream, "findPostsByIds").resolves([post2]);
@ -990,9 +998,13 @@ module("Unit | Model | post-stream", function () {
}); });
test("postsWithPlaceholders", async function (assert) { test("postsWithPlaceholders", async function (assert) {
const postStream = buildStream(4964, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); const postStream = buildStream.call(
this,
4964,
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
);
const postsWithPlaceholders = postStream.get("postsWithPlaceholders"); const postsWithPlaceholders = postStream.get("postsWithPlaceholders");
const store = postStream.store; const store = getOwner(this).lookup("service:store");
const testProxy = ArrayProxy.create({ content: postsWithPlaceholders }); const testProxy = ArrayProxy.create({ content: postsWithPlaceholders });
@ -1039,7 +1051,7 @@ module("Unit | Model | post-stream", function () {
}); });
test("filteredPostsCount", function (assert) { test("filteredPostsCount", function (assert) {
const postStream = buildStream(4567, [1, 3, 4]); const postStream = buildStream.call(this, 4567, [1, 3, 4]);
assert.strictEqual(postStream.get("filteredPostsCount"), 3); assert.strictEqual(postStream.get("filteredPostsCount"), 3);
@ -1051,7 +1063,7 @@ module("Unit | Model | post-stream", function () {
}); });
test("lastPostId", function (assert) { test("lastPostId", function (assert) {
const postStream = buildStream(4567, [1, 3, 4]); const postStream = buildStream.call(this, 4567, [1, 3, 4]);
assert.strictEqual(postStream.get("lastPostId"), 4); assert.strictEqual(postStream.get("lastPostId"), 4);
@ -1064,8 +1076,8 @@ module("Unit | Model | post-stream", function () {
}); });
test("progressIndexOfPostId", function (assert) { test("progressIndexOfPostId", function (assert) {
const postStream = buildStream(4567, [1, 3, 4]); const postStream = buildStream.call(this, 4567, [1, 3, 4]);
const store = createStore(); const store = getOwner(this).lookup("service:store");
const post = store.createRecord("post", { id: 1, post_number: 5 }); const post = store.createRecord("post", { id: 1, post_number: 5 });
assert.strictEqual(postStream.progressIndexOfPostId(post), 1); assert.strictEqual(postStream.progressIndexOfPostId(post), 1);

View File

@ -1,12 +1,15 @@
import { module, test } from "qunit"; import { module, test } from "qunit";
import RestAdapter from "discourse/adapters/rest"; import RestAdapter from "discourse/adapters/rest";
import RestModel from "discourse/models/rest"; import RestModel from "discourse/models/rest";
import createStore from "discourse/tests/helpers/create-store";
import sinon from "sinon"; import sinon from "sinon";
import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
module("Unit | Model | rest-model", function (hooks) {
setupTest(hooks);
module("Unit | Model | rest-model", function () {
test("munging", function (assert) { test("munging", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const Grape = RestModel.extend(); const Grape = RestModel.extend();
Grape.reopenClass({ Grape.reopenClass({
munge: function (json) { munge: function (json) {
@ -20,7 +23,7 @@ module("Unit | Model | rest-model", function () {
}); });
test("update", async function (assert) { test("update", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = await store.find("widget", 123); const widget = await store.find("widget", 123);
assert.strictEqual(widget.get("name"), "Trout Lure"); assert.strictEqual(widget.get("name"), "Trout Lure");
assert.ok(!widget.get("isSaving"), "it is not saving"); assert.ok(!widget.get("isSaving"), "it is not saving");
@ -43,7 +46,7 @@ module("Unit | Model | rest-model", function () {
test("updating simultaneously", async function (assert) { test("updating simultaneously", async function (assert) {
assert.expect(2); assert.expect(2);
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = await store.find("widget", 123); const widget = await store.find("widget", 123);
const firstPromise = widget.update({ name: "new name" }); const firstPromise = widget.update({ name: "new name" });
@ -59,7 +62,7 @@ module("Unit | Model | rest-model", function () {
}); });
test("save new", async function (assert) { test("save new", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = store.createRecord("widget"); const widget = store.createRecord("widget");
assert.ok(widget.get("isNew"), "it is a new record"); assert.ok(widget.get("isNew"), "it is a new record");
@ -87,7 +90,7 @@ module("Unit | Model | rest-model", function () {
test("creating simultaneously", function (assert) { test("creating simultaneously", function (assert) {
assert.expect(2); assert.expect(2);
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = store.createRecord("widget"); const widget = store.createRecord("widget");
const firstPromise = widget.save({ name: "Evil Widget" }); const firstPromise = widget.save({ name: "Evil Widget" });
@ -102,24 +105,24 @@ module("Unit | Model | rest-model", function () {
}); });
test("destroyRecord", async function (assert) { test("destroyRecord", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = await store.find("widget", 123); const widget = await store.find("widget", 123);
assert.ok(await widget.destroyRecord()); assert.ok(await widget.destroyRecord());
}); });
test("custom api name", async function (assert) { test("custom api name", async function (assert) {
const store = createStore((type) => { const store = getOwner(this).lookup("service:store");
if (type === "adapter:my-widget") { getOwner(this).register(
return RestAdapter.extend({ "adapter:my-widget",
// An adapter like this is used when the server-side key/url class extends RestAdapter {
// do not match the name of the es6 class // An adapter like this is used when the server-side key/url
apiNameFor() { // do not match the name of the es6 class
return "widget"; apiNameFor() {
}, return "widget";
}).create(); }
} }
}); );
// The pretenders only respond to requests for `widget` // The pretenders only respond to requests for `widget`
// If these basic tests pass, the name override worked correctly // If these basic tests pass, the name override worked correctly

View File

@ -1,8 +1,11 @@
import { module, test } from "qunit"; import { module, test } from "qunit";
import ResultSet from "discourse/models/result-set"; import ResultSet from "discourse/models/result-set";
import createStore from "discourse/tests/helpers/create-store"; import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
module("Unit | Model | result-set", function (hooks) {
setupTest(hooks);
module("Unit | Model | result-set", function () {
test("defaults", function (assert) { test("defaults", function (assert) {
const resultSet = ResultSet.create({ content: [] }); const resultSet = ResultSet.create({ content: [] });
assert.strictEqual(resultSet.get("length"), 0); assert.strictEqual(resultSet.get("length"), 0);
@ -14,7 +17,7 @@ module("Unit | Model | result-set", function () {
}); });
test("pagination support", async function (assert) { test("pagination support", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const resultSet = await store.findAll("widget"); const resultSet = await store.findAll("widget");
assert.strictEqual(resultSet.get("length"), 2); assert.strictEqual(resultSet.get("length"), 2);
assert.strictEqual(resultSet.get("totalRows"), 4); assert.strictEqual(resultSet.get("totalRows"), 4);
@ -33,7 +36,7 @@ module("Unit | Model | result-set", function () {
}); });
test("refresh support", async function (assert) { test("refresh support", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const resultSet = await store.findAll("widget"); const resultSet = await store.findAll("widget");
assert.strictEqual( assert.strictEqual(
resultSet.get("refreshUrl"), resultSet.get("refreshUrl"),

View File

@ -1,8 +1,11 @@
import { module, test } from "qunit"; import { module, test } from "qunit";
import Site from "discourse/models/site"; import Site from "discourse/models/site";
import createStore from "discourse/tests/helpers/create-store"; import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
module("Unit | Model | site", function (hooks) {
setupTest(hooks);
module("Unit | Model | site", function () {
test("create", function (assert) { test("create", function (assert) {
assert.ok(Site.create(), "it can create with no parameters"); assert.ok(Site.create(), "it can create with no parameters");
}); });
@ -26,7 +29,7 @@ module("Unit | Model | site", function () {
}); });
test("create categories", function (assert) { test("create categories", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const site = store.createRecord("site", { const site = store.createRecord("site", {
categories: [ categories: [
{ id: 3456, name: "Test Subcategory", parent_category_id: 1234 }, { id: 3456, name: "Test Subcategory", parent_category_id: 1234 },
@ -78,7 +81,7 @@ module("Unit | Model | site", function () {
}); });
test("sortedCategories returns categories sorted by topic counts and sorts child categories after parent", function (assert) { test("sortedCategories returns categories sorted by topic counts and sorts child categories after parent", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const site = store.createRecord("site", { const site = store.createRecord("site", {
categories: [ categories: [
{ {

View File

@ -1,13 +1,13 @@
import { module, test } from "qunit";
import Category from "discourse/models/category"; import Category from "discourse/models/category";
import Topic from "discourse/models/topic"; import Topic from "discourse/models/topic";
import User from "discourse/models/user";
import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
import { test } from "qunit";
import { IMAGE_VERSION as v } from "pretty-text/emoji/version"; import { IMAGE_VERSION as v } from "pretty-text/emoji/version";
import createStore from "discourse/tests/helpers/create-store";
import { getOwner } from "discourse-common/lib/get-owner"; import { getOwner } from "discourse-common/lib/get-owner";
import { setupTest } from "ember-qunit";
module("Unit | Model | topic", function (hooks) {
setupTest(hooks);
discourseModule("Unit | Model | topic", function (hooks) {
hooks.beforeEach(function () { hooks.beforeEach(function () {
this.store = getOwner(this).lookup("service:store"); this.store = getOwner(this).lookup("service:store");
}); });
@ -74,8 +74,7 @@ discourseModule("Unit | Model | topic", function (hooks) {
}); });
test("lastUnreadUrl with navigate_to_first_post_after_read setting", function (assert) { test("lastUnreadUrl with navigate_to_first_post_after_read setting", function (assert) {
const store = createStore(); const category = this.store.createRecord("category", {
const category = store.createRecord("category", {
id: 22, id: 22,
navigate_to_first_post_after_read: true, navigate_to_first_post_after_read: true,
}); });
@ -92,8 +91,7 @@ discourseModule("Unit | Model | topic", function (hooks) {
}); });
test("lastUnreadUrl with navigate_to_first_post_after_read setting and unread posts", function (assert) { test("lastUnreadUrl with navigate_to_first_post_after_read setting and unread posts", function (assert) {
const store = createStore(); const category = this.store.createRecord("category", {
const category = store.createRecord("category", {
id: 22, id: 22,
navigate_to_first_post_after_read: true, navigate_to_first_post_after_read: true,
}); });
@ -184,7 +182,7 @@ discourseModule("Unit | Model | topic", function (hooks) {
}); });
test("recover", async function (assert) { test("recover", async function (assert) {
const user = User.create({ username: "eviltrout" }); const user = this.store.createRecord("user", { username: "eviltrout" });
const topic = this.store.createRecord("topic", { const topic = this.store.createRecord("topic", {
id: 1234, id: 1234,
deleted_at: new Date(), deleted_at: new Date(),
@ -217,7 +215,9 @@ discourseModule("Unit | Model | topic", function (hooks) {
fancy_title: "This is a test", fancy_title: "This is a test",
}); });
this.siteSettings.support_mixed_text_direction = true; const siteSettings = getOwner(this).lookup("service:site-settings");
siteSettings.support_mixed_text_direction = true;
assert.strictEqual( assert.strictEqual(
rtlTopic.get("fancyTitle"), rtlTopic.get("fancyTitle"),
`<span dir="rtl">هذا اختبار</span>`, `<span dir="rtl">هذا اختبار</span>`,

View File

@ -11,7 +11,6 @@ import {
import { NotificationLevels } from "discourse/lib/notification-levels"; import { NotificationLevels } from "discourse/lib/notification-levels";
import TopicTrackingState from "discourse/models/topic-tracking-state"; import TopicTrackingState from "discourse/models/topic-tracking-state";
import User from "discourse/models/user"; import User from "discourse/models/user";
import createStore from "discourse/tests/helpers/create-store";
import sinon from "sinon"; import sinon from "sinon";
import { getOwner } from "discourse-common/lib/get-owner"; import { getOwner } from "discourse-common/lib/get-owner";
@ -1019,7 +1018,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) {
}); });
test("getSubCategoryIds", function (assert) { test("getSubCategoryIds", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const foo = store.createRecord("category", { id: 1, slug: "foo" }); const foo = store.createRecord("category", { id: 1, slug: "foo" });
const bar = store.createRecord("category", { const bar = store.createRecord("category", {
id: 2, id: 2,
@ -1040,7 +1039,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) {
}); });
test("countNew", function (assert) { test("countNew", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const foo = store.createRecord("category", { const foo = store.createRecord("category", {
id: 1, id: 1,
slug: "foo", slug: "foo",

View File

@ -1,22 +1,17 @@
import { import { module, test } from "qunit";
currentUser, import { setupTest } from "ember-qunit";
discourseModule, import { getOwner } from "discourse-common/lib/get-owner";
} from "discourse/tests/helpers/qunit-helpers"; import { currentUser } from "discourse/tests/helpers/qunit-helpers";
import DocumentTitle from "discourse/services/document-title";
import AppEvents from "discourse/services/app-events";
import Session from "discourse/models/session"; import Session from "discourse/models/session";
import { test } from "qunit";
discourseModule("Unit | Service | document-title", function (hooks) { module("Unit | Service | document-title", function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () { hooks.beforeEach(function () {
const session = Session.current(); const session = Session.current();
session.hasFocus = true; session.hasFocus = true;
this.documentTitle = DocumentTitle.create({ this.documentTitle = getOwner(this).lookup("service:document-title");
session,
appEvents: AppEvents.create(),
});
this.documentTitle.currentUser = null;
}); });
hooks.afterEach(function () { hooks.afterEach(function () {

View File

@ -1,9 +1,12 @@
import { discourseModule } from "discourse/tests/helpers/qunit-helpers"; import { module, test } from "qunit";
import { test } from "qunit"; import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Unit | Service | emoji-store", function (hooks) {
setupTest(hooks);
discourseModule("Unit | Service | emoji-store", function (hooks) {
hooks.beforeEach(function () { hooks.beforeEach(function () {
this.emojiStore = this.container.lookup("service:emoji-store"); this.emojiStore = getOwner(this).lookup("service:emoji-store");
this.emojiStore.reset(); this.emojiStore.reset();
}); });

View File

@ -1,9 +1,13 @@
import { module, test } from "qunit"; import { module, test } from "qunit";
import createStore from "discourse/tests/helpers/create-store"; import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
module("Unit | Service | store", function (hooks) {
setupTest(hooks);
module("Unit | Service | store", function () {
test("createRecord", function (assert) { test("createRecord", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = store.createRecord("widget", { id: 111, name: "hello" }); const widget = store.createRecord("widget", { id: 111, name: "hello" });
assert.ok(!widget.get("isNew"), "it is not a new record"); assert.ok(!widget.get("isNew"), "it is not a new record");
@ -12,7 +16,7 @@ module("Unit | Service | store", function () {
}); });
test("createRecord without an `id`", function (assert) { test("createRecord without an `id`", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = store.createRecord("widget", { name: "hello" }); const widget = store.createRecord("widget", { name: "hello" });
assert.ok(widget.get("isNew"), "it is a new record"); assert.ok(widget.get("isNew"), "it is a new record");
@ -20,7 +24,7 @@ module("Unit | Service | store", function () {
}); });
test("createRecord doesn't modify the input `id` field", function (assert) { test("createRecord doesn't modify the input `id` field", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = store.createRecord("widget", { id: 1, name: "hello" }); const widget = store.createRecord("widget", { id: 1, name: "hello" });
const obj = { id: 1, name: "something" }; const obj = { id: 1, name: "something" };
@ -32,7 +36,7 @@ module("Unit | Service | store", function () {
}); });
test("createRecord without attributes", function (assert) { test("createRecord without attributes", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = store.createRecord("widget"); const widget = store.createRecord("widget");
assert.ok(!widget.get("id"), "there is no id"); assert.ok(!widget.get("id"), "there is no id");
@ -40,7 +44,7 @@ module("Unit | Service | store", function () {
}); });
test("createRecord with a record as attributes returns that record from the map", function (assert) { test("createRecord with a record as attributes returns that record from the map", function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = store.createRecord("widget", { id: 33 }); const widget = store.createRecord("widget", { id: 33 });
const secondWidget = store.createRecord("widget", { id: 33 }); const secondWidget = store.createRecord("widget", { id: 33 });
@ -48,7 +52,7 @@ module("Unit | Service | store", function () {
}); });
test("find", async function (assert) { test("find", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = await store.find("widget", 123); const widget = await store.find("widget", 123);
assert.strictEqual(widget.get("name"), "Trout Lure"); assert.strictEqual(widget.get("name"), "Trout Lure");
@ -71,19 +75,19 @@ module("Unit | Service | store", function () {
}); });
test("find with object id", async function (assert) { test("find with object id", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = await store.find("widget", { id: 123 }); const widget = await store.find("widget", { id: 123 });
assert.strictEqual(widget.get("firstObject.name"), "Trout Lure"); assert.strictEqual(widget.get("firstObject.name"), "Trout Lure");
}); });
test("find with query param", async function (assert) { test("find with query param", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = await store.find("widget", { name: "Trout Lure" }); const widget = await store.find("widget", { name: "Trout Lure" });
assert.strictEqual(widget.get("firstObject.id"), 123); assert.strictEqual(widget.get("firstObject.id"), 123);
}); });
test("findStale with no stale results", async function (assert) { test("findStale with no stale results", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const stale = store.findStale("widget", { name: "Trout Lure" }); const stale = store.findStale("widget", { name: "Trout Lure" });
assert.ok(!stale.hasResults, "there are no stale results"); assert.ok(!stale.hasResults, "there are no stale results");
@ -97,33 +101,46 @@ module("Unit | Service | store", function () {
}); });
test("rehydrating stale results with implicit injections", async function (assert) { test("rehydrating stale results with implicit injections", async function (assert) {
const store = createStore(); pretender.get("/notifications", ({ queryParams }) => {
if (queryParams.slug === "souna") {
return response({
notifications: [
{
id: 915,
slug: "souna",
},
],
});
}
});
const cat = (await store.find("cached-cat", { name: "souna" })).content[0]; const store = getOwner(this).lookup("service:store");
const notifications = await store.find("notification", { slug: "souna" });
assert.strictEqual(notifications.content[0].slug, "souna");
assert.strictEqual(cat.name, "souna"); const stale = store.findStale("notification", { slug: "souna" });
assert.true(stale.hasResults);
assert.strictEqual(stale.results.content[0].slug, "souna");
const stale = store.findStale("cached-cat", { name: "souna" });
const refreshed = await stale.refresh(); const refreshed = await stale.refresh();
assert.strictEqual(refreshed.content[0].slug, "souna");
assert.strictEqual(refreshed.content[0].name, "souna");
}); });
test("update", async function (assert) { test("update", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const result = await store.update("widget", 123, { name: "hello" }); const result = await store.update("widget", 123, { name: "hello" });
assert.ok(result); assert.ok(result);
}); });
test("update with a multi world name", async function (assert) { test("update with a multi world name", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const result = await store.update("cool-thing", 123, { name: "hello" }); const result = await store.update("cool-thing", 123, { name: "hello" });
assert.ok(result); assert.ok(result);
assert.strictEqual(result.payload.name, "hello"); assert.strictEqual(result.payload.name, "hello");
}); });
test("findAll", async function (assert) { test("findAll", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const result = await store.findAll("widget"); const result = await store.findAll("widget");
assert.strictEqual(result.get("length"), 2); assert.strictEqual(result.get("length"), 2);
@ -133,21 +150,21 @@ module("Unit | Service | store", function () {
}); });
test("destroyRecord", async function (assert) { test("destroyRecord", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = await store.find("widget", 123); const widget = await store.find("widget", 123);
assert.ok(await store.destroyRecord("widget", widget)); assert.ok(await store.destroyRecord("widget", widget));
}); });
test("destroyRecord when new", async function (assert) { test("destroyRecord when new", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const widget = store.createRecord("widget", { name: "hello" }); const widget = store.createRecord("widget", { name: "hello" });
assert.ok(await store.destroyRecord("widget", widget)); assert.ok(await store.destroyRecord("widget", widget));
}); });
test("find embedded", async function (assert) { test("find embedded", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const fruit = await store.find("fruit", 1); const fruit = await store.find("fruit", 1);
assert.ok(fruit.get("farmer"), "it has the embedded object"); assert.ok(fruit.get("farmer"), "it has the embedded object");
@ -158,7 +175,7 @@ module("Unit | Service | store", function () {
}); });
test("embedded records can be cleared", async function (assert) { test("embedded records can be cleared", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
let fruit = await store.find("fruit", 4); let fruit = await store.find("fruit", 4);
fruit.set("farmer", { dummy: "object" }); fruit.set("farmer", { dummy: "object" });
@ -167,7 +184,7 @@ module("Unit | Service | store", function () {
}); });
test("meta types", async function (assert) { test("meta types", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const barn = await store.find("barn", 1); const barn = await store.find("barn", 1);
assert.strictEqual( assert.strictEqual(
barn.get("owner.name"), barn.get("owner.name"),
@ -177,7 +194,7 @@ module("Unit | Service | store", function () {
}); });
test("findAll embedded", async function (assert) { test("findAll embedded", async function (assert) {
const store = createStore(); const store = getOwner(this).lookup("service:store");
const fruits = await store.findAll("fruit"); const fruits = await store.findAll("fruit");
assert.strictEqual(fruits.objectAt(0).get("farmer.name"), "Old MacDonald"); assert.strictEqual(fruits.objectAt(0).get("farmer.name"), "Old MacDonald");
assert.strictEqual( assert.strictEqual(
@ -200,8 +217,19 @@ module("Unit | Service | store", function () {
}); });
test("custom primaryKey", async function (assert) { test("custom primaryKey", async function (assert) {
const store = createStore(); pretender.get("/users", () => {
const cats = await store.findAll("cat"); return response({
assert.strictEqual(cats.objectAt(0).name, "souna"); users: [
{
id: 915,
username: "souna",
},
],
});
});
const store = getOwner(this).lookup("service:store");
const users = await store.findAll("user");
assert.strictEqual(users.objectAt(0).username, "souna");
}); });
}); });