Skip to content

Commit

Permalink
Unit test tsc fixes part 15 (#8104)
Browse files Browse the repository at this point in the history
* fix ts issues in MPollBody test

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

* fix ts issues in PollCreateDialog

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

* fix settings components

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

* fix DateSeparator

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

* fix loosies

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

* update tsconfig

Signed-off-by: Kerry Archibald <kerrya@element.io>
  • Loading branch information
Kerry committed Mar 22, 2022
1 parent 2bf1d2b commit abc225d
Show file tree
Hide file tree
Showing 16 changed files with 2,379 additions and 2,754 deletions.
14 changes: 8 additions & 6 deletions test/components/views/elements/AppTile-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ describe("AppTile", () => {
await RightPanelStore.instance.onReady();
});

beforeEach(() => {
jest.spyOn(SettingsStore, "getValue").mockRestore();
});

it("tracks live tiles correctly", () => {
expect(AppTile.isLive("1", "r1")).toEqual(false);

Expand Down Expand Up @@ -196,7 +200,7 @@ describe("AppTile", () => {
it("distinguishes widgets with the same ID in different rooms", async () => {
// Set up right panel state
const realGetValue = SettingsStore.getValue;
SettingsStore.getValue = (name, roomId) => {
jest.spyOn(SettingsStore, 'getValue').mockImplementation((name, roomId) => {
if (name === "RightPanel.phases") {
if (roomId === "r1") {
return {
Expand All @@ -212,7 +216,7 @@ describe("AppTile", () => {
return null;
}
return realGetValue(name, roomId);
};
});

// Run initial render with room 1, and also running lifecycle methods
const renderer = TestRenderer.create(<MatrixClientContext.Provider value={cli}>
Expand All @@ -232,7 +236,7 @@ describe("AppTile", () => {
expect(AppTile.isLive("1", "r1")).toBe(true);
expect(AppTile.isLive("1", "r2")).toBe(false);

SettingsStore.getValue = (name, roomId) => {
jest.spyOn(SettingsStore, "getValue").mockImplementation((name, roomId) => {
if (name === "RightPanel.phases") {
if (roomId === "r2") {
return {
Expand All @@ -248,7 +252,7 @@ describe("AppTile", () => {
return null;
}
return realGetValue(name, roomId);
};
});
// Wait for RPS room 2 updates to fire
const rpsUpdated2 = waitForRps("r2");
// Switch to room 2
Expand All @@ -266,8 +270,6 @@ describe("AppTile", () => {

expect(AppTile.isLive("1", "r1")).toBe(false);
expect(AppTile.isLive("1", "r2")).toBe(true);

SettingsStore.getValue = realGetValue;
});

it("preserves non-persisted widget on container move", async () => {
Expand Down
58 changes: 20 additions & 38 deletions test/components/views/elements/PollCreateDialog-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ import {
M_TEXT,
PollStartEvent,
} from 'matrix-events-sdk';
import { IContent, MatrixEvent } from 'matrix-js-sdk/src/models/event';
import { MatrixEvent } from 'matrix-js-sdk/src/models/event';

import {
wrapInMatrixClientContext,
findById,
stubClient,
getMockClientWithEventEmitter,
} from '../../../test-utils';
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import _PollCreateDialog from "../../../../src/components/views/elements/PollCreateDialog";
const PollCreateDialog = wrapInMatrixClientContext(_PollCreateDialog);
import PollCreateDialog from "../../../../src/components/views/elements/PollCreateDialog";
import MatrixClientContext from '../../../../src/contexts/MatrixClientContext';

// Fake date to give a predictable snapshot
const realDateNow = Date.now;
Expand All @@ -51,9 +50,21 @@ afterAll(() => {
});

describe("PollCreateDialog", () => {
const mockClient = getMockClientWithEventEmitter({
sendEvent: jest.fn().mockResolvedValue({ event_id: '1' }),
});

beforeEach(() => {
mockClient.sendEvent.mockClear();
});

it("renders a blank poll", () => {
const dialog = mount(
<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />,
{
wrappingComponent: MatrixClientContext.Provider,
wrappingComponentProps: { value: mockClient },
},
);
expect(dialog.html()).toMatchSnapshot();
});
Expand Down Expand Up @@ -207,9 +218,6 @@ describe("PollCreateDialog", () => {
});

it("displays a spinner after submitting", () => {
stubClient();
MatrixClientPeg.get().sendEvent = jest.fn(() => Promise.resolve());

const dialog = mount(
<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />,
);
Expand All @@ -223,21 +231,6 @@ describe("PollCreateDialog", () => {
});

it("sends a poll create event when submitted", () => {
stubClient();
let sentEventContent: IContent = null;
MatrixClientPeg.get().sendEvent = jest.fn(
(
_roomId: string,
_threadId: string,
eventType: string,
content: IContent,
) => {
expect(M_POLL_START.matches(eventType)).toBeTruthy();
sentEventContent = content;
return Promise.resolve();
},
);

const dialog = mount(
<PollCreateDialog room={createRoom()} onFinished={jest.fn()} />,
);
Expand All @@ -246,6 +239,8 @@ describe("PollCreateDialog", () => {
changeValue(dialog, "Option 2", "A2");

dialog.find("button").simulate("click");
const [, , eventType, sentEventContent] = mockClient.sendEvent.mock.calls[0];
expect(M_POLL_START.matches(eventType)).toBeTruthy();
expect(sentEventContent).toEqual(
{
[M_TEXT.name]: "Q\n1. A1\n2. A2",
Expand Down Expand Up @@ -275,21 +270,6 @@ describe("PollCreateDialog", () => {
});

it("sends a poll edit event when editing", () => {
stubClient();
let sentEventContent: IContent = null;
MatrixClientPeg.get().sendEvent = jest.fn(
(
_roomId: string,
_threadId: string,
eventType: string,
content: IContent,
) => {
expect(M_POLL_START.matches(eventType)).toBeTruthy();
sentEventContent = content;
return Promise.resolve();
},
);

const previousEvent: MatrixEvent = new MatrixEvent(
PollStartEvent.from(
"Poll Q",
Expand All @@ -312,6 +292,8 @@ describe("PollCreateDialog", () => {
changeKind(dialog, M_POLL_KIND_UNDISCLOSED.name);
dialog.find("button").simulate("click");

const [, , eventType, sentEventContent] = mockClient.sendEvent.mock.calls[0];
expect(M_POLL_START.matches(eventType)).toBeTruthy();
expect(sentEventContent).toEqual(
{
"m.new_content": {
Expand Down
15 changes: 10 additions & 5 deletions test/components/views/messages/DateSeparator-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ limitations under the License.

import React from "react";
import { mount } from "enzyme";
import { mocked } from "jest-mock";

import sdk from "../../../skinned-sdk";
import * as TestUtils from "../../../test-utils";
import { formatFullDateNoTime } from "../../../../src/DateUtils";
import SettingsStore from "../../../../src/settings/SettingsStore";
import { UIFeature } from "../../../../src/settings/UIFeature";
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
import { getMockClientWithEventEmitter } from "../../../test-utils";

jest.mock("../../../../src/settings/SettingsStore");

const _DateSeparator = sdk.getComponent("views.messages.DateSeparator");
const DateSeparator = TestUtils.wrapInMatrixClientContext(_DateSeparator);
const DateSeparator = sdk.getComponent("views.messages.DateSeparator");

describe("DateSeparator", () => {
const HOUR_MS = 3600000;
Expand All @@ -45,8 +46,12 @@ describe("DateSeparator", () => {
}
}

const mockClient = getMockClientWithEventEmitter({});
const getComponent = (props = {}) =>
mount(<DateSeparator {...defaultProps} {...props} />);
mount(<DateSeparator {...defaultProps} {...props} />, {
wrappingComponent: MatrixClientContext.Provider,
wrappingComponentProps: { value: mockClient },
});

type TestCase = [string, number, string];
const testCases: TestCase[] = [
Expand Down Expand Up @@ -106,7 +111,7 @@ describe("DateSeparator", () => {

describe('when feature_jump_to_date is enabled', () => {
beforeEach(() => {
(SettingsStore.getValue as jest.Mock) = jest.fn((arg) => {
mocked(SettingsStore).getValue.mockImplementation((arg) => {
if (arg === "feature_jump_to_date") {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion test/components/views/messages/MLocationBody-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe("MLocationBody", () => {

describe("isSelfLocation", () => {
it("Returns true for a full m.asset event", () => {
const content = makeLocationContent("", 0);
const content = makeLocationContent("", '0');
expect(isSelfLocation(content)).toBe(true);
});

Expand Down
Loading

0 comments on commit abc225d

Please sign in to comment.