Files
blap/apps/web/test/unit-tests/modules/ComposerApi-test.ts
T
Will Hunt 02b6520f09 Module API for adding new file upload mechanisms (#33355)
* Initial reword of upload to MVVM.

* Update tests

* More incremental improvements

* Refactor tests to use helper method for composer uploads.

* Add drag and drop tests

* lint

* Add commentary

* fixup test

* More precise selector

* Retarget uploads

* lint

* fixup

* one more type

* update snap

* Fixup composerUploadFiles

* fix import

* lint

* Copy and paste fixes too

* Add tests for pasting

* Add tests for pasting files.

* Remove redundant fn

* rm comment

* tidy up

* Test cleanup

* More clean up

* another fix

* Begin fleshing out

* Park changes

* More stuff

* Use condensed version

* Cleanup tests

* more cleaning

* last bity

* Add a test for the composer

* Park up changes

* Rewrite Measured to be a functional component

* Add tests to cover narrow viewports

* lint

* breakpoint is optional

* Cleanup

* Support narrow mode

* fixup

* begone

* Provide default value

* add label

* fixup test

* update copyright

* cleanup

* Be a bit more lazy with FileDropTarget

* remove a debug statement

* Fixup

* fix two snaps

* Update screenshot

* and the other one

* Update snaps

* unfake CIDER

* update screens again

* remove extra test

* Undo accidental snapshots

* Bit of tidyup

* fixup

* even more tidyup

* may drag and drop file

* tidy up again

* snap snap snap

* Use load to make sonarQube happy

* Bunch of refactors

* More cleanup

* cleanup debug code

* tweaks

* remove a test we no longer need

* make it happy

* fix import

* fixup

* Update snaps

* typo

* one off

* Add tests

* lint

* remove only

* Reduce screenshot scope

* fix snapshot usage

* cleanup
2026-05-18 21:41:38 +00:00

84 lines
4.0 KiB
TypeScript

/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import { TimelineRenderingType } from "../../../src/contexts/RoomContext";
import { Action } from "../../../src/dispatcher/actions";
import { MatrixDispatcher } from "../../../src/dispatcher/dispatcher";
import { type ComposerInsertPayload, ComposerType } from "../../../src/dispatcher/payloads/ComposerInsertPayload";
import { ComposerApi, ModuleComposerApiEvents } from "../../../src/modules/ComposerApi";
import type { ComposerInsertFilesPayload } from "../../../src/dispatcher/payloads/ComposerInsertFilePayload";
describe("ComposerApi", () => {
describe("insertPlaintextIntoComposer()", () => {
it("should be able to insert text", () => {
const dispatcher = {
dispatch: jest.fn(),
} as unknown as MatrixDispatcher;
const api = new ComposerApi(dispatcher);
api.insertPlaintextIntoComposer("Hello world", { view: "room" });
expect(dispatcher.dispatch).toHaveBeenCalledWith({
action: Action.ComposerInsert,
text: "Hello world",
timelineRenderingType: TimelineRenderingType.Room,
composerType: ComposerType.Send,
} satisfies ComposerInsertPayload);
});
it("throws if called with an invalid view", () => {
const api = new ComposerApi(new MatrixDispatcher());
expect(() => api.insertPlaintextIntoComposer("text", { view: "bleh" as "room" })).toThrow(
"Invalid view 'bleh'",
);
});
});
describe("openFileUploadConfirmation()", () => {
it("should be able to initiate a file upload", () => {
const dispatcher = {
dispatch: jest.fn(),
} as unknown as MatrixDispatcher;
const api = new ComposerApi(dispatcher);
const files = [new File(["test"], "test.txt")];
api.openFileUploadConfirmation(files, { view: "room" });
expect(dispatcher.dispatch).toHaveBeenCalledWith({
action: Action.ComposerFileInsert,
files: files,
timelineRenderingType: TimelineRenderingType.Room,
} satisfies ComposerInsertFilesPayload);
});
it("throws if called with an invalid view", () => {
const api = new ComposerApi(new MatrixDispatcher());
const files = [new File(["test"], "test.txt")];
expect(() => api.openFileUploadConfirmation(files, { view: "bleh" as "room" })).toThrow(
"Invalid view 'bleh'",
);
});
});
describe("addFileUploadOption()", () => {
it("should be able to add a file upload option", () => {
const api = new ComposerApi(new MatrixDispatcher());
const eventCb = jest.fn();
api.on(ModuleComposerApiEvents.UploaderOptionsChanged, eventCb);
const option = { type: "an_option", label: "New option", onSelected: () => {} };
api.addFileUploadOption(option);
expect(api.fileUploadOptions).toHaveLength(1);
expect(api.fileUploadOptions).toContain(option);
expect(eventCb).toHaveBeenCalledWith(option);
});
it("throws if called with the type 'local'", () => {
const api = new ComposerApi(new MatrixDispatcher());
expect(() => api.addFileUploadOption({ type: "local", label: "New option", onSelected: () => {} })).toThrow(
'Option "local" is reserved',
);
});
it("throws if called with a duplicate type", () => {
const api = new ComposerApi(new MatrixDispatcher());
const option = { type: "an_option", label: "New option", onSelected: () => {} };
api.addFileUploadOption(option);
expect(() => api.addFileUploadOption({ ...option })).toThrow('Option "an_option" already exists');
});
});
});