1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-07-31 13:44:28 +03:00

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
8 changed files with 63 additions and 65 deletions

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