discourse/plugins/chat/test/javascripts/unit/services/chat-drafts-manager-test.js
Joffrey JAFFEUX 4de1d3952b
FIX: improves draft for channels (#21724)
This commit attempts to correctly change draft when the channel changes. It moves responsibility to the composer instead of the channel.

A new service `chatDraftsManager` is being introduced here to allow finer control and pave the way for future thread draft support.

These changes also now allow an editing message to be stored as a draft.
2023-05-24 15:36:46 +02:00

54 lines
1.4 KiB
JavaScript

import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import { getOwner } from "discourse-common/lib/get-owner";
import fabricators from "discourse/plugins/chat/discourse/lib/fabricators";
module(
"Discourse Chat | Unit | Service | chat-drafts-manager",
function (hooks) {
setupTest(hooks);
hooks.beforeEach(function () {
this.subject = getOwner(this).lookup("service:chat-drafts-manager");
});
hooks.afterEach(function () {
this.subject.reset();
});
test("storing and retrieving message", function (assert) {
const message1 = fabricators.message();
this.subject.add(message1);
assert.strictEqual(
this.subject.get({ channelId: message1.channel.id }),
message1
);
const message2 = fabricators.message();
this.subject.add(message2);
assert.strictEqual(
this.subject.get({ channelId: message2.channel.id }),
message2
);
});
test("stores only chat messages", function (assert) {
assert.throws(function () {
this.subject.add({ foo: "bar" });
}, /instance of ChatMessage/);
});
test("#reset", function (assert) {
this.subject.add(fabricators.message());
assert.strictEqual(Object.keys(this.subject.drafts).length, 1);
this.subject.reset();
assert.strictEqual(Object.keys(this.subject.drafts).length, 0);
});
}
);