From 25aa0bc10d5867d30ea62ebe016e6fe952bd3b58 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Wed, 16 Nov 2022 10:54:46 +0100 Subject: [PATCH] DEV: Deprecate `create-store` test helper (#19021) Use the regular Store service instead. --- .../discourse/tests/helpers/create-store.js | 12 ++ .../components/pending-post-test.js | 4 +- .../select-kit/category-chooser-test.js | 6 +- .../components/widgets/post-test.js | 6 +- .../components/widgets/topic-status-test.js | 8 +- .../controllers/reorder-categories-test.js | 32 ++-- .../tests/unit/lib/category-badge-test.js | 35 +++-- .../tests/unit/models/category-test.js | 73 ++++----- .../tests/unit/models/composer-test.js | 59 ++++---- .../tests/unit/models/nav-item-test.js | 17 ++- .../tests/unit/models/post-stream-test.js | 142 ++++++++++-------- .../tests/unit/models/rest-model-test.js | 39 ++--- .../tests/unit/models/result-set-test.js | 11 +- .../discourse/tests/unit/models/site-test.js | 11 +- .../discourse/tests/unit/models/topic-test.js | 22 +-- .../unit/models/topic-tracking-state-test.js | 5 +- .../unit/services/document-title-test.js | 21 +-- .../tests/unit/services/emoji-store-test.js | 11 +- .../tests/unit/services/store-test.js | 86 +++++++---- 19 files changed, 338 insertions(+), 262 deletions(-) diff --git a/app/assets/javascripts/discourse/tests/helpers/create-store.js b/app/assets/javascripts/discourse/tests/helpers/create-store.js index 61f0f74b8be..8d5d3bffb88 100644 --- a/app/assets/javascripts/discourse/tests/helpers/create-store.js +++ b/app/assets/javascripts/discourse/tests/helpers/create-store.js @@ -7,6 +7,7 @@ import { buildResolver } from "discourse-common/resolver"; import { currentSettings } from "discourse/tests/helpers/site-settings"; import Site from "discourse/models/site"; import RestModel from "discourse/models/rest"; +import deprecated from "discourse-common/lib/deprecated"; const CatAdapter = RestAdapter.extend({ primaryKey: "cat_id", @@ -33,6 +34,17 @@ const CachedCat = RestModel.extend({ }); 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({ namespace: { modulePrefix: "discourse" }, }); diff --git a/app/assets/javascripts/discourse/tests/integration/components/pending-post-test.js b/app/assets/javascripts/discourse/tests/integration/components/pending-post-test.js index 384954f2708..d2a1e7d57df 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/pending-post-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/pending-post-test.js @@ -3,13 +3,13 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { render } from "@ember/test-helpers"; import { query } from "discourse/tests/helpers/qunit-helpers"; 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) { setupRenderingTest(hooks); test("it renders", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); store.createRecord("category", { id: 2 }); const post = store.createRecord("pending-post", { id: 1, diff --git a/app/assets/javascripts/discourse/tests/integration/components/select-kit/category-chooser-test.js b/app/assets/javascripts/discourse/tests/integration/components/select-kit/category-chooser-test.js index cf89f6f18cd..dbffb73969a 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/select-kit/category-chooser-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/select-kit/category-chooser-test.js @@ -2,9 +2,9 @@ import { module, test } from "qunit"; import { setupRenderingTest } from "discourse/tests/helpers/component-test"; import { render } from "@ember/test-helpers"; import I18n from "I18n"; -import createStore from "discourse/tests/helpers/create-store"; import { hbs } from "ember-cli-htmlbars"; import selectKit from "discourse/tests/helpers/select-kit-helper"; +import { getOwner } from "discourse-common/lib/get-owner"; module( "Integration | Component | select-kit/category-chooser", @@ -267,7 +267,7 @@ module( }); test("filter works with non english characters", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); store.createRecord("category", { id: 1, name: "chữ Quốc ngữ", @@ -287,7 +287,7 @@ module( }); test("decodes entities in row title", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); store.createRecord("category", { id: 1, name: "cat-with-entities", diff --git a/app/assets/javascripts/discourse/tests/integration/components/widgets/post-test.js b/app/assets/javascripts/discourse/tests/integration/components/widgets/post-test.js index 23226ec1a7e..6f7ca2c89a5 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/widgets/post-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/widgets/post-test.js @@ -10,8 +10,8 @@ import { import { hbs } from "ember-cli-htmlbars"; import EmberObject from "@ember/object"; import I18n from "I18n"; -import createStore from "discourse/tests/helpers/create-store"; import User from "discourse/models/user"; +import { getOwner } from "discourse-common/lib/get-owner"; module("Integration | Component | Widget | post", function (hooks) { setupRenderingTest(hooks); @@ -163,7 +163,7 @@ module("Integration | Component | Widget | post", function (hooks) { }); 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 post = store.createRecord("post", { id: 1, @@ -507,7 +507,7 @@ module("Integration | Component | Widget | post", function (hooks) { }); test("expand first post", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); this.set("args", { expandablePost: true }); this.set("post", store.createRecord("post", { id: 1234 })); diff --git a/app/assets/javascripts/discourse/tests/integration/components/widgets/topic-status-test.js b/app/assets/javascripts/discourse/tests/integration/components/widgets/topic-status-test.js index b75335bcac4..445fe53168b 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/widgets/topic-status-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/widgets/topic-status-test.js @@ -4,13 +4,13 @@ import { click, render } from "@ember/test-helpers"; import { exists } from "discourse/tests/helpers/qunit-helpers"; import { hbs } from "ember-cli-htmlbars"; 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) { setupRenderingTest(hooks); test("basics", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); this.set("args", { topic: store.createRecord("topic", { closed: true }), disableActions: true, @@ -24,7 +24,7 @@ module("Integration | Component | Widget | topic-status", function (hooks) { }); test("extendability", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); TopicStatusIcons.addObject([ "has_accepted_answer", "far-check-square", @@ -45,7 +45,7 @@ module("Integration | Component | Widget | topic-status", function (hooks) { }); test("toggling pin status", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); this.set("args", { topic: store.createRecord("topic", { closed: true, pinned: true }), }); diff --git a/app/assets/javascripts/discourse/tests/unit/controllers/reorder-categories-test.js b/app/assets/javascripts/discourse/tests/unit/controllers/reorder-categories-test.js index 2ff3fd18ca0..afc6ed387fe 100644 --- a/app/assets/javascripts/discourse/tests/unit/controllers/reorder-categories-test.js +++ b/app/assets/javascripts/discourse/tests/unit/controllers/reorder-categories-test.js @@ -1,15 +1,15 @@ import { module, test } from "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) { setupTest(hooks); test("reorder set unique position number", function (assert) { - const controller = this.owner.lookup("controller:reorder-categories"); - const store = createStore(); + const controller = getOwner(this).lookup("controller:reorder-categories"); + const store = getOwner(this).lookup("service:store"); - const site = this.owner.lookup("service:site"); + const site = getOwner(this).lookup("service:site"); site.set("categories", [ store.createRecord("category", { id: 1, 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) { - const controller = this.owner.lookup("controller:reorder-categories"); - const store = createStore(); + const controller = getOwner(this).lookup("controller:reorder-categories"); + const store = getOwner(this).lookup("service:store"); const parent = store.createRecord("category", { id: 1, @@ -51,7 +51,7 @@ module("Unit | Controller | reorder-categories", function (hooks) { }); 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]); 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) { - const controller = this.owner.lookup("controller:reorder-categories"); - const store = createStore(); + const controller = getOwner(this).lookup("controller:reorder-categories"); + const store = getOwner(this).lookup("service:store"); const elem1 = store.createRecord("category", { id: 1, @@ -84,7 +84,7 @@ module("Unit | Controller | reorder-categories", function (hooks) { slug: "test", }); - const site = this.owner.lookup("service:site"); + const site = getOwner(this).lookup("service:site"); site.set("categories", [elem1, elem2, elem3]); // 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) { - const controller = this.owner.lookup("controller:reorder-categories"); - const store = createStore(); + const controller = getOwner(this).lookup("controller:reorder-categories"); + const store = getOwner(this).lookup("service:store"); const elem1 = store.createRecord("category", { id: 1, @@ -126,7 +126,7 @@ module("Unit | Controller | reorder-categories", function (hooks) { slug: "test", }); - const site = this.owner.lookup("service:site"); + const site = getOwner(this).lookup("service:site"); site.set("categories", [elem1, child1, elem2, elem3]); 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) { - const controller = this.owner.lookup("controller:reorder-categories"); - const store = createStore(); + const controller = getOwner(this).lookup("controller:reorder-categories"); + const store = getOwner(this).lookup("service:store"); const child2 = store.createRecord("category", { id: 105, @@ -177,7 +177,7 @@ module("Unit | Controller | reorder-categories", function (hooks) { slug: "test", }); - const site = this.owner.lookup("service:site"); + const site = getOwner(this).lookup("service:site"); site.set("categories", [elem1, child1, child2, elem2, elem3]); controller.reorder(); diff --git a/app/assets/javascripts/discourse/tests/unit/lib/category-badge-test.js b/app/assets/javascripts/discourse/tests/unit/lib/category-badge-test.js index ae4ae6f25ec..dfde32f150e 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/category-badge-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/category-badge-test.js @@ -1,17 +1,19 @@ +import { module, test } from "qunit"; import Site from "discourse/models/site"; 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 { 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) { assert.blank(categoryBadgeHTML(), "it returns no HTML"); }); test("Regular categoryBadge", function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const category = store.createRecord("category", { name: "hello", id: 123, @@ -42,7 +44,7 @@ discourseModule("Unit | Utility | category-badge", function () { }); 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 tag = $.parseHTML(categoryBadgeHTML(noColor))[0]; @@ -53,7 +55,7 @@ discourseModule("Unit | Utility | category-badge", function () { }); test("topic count", function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const category = store.createRecord("category", { name: "hello", id: 123 }); assert.ok( @@ -68,7 +70,7 @@ discourseModule("Unit | Utility | category-badge", function () { }); test("allowUncategorized", function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const uncategorized = store.createRecord("category", { name: "uncategorized", id: 345, @@ -90,8 +92,10 @@ discourseModule("Unit | Utility | category-badge", function () { }); test("category names are wrapped in dir-spans", function (assert) { - this.siteSettings.support_mixed_text_direction = true; - const store = createStore(); + const siteSettings = getOwner(this).lookup("service:site-settings"); + siteSettings.support_mixed_text_direction = true; + + const store = getOwner(this).lookup("service:store"); const rtlCategory = store.createRecord("category", { name: "תכנות עם Ruby", id: 123, @@ -115,7 +119,8 @@ discourseModule("Unit | Utility | category-badge", function () { }); 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", { name: "foo", @@ -134,20 +139,20 @@ discourseModule("Unit | Utility | category-badge", function () { 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("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("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("bar")); 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("bar")); assert.ok(categoryBadgeHTML(baz, { recursive: true }).includes("foo")); diff --git a/app/assets/javascripts/discourse/tests/unit/models/category-test.js b/app/assets/javascripts/discourse/tests/unit/models/category-test.js index 4967bef84e4..bc9ed94c368 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/category-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/category-test.js @@ -1,11 +1,14 @@ import { module, test } from "qunit"; import Category from "discourse/models/category"; -import createStore from "discourse/tests/helpers/create-store"; 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) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const slugFor = function (cat, val, text) { assert.strictEqual(Category.slugFor(cat), val, text); @@ -68,7 +71,7 @@ module("Unit | Model | category", function () { test("findBySlug", function (assert) { assert.expect(6); - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const darth = store.createRecord("category", { id: 1, slug: "darth" }), luke = store.createRecord("category", { id: 2, @@ -133,7 +136,7 @@ module("Unit | Model | category", function () { test("findSingleBySlug", function (assert) { assert.expect(6); - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const darth = store.createRecord("category", { id: 1, slug: "darth" }), luke = store.createRecord("category", { id: 2, @@ -196,7 +199,7 @@ module("Unit | Model | category", function () { }); test("findBySlugPathWithID", function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const foo = store.createRecord("category", { id: 1, slug: "foo" }); const bar = store.createRecord("category", { @@ -220,7 +223,7 @@ module("Unit | Model | category", function () { }); test("minimumRequiredTags", function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); let foo = store.createRecord("category", { id: 1, @@ -263,23 +266,23 @@ module("Unit | Model | category", function () { }); test("search with category name", function (assert) { - const store = createStore(), - category1 = store.createRecord("category", { - id: 1, - name: "middle term", - slug: "different-slug", - }), - category2 = store.createRecord("category", { - id: 2, - name: "middle term", - slug: "another-different-slug", - }), - subcategory = store.createRecord("category", { - id: 3, - name: "middle term", - slug: "another-different-slug2", - parent_category_id: 2, - }); + const store = getOwner(this).lookup("service:store"); + const category1 = store.createRecord("category", { + id: 1, + name: "middle term", + slug: "different-slug", + }); + const category2 = store.createRecord("category", { + id: 2, + name: "middle term", + slug: "another-different-slug", + }); + const subcategory = store.createRecord("category", { + id: 3, + name: "middle term", + slug: "another-different-slug2", + parent_category_id: 2, + }); sinon .stub(Category, "listByActivity") @@ -369,17 +372,17 @@ module("Unit | Model | category", function () { }); test("search with category slug", function (assert) { - const store = createStore(), - category1 = store.createRecord("category", { - id: 1, - name: "middle term", - slug: "different-slug", - }), - category2 = store.createRecord("category", { - id: 2, - name: "middle term", - slug: "another-different-slug", - }); + const store = getOwner(this).lookup("service:store"); + const category1 = store.createRecord("category", { + id: 1, + name: "middle term", + slug: "different-slug", + }); + const category2 = store.createRecord("category", { + id: 2, + name: "middle term", + slug: "another-different-slug", + }); sinon.stub(Category, "listByActivity").returns([category1, category2]); diff --git a/app/assets/javascripts/discourse/tests/unit/models/composer-test.js b/app/assets/javascripts/discourse/tests/unit/models/composer-test.js index 216d007080b..933569466a3 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/composer-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/composer-test.js @@ -4,33 +4,36 @@ import { PRIVATE_MESSAGE, REPLY, } from "discourse/models/composer"; -import { - currentUser, - discourseModule, -} from "discourse/tests/helpers/qunit-helpers"; +import { currentUser } from "discourse/tests/helpers/qunit-helpers"; import AppEvents from "discourse/services/app-events"; import EmberObject from "@ember/object"; -import createStore from "discourse/tests/helpers/create-store"; -import { test } from "qunit"; +import { module, test } from "qunit"; import { getOwner } from "discourse-common/lib/get-owner"; +import { setupTest } from "ember-qunit"; -function createComposer(opts) { - opts = opts || {}; - opts.user = opts.user || currentUser(); +function createComposer(opts = {}) { + opts.user ??= currentUser(); opts.appEvents = AppEvents.create(); - return createStore().createRecord("composer", opts); + const store = getOwner(this).lookup("service:store"); + return store.createRecord("composer", opts); } function openComposer(opts) { - const composer = createComposer(opts); + const composer = createComposer.call(this, opts); composer.open(opts); 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) { const replyLength = function (val, expectedLength) { - const composer = createComposer({ reply: val }); + const composer = createComposer.call(this, { reply: val }); assert.strictEqual(composer.get("replyLength"), expectedLength); }; @@ -78,7 +81,7 @@ discourseModule("Unit | Model | composer", function () { if (isFirstPost) { action = CREATE_TOPIC; } - const composer = createComposer({ reply: val, action }); + const composer = createComposer.call(this, { reply: val, action }); assert.strictEqual( composer.get("missingReplyCharacters"), expected, @@ -111,7 +114,7 @@ discourseModule("Unit | Model | composer", function () { const link = "http://imgur.com/gallery/grxX8"; this.siteSettings.topic_featured_link_enabled = true; this.siteSettings.topic_featured_link_allowed_category_ids = 12345; - const composer = createComposer({ + const composer = createComposer.call(this, { title: link, categoryId: 12345, featuredLink: link, @@ -128,7 +131,7 @@ discourseModule("Unit | Model | composer", function () { test("missingTitleCharacters", function (assert) { const missingTitleCharacters = function (val, isPM, expected, message) { - const composer = createComposer({ + const composer = createComposer.call(this, { title: val, action: isPM ? PRIVATE_MESSAGE : REPLY, }); @@ -252,7 +255,7 @@ discourseModule("Unit | Model | composer", function () { test("Title length for private messages", function (assert) { this.siteSettings.min_personal_message_title_length = 5; 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"); 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) { - const composer = createComposer({ + const composer = createComposer.call(this, { topic: EmberObject.create({ pm_with_non_human_user: true }), }); @@ -293,7 +296,7 @@ discourseModule("Unit | Model | composer", function () { test("clearState", function (assert) { const store = getOwner(this).lookup("service:store"); - const composer = createComposer({ + const composer = createComposer.call(this, { originalText: "asdf", reply: "asdf2", post: store.createRecord("post", { id: 1 }), @@ -310,7 +313,7 @@ discourseModule("Unit | Model | composer", function () { test("initial category when uncategorized is allowed", function (assert) { this.siteSettings.allow_uncategorized_topics = true; - const composer = openComposer({ + const composer = openComposer.call(this, { action: CREATE_TOPIC, draftKey: "asfd", draftSequence: 1, @@ -320,7 +323,7 @@ discourseModule("Unit | Model | composer", function () { test("initial category when uncategorized is not allowed", function (assert) { this.siteSettings.allow_uncategorized_topics = false; - const composer = openComposer({ + const composer = openComposer.call(this, { action: CREATE_TOPIC, draftKey: "asfd", draftSequence: 1, @@ -335,7 +338,7 @@ discourseModule("Unit | Model | composer", function () { const quote = '[quote="neil, post:5, topic:413"]\nSimmer down you two.\n[/quote]'; const newComposer = function () { - return openComposer({ + return openComposer.call(this, { action: REPLY, draftKey: "asfd", draftSequence: 1, @@ -386,14 +389,14 @@ discourseModule("Unit | Model | composer", function () { test("title placeholder depends on what you're doing", function (assert) { this.siteSettings.topic_featured_link_enabled = false; - let composer = createComposer({ action: CREATE_TOPIC }); + let composer = createComposer.call(this, { action: CREATE_TOPIC }); assert.strictEqual( composer.get("titlePlaceholder"), "composer.title_placeholder", "placeholder for normal topic" ); - composer = createComposer({ action: PRIVATE_MESSAGE }); + composer = createComposer.call(this, { action: PRIVATE_MESSAGE }); assert.strictEqual( composer.get("titlePlaceholder"), "composer.title_placeholder", @@ -402,14 +405,14 @@ discourseModule("Unit | Model | composer", function () { this.siteSettings.topic_featured_link_enabled = true; - composer = createComposer({ action: CREATE_TOPIC }); + composer = createComposer.call(this, { action: CREATE_TOPIC }); assert.strictEqual( composer.get("titlePlaceholder"), "composer.title_or_link_placeholder", "placeholder invites you to paste a link" ); - composer = createComposer({ action: PRIVATE_MESSAGE }); + composer = createComposer.call(this, { action: PRIVATE_MESSAGE }); assert.strictEqual( composer.get("titlePlaceholder"), "composer.title_placeholder", @@ -420,7 +423,7 @@ discourseModule("Unit | Model | composer", function () { 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({ action: CREATE_TOPIC }); + let composer = createComposer.call(this, { action: CREATE_TOPIC }); assert.strictEqual( composer.get("titlePlaceholder"), "composer.title_or_link_placeholder", @@ -430,7 +433,7 @@ discourseModule("Unit | Model | composer", function () { }); test("targetRecipientsArray contains types", function (assert) { - let composer = createComposer({ + let composer = createComposer.call(this, { targetRecipients: "test,codinghorror,staff,foo@bar.com", }); assert.ok(composer.targetRecipientsArray, [ diff --git a/app/assets/javascripts/discourse/tests/unit/models/nav-item-test.js b/app/assets/javascripts/discourse/tests/unit/models/nav-item-test.js index a7a1386fc7c..97239263724 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/nav-item-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/nav-item-test.js @@ -2,13 +2,17 @@ import { module, test } from "qunit"; import Category from "discourse/models/category"; import NavItem from "discourse/models/nav-item"; import Site from "discourse/models/site"; -import createStore from "discourse/tests/helpers/create-store"; 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) { + setupTest(hooks); + hooks.beforeEach(function () { run(function () { - const fooCategory = Category.create({ + const store = getOwner(this).lookup("service:store"); + const fooCategory = store.createRecord("category", { slug: "foo", id: 123, }); @@ -39,7 +43,8 @@ module("Unit | Model | nav-item", function (hooks) { }); 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"); @@ -59,7 +64,8 @@ module("Unit | Model | nav-item", function (hooks) { }); 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", }); @@ -73,7 +79,8 @@ module("Unit | Model | nav-item", function (hooks) { }); 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", }); diff --git a/app/assets/javascripts/discourse/tests/unit/models/post-stream-test.js b/app/assets/javascripts/discourse/tests/unit/models/post-stream-test.js index d1f54cc5b10..61ff0cb2840 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/post-stream-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/post-stream-test.js @@ -1,28 +1,30 @@ import { module, test } from "qunit"; -import AppEvents from "discourse/services/app-events"; import ArrayProxy from "@ember/array/proxy"; import Post from "discourse/models/post"; import User from "discourse/models/user"; -import createStore from "discourse/tests/helpers/create-store"; import pretender, { response } from "discourse/tests/helpers/create-pretender"; import sinon from "sinon"; +import { getOwner } from "discourse-common/lib/get-owner"; +import { setupTest } from "ember-qunit"; function buildStream(id, stream) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const topic = store.createRecord("topic", { id, chunk_size: 5 }); - const ps = topic.postStream; + if (stream) { - ps.set("stream", stream); + topic.postStream.set("stream", stream); } - ps.appEvents = AppEvents.create(); - return ps; + + return topic.postStream; } const participant = { username: "eviltrout" }; -module("Unit | Model | post-stream", function () { +module("Unit | Model | post-stream", function (hooks) { + setupTest(hooks); + test("create", function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); assert.ok( store.createRecord("postStream"), "it can be created with no parameters" @@ -30,15 +32,15 @@ module("Unit | Model | post-stream", function () { }); 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.ok(!postStream.get("loaded"), "it has never loaded"); assert.present(postStream.get("topic")); }); test("appending posts", function (assert) { - const postStream = buildStream(4567, [1, 3, 4]); - const store = postStream.store; + const postStream = buildStream.call(this, 4567, [1, 3, 4]); + const store = getOwner(this).lookup("service:store"); assert.strictEqual( postStream.get("lastPostId"), @@ -121,8 +123,8 @@ module("Unit | Model | post-stream", function () { }); test("closestPostNumberFor", function (assert) { - const postStream = buildStream(1231); - const store = postStream.store; + const postStream = buildStream.call(this, 1231); + const store = getOwner(this).lookup("service:store"); assert.blank( postStream.closestPostNumberFor(1), @@ -159,7 +161,7 @@ module("Unit | Model | post-stream", function () { }); test("closestDaysAgoFor", function (assert) { - const postStream = buildStream(1231); + const postStream = buildStream.call(this, 1231); postStream.set("timelineLookup", [ [1, 10], [3, 8], @@ -182,14 +184,14 @@ module("Unit | Model | post-stream", function () { }); test("closestDaysAgoFor - empty", function (assert) { - const postStream = buildStream(1231); + const postStream = buildStream.call(this, 1231); postStream.set("timelineLookup", []); assert.strictEqual(postStream.closestDaysAgoFor(1), undefined); }); test("updateFromJson", function (assert) { - const postStream = buildStream(1231); + const postStream = buildStream.call(this, 1231); postStream.updateFromJson({ posts: [{ id: 1 }], @@ -208,8 +210,8 @@ module("Unit | Model | post-stream", function () { }); test("removePosts", function (assert) { - const postStream = buildStream(10000001, [1, 2, 3]); - const store = postStream.store; + const postStream = buildStream.call(this, 10000001, [1, 2, 3]); + const store = getOwner(this).lookup("service:store"); const p1 = store.createRecord("post", { id: 1, post_number: 2 }), p2 = store.createRecord("post", { id: 2, post_number: 3 }), @@ -229,7 +231,7 @@ module("Unit | Model | post-stream", function () { }); test("cancelFilter", function (assert) { - const postStream = buildStream(1235); + const postStream = buildStream.call(this, 1235); sinon.stub(postStream, "refresh").resolves(); @@ -246,7 +248,11 @@ module("Unit | Model | post-stream", function () { }); 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] } }); assert.strictEqual( @@ -272,7 +278,7 @@ module("Unit | Model | post-stream", function () { }); test("fillGapBefore", function (assert) { - const postStream = buildStream(1234, [60]); + const postStream = buildStream.call(this, 1234, [60]); sinon.stub(postStream, "findPostsByIds").resolves([]); let post = postStream.store.createRecord("post", { id: 60, @@ -292,7 +298,7 @@ module("Unit | Model | post-stream", function () { }); test("filterParticipant", function (assert) { - const postStream = buildStream(1236); + const postStream = buildStream.call(this, 1236); sinon.stub(postStream, "refresh").resolves(); assert.strictEqual( @@ -312,7 +318,7 @@ module("Unit | Model | post-stream", function () { }); test("filterReplies", function (assert) { - const postStream = buildStream(1234), + const postStream = buildStream.call(this, 1234), store = postStream.store; postStream.appendPost( @@ -343,7 +349,7 @@ module("Unit | Model | post-stream", function () { }); test("filterUpwards", function (assert) { - const postStream = buildStream(1234), + const postStream = buildStream.call(this, 1234), store = postStream.store; postStream.appendPost( @@ -374,7 +380,7 @@ module("Unit | Model | post-stream", function () { }); test("streamFilters", function (assert) { - const postStream = buildStream(1237); + const postStream = buildStream.call(this, 1237); sinon.stub(postStream, "refresh").resolves(); assert.deepEqual( @@ -424,23 +430,24 @@ module("Unit | Model | post-stream", function () { }); 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"); postStream.set("loadingAbove", true); assert.ok(postStream.get("loading"), "we're loading if loading above"); - postStream = buildStream(1234); + postStream = buildStream.call(this, 1234); postStream.set("loadingBelow", true); assert.ok(postStream.get("loading"), "we're loading if loading below"); - postStream = buildStream(1234); + postStream = buildStream.call(this, 1234); postStream.set("loadingFilter", true); assert.ok(postStream.get("loading"), "we're loading if loading a filter"); }); test("nextWindow", function (assert) { - const postStream = buildStream( + const postStream = buildStream.call( + this, 1234, [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) { - const postStream = buildStream( + const postStream = buildStream.call( + this, 1234, [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) { - const postStream = buildStream(1234), + const postStream = buildStream.call(this, 1234), store = postStream.store, post = store.createRecord("post", { id: 1, @@ -552,8 +560,8 @@ module("Unit | Model | post-stream", function () { }); test("identity map", async function (assert) { - const postStream = buildStream(1234); - const store = postStream.store; + const postStream = buildStream.call(this, 1234); + const store = getOwner(this).lookup("service:store"); const p1 = postStream.appendPost( 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) { - const result = await buildStream(1234).loadIntoIdentityMap([]); + const result = await buildStream.call(this, 1234).loadIntoIdentityMap([]); assert.strictEqual( result.length, 0, @@ -587,7 +595,7 @@ module("Unit | Model | post-stream", function () { }); test("loadIntoIdentityMap with post ids", async function (assert) { - const postStream = buildStream(1234); + const postStream = buildStream.call(this, 1234); await postStream.loadIntoIdentityMap([10]); assert.present( @@ -597,8 +605,8 @@ module("Unit | Model | post-stream", function () { }); test("appendMore for megatopic", async function (assert) { - const postStream = buildStream(1234); - const store = createStore(); + const postStream = buildStream.call(this, 1234); + const store = getOwner(this).lookup("service:store"); const post = store.createRecord("post", { id: 1, post_number: 1 }); postStream.setProperties({ @@ -620,8 +628,8 @@ module("Unit | Model | post-stream", function () { }); test("prependMore for megatopic", async function (assert) { - const postStream = buildStream(1234); - const store = createStore(); + const postStream = buildStream.call(this, 1234); + const store = getOwner(this).lookup("service:store"); const post = store.createRecord("post", { id: 6, post_number: 6 }); postStream.setProperties({ @@ -643,8 +651,8 @@ module("Unit | Model | post-stream", function () { }); test("staging and undoing a new post", function (assert) { - const postStream = buildStream(10101, [1]); - const store = postStream.store; + const postStream = buildStream.call(this, 10101, [1]); + const store = getOwner(this).lookup("service:store"); const original = store.createRecord("post", { id: 1, @@ -658,7 +666,7 @@ module("Unit | Model | post-stream", function () { "the original post is lastAppended" ); - const user = User.create({ + const user = store.createRecord("user", { username: "eviltrout", name: "eviltrout", id: 321, @@ -759,8 +767,8 @@ module("Unit | Model | post-stream", function () { }); test("staging and committing a post", function (assert) { - const postStream = buildStream(10101, [1]); - const store = postStream.store; + const postStream = buildStream.call(this, 10101, [1]); + const store = getOwner(this).lookup("service:store"); const original = store.createRecord("post", { id: 1, @@ -774,7 +782,7 @@ module("Unit | Model | post-stream", function () { "the original post is lastAppended" ); - const user = User.create({ + const user = store.createRecord("user", { username: "eviltrout", name: "eviltrout", id: 321, @@ -843,8 +851,8 @@ module("Unit | Model | post-stream", function () { 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 // message bus. If the id of a post changes we should reconsider the loadedAllPosts property. - const postStream = buildStream(10101, [1, 2]); - const store = postStream.store; + const postStream = buildStream.call(this, 10101, [1, 2]); + const store = getOwner(this).lookup("service:store"); const postWithoutId = store.createRecord("post", { raw: "hello world this is my new post", }); @@ -863,8 +871,8 @@ module("Unit | Model | post-stream", function () { }); test("triggerRecoveredPost", async function (assert) { - const postStream = buildStream(4567); - const store = postStream.store; + const postStream = buildStream.call(this, 4567); + const store = getOwner(this).lookup("service:store"); [1, 2, 3, 5].forEach((id) => { postStream.appendPost( @@ -892,13 +900,13 @@ module("Unit | Model | post-stream", function () { }); test("committing and triggerNewPostsInStream race condition", function (assert) { - const postStream = buildStream(4964); - const store = postStream.store; + const postStream = buildStream.call(this, 4964); + const store = getOwner(this).lookup("service:store"); postStream.appendPost( store.createRecord("post", { id: 1, post_number: 1 }) ); - const user = User.create({ + const user = store.createRecord("user", { username: "eviltrout", name: "eviltrout", id: 321, @@ -932,14 +940,14 @@ module("Unit | Model | post-stream", function () { }); test("triggerNewPostInStream for ignored posts", async function (assert) { - const postStream = buildStream(280, [1]); - const store = postStream.store; + const postStream = buildStream.call(this, 280, [1]); + const store = getOwner(this).lookup("service:store"); User.resetCurrent( - User.create({ + store.createRecord("user", { username: "eviltrout", name: "eviltrout", id: 321, - ignored_users: ["ignoreduser"], + ignored_users: ["ignored-user"], }) ); @@ -950,13 +958,13 @@ module("Unit | Model | post-stream", function () { const post2 = store.createRecord("post", { id: 101, post_number: 2, - username: "regularuser", + username: "regular-user", }); const post3 = store.createRecord("post", { id: 102, post_number: 3, - username: "ignoreduser", + username: "ignored-user", }); let stub = sinon.stub(postStream, "findPostsByIds").resolves([post2]); @@ -990,9 +998,13 @@ module("Unit | Model | post-stream", function () { }); 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 store = postStream.store; + const store = getOwner(this).lookup("service:store"); const testProxy = ArrayProxy.create({ content: postsWithPlaceholders }); @@ -1039,7 +1051,7 @@ module("Unit | Model | post-stream", function () { }); 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); @@ -1051,7 +1063,7 @@ module("Unit | Model | post-stream", function () { }); 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); @@ -1064,8 +1076,8 @@ module("Unit | Model | post-stream", function () { }); test("progressIndexOfPostId", function (assert) { - const postStream = buildStream(4567, [1, 3, 4]); - const store = createStore(); + const postStream = buildStream.call(this, 4567, [1, 3, 4]); + const store = getOwner(this).lookup("service:store"); const post = store.createRecord("post", { id: 1, post_number: 5 }); assert.strictEqual(postStream.progressIndexOfPostId(post), 1); diff --git a/app/assets/javascripts/discourse/tests/unit/models/rest-model-test.js b/app/assets/javascripts/discourse/tests/unit/models/rest-model-test.js index 539beb88d09..b97671ad1fd 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/rest-model-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/rest-model-test.js @@ -1,12 +1,15 @@ import { module, test } from "qunit"; import RestAdapter from "discourse/adapters/rest"; import RestModel from "discourse/models/rest"; -import createStore from "discourse/tests/helpers/create-store"; 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) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const Grape = RestModel.extend(); Grape.reopenClass({ munge: function (json) { @@ -20,7 +23,7 @@ module("Unit | Model | rest-model", function () { }); test("update", async function (assert) { - const store = createStore(); + 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"); @@ -43,7 +46,7 @@ module("Unit | Model | rest-model", function () { test("updating simultaneously", async function (assert) { assert.expect(2); - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const widget = await store.find("widget", 123); const firstPromise = widget.update({ name: "new name" }); @@ -59,7 +62,7 @@ module("Unit | Model | rest-model", function () { }); test("save new", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const widget = store.createRecord("widget"); 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) { assert.expect(2); - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const widget = store.createRecord("widget"); const firstPromise = widget.save({ name: "Evil Widget" }); @@ -102,24 +105,24 @@ module("Unit | Model | rest-model", function () { }); test("destroyRecord", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const widget = await store.find("widget", 123); assert.ok(await widget.destroyRecord()); }); test("custom api name", async function (assert) { - const store = createStore((type) => { - if (type === "adapter:my-widget") { - return RestAdapter.extend({ - // An adapter like this is used when the server-side key/url - // do not match the name of the es6 class - apiNameFor() { - return "widget"; - }, - }).create(); + const store = getOwner(this).lookup("service:store"); + getOwner(this).register( + "adapter:my-widget", + class extends RestAdapter { + // An adapter like this is used when the server-side key/url + // do not match the name of the es6 class + apiNameFor() { + return "widget"; + } } - }); + ); // The pretenders only respond to requests for `widget` // If these basic tests pass, the name override worked correctly diff --git a/app/assets/javascripts/discourse/tests/unit/models/result-set-test.js b/app/assets/javascripts/discourse/tests/unit/models/result-set-test.js index c1e7660b335..78a77bce4d0 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/result-set-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/result-set-test.js @@ -1,8 +1,11 @@ import { module, test } from "qunit"; 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) { const resultSet = ResultSet.create({ content: [] }); assert.strictEqual(resultSet.get("length"), 0); @@ -14,7 +17,7 @@ module("Unit | Model | result-set", function () { }); test("pagination support", async function (assert) { - const store = createStore(); + 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); @@ -33,7 +36,7 @@ module("Unit | Model | result-set", function () { }); test("refresh support", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const resultSet = await store.findAll("widget"); assert.strictEqual( resultSet.get("refreshUrl"), diff --git a/app/assets/javascripts/discourse/tests/unit/models/site-test.js b/app/assets/javascripts/discourse/tests/unit/models/site-test.js index edf509fc738..8a6b92fa855 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/site-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/site-test.js @@ -1,8 +1,11 @@ import { module, test } from "qunit"; 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) { assert.ok(Site.create(), "it can create with no parameters"); }); @@ -26,7 +29,7 @@ module("Unit | Model | site", function () { }); test("create categories", function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const site = store.createRecord("site", { categories: [ { 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) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const site = store.createRecord("site", { categories: [ { diff --git a/app/assets/javascripts/discourse/tests/unit/models/topic-test.js b/app/assets/javascripts/discourse/tests/unit/models/topic-test.js index c4dc505d923..cc0cee5824e 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/topic-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/topic-test.js @@ -1,13 +1,13 @@ +import { module, test } from "qunit"; import Category from "discourse/models/category"; 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 createStore from "discourse/tests/helpers/create-store"; 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 () { 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) { - const store = createStore(); - const category = store.createRecord("category", { + const category = this.store.createRecord("category", { id: 22, 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) { - const store = createStore(); - const category = store.createRecord("category", { + const category = this.store.createRecord("category", { id: 22, navigate_to_first_post_after_read: true, }); @@ -184,7 +182,7 @@ discourseModule("Unit | Model | topic", function (hooks) { }); 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", { id: 1234, deleted_at: new Date(), @@ -217,7 +215,9 @@ discourseModule("Unit | Model | topic", function (hooks) { 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( rtlTopic.get("fancyTitle"), `هذا اختبار`, diff --git a/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js b/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js index 482ea1a0d88..37363e078d3 100644 --- a/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js +++ b/app/assets/javascripts/discourse/tests/unit/models/topic-tracking-state-test.js @@ -11,7 +11,6 @@ import { import { NotificationLevels } from "discourse/lib/notification-levels"; import TopicTrackingState from "discourse/models/topic-tracking-state"; import User from "discourse/models/user"; -import createStore from "discourse/tests/helpers/create-store"; import sinon from "sinon"; import { getOwner } from "discourse-common/lib/get-owner"; @@ -1019,7 +1018,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { }); test("getSubCategoryIds", function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const foo = store.createRecord("category", { id: 1, slug: "foo" }); const bar = store.createRecord("category", { id: 2, @@ -1040,7 +1039,7 @@ discourseModule("Unit | Model | topic-tracking-state", function (hooks) { }); test("countNew", function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const foo = store.createRecord("category", { id: 1, slug: "foo", diff --git a/app/assets/javascripts/discourse/tests/unit/services/document-title-test.js b/app/assets/javascripts/discourse/tests/unit/services/document-title-test.js index 196f4f1b14a..5fc5919f6bc 100644 --- a/app/assets/javascripts/discourse/tests/unit/services/document-title-test.js +++ b/app/assets/javascripts/discourse/tests/unit/services/document-title-test.js @@ -1,22 +1,17 @@ -import { - currentUser, - discourseModule, -} from "discourse/tests/helpers/qunit-helpers"; -import DocumentTitle from "discourse/services/document-title"; -import AppEvents from "discourse/services/app-events"; +import { module, test } from "qunit"; +import { setupTest } from "ember-qunit"; +import { getOwner } from "discourse-common/lib/get-owner"; +import { currentUser } from "discourse/tests/helpers/qunit-helpers"; 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 () { const session = Session.current(); session.hasFocus = true; - this.documentTitle = DocumentTitle.create({ - session, - appEvents: AppEvents.create(), - }); - this.documentTitle.currentUser = null; + this.documentTitle = getOwner(this).lookup("service:document-title"); }); hooks.afterEach(function () { diff --git a/app/assets/javascripts/discourse/tests/unit/services/emoji-store-test.js b/app/assets/javascripts/discourse/tests/unit/services/emoji-store-test.js index 41f10e0bb09..17e963909a6 100644 --- a/app/assets/javascripts/discourse/tests/unit/services/emoji-store-test.js +++ b/app/assets/javascripts/discourse/tests/unit/services/emoji-store-test.js @@ -1,9 +1,12 @@ -import { discourseModule } from "discourse/tests/helpers/qunit-helpers"; -import { test } from "qunit"; +import { module, 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 () { - this.emojiStore = this.container.lookup("service:emoji-store"); + this.emojiStore = getOwner(this).lookup("service:emoji-store"); this.emojiStore.reset(); }); diff --git a/app/assets/javascripts/discourse/tests/unit/services/store-test.js b/app/assets/javascripts/discourse/tests/unit/services/store-test.js index 64caea852e6..33129858c94 100644 --- a/app/assets/javascripts/discourse/tests/unit/services/store-test.js +++ b/app/assets/javascripts/discourse/tests/unit/services/store-test.js @@ -1,9 +1,13 @@ 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) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const widget = store.createRecord("widget", { id: 111, name: "hello" }); 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) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const widget = store.createRecord("widget", { name: "hello" }); 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) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const widget = store.createRecord("widget", { id: 1, name: "hello" }); const obj = { id: 1, name: "something" }; @@ -32,7 +36,7 @@ module("Unit | Service | store", function () { }); test("createRecord without attributes", function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const widget = store.createRecord("widget"); 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) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const widget = 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) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const widget = await store.find("widget", 123); assert.strictEqual(widget.get("name"), "Trout Lure"); @@ -71,19 +75,19 @@ module("Unit | Service | store", function () { }); 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 }); assert.strictEqual(widget.get("firstObject.name"), "Trout Lure"); }); 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" }); assert.strictEqual(widget.get("firstObject.id"), 123); }); 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" }); 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) { - 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(); - - assert.strictEqual(refreshed.content[0].name, "souna"); + assert.strictEqual(refreshed.content[0].slug, "souna"); }); test("update", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const result = await store.update("widget", 123, { name: "hello" }); assert.ok(result); }); 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" }); assert.ok(result); assert.strictEqual(result.payload.name, "hello"); }); test("findAll", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const result = await store.findAll("widget"); assert.strictEqual(result.get("length"), 2); @@ -133,21 +150,21 @@ module("Unit | Service | store", function () { }); test("destroyRecord", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const widget = await store.find("widget", 123); assert.ok(await store.destroyRecord("widget", widget)); }); test("destroyRecord when new", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const widget = store.createRecord("widget", { name: "hello" }); assert.ok(await store.destroyRecord("widget", widget)); }); test("find embedded", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const fruit = await store.find("fruit", 1); 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) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); let fruit = await store.find("fruit", 4); fruit.set("farmer", { dummy: "object" }); @@ -167,7 +184,7 @@ module("Unit | Service | store", function () { }); test("meta types", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const barn = await store.find("barn", 1); assert.strictEqual( barn.get("owner.name"), @@ -177,7 +194,7 @@ module("Unit | Service | store", function () { }); test("findAll embedded", async function (assert) { - const store = createStore(); + const store = getOwner(this).lookup("service:store"); const fruits = await store.findAll("fruit"); assert.strictEqual(fruits.objectAt(0).get("farmer.name"), "Old MacDonald"); assert.strictEqual( @@ -200,8 +217,19 @@ module("Unit | Service | store", function () { }); test("custom primaryKey", async function (assert) { - const store = createStore(); - const cats = await store.findAll("cat"); - assert.strictEqual(cats.objectAt(0).name, "souna"); + pretender.get("/users", () => { + return response({ + 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"); }); });