diff --git a/src/PosthogAnalytics.ts b/src/PosthogAnalytics.ts index 4187088253..01a8f52d8a 100644 --- a/src/PosthogAnalytics.ts +++ b/src/PosthogAnalytics.ts @@ -41,7 +41,7 @@ import { ScreenName } from "./PosthogTrackers"; * - If both flags are false or not set, events are not sent. */ -interface IEvent { +export interface IPosthogEvent { // The event name that will be used by PostHog. Event names should use camelCase. eventName: string; @@ -272,7 +272,7 @@ export class PosthogAnalytics { this.setAnonymity(Anonymity.Disabled); } - public trackEvent({ eventName, ...properties }: E): void { + public trackEvent({ eventName, ...properties }: E): void { if (this.anonymity == Anonymity.Disabled || this.anonymity == Anonymity.Anonymous) return; this.capture(eventName, properties); } diff --git a/test/CallHandler-test.ts b/test/CallHandler-test.ts index e0696fa287..53d341a6f4 100644 --- a/test/CallHandler-test.ts +++ b/test/CallHandler-test.ts @@ -53,7 +53,7 @@ const VIRTUAL_ROOM_BOB = "$virtual_bob_room:example.org"; const BOB_PHONE_NUMBER = "01818118181"; function mkStubDM(roomId, userId) { - const room = mkStubRoom(roomId); + const room = mkStubRoom(roomId, 'room', MatrixClientPeg.get()); room.getJoinedMembers = jest.fn().mockReturnValue([ { userId: '@me:example.org', @@ -312,9 +312,9 @@ describe('CallHandler', () => { fakeCall.emit(CallEvent.AssertedIdentityChanged); // Now set the config option - SdkConfig.put({ + SdkConfig.add({ voip: { - obeyAssertedIdentity: true, + obey_asserted_identity: true, }, }); diff --git a/test/PosthogAnalytics-test.ts b/test/PosthogAnalytics-test.ts index c0848b5c94..726b7b5afa 100644 --- a/test/PosthogAnalytics-test.ts +++ b/test/PosthogAnalytics-test.ts @@ -14,37 +14,34 @@ See the License for the specific language governing permissions and limitations under the License. */ +import { mocked } from 'jest-mock'; +import { PostHog } from 'posthog-js'; + import { Anonymity, + getRedactedCurrentLocation, - IEvent, + IPosthogEvent, PosthogAnalytics, } from '../src/PosthogAnalytics'; import SdkConfig from '../src/SdkConfig'; +import { getMockClientWithEventEmitter } from './test-utils'; -class FakePosthog { - public capture; - public init; - public identify; - public reset; - public register; +const getFakePosthog = (): PostHog => ({ + capture: jest.fn(), + init: jest.fn(), + identify: jest.fn(), + reset: jest.fn(), + register: jest.fn(), +} as unknown as PostHog); - constructor() { - this.capture = jest.fn(); - this.init = jest.fn(); - this.identify = jest.fn(); - this.reset = jest.fn(); - this.register = jest.fn(); - } -} - -export interface ITestEvent extends IEvent { +export interface ITestEvent extends IPosthogEvent { eventName: "JestTestEvents"; foo: string; } describe("PosthogAnalytics", () => { - let fakePosthog: FakePosthog; + let fakePosthog: PostHog; const shaHashes = { "42": "73475cb40a568e8da8a045ced110137e159f890ac4da883b6b17dc651b3a8049", "some": "a6b46dd0d1ae5e86cbc8f37e75ceeb6760230c1ca4ffbcb0c97b96dd7d9c464b", @@ -53,7 +50,7 @@ describe("PosthogAnalytics", () => { }; beforeEach(() => { - fakePosthog = new FakePosthog(); + fakePosthog = getFakePosthog(); window.crypto = { subtle: { @@ -64,10 +61,10 @@ describe("PosthogAnalytics", () => { for (let c = 0; c < hexHash.length; c += 2) { bytes.push(parseInt(hexHash.substr(c, 2), 16)); } - return bytes; + return bytes as unknown as ArrayBuffer; }, - }, - }; + } as unknown as SubtleCrypto, + } as unknown as Crypto; }); afterEach(() => { @@ -118,8 +115,8 @@ describe("PosthogAnalytics", () => { eventName: "JestTestEvents", foo: "bar", }); - expect(fakePosthog.capture.mock.calls[0][0]).toBe("JestTestEvents"); - expect(fakePosthog.capture.mock.calls[0][1]["foo"]).toEqual("bar"); + expect(mocked(fakePosthog).capture.mock.calls[0][0]).toBe("JestTestEvents"); + expect(mocked(fakePosthog).capture.mock.calls[0][1]["foo"]).toEqual("bar"); }); it("Should not track events if anonymous", async () => { @@ -128,7 +125,7 @@ describe("PosthogAnalytics", () => { eventName: "JestTestEvents", foo: "bar", }); - expect(fakePosthog.capture.mock.calls.length).toBe(0); + expect(fakePosthog.capture).not.toHaveBeenCalled(); }); it("Should not track any events if disabled", async () => { @@ -137,7 +134,7 @@ describe("PosthogAnalytics", () => { eventName: "JestTestEvents", foo: "bar", }); - expect(fakePosthog.capture.mock.calls.length).toBe(0); + expect(fakePosthog.capture).not.toHaveBeenCalled(); }); it("Should anonymise location of a known screen", async () => { @@ -157,28 +154,30 @@ describe("PosthogAnalytics", () => { it("Should identify the user to posthog if pseudonymous", async () => { analytics.setAnonymity(Anonymity.Pseudonymous); - class FakeClient { - getAccountDataFromServer = jest.fn().mockResolvedValue(null); - setAccountData = jest.fn().mockResolvedValue({}); - } - await analytics.identifyUser(new FakeClient(), () => "analytics_id"); - expect(fakePosthog.identify.mock.calls[0][0]).toBe("analytics_id"); + const client = getMockClientWithEventEmitter({ + getAccountDataFromServer: jest.fn().mockResolvedValue(null), + setAccountData: jest.fn().mockResolvedValue({}), + }); + await analytics.identifyUser(client, () => "analytics_id"); + expect(mocked(fakePosthog).identify.mock.calls[0][0]).toBe("analytics_id"); }); it("Should not identify the user to posthog if anonymous", async () => { analytics.setAnonymity(Anonymity.Anonymous); - await analytics.identifyUser(null); - expect(fakePosthog.identify.mock.calls.length).toBe(0); + const client = getMockClientWithEventEmitter({}); + await analytics.identifyUser(client, () => "analytics_id"); + expect(mocked(fakePosthog).identify.mock.calls.length).toBe(0); }); it("Should identify using the server's analytics id if present", async () => { analytics.setAnonymity(Anonymity.Pseudonymous); - class FakeClient { - getAccountDataFromServer = jest.fn().mockResolvedValue({ id: "existing_analytics_id" }); - setAccountData = jest.fn().mockResolvedValue({}); - } - await analytics.identifyUser(new FakeClient(), () => "new_analytics_id"); - expect(fakePosthog.identify.mock.calls[0][0]).toBe("existing_analytics_id"); + + const client = getMockClientWithEventEmitter({ + getAccountDataFromServer: jest.fn().mockResolvedValue({ id: "existing_analytics_id" }), + setAccountData: jest.fn().mockResolvedValue({}), + }); + await analytics.identifyUser(client, () => "new_analytics_id"); + expect(mocked(fakePosthog).identify.mock.calls[0][0]).toBe("existing_analytics_id"); }); }); }); diff --git a/test/RoomNotifs-test.ts b/test/RoomNotifs-test.ts index 95a8a18bdc..703f681e32 100644 --- a/test/RoomNotifs-test.ts +++ b/test/RoomNotifs-test.ts @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +import { mocked } from 'jest-mock'; import { ConditionKind, PushRuleActionName, TweakName } from "matrix-js-sdk/src/@types/PushRules"; import { stubClient } from "./test-utils"; @@ -26,7 +27,7 @@ describe("RoomNotifs test", () => { }); it("getRoomNotifsState handles rules with no conditions", () => { - MatrixClientPeg.get().pushRules = { + mocked(MatrixClientPeg.get()).pushRules = { global: { override: [{ rule_id: "!roomId:server", @@ -40,7 +41,7 @@ describe("RoomNotifs test", () => { }); it("getRoomNotifsState handles guest users", () => { - MatrixClientPeg.get().isGuest.mockReturnValue(true); + mocked(MatrixClientPeg.get()).isGuest.mockReturnValue(true); expect(getRoomNotifsState("!roomId:server")).toBe(RoomNotifState.AllMessages); }); diff --git a/test/accessibility/KeyboardShortcutUtils-test.ts b/test/accessibility/KeyboardShortcutUtils-test.ts index 4622198582..39f5bc3910 100644 --- a/test/accessibility/KeyboardShortcutUtils-test.ts +++ b/test/accessibility/KeyboardShortcutUtils-test.ts @@ -19,9 +19,13 @@ import { mock, } from "../../src/accessibility/KeyboardShortcuts"; import { getKeyboardShortcuts, getKeyboardShortcutsForUI } from "../../src/accessibility/KeyboardShortcutUtils"; -import PlatformPeg from "../../src/PlatformPeg"; +import { mockPlatformPeg, unmockPlatformPeg } from "../test-utils"; describe("KeyboardShortcutUtils", () => { + afterEach(() => { + unmockPlatformPeg(); + }); + it("doesn't change KEYBOARD_SHORTCUTS when getting shortcuts", async () => { mock({ keyboardShortcuts: { @@ -31,7 +35,7 @@ describe("KeyboardShortcutUtils", () => { macOnlyShortcuts: ["Keybind1"], desktopShortcuts: ["Keybind2"], }); - PlatformPeg.get = () => ({ overrideBrowserShortcuts: () => false }); + mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) }); const copyKeyboardShortcuts = Object.assign({}, KEYBOARD_SHORTCUTS); getKeyboardShortcuts(); @@ -52,7 +56,7 @@ describe("KeyboardShortcutUtils", () => { desktopShortcuts: ["Keybind2"], }); - PlatformPeg.get = () => ({ overrideBrowserShortcuts: () => false }); + mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) }); expect(getKeyboardShortcuts()).toEqual({ "Keybind4": {} }); mock({ @@ -63,8 +67,7 @@ describe("KeyboardShortcutUtils", () => { macOnlyShortcuts: undefined, desktopShortcuts: ["Keybind2"], }); - PlatformPeg.get = () => ({ overrideBrowserShortcuts: () => true }); + mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(true) }); expect(getKeyboardShortcuts()).toEqual({ "Keybind1": {}, "Keybind2": {} }); - jest.resetModules(); }); }); diff --git a/test/accessibility/RovingTabIndex-test.tsx b/test/accessibility/RovingTabIndex-test.tsx index b819de7222..e487098e10 100644 --- a/test/accessibility/RovingTabIndex-test.tsx +++ b/test/accessibility/RovingTabIndex-test.tsx @@ -108,7 +108,10 @@ describe("RovingTabIndex", () => { { button2 } { ({ onFocus, isActive, ref }) => - + } } diff --git a/test/components/views/rooms/SendMessageComposer-test.tsx b/test/components/views/rooms/SendMessageComposer-test.tsx index 2ebe162ea8..d2f54c3358 100644 --- a/test/components/views/rooms/SendMessageComposer-test.tsx +++ b/test/components/views/rooms/SendMessageComposer-test.tsx @@ -35,10 +35,9 @@ import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; import defaultDispatcher from "../../../../src/dispatcher/dispatcher"; import DocumentOffset from '../../../../src/editor/offset'; import { Layout } from '../../../../src/settings/enums/Layout'; -import PlatformPeg from "../../../../src/PlatformPeg"; import { IRoomState } from "../../../../src/components/structures/RoomView"; import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks"; -import BasePlatform from "../../../../src/BasePlatform"; +import { mockPlatformPeg } from "../../../test-utils/platform"; const WrapWithProviders: React.FC<{ roomContext: IRoomState; @@ -271,8 +270,7 @@ describe('', () => { }); it("persists to session history upon sending", async () => { - jest.spyOn(PlatformPeg, 'get').mockReturnValue( - { overrideBrowserShortcuts: () => false } as unknown as BasePlatform); + mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) }); const wrapper = getComponent({ replyToEvent: mockEvent }); diff --git a/tsconfig.json b/tsconfig.json index c44aaff28e..ff416f630c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,14 +22,8 @@ "include": [ "./src/**/*.ts", "./src/**/*.tsx", - "./test/test-utils/**/*.ts", - "./test/test-utils/**/*.tsx", - "./test/utils/**/*.ts", - "./test/utils/**/*.tsx", - "./test/stores/**/*.ts", - "./test/stores/**/*.tsx", - "./test/components/**/*.tsx", - "./test/components/**/*.ts", + "./test/**/*.ts", + "./test/**/*.tsx", ], "exclude": [ "./test/end-to-end-tests/"