Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
WIP - adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alun Turner committed Jul 14, 2023
1 parent 0b79138 commit 0944cd9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/components/views/rooms/SendMessageComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { doMaybeLocalRoomAction } from "../../../utils/local-room";
import { Caret } from "../../../editor/caret";
import { IDiff } from "../../../editor/diff";
import { getBlobSafeMimeType } from "../../../utils/blobs";
import { trackSlashCommandAnalyticEvent } from "./wysiwyg_composer/utils/message";

/**
* Build the mentions information based on the editor model (and any related events):
Expand Down Expand Up @@ -181,6 +182,7 @@ export function createMessageContent(
const isEmote = containsEmote(model);
if (isEmote) {
model = stripEmoteCommand(model);
trackSlashCommandAnalyticEvent("me", "Legacy");
}
if (startsWith(model, "//")) {
model = stripPrefix(model, "/");
Expand Down Expand Up @@ -491,6 +493,8 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro
return; // errored
}

trackSlashCommandAnalyticEvent(cmd.command, "Legacy");

if (content && [CommandCategories.messages, CommandCategories.effects].includes(cmd.category)) {
// Attach any mentions which might be contained in the command content.
attachMentions(this.props.mxClient.getSafeUserId(), content, model, replyToEvent);
Expand Down
5 changes: 1 addition & 4 deletions src/components/views/rooms/wysiwyg_composer/utils/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,7 @@ export async function editMessage(
* @param command - the slash command that will be recorded in the analytic event that is fired
* @returns void
*/
export function trackSlashCommandAnalyticEvent(
command: string,
editor: Exclude<SlashCommand["editor"], "Legacy">,
): void {
export function trackSlashCommandAnalyticEvent(command: string, editor: SlashCommand["editor"]): void {
PosthogAnalytics.instance.trackEvent<SlashCommand>({
eventName: "SlashCommand",
editor,
Expand Down
27 changes: 26 additions & 1 deletion test/components/views/rooms/SendMessageComposer-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import React from "react";
import { fireEvent, render, waitFor } from "@testing-library/react";
import { fireEvent, render, waitFor, screen } from "@testing-library/react";
import { IContent, MatrixClient, MsgType } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";
import userEvent from "@testing-library/user-event";
Expand All @@ -41,6 +41,7 @@ import { doMaybeLocalRoomAction } from "../../../../src/utils/local-room";
import { addTextToComposer } from "../../../test-utils/composer";
import dis from "../../../../src/dispatcher/dispatcher";
import SettingsStore from "../../../../src/settings/SettingsStore";
import * as mockPosthogAnalytics from "../../../../src/PosthogAnalytics";

jest.mock("../../../../src/utils/local-room", () => ({
doMaybeLocalRoomAction: jest.fn(),
Expand Down Expand Up @@ -539,6 +540,30 @@ describe("<SendMessageComposer/>", () => {

expect(dis.dispatch).not.toHaveBeenCalledWith({ action: `effects.confetti` });
});

describe.only("Analytics", () => {
beforeEach(async () => {
jest.spyOn(mockPosthogAnalytics.PosthogAnalytics.instance, "trackEvent").mockImplementation(() => {});
});
afterEach(() => {
jest.restoreAllMocks();
});

it("does something", () => {
getComponent();

userEvent.type(screen.getByRole("textbox"), "/me says something{Enter}");
screen.debug();

// note we expect two analytics events here - one for sending a message, one for the slash command
expect(mockPosthogAnalytics.PosthogAnalytics.instance.trackEvent).toHaveBeenCalledTimes(2);
expect(mockPosthogAnalytics.PosthogAnalytics.instance.trackEvent).toHaveBeenLastCalledWith({
eventName: "SlashCommand",
editor: "RtePlain",
command: "me",
});
});
});
});

describe("isQuickReaction", () => {
Expand Down

0 comments on commit 0944cd9

Please sign in to comment.