This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/unit/lib/preload-store-test.js
Jarek Radosz a17d54d0bf
DEV: De-arrowify tests (#11068)
Using arrow functions changes `this` context, which is undesired in tests, e.g. it makes it impossible to setup things like pretender (`this.server`) in `beforeEach` hooks.

Ember guides always use classic functions in examples (e.g. https://guides.emberjs.com/release/testing/test-types/), and that's what it uses in its own test suite, as do various addons and ember apps.

It was also already used in Discourse where `this` was required. Moving forward, it will be needed in more places as we migrate toward ember-cli.

(I might later add a custom rule to eslint-discourse-ember to enforce this)
2020-10-30 17:37:32 +01:00

55 lines
1.7 KiB
JavaScript

import { test, module } from "qunit";
import PreloadStore from "discourse/lib/preload-store";
import { Promise } from "rsvp";
module("preload-store", {
beforeEach() {
PreloadStore.store("bane", "evil");
},
});
test("get", function (assert) {
assert.blank(PreloadStore.get("joker"), "returns blank for a missing key");
assert.equal(
PreloadStore.get("bane"),
"evil",
"returns the value for that key"
);
});
test("remove", function (assert) {
PreloadStore.remove("bane");
assert.blank(PreloadStore.get("bane"), "removes the value if the key exists");
});
test("getAndRemove returns a promise that resolves to null", async function (assert) {
assert.blank(await PreloadStore.getAndRemove("joker"));
});
test("getAndRemove returns a promise that resolves to the result of the finder", async function (assert) {
const finder = () => "batdance";
const result = await PreloadStore.getAndRemove("joker", finder);
assert.equal(result, "batdance");
});
test("getAndRemove returns a promise that resolves to the result of the finder's promise", async function (assert) {
const finder = () => Promise.resolve("hahahah");
const result = await PreloadStore.getAndRemove("joker", finder);
assert.equal(result, "hahahah");
});
test("returns a promise that rejects with the result of the finder's rejected promise", async function (assert) {
const finder = () => Promise.reject("error");
await PreloadStore.getAndRemove("joker", finder).catch((result) => {
assert.equal(result, "error");
});
});
test("returns a promise that resolves to 'evil'", async function (assert) {
const result = await PreloadStore.getAndRemove("bane");
assert.equal(result, "evil");
});