From 4c8b4116eba574506bc6ac67764a03e5398fb117 Mon Sep 17 00:00:00 2001 From: Germain Date: Thu, 13 Oct 2022 14:32:45 +0100 Subject: [PATCH] Migrate KeyboardShortcut tests from Enzyme to RTL (#9405) * Migrate tests from Enzyme to RTL * Fix languageHandler tests Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/settings/KeyboardShortcut-test.tsx | 29 +- .../KeyboardShortcut-test.tsx.snap | 111 +- .../user/KeyboardUserSettingsTab-test.tsx | 17 +- .../KeyboardUserSettingsTab-test.tsx.snap | 1178 ++++++++++++++--- test/i18n-test/languageHandler-test.tsx | 28 +- 5 files changed, 1082 insertions(+), 281 deletions(-) diff --git a/test/components/views/settings/KeyboardShortcut-test.tsx b/test/components/views/settings/KeyboardShortcut-test.tsx index 5fcb65ddba..d26c0dd1e9 100644 --- a/test/components/views/settings/KeyboardShortcut-test.tsx +++ b/test/components/views/settings/KeyboardShortcut-test.tsx @@ -16,17 +16,14 @@ limitations under the License. */ import React from "react"; -// eslint-disable-next-line deprecate/import -import { mount, ReactWrapper } from "enzyme"; +import { render } from "@testing-library/react"; import { Key } from "../../../../src/Keyboard"; import { mockPlatformPeg, unmockPlatformPeg } from "../../../test-utils/platform"; +import { KeyboardKey, KeyboardShortcut } from "../../../../src/components/views/settings/KeyboardShortcut"; -const PATH_TO_COMPONENT = "../../../../src/components/views/settings/KeyboardShortcut.tsx"; - -const renderKeyboardShortcut = async (component, props?): Promise => { - const Component = (await import(PATH_TO_COMPONENT))[component]; - return mount(); +const renderKeyboardShortcut = (Component, props?) => { + return render().container; }; describe("KeyboardShortcut", () => { @@ -35,24 +32,24 @@ describe("KeyboardShortcut", () => { unmockPlatformPeg(); }); - it("renders key icon", async () => { - const body = await renderKeyboardShortcut("KeyboardKey", { name: Key.ARROW_DOWN }); + it("renders key icon", () => { + const body = renderKeyboardShortcut(KeyboardKey, { name: Key.ARROW_DOWN }); expect(body).toMatchSnapshot(); }); - it("renders alternative key name", async () => { - const body = await renderKeyboardShortcut("KeyboardKey", { name: Key.PAGE_DOWN }); + it("renders alternative key name", () => { + const body = renderKeyboardShortcut(KeyboardKey, { name: Key.PAGE_DOWN }); expect(body).toMatchSnapshot(); }); - it("doesn't render + if last", async () => { - const body = await renderKeyboardShortcut("KeyboardKey", { name: Key.A, last: true }); + it("doesn't render + if last", () => { + const body = renderKeyboardShortcut(KeyboardKey, { name: Key.A, last: true }); expect(body).toMatchSnapshot(); }); - it("doesn't render same modifier twice", async () => { + it("doesn't render same modifier twice", () => { mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) }); - const body1 = await renderKeyboardShortcut("KeyboardShortcut", { + const body1 = renderKeyboardShortcut(KeyboardShortcut, { value: { key: Key.A, ctrlOrCmdKey: true, @@ -61,7 +58,7 @@ describe("KeyboardShortcut", () => { }); expect(body1).toMatchSnapshot(); - const body2 = await renderKeyboardShortcut("KeyboardShortcut", { + const body2 = renderKeyboardShortcut(KeyboardShortcut, { value: { key: Key.A, ctrlOrCmdKey: true, diff --git a/test/components/views/settings/__snapshots__/KeyboardShortcut-test.tsx.snap b/test/components/views/settings/__snapshots__/KeyboardShortcut-test.tsx.snap index 23062d7980..e452b0a47a 100644 --- a/test/components/views/settings/__snapshots__/KeyboardShortcut-test.tsx.snap +++ b/test/components/views/settings/__snapshots__/KeyboardShortcut-test.tsx.snap @@ -1,116 +1,73 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`KeyboardShortcut doesn't render + if last 1`] = ` - +
a - +
`; exports[`KeyboardShortcut doesn't render same modifier twice 1`] = ` - +
- - - - Ctrl - - - + - - - - - a - - - + + + Ctrl + + + + + + + a + +
- +
`; exports[`KeyboardShortcut doesn't render same modifier twice 2`] = ` - +
- - - - Ctrl - - - + - - - - - a - - - + + + Ctrl + + + + + + + a + +
- +
`; exports[`KeyboardShortcut renders alternative key name 1`] = ` - +
Page Down + - +
`; exports[`KeyboardShortcut renders key icon 1`] = ` - +
+ - +
`; diff --git a/test/components/views/settings/tabs/user/KeyboardUserSettingsTab-test.tsx b/test/components/views/settings/tabs/user/KeyboardUserSettingsTab-test.tsx index 57295a96fe..a96b3a6533 100644 --- a/test/components/views/settings/tabs/user/KeyboardUserSettingsTab-test.tsx +++ b/test/components/views/settings/tabs/user/KeyboardUserSettingsTab-test.tsx @@ -15,15 +15,16 @@ See the License for the specific language governing permissions and limitations under the License. */ +import { render } from "@testing-library/react"; import React from "react"; -// eslint-disable-next-line deprecate/import -import { mount, ReactWrapper } from "enzyme"; +import KeyboardUserSettingsTab from + "../../../../../../src/components/views/settings/tabs/user/KeyboardUserSettingsTab"; import { Key } from "../../../../../../src/Keyboard"; +import { mockPlatformPeg } from "../../../../../test-utils/platform"; const PATH_TO_KEYBOARD_SHORTCUTS = "../../../../../../src/accessibility/KeyboardShortcuts"; const PATH_TO_KEYBOARD_SHORTCUT_UTILS = "../../../../../../src/accessibility/KeyboardShortcutUtils"; -const PATH_TO_COMPONENT = "../../../../../../src/components/views/settings/tabs/user/KeyboardUserSettingsTab"; const mockKeyboardShortcuts = (override) => { jest.doMock(PATH_TO_KEYBOARD_SHORTCUTS, () => { @@ -45,17 +46,17 @@ const mockKeyboardShortcutUtils = (override) => { }); }; -const renderKeyboardUserSettingsTab = async (component): Promise => { - const Component = (await import(PATH_TO_COMPONENT))[component]; - return mount(); +const renderKeyboardUserSettingsTab = () => { + return render().container; }; describe("KeyboardUserSettingsTab", () => { beforeEach(() => { jest.resetModules(); + mockPlatformPeg(); }); - it("renders list of keyboard shortcuts", async () => { + it("renders list of keyboard shortcuts", () => { mockKeyboardShortcuts({ "CATEGORIES": { "Composer": { @@ -101,7 +102,7 @@ describe("KeyboardUserSettingsTab", () => { }, }); - const body = await renderKeyboardUserSettingsTab("default"); + const body = renderKeyboardUserSettingsTab(); expect(body).toMatchSnapshot(); }); }); diff --git a/test/components/views/settings/tabs/user/__snapshots__/KeyboardUserSettingsTab-test.tsx.snap b/test/components/views/settings/tabs/user/__snapshots__/KeyboardUserSettingsTab-test.tsx.snap index 71bab7f612..6151e4b959 100644 --- a/test/components/views/settings/tabs/user/__snapshots__/KeyboardUserSettingsTab-test.tsx.snap +++ b/test/components/views/settings/tabs/user/__snapshots__/KeyboardUserSettingsTab-test.tsx.snap @@ -1,190 +1,1026 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`KeyboardUserSettingsTab renders list of keyboard shortcuts 1`] = ` - +
Keyboard
-
-
- Composer -
-
- - -
- Cancel replying to a message - -
- - - - Ctrl - - - + - - - - - a - - - -
-
-
-
- -
- Toggle Bold - -
- - - - Ctrl - - - + - - - - - b - - - -
-
-
-
- -
+ Composer
-
- + +
+ Send message +
+ + + Enter + + +
+
+
+ New line +
+ + + Shift + + + + + + + Enter + + +
+
+
+ Toggle Bold +
+ + + Ctrl + + + + + + + b + + +
+
+
+ Toggle Italics +
+ + + Ctrl + + + + + + + i + + +
+
+
+ Toggle Quote +
+ + + Ctrl + + + + + + + Shift + + + + + + + > + + +
+
+
+ Toggle Link +
+ + + Ctrl + + + + + + + Shift + + + + + + + l + + +
+
+
+ Toggle Code Block +
+ + + Ctrl + + + + + + + e + + +
+
+
+ Undo edit +
+ + + Ctrl + + + + + + + z + + +
+
+
+ Redo edit +
+ + + Ctrl + + + + + + + y + + +
+
+
+ Jump to start of the composer +
+ + + Ctrl + + + + + + + Home + + +
+
+
+ Jump to end of the composer +
+ + + Ctrl + + + + + + + End + + +
+
+
+ Cancel replying to a message +
+ + + Esc + + +
+
+
+ Navigate to next message to edit +
+ + + ↓ + + +
+
+
+ Navigate to previous message to edit +
+ + + ↑ + + +
+
+
+ Navigate to next message in composer history +
+ + + Ctrl + + + + + + + Alt + + + + + + + ↓ + + +
+
+
+ Navigate to previous message in composer history +
+ + + Ctrl + + + + + + + Alt + + + + + + + ↑ + + +
+
+
+ Send a sticker +
+ + + Ctrl + + + + + + + ; + + +
+
+ +
+
+
-
- Navigation -
-
- - -
- Select room from the room list - -
- - - - Enter - - - -
-
-
-
- -
+ Calls
- +
+ +
+ Toggle microphone mute +
+ + + Ctrl + + + + + + + d + + +
+
+
+ Toggle webcam on/off +
+ + + Ctrl + + + + + + + e + + +
+
+ +
+
+
+
+ Room +
+
+ +
+ Search (must be enabled) +
+ + + Ctrl + + + + + + + f + + +
+
+
+ Upload a file +
+ + + Ctrl + + + + + + + Shift + + + + + + + u + + +
+
+
+ Dismiss read marker and jump to bottom +
+ + + Esc + + +
+
+
+ Jump to oldest unread message +
+ + + Shift + + + + + + + Page Up + + +
+
+
+ Scroll up in the timeline +
+ + + Page Up + + +
+
+
+ Scroll down in the timeline +
+ + + Page Down + + +
+
+
+ Jump to first message +
+ + + Ctrl + + + + + + + Home + + +
+
+
+ Jump to last message +
+ + + Ctrl + + + + + + + End + + +
+
+ +
+
+
+
+ Room List +
+
+ +
+ Select room from the room list +
+ + + Enter + + +
+
+
+ Collapse room list section +
+ + + ← + + +
+
+
+ Expand room list section +
+ + + → + + +
+
+
+ Navigate down in the room list +
+ + + ↓ + + +
+
+
+ Navigate up in the room list +
+ + + ↑ + + +
+
+ +
+
+
+
+ Accessibility +
+
+ +
+ Close dialog or context menu +
+ + + Esc + + +
+
+
+ Activate selected button +
+ + + Enter + + +
+
+ +
+
+
+
+ Navigation +
+
+ +
+ Toggle the top left menu +
+ + + Ctrl + + + + + + + \` + + +
+
+
+ Toggle right panel +
+ + + Ctrl + + + + + + + . + + +
+
+
+ Toggle space panel +
+ + + Ctrl + + + + + + + Shift + + + + + + + d + + +
+
+
+ Open this settings tab +
+ + + Ctrl + + + + + + + / + + +
+
+
+ Go to Home View +
+ + + Ctrl + + + + + + + Alt + + + + + + + h + + +
+
+
+ Jump to room search +
+ + + Ctrl + + + + + + + k + + +
+
+
+ Next unread room or DM +
+ + + Alt + + + + + + + Shift + + + + + + + ↓ + + +
+
+
+ Previous unread room or DM +
+ + + Alt + + + + + + + Shift + + + + + + + ↑ + + +
+
+
+ Next room or DM +
+ + + Alt + + + + + + + ↓ + + +
+
+
+ Previous room or DM +
+ + + Alt + + + + + + + ↑ + + +
+
+ +
+
+
+
+ Autocomplete +
+
+ +
+ Cancel autocomplete +
+ + + Esc + + +
+
+
+ Next autocomplete suggestion +
+ + + ↓ + + +
+
+
+ Previous autocomplete suggestion +
+ + + ↑ + + +
+
+
+ Complete +
+ + + Enter + + +
+
+
+ Force complete +
+ + + Tab + + +
+
+ +
+
-
+ `; diff --git a/test/i18n-test/languageHandler-test.tsx b/test/i18n-test/languageHandler-test.tsx index d492011032..a69ecd7dd5 100644 --- a/test/i18n-test/languageHandler-test.tsx +++ b/test/i18n-test/languageHandler-test.tsx @@ -100,6 +100,16 @@ describe('languageHandler', function() { ], ]; + let oldNodeEnv; + beforeAll(() => { + oldNodeEnv = process.env.NODE_ENV; + process.env.NODE_ENV = "test"; + }); + + afterAll(() => { + process.env.NODE_ENV = oldNodeEnv; + }); + describe('when translations exist in language', () => { beforeEach(function(done) { stubClient(); @@ -115,7 +125,7 @@ describe('languageHandler', function() { }).then(done); }); - it.each(testCasesEn)("%s", async (_d, translationString, variables, tags, result) => { + it.each(testCasesEn)("%s", (_d, translationString, variables, tags, result) => { expect(_t(translationString, variables, tags)).toEqual(result); }); @@ -137,9 +147,9 @@ describe('languageHandler', function() { }); describe('for a non-en language', () => { - beforeEach(async () => { + beforeEach(() => { stubClient(); - await setLanguage('lv'); + setLanguage('lv'); // counterpart doesnt expose any way to restore default config // missingEntryGenerator is mocked in the root setup file // reset to default here @@ -178,7 +188,7 @@ describe('languageHandler', function() { }); it.each(pluralCases)( "%s", - async (_d, translationString, variables, tags, result) => { + (_d, translationString, variables, tags, result) => { expect(_t(translationString, variables, tags)).toEqual(result); }, ); @@ -192,7 +202,7 @@ describe('languageHandler', function() { }); it.each(pluralCases)( "%s and translates with fallback locale, attributes fallback locale", - async (_d, translationString, variables, tags, result) => { + (_d, translationString, variables, tags, result) => { expect(_tDom(translationString, variables, tags)).toEqual({ result }); }, ); @@ -203,7 +213,7 @@ describe('languageHandler', function() { describe('_t', () => { it.each(testCasesEn)( "%s and translates with fallback locale", - async (_d, translationString, variables, tags, result) => { + (_d, translationString, variables, tags, result) => { expect(_t(translationString, variables, tags)).toEqual(result); }, ); @@ -212,7 +222,7 @@ describe('languageHandler', function() { describe('_tDom()', () => { it.each(testCasesEn)( "%s and translates with fallback locale, attributes fallback locale", - async (_d, translationString, variables, tags, result) => { + (_d, translationString, variables, tags, result) => { expect(_tDom(translationString, variables, tags)).toEqual({ result }); }, ); @@ -221,12 +231,12 @@ describe('languageHandler', function() { }); describe('when languages dont load', () => { - it('_t', async () => { + it('_t', () => { const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary"; expect(_t(STRING_NOT_IN_THE_DICTIONARY, {}, undefined)).toEqual(STRING_NOT_IN_THE_DICTIONARY); }); - it('_tDom', async () => { + it('_tDom', () => { const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary"; expect(_tDom(STRING_NOT_IN_THE_DICTIONARY, {}, undefined)).toEqual( { STRING_NOT_IN_THE_DICTIONARY });