2020-05-07 03:28:06 +08:00
|
|
|
import PreloadStore from "discourse/lib/preload-store";
|
2020-02-06 00:14:42 +08:00
|
|
|
import { Promise } from "rsvp";
|
2015-08-12 00:27:07 +08:00
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.module("preload-store", {
|
|
|
|
beforeEach() {
|
2013-06-20 03:06:23 +08:00
|
|
|
PreloadStore.store("bane", "evil");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("get", assert => {
|
|
|
|
assert.blank(PreloadStore.get("joker"), "returns blank for a missing key");
|
|
|
|
assert.equal(
|
|
|
|
PreloadStore.get("bane"),
|
|
|
|
"evil",
|
|
|
|
"returns the value for that key"
|
|
|
|
);
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("remove", assert => {
|
2013-06-20 03:06:23 +08:00
|
|
|
PreloadStore.remove("bane");
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.blank(PreloadStore.get("bane"), "removes the value if the key exists");
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
QUnit.test(
|
2017-06-15 01:57:58 +08:00
|
|
|
"getAndRemove returns a promise that resolves to null",
|
2020-03-03 04:20:19 +08:00
|
|
|
async assert => {
|
|
|
|
assert.blank(await PreloadStore.getAndRemove("joker"));
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
QUnit.test(
|
2017-06-15 01:57:58 +08:00
|
|
|
"getAndRemove returns a promise that resolves to the result of the finder",
|
2020-03-03 04:20:19 +08:00
|
|
|
async assert => {
|
|
|
|
const finder = () => "batdance";
|
|
|
|
const result = await PreloadStore.getAndRemove("joker", finder);
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
assert.equal(result, "batdance");
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
QUnit.test(
|
2013-06-20 03:06:23 +08:00
|
|
|
"getAndRemove returns a promise that resolves to the result of the finder's promise",
|
2020-03-03 04:20:19 +08:00
|
|
|
async assert => {
|
|
|
|
const finder = () => Promise.resolve("hahahah");
|
|
|
|
const result = await PreloadStore.getAndRemove("joker", finder);
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
assert.equal(result, "hahahah");
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
QUnit.test(
|
2017-06-15 01:57:58 +08:00
|
|
|
"returns a promise that rejects with the result of the finder's rejected promise",
|
2020-03-03 04:20:19 +08:00
|
|
|
async assert => {
|
|
|
|
const finder = () => Promise.reject("error");
|
2018-06-15 23:03:24 +08:00
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
await PreloadStore.getAndRemove("joker", finder).catch(result => {
|
2017-06-15 01:57:58 +08:00
|
|
|
assert.equal(result, "error");
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
|
|
|
);
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2020-03-03 04:20:19 +08:00
|
|
|
QUnit.test("returns a promise that resolves to 'evil'", async assert => {
|
|
|
|
const result = await PreloadStore.getAndRemove("bane");
|
|
|
|
assert.equal(result, "evil");
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|