From 4efa0b215fa4f6fc49c79571e35fd61d707ee5ed Mon Sep 17 00:00:00 2001 From: Dariusz Niemczyk <3636685+Palid@users.noreply.github.com> Date: Fri, 1 Oct 2021 16:48:57 +0200 Subject: [PATCH] Add typescript support and fix tests after threads work (#19247) * Typescriptify tests * Fix tests after threads work * Add proper types for jest --- package.json | 3 +- .../{joining-test.js => joining-test.tsx} | 9 ++- .../{loading-test.js => loading-test.tsx} | 20 +++--- yarn.lock | 65 ++++++++++++++++++- 4 files changed, 79 insertions(+), 18 deletions(-) rename test/app-tests/{joining-test.js => joining-test.tsx} (97%) rename test/app-tests/{loading-test.js => loading-test.tsx} (97%) diff --git a/package.json b/package.json index d35eeb16b9..27a76219d9 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,7 @@ "@pmmmwh/react-refresh-webpack-plugin": "^0.4.3", "@principalstudio/html-webpack-inject-preload": "^1.2.7", "@types/flux": "^3.1.9", + "@types/jest": "^27.0.2", "@types/modernizr": "^3.5.3", "@types/node": "^14.14.22", "@types/react": "17.0.14", @@ -166,7 +167,7 @@ "jest": { "testEnvironment": "jest-environment-jsdom-sixteen", "testMatch": [ - "/test/**/*-test.[tj]s" + "/test/**/*-test.[tj]s?(x)" ], "setupFilesAfterEnv": [ "/node_modules/matrix-react-sdk/test/setupTests.js" diff --git a/test/app-tests/joining-test.js b/test/app-tests/joining-test.tsx similarity index 97% rename from test/app-tests/joining-test.js rename to test/app-tests/joining-test.tsx index 744037325c..b5601875b9 100644 --- a/test/app-tests/joining-test.js +++ b/test/app-tests/joining-test.tsx @@ -31,12 +31,12 @@ import {sleep} from "../test-utils"; import * as test_utils from "../test-utils"; import MockHttpBackend from "matrix-mock-request"; import "fake-indexeddb/auto"; +import { RoomView as RoomViewClass } from 'matrix-react-sdk/src/components/structures/RoomView'; const MatrixChat = sdk.getComponent('structures.MatrixChat'); const RoomDirectory = sdk.getComponent('structures.RoomDirectory'); const RoomPreviewBar = sdk.getComponent('rooms.RoomPreviewBar'); -const RoomView = sdk.getComponent('structures.RoomView'); const HS_URL='http://localhost'; const IS_URL='http://localhost'; @@ -50,7 +50,6 @@ describe('joining a room', function() { let matrixChat; beforeEach(function() { - test_utils.beforeEach(this); httpBackend = new MockHttpBackend(); jssdk.request(httpBackend.requestFn); parentDiv = document.createElement('div'); @@ -148,7 +147,7 @@ describe('joining a room', function() { // enter an alias in the input, and simulate enter const input = ReactTestUtils.findRenderedDOMComponentWithTag( - roomDir, 'input'); + roomDir, 'input') as HTMLInputElement; input.value = ROOM_ALIAS; ReactTestUtils.Simulate.change(input); ReactTestUtils.Simulate.keyUp(input, {key: 'Enter'}); @@ -165,7 +164,7 @@ describe('joining a room', function() { // we should now have a roomview roomView = ReactTestUtils.findRenderedComponentWithType( - matrixChat, RoomView); + matrixChat, RoomViewClass); // the preview bar may take a tick to be displayed return sleep(1); @@ -228,7 +227,7 @@ describe('joining a room', function() { return httpBackend.flush(); }).then(() => { // now the room should have loaded - expect(roomView.state.room).toExist(); + expect(roomView.state.room).toBeTruthy(); }); }); }); diff --git a/test/app-tests/loading-test.js b/test/app-tests/loading-test.tsx similarity index 97% rename from test/app-tests/loading-test.js rename to test/app-tests/loading-test.tsx index f08cb957f5..d2c7697c7b 100644 --- a/test/app-tests/loading-test.js +++ b/test/app-tests/loading-test.tsx @@ -39,6 +39,8 @@ import {sleep} from "../test-utils"; import "fake-indexeddb/auto"; import {cleanLocalstorage} from "../test-utils"; import {IndexedDBCryptoStore} from "matrix-js-sdk/src/crypto/store/indexeddb-crypto-store"; +import { RoomView as RoomViewClass } from 'matrix-react-sdk/src/components/structures/RoomView'; +import LoginComponent from 'matrix-react-sdk/src/components/structures/auth/Login'; const DEFAULT_HS_URL='http://my_server'; const DEFAULT_IS_URL='http://my_is'; @@ -94,7 +96,7 @@ describe('loading:', function() { * TODO: it would be nice to factor some of this stuff out of index.js so * that we can test it rather than our own implementation of it. */ - function loadApp(opts) { + function loadApp(opts?) { opts = opts || {}; const queryString = opts.queryString || ""; const uriFragment = opts.uriFragment || ""; @@ -164,7 +166,7 @@ describe('loading:', function() { // http requests until we do. // // returns a promise resolving to the received request - async function expectAndAwaitSync(opts) { + async function expectAndAwaitSync(opts?) { let syncRequest = null; const isGuest = opts && opts.isGuest; if (!isGuest) { @@ -633,7 +635,7 @@ describe('loading:', function() { async function completeLogin(matrixChat) { // we expect a single component const login = ReactTestUtils.findRenderedComponentWithType( - matrixChat, sdk.getComponent('structures.auth.Login')); + matrixChat, LoginComponent); // When we switch to the login component, it'll hit the login endpoint // for proof of life and to get flows. We'll only give it one option. @@ -673,7 +675,7 @@ describe('loading:', function() { // assert that we are on the loading page function assertAtLoadingSpinner(matrixChat) { - const domComponent = ReactDOM.findDOMNode(matrixChat); + const domComponent = ReactDOM.findDOMNode(matrixChat) as Element; expect(domComponent.className).toEqual("mx_MatrixChat_splash"); // just the spinner @@ -691,14 +693,14 @@ function awaitLoggedIn(matrixChat) { } console.log(Date.now() + ": Received on_logged_in action"); dis.unregister(dispatcherRef); - resolve(); + resolve(undefined); }; const dispatcherRef = dis.register(onAction); console.log(Date.now() + ": Waiting for on_logged_in action"); }); } -function awaitRoomView(matrixChat, retryLimit, retryCount) { +function awaitRoomView(matrixChat, retryLimit?, retryCount?) { if (retryLimit === undefined) { retryLimit = 5; } @@ -721,17 +723,17 @@ function awaitRoomView(matrixChat, retryLimit, retryCount) { // state looks good, check the rendered output ReactTestUtils.findRenderedComponentWithType( - matrixChat, sdk.getComponent('structures.RoomView')); + matrixChat, RoomViewClass); return Promise.resolve(); } -function awaitLoginComponent(matrixChat, attempts) { +function awaitLoginComponent(matrixChat, attempts?) { return MatrixReactTestUtils.waitForRenderedComponentWithType( matrixChat, sdk.getComponent('structures.auth.Login'), attempts, ); } -function awaitWelcomeComponent(matrixChat, attempts) { +function awaitWelcomeComponent(matrixChat, attempts?) { return MatrixReactTestUtils.waitForRenderedComponentWithType( matrixChat, sdk.getComponent('auth.Welcome'), attempts, ); diff --git a/yarn.lock b/yarn.lock index 0bcfa42d16..458097efe2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1293,6 +1293,17 @@ "@types/yargs" "^15.0.0" chalk "^4.0.0" +"@jest/types@^27.2.4": + version "27.2.4" + resolved "https://registry.yarnpkg.com/@jest/types/-/types-27.2.4.tgz#2430042a66e00dc5b140c3636f4474d464c21ee8" + integrity sha512-IDO2ezTxeMvQAHxzG/ZvEyA47q0aVfzT95rGFl7bZs/Go0aIucvfDbS2rmnoEdXxlLQhcolmoG/wvL/uKx4tKA== + dependencies: + "@types/istanbul-lib-coverage" "^2.0.0" + "@types/istanbul-reports" "^3.0.0" + "@types/node" "*" + "@types/yargs" "^16.0.0" + chalk "^4.0.0" + "@matrix-org/olm@https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.3.tgz": version "3.2.3" resolved "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.3.tgz#cc332fdd25c08ef0e40f4d33fc3f822a0f98b6f4" @@ -1652,6 +1663,14 @@ dependencies: "@types/istanbul-lib-report" "*" +"@types/jest@^27.0.2": + version "27.0.2" + resolved "https://registry.yarnpkg.com/@types/jest/-/jest-27.0.2.tgz#ac383c4d4aaddd29bbf2b916d8d105c304a5fcd7" + integrity sha512-4dRxkS/AFX0c5XW6IPMNOydLn2tEhNhJV7DnYK+0bjoJZ+QTmfucBlihX7aoEsh/ocYtkLC73UbnBXBXIxsULA== + dependencies: + jest-diff "^27.0.0" + pretty-format "^27.0.0" + "@types/json-schema@^7.0.5", "@types/json-schema@^7.0.7", "@types/json-schema@^7.0.8": version "7.0.9" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.9.tgz#97edc9037ea0c38585320b28964dde3b39e4660d" @@ -1822,6 +1841,13 @@ dependencies: "@types/yargs-parser" "*" +"@types/yargs@^16.0.0": + version "16.0.4" + resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-16.0.4.tgz#26aad98dd2c2a38e421086ea9ad42b9e51642977" + integrity sha512-T8Yc9wt/5LbJyCaLiHPReJa0kApcIgJ7Bn735GjItUfh08Z1pJvu8QZqb9s+mMvKV6WUQRV7K2R46YbjMXTTJw== + dependencies: + "@types/yargs-parser" "*" + "@typescript-eslint/eslint-plugin@^4.17.0": version "4.31.1" resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-4.31.1.tgz#e938603a136f01dcabeece069da5fb2e331d4498" @@ -2193,7 +2219,7 @@ ansi-regex@^4.1.0: resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-4.1.0.tgz#8b9f8f08cf1acb843756a839ca8c7e3168c51997" integrity sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg== -ansi-regex@^5.0.0: +ansi-regex@^5.0.0, ansi-regex@^5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304" integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ== @@ -2217,6 +2243,11 @@ ansi-styles@^4.0.0, ansi-styles@^4.1.0: dependencies: color-convert "^2.0.1" +ansi-styles@^5.0.0: + version "5.2.0" + resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-5.2.0.tgz#07449690ad45777d1924ac2abb2fc8895dba836b" + integrity sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA== + anymatch@^1.3.0: version "1.3.2" resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-1.3.2.tgz#553dcb8f91e3c889845dfdba34c77721b90b9d7a" @@ -4115,6 +4146,11 @@ diff-sequences@^26.6.2: resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-26.6.2.tgz#48ba99157de1923412eed41db6b6d4aa9ca7c0b1" integrity sha512-Mv/TDa3nZ9sbc5soK+OoA74BsS3mL37yixCvUAQkiuA4Wz6YtwP/K47n2rv2ovzHZvoiQeA5FTQOschKkEwB0Q== +diff-sequences@^27.0.6: + version "27.0.6" + resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-27.0.6.tgz#3305cb2e55a033924054695cc66019fd7f8e5723" + integrity sha512-ag6wfpBFyNXZ0p8pcuIDS//D8H062ZQJ3fzYxjpmeKjnz8W4pekL3AI8VohmyZmsWW2PWaHgjsmqR6L13101VQ== + diffie-hellman@^5.0.0: version "5.0.3" resolved "https://registry.yarnpkg.com/diffie-hellman/-/diffie-hellman-5.0.3.tgz#40e8ee98f55a2149607146921c63e1ae5f3d2875" @@ -6867,6 +6903,16 @@ jest-diff@^26.6.2: jest-get-type "^26.3.0" pretty-format "^26.6.2" +jest-diff@^27.0.0: + version "27.2.4" + resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-27.2.4.tgz#171c51d3d2c105c457100fee6e7bf7cee51c8d8c" + integrity sha512-bLAVlDSCR3gqUPGv+4nzVpEXGsHh98HjUL7Vb2hVyyuBDoQmja8eJb0imUABsuxBeUVmf47taJSAd9nDrwWKEg== + dependencies: + chalk "^4.0.0" + diff-sequences "^27.0.6" + jest-get-type "^27.0.6" + pretty-format "^27.2.4" + jest-docblock@^26.0.0: version "26.0.0" resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-26.0.0.tgz#3e2fa20899fc928cb13bd0ff68bd3711a36889b5" @@ -6925,6 +6971,11 @@ jest-get-type@^26.3.0: resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-26.3.0.tgz#e97dc3c3f53c2b406ca7afaed4493b1d099199e0" integrity sha512-TpfaviN1R2pQWkIihlfEanwOXK0zcxrKEE4MlU6Tn7keoXdN6/3gK/xl0yEh8DOunn5pOVGKf8hB4R9gVh04ig== +jest-get-type@^27.0.6: + version "27.0.6" + resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-27.0.6.tgz#0eb5c7f755854279ce9b68a9f1a4122f69047cfe" + integrity sha512-XTkK5exIeUbbveehcSR8w0bhH+c0yloW/Wpl+9vZrjzztCPWrxhHwkIFpZzCt71oRBsgxmuUfxEqOYoZI2macg== + jest-haste-map@^26.6.2: version "26.6.2" resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-26.6.2.tgz#dd7e60fe7dc0e9f911a23d79c5ff7fb5c2cafeaa" @@ -9780,6 +9831,16 @@ pretty-format@^26.6.2: ansi-styles "^4.0.0" react-is "^17.0.1" +pretty-format@^27.0.0, pretty-format@^27.2.4: + version "27.2.4" + resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-27.2.4.tgz#08ea39c5eab41b082852d7093059a091f6ddc748" + integrity sha512-NUjw22WJHldzxyps2YjLZkUj6q1HvjqFezkB9Y2cklN8NtVZN/kZEXGZdFw4uny3oENzV5EEMESrkI0YDUH8vg== + dependencies: + "@jest/types" "^27.2.4" + ansi-regex "^5.0.1" + ansi-styles "^5.0.0" + react-is "^17.0.1" + process-nextick-args@~2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2" @@ -12204,10 +12265,8 @@ watchpack@^1.7.4: resolved "https://registry.yarnpkg.com/watchpack/-/watchpack-1.7.5.tgz#1267e6c55e0b9b5be44c2023aed5437a2c26c453" integrity sha512-9P3MWk6SrKjHsGkLT2KHXdQ/9SNkyoJbabxnKOoJepsvJjJG8uYTR3yTPxPQvNDI3w4Nz1xnE0TLHK4RIVe/MQ== dependencies: - chokidar "^3.4.1" graceful-fs "^4.1.2" neo-async "^2.5.0" - watchpack-chokidar2 "^2.0.1" optionalDependencies: chokidar "^3.4.1" watchpack-chokidar2 "^2.0.1"