discourse/plugins/chat/test/javascripts/unit/lib/chat-emoji-reaction-store-test.js
Martin Brennan ac7bf98ad1
DEV: Load client site settings YML into JS tests (#18413)
Our method of loading a subset of client settings into tests via
tests/helpers/site-settings.js can be improved upon. Currently we have a
hardcoded subset of the client settings, which may get out of date and not have
the correct defaults. As well as this plugins do not get their settings into the
tests, so whenever you need a setting from a plugin, even if it has a default,
you have to do needs.setting({ ... }) which is inconvenient.

This commit introduces an ember CLI build step to take the site_settings.yml and
all the plugin settings.yml files, pull out the client settings, and dump them
into a variable in a single JS file we can load in our tests, so we have the
correct selection of settings and default values in our JS tests. It also fixes
many, many tests that were operating under incorrect assumptions or old
settings.

Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
2022-11-08 09:17:43 +10:00

129 lines
3.9 KiB
JavaScript

import { module, test } from "qunit";
import { getOwner } from "discourse-common/lib/get-owner";
module("Discourse Chat | Unit | chat-emoji-reaction-store", function (hooks) {
hooks.beforeEach(function () {
this.siteSettings = getOwner(this).lookup("service:site-settings");
this.chatEmojiReactionStore = getOwner(this).lookup(
"service:chat-emoji-reaction-store"
);
this.chatEmojiReactionStore.siteSettings = this.siteSettings;
this.chatEmojiReactionStore.reset();
});
hooks.afterEach(function () {
this.chatEmojiReactionStore.reset();
});
// TODO (martin) Remove site setting workarounds after core PR#1290
test("defaults", function (assert) {
assert.deepEqual(
this.chatEmojiReactionStore.favorites,
(this.siteSettings.default_emoji_reactions || "")
.split("|")
.filter((val) => val)
);
});
test("diversity", function (assert) {
assert.strictEqual(this.chatEmojiReactionStore.diversity, 1);
this.chatEmojiReactionStore.diversity = 2;
assert.strictEqual(this.chatEmojiReactionStore.diversity, 2);
});
test("#favorites with defaults", function (assert) {
this.siteSettings.default_emoji_reactions = "smile|heart|tada";
assert.deepEqual(this.chatEmojiReactionStore.favorites, [
"smile",
"heart",
"tada",
]);
});
test("#favorites", function (assert) {
this.chatEmojiReactionStore.storedFavorites = ["grinning"];
assert.deepEqual(this.chatEmojiReactionStore.favorites, ["grinning"]);
});
test("#favorites when tracking multiple times the same emoji", function (assert) {
this.chatEmojiReactionStore.storedFavorites = [
"grinning",
"yum",
"not_yum",
"yum",
];
assert.deepEqual(
this.chatEmojiReactionStore.favorites,
["yum", "grinning", "not_yum"],
"it favors count over order"
);
});
test("#favorites when reaching displayed limit", function (assert) {
this.chatEmojiReactionStore.storedFavorites = [];
[...Array(this.chatEmojiReactionStore.MAX_TRACKED_EMOJIS)].forEach(
(_, index) => {
this.chatEmojiReactionStore.track("yum" + index);
}
);
this.chatEmojiReactionStore.track("grinning");
assert.strictEqual(
this.chatEmojiReactionStore.favorites.length,
this.chatEmojiReactionStore.MAX_DISPLAYED_EMOJIS,
"it enforces the max length"
);
});
test("#storedFavorites", function (assert) {
this.chatEmojiReactionStore.storedFavorites = [];
this.chatEmojiReactionStore.track("yum");
assert.deepEqual(
this.chatEmojiReactionStore.storedFavorites,
["yum"].concat(this.siteSettings.default_emoji_reactions.split("|"))
);
});
test("#storedFavorites when tracking different emojis", function (assert) {
this.chatEmojiReactionStore.storedFavorites = [];
this.chatEmojiReactionStore.track("yum");
this.chatEmojiReactionStore.track("not_yum");
this.chatEmojiReactionStore.track("yum");
this.chatEmojiReactionStore.track("grinning");
assert.deepEqual(
this.chatEmojiReactionStore.storedFavorites,
["grinning", "yum", "not_yum", "yum"].concat(
this.siteSettings.default_emoji_reactions.split("|")
),
"it ensures last in is first"
);
});
test("#storedFavorites when tracking an emoji after reaching the limit", function (assert) {
this.chatEmojiReactionStore.storedFavorites = [];
[...Array(this.chatEmojiReactionStore.MAX_TRACKED_EMOJIS)].forEach(() => {
this.chatEmojiReactionStore.track("yum");
});
this.chatEmojiReactionStore.track("grinning");
assert.strictEqual(
this.chatEmojiReactionStore.storedFavorites.length,
this.chatEmojiReactionStore.MAX_TRACKED_EMOJIS,
"it enforces the max length"
);
assert.strictEqual(
this.chatEmojiReactionStore.storedFavorites.firstObject,
"grinning",
"it correctly stores the last tracked emoji"
);
});
});