Include /test in tsc config, fix rest of issues (#8119)

* fix ts issue in PosthogAnalytics test

Signed-off-by: Kerry Archibald <kerrya@element.io>

* fix remaining ts issues

Signed-off-by: Kerry Archibald <kerrya@element.io>

* tsconfig change

Signed-off-by: Kerry Archibald <kerrya@element.io>

* use sdkconfig patch instead of put

Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
Kerry 2022-03-23 11:27:28 +01:00 committed by GitHub
parent 752ad6a9f9
commit a8d65ab5c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 63 additions and 65 deletions

View file

@ -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<E extends IEvent>({ eventName, ...properties }: E): void {
public trackEvent<E extends IPosthogEvent>({ eventName, ...properties }: E): void {
if (this.anonymity == Anonymity.Disabled || this.anonymity == Anonymity.Anonymous) return;
this.capture(eventName, properties);
}

View file

@ -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,
},
});

View file

@ -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");
});
});
});

View file

@ -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);
});

View file

@ -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();
});
});

View file

@ -108,7 +108,10 @@ describe("RovingTabIndex", () => {
{ button2 }
<RovingTabIndexWrapper>
{ ({ onFocus, isActive, ref }) =>
<button onFocus={onFocus} tabIndex={isActive ? 0 : -1} ref={ref}>.</button>
<button
onFocus={onFocus}
tabIndex={isActive ? 0 : -1}
ref={ref as React.RefObject<HTMLButtonElement>}>.</button>
}
</RovingTabIndexWrapper>
</React.Fragment> }

View file

@ -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('<SendMessageComposer/>', () => {
});
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 });

View file

@ -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/"