Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
fix more type issues in unit tests (#8053)
Browse files Browse the repository at this point in the history
* fix ts issues in test/components/structures

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

* fix ts issues in test/components/views/context_menus

Signed-off-by: Kerry Archibald <kerrya@element.io>
  • Loading branch information
Kerry committed Mar 15, 2022
1 parent 813a60a commit bc8fdac
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
14 changes: 7 additions & 7 deletions test/components/structures/CallEventGrouper-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

import "../../skinned-sdk";
import { MatrixClient } from 'matrix-js-sdk/src/matrix';
import { MatrixClient, MatrixEvent } from 'matrix-js-sdk/src/matrix';
import { EventType } from "matrix-js-sdk/src/@types/event";
import { CallState } from "matrix-js-sdk/src/webrtc/call";

Expand Down Expand Up @@ -52,7 +52,7 @@ describe('CallEventGrouper', () => {
sender: {
userId: THEIR_USER_ID,
},
});
} as unknown as MatrixEvent);

expect(grouper.state).toBe(CustomCallState.Missed);
});
Expand All @@ -73,7 +73,7 @@ describe('CallEventGrouper', () => {
sender: {
userId: MY_USER_ID,
},
});
} as unknown as MatrixEvent);
grouperHangup.add({
getContent: () => {
return {
Expand All @@ -86,7 +86,7 @@ describe('CallEventGrouper', () => {
sender: {
userId: THEIR_USER_ID,
},
});
} as unknown as MatrixEvent);

grouperReject.add({
getContent: () => {
Expand All @@ -100,7 +100,7 @@ describe('CallEventGrouper', () => {
sender: {
userId: MY_USER_ID,
},
});
} as unknown as MatrixEvent);
grouperReject.add({
getContent: () => {
return {
Expand All @@ -113,7 +113,7 @@ describe('CallEventGrouper', () => {
sender: {
userId: THEIR_USER_ID,
},
});
} as unknown as MatrixEvent);

expect(grouperHangup.state).toBe(CallState.Ended);
expect(grouperReject.state).toBe(CallState.Ended);
Expand All @@ -134,7 +134,7 @@ describe('CallEventGrouper', () => {
getType: () => {
return EventType.CallInvite;
},
});
} as unknown as MatrixEvent);

expect(grouper.isVoice).toBe(false);
});
Expand Down
15 changes: 8 additions & 7 deletions test/components/structures/auth/Login-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ limitations under the License.
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import ReactTestUtils from 'react-dom/test-utils';
import { createClient } from "matrix-js-sdk/src/matrix";
import { mocked } from 'jest-mock';
import { createClient, MatrixClient } from "matrix-js-sdk/src/matrix";

import sdk from '../../../skinned-sdk';
import SdkConfig from '../../../../src/SdkConfig';
Expand All @@ -35,10 +36,10 @@ const Login = sdk.getComponent(

describe('Login', function() {
let parentDiv;
const mockClient = {
const mockClient = mocked({
login: jest.fn().mockResolvedValue({}),
loginFlows: jest.fn(),
};
} as unknown as MatrixClient);

beforeEach(function() {
jest.spyOn(SdkConfig, "get").mockReturnValue({
Expand All @@ -47,7 +48,7 @@ describe('Login', function() {
});
mockClient.login.mockClear().mockResolvedValue({});
mockClient.loginFlows.mockClear().mockResolvedValue({ flows: [{ type: "m.login.password" }] });
createClient.mockReturnValue(mockClient);
mocked(createClient).mockReturnValue(mockClient);

parentDiv = document.createElement('div');
document.body.appendChild(parentDiv);
Expand Down Expand Up @@ -101,7 +102,7 @@ describe('Login', function() {
});

it("should show SSO button if that flow is available", async () => {
mockClient.loginFlows.mockReturnValue({ flows: [{ type: "m.login.sso" }] });
mockClient.loginFlows.mockResolvedValue({ flows: [{ type: "m.login.sso" }] });

const root = render();
await flushPromises();
Expand All @@ -111,7 +112,7 @@ describe('Login', function() {
});

it("should show both SSO button and username+password if both are available", async () => {
mockClient.loginFlows.mockReturnValue({ flows: [{ type: "m.login.password" }, { type: "m.login.sso" }] });
mockClient.loginFlows.mockResolvedValue({ flows: [{ type: "m.login.password" }, { type: "m.login.sso" }] });

const root = render();
await flushPromises();
Expand All @@ -124,7 +125,7 @@ describe('Login', function() {
});

it("should show multiple SSO buttons if multiple identity_providers are available", async () => {
mockClient.loginFlows.mockReturnValue({
mockClient.loginFlows.mockResolvedValue({
flows: [{
"type": "m.login.sso",
"identity_providers": [{
Expand Down
32 changes: 21 additions & 11 deletions test/components/views/context_menus/ContextMenu-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import React from "react";
import { mount } from "enzyme";

import "../../../skinned-sdk";
import ContextMenu, { ChevronFace } from "../../../../src/components/structures/ContextMenu.tsx";
import UIStore from "../../../../src/stores/UIStore.ts";
import ContextMenu, { ChevronFace } from "../../../../src/components/structures/ContextMenu";
import UIStore from "../../../../src/stores/UIStore";

describe("<ContextMenu />", () => {
// Hardcode window and menu dimensions
Expand All @@ -28,7 +28,7 @@ describe("<ContextMenu />", () => {
jest.spyOn(UIStore, "instance", "get").mockImplementation(() => ({
windowWidth: windowSize,
windowHeight: windowSize,
}));
}) as unknown as UIStore);
window.Element.prototype.getBoundingClientRect = jest.fn().mockReturnValue({
width: menuSize,
height: menuSize,
Expand All @@ -38,19 +38,22 @@ describe("<ContextMenu />", () => {

describe("near top edge of window", () => {
const targetY = -50;
const onFinished = jest.fn();

const wrapper = mount(
<ContextMenu
bottom={windowSize - targetY - menuSize}
right={menuSize}
onFinished={onFinished}
chevronFace={ChevronFace.Left}
chevronOffset={targetChevronOffset}
/>,
);
const chevron = wrapper.find(".mx_ContextualMenu_chevron_left");

const actualY = windowSize - parseInt(wrapper.getDOMNode().style.getPropertyValue("bottom")) - menuSize;
const actualChevronOffset = parseInt(chevron.getDOMNode().style.getPropertyValue("top"));
const bottomStyle = parseInt(wrapper.getDOMNode<HTMLElement>().style.getPropertyValue("bottom"));
const actualY = windowSize - bottomStyle - menuSize;
const actualChevronOffset = parseInt(chevron.getDOMNode<HTMLElement>().style.getPropertyValue("top"));

it("stays within the window", () => {
expect(actualY).toBeGreaterThanOrEqual(0);
Expand All @@ -62,19 +65,21 @@ describe("<ContextMenu />", () => {

describe("near right edge of window", () => {
const targetX = windowSize - menuSize + 50;
const onFinished = jest.fn();

const wrapper = mount(
<ContextMenu
bottom={0}
onFinished={onFinished}
left={targetX}
chevronFace={ChevronFace.Top}
chevronOffset={targetChevronOffset}
/>,
);
const chevron = wrapper.find(".mx_ContextualMenu_chevron_top");

const actualX = parseInt(wrapper.getDOMNode().style.getPropertyValue("left"));
const actualChevronOffset = parseInt(chevron.getDOMNode().style.getPropertyValue("left"));
const actualX = parseInt(wrapper.getDOMNode<HTMLElement>().style.getPropertyValue("left"));
const actualChevronOffset = parseInt(chevron.getDOMNode<HTMLElement>().style.getPropertyValue("left"));

it("stays within the window", () => {
expect(actualX + menuSize).toBeLessThanOrEqual(windowSize);
Expand All @@ -86,19 +91,21 @@ describe("<ContextMenu />", () => {

describe("near bottom edge of window", () => {
const targetY = windowSize - menuSize + 50;
const onFinished = jest.fn();

const wrapper = mount(
<ContextMenu
top={targetY}
left={0}
onFinished={onFinished}
chevronFace={ChevronFace.Right}
chevronOffset={targetChevronOffset}
/>,
);
const chevron = wrapper.find(".mx_ContextualMenu_chevron_right");

const actualY = parseInt(wrapper.getDOMNode().style.getPropertyValue("top"));
const actualChevronOffset = parseInt(chevron.getDOMNode().style.getPropertyValue("top"));
const actualY = parseInt(wrapper.getDOMNode<HTMLElement>().style.getPropertyValue("top"));
const actualChevronOffset = parseInt(chevron.getDOMNode<HTMLElement>().style.getPropertyValue("top"));

it("stays within the window", () => {
expect(actualY + menuSize).toBeLessThanOrEqual(windowSize);
Expand All @@ -110,19 +117,22 @@ describe("<ContextMenu />", () => {

describe("near left edge of window", () => {
const targetX = -50;
const onFinished = jest.fn();

const wrapper = mount(
<ContextMenu
top={0}
right={windowSize - targetX - menuSize}
chevronFace={ChevronFace.Bottom}
onFinished={onFinished}
chevronOffset={targetChevronOffset}
/>,
);
const chevron = wrapper.find(".mx_ContextualMenu_chevron_bottom");

const actualX = windowSize - parseInt(wrapper.getDOMNode().style.getPropertyValue("right")) - menuSize;
const actualChevronOffset = parseInt(chevron.getDOMNode().style.getPropertyValue("left"));
const rightStyle = parseInt(wrapper.getDOMNode<HTMLElement>().style.getPropertyValue("right"));
const actualX = windowSize - rightStyle - menuSize;
const actualChevronOffset = parseInt(chevron.getDOMNode<HTMLElement>().style.getPropertyValue("left"));

it("stays within the window", () => {
expect(actualX).toBeGreaterThanOrEqual(0);
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@
"./test/utils/**/*.tsx",
"./test/stores/**/*.ts",
"./test/stores/**/*.tsx",
"./test/components/structures/**/*.ts",
"./test/components/structures/**/*.tsx",
"./test/components/views/context_menus/**/*.ts",
"./test/components/views/context_menus/**/*.tsx",
],
}

0 comments on commit bc8fdac

Please sign in to comment.