From 0944cd97d0bf69267cd052951554d766b76c7287 Mon Sep 17 00:00:00 2001 From: Alun Turner Date: Fri, 14 Jul 2023 10:34:12 +0100 Subject: [PATCH] WIP - adding tests --- .../views/rooms/SendMessageComposer.tsx | 4 +++ .../rooms/wysiwyg_composer/utils/message.ts | 5 +--- .../views/rooms/SendMessageComposer-test.tsx | 27 ++++++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/components/views/rooms/SendMessageComposer.tsx b/src/components/views/rooms/SendMessageComposer.tsx index 3620da6d22b..90861f0c82e 100644 --- a/src/components/views/rooms/SendMessageComposer.tsx +++ b/src/components/views/rooms/SendMessageComposer.tsx @@ -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): @@ -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, "/"); @@ -491,6 +493,8 @@ export class SendMessageComposer extends React.Component, -): void { +export function trackSlashCommandAnalyticEvent(command: string, editor: SlashCommand["editor"]): void { PosthogAnalytics.instance.trackEvent({ eventName: "SlashCommand", editor, diff --git a/test/components/views/rooms/SendMessageComposer-test.tsx b/test/components/views/rooms/SendMessageComposer-test.tsx index bf9fb0e1851..2b39b3a4602 100644 --- a/test/components/views/rooms/SendMessageComposer-test.tsx +++ b/test/components/views/rooms/SendMessageComposer-test.tsx @@ -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"; @@ -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(), @@ -539,6 +540,30 @@ describe("", () => { 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", () => {