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>
This commit is contained in:
Germain 2022-10-13 14:32:45 +01:00 committed by GitHub
parent aa9f8eac52
commit 4c8b4116eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 1082 additions and 281 deletions

View file

@ -16,17 +16,14 @@ limitations under the License.
*/ */
import React from "react"; import React from "react";
// eslint-disable-next-line deprecate/import import { render } from "@testing-library/react";
import { mount, ReactWrapper } from "enzyme";
import { Key } from "../../../../src/Keyboard"; import { Key } from "../../../../src/Keyboard";
import { mockPlatformPeg, unmockPlatformPeg } from "../../../test-utils/platform"; 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 = (Component, props?) => {
return render(<Component {...props} />).container;
const renderKeyboardShortcut = async (component, props?): Promise<ReactWrapper> => {
const Component = (await import(PATH_TO_COMPONENT))[component];
return mount(<Component {...props} />);
}; };
describe("KeyboardShortcut", () => { describe("KeyboardShortcut", () => {
@ -35,24 +32,24 @@ describe("KeyboardShortcut", () => {
unmockPlatformPeg(); unmockPlatformPeg();
}); });
it("renders key icon", async () => { it("renders key icon", () => {
const body = await renderKeyboardShortcut("KeyboardKey", { name: Key.ARROW_DOWN }); const body = renderKeyboardShortcut(KeyboardKey, { name: Key.ARROW_DOWN });
expect(body).toMatchSnapshot(); expect(body).toMatchSnapshot();
}); });
it("renders alternative key name", async () => { it("renders alternative key name", () => {
const body = await renderKeyboardShortcut("KeyboardKey", { name: Key.PAGE_DOWN }); const body = renderKeyboardShortcut(KeyboardKey, { name: Key.PAGE_DOWN });
expect(body).toMatchSnapshot(); expect(body).toMatchSnapshot();
}); });
it("doesn't render + if last", async () => { it("doesn't render + if last", () => {
const body = await renderKeyboardShortcut("KeyboardKey", { name: Key.A, last: true }); const body = renderKeyboardShortcut(KeyboardKey, { name: Key.A, last: true });
expect(body).toMatchSnapshot(); expect(body).toMatchSnapshot();
}); });
it("doesn't render same modifier twice", async () => { it("doesn't render same modifier twice", () => {
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) }); mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
const body1 = await renderKeyboardShortcut("KeyboardShortcut", { const body1 = renderKeyboardShortcut(KeyboardShortcut, {
value: { value: {
key: Key.A, key: Key.A,
ctrlOrCmdKey: true, ctrlOrCmdKey: true,
@ -61,7 +58,7 @@ describe("KeyboardShortcut", () => {
}); });
expect(body1).toMatchSnapshot(); expect(body1).toMatchSnapshot();
const body2 = await renderKeyboardShortcut("KeyboardShortcut", { const body2 = renderKeyboardShortcut(KeyboardShortcut, {
value: { value: {
key: Key.A, key: Key.A,
ctrlOrCmdKey: true, ctrlOrCmdKey: true,

View file

@ -1,116 +1,73 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`KeyboardShortcut doesn't render + if last 1`] = ` exports[`KeyboardShortcut doesn't render + if last 1`] = `
<KeyboardKey <div>
last={true}
name="a"
>
<kbd> <kbd>
a a
</kbd> </kbd>
</KeyboardKey> </div>
`; `;
exports[`KeyboardShortcut doesn't render same modifier twice 1`] = ` exports[`KeyboardShortcut doesn't render same modifier twice 1`] = `
<KeyboardShortcut <div>
value={
Object {
"ctrlOrCmdKey": true,
"key": "a",
"metaKey": true,
}
}
>
<div <div
className="mx_KeyboardShortcut" class="mx_KeyboardShortcut"
> >
<KeyboardKey <kbd>
key="ctrlOrCmdKey"
name="Control" Ctrl
>
<kbd> </kbd>
+
Ctrl <kbd>
</kbd> a
+
</KeyboardKey> </kbd>
<KeyboardKey
last={true}
name="a"
>
<kbd>
a
</kbd>
</KeyboardKey>
</div> </div>
</KeyboardShortcut> </div>
`; `;
exports[`KeyboardShortcut doesn't render same modifier twice 2`] = ` exports[`KeyboardShortcut doesn't render same modifier twice 2`] = `
<KeyboardShortcut <div>
value={
Object {
"ctrlKey": true,
"ctrlOrCmdKey": true,
"key": "a",
}
}
>
<div <div
className="mx_KeyboardShortcut" class="mx_KeyboardShortcut"
> >
<KeyboardKey <kbd>
key="ctrlOrCmdKey"
name="Control" Ctrl
>
<kbd> </kbd>
+
Ctrl <kbd>
</kbd> a
+
</KeyboardKey> </kbd>
<KeyboardKey
last={true}
name="a"
>
<kbd>
a
</kbd>
</KeyboardKey>
</div> </div>
</KeyboardShortcut> </div>
`; `;
exports[`KeyboardShortcut renders alternative key name 1`] = ` exports[`KeyboardShortcut renders alternative key name 1`] = `
<KeyboardKey <div>
name="PageDown"
>
<kbd> <kbd>
Page Down Page Down
</kbd> </kbd>
+ +
</KeyboardKey> </div>
`; `;
exports[`KeyboardShortcut renders key icon 1`] = ` exports[`KeyboardShortcut renders key icon 1`] = `
<KeyboardKey <div>
name="ArrowDown"
>
<kbd> <kbd>
</kbd> </kbd>
+ +
</KeyboardKey> </div>
`; `;

View file

@ -15,15 +15,16 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { render } from "@testing-library/react";
import React from "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 { Key } from "../../../../../../src/Keyboard";
import { mockPlatformPeg } from "../../../../../test-utils/platform";
const PATH_TO_KEYBOARD_SHORTCUTS = "../../../../../../src/accessibility/KeyboardShortcuts"; const PATH_TO_KEYBOARD_SHORTCUTS = "../../../../../../src/accessibility/KeyboardShortcuts";
const PATH_TO_KEYBOARD_SHORTCUT_UTILS = "../../../../../../src/accessibility/KeyboardShortcutUtils"; const PATH_TO_KEYBOARD_SHORTCUT_UTILS = "../../../../../../src/accessibility/KeyboardShortcutUtils";
const PATH_TO_COMPONENT = "../../../../../../src/components/views/settings/tabs/user/KeyboardUserSettingsTab";
const mockKeyboardShortcuts = (override) => { const mockKeyboardShortcuts = (override) => {
jest.doMock(PATH_TO_KEYBOARD_SHORTCUTS, () => { jest.doMock(PATH_TO_KEYBOARD_SHORTCUTS, () => {
@ -45,17 +46,17 @@ const mockKeyboardShortcutUtils = (override) => {
}); });
}; };
const renderKeyboardUserSettingsTab = async (component): Promise<ReactWrapper> => { const renderKeyboardUserSettingsTab = () => {
const Component = (await import(PATH_TO_COMPONENT))[component]; return render(<KeyboardUserSettingsTab />).container;
return mount(<Component />);
}; };
describe("KeyboardUserSettingsTab", () => { describe("KeyboardUserSettingsTab", () => {
beforeEach(() => { beforeEach(() => {
jest.resetModules(); jest.resetModules();
mockPlatformPeg();
}); });
it("renders list of keyboard shortcuts", async () => { it("renders list of keyboard shortcuts", () => {
mockKeyboardShortcuts({ mockKeyboardShortcuts({
"CATEGORIES": { "CATEGORIES": {
"Composer": { "Composer": {
@ -101,7 +102,7 @@ describe("KeyboardUserSettingsTab", () => {
}, },
}); });
const body = await renderKeyboardUserSettingsTab("default"); const body = renderKeyboardUserSettingsTab();
expect(body).toMatchSnapshot(); expect(body).toMatchSnapshot();
}); });
}); });

View file

@ -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', () => { describe('when translations exist in language', () => {
beforeEach(function(done) { beforeEach(function(done) {
stubClient(); stubClient();
@ -115,7 +125,7 @@ describe('languageHandler', function() {
}).then(done); }).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); expect(_t(translationString, variables, tags)).toEqual(result);
}); });
@ -137,9 +147,9 @@ describe('languageHandler', function() {
}); });
describe('for a non-en language', () => { describe('for a non-en language', () => {
beforeEach(async () => { beforeEach(() => {
stubClient(); stubClient();
await setLanguage('lv'); setLanguage('lv');
// counterpart doesnt expose any way to restore default config // counterpart doesnt expose any way to restore default config
// missingEntryGenerator is mocked in the root setup file // missingEntryGenerator is mocked in the root setup file
// reset to default here // reset to default here
@ -178,7 +188,7 @@ describe('languageHandler', function() {
}); });
it.each(pluralCases)( it.each(pluralCases)(
"%s", "%s",
async (_d, translationString, variables, tags, result) => { (_d, translationString, variables, tags, result) => {
expect(_t(translationString, variables, tags)).toEqual(result); expect(_t(translationString, variables, tags)).toEqual(result);
}, },
); );
@ -192,7 +202,7 @@ describe('languageHandler', function() {
}); });
it.each(pluralCases)( it.each(pluralCases)(
"%s and translates with fallback locale, attributes fallback locale", "%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(<span lang="en">{ result }</span>); expect(_tDom(translationString, variables, tags)).toEqual(<span lang="en">{ result }</span>);
}, },
); );
@ -203,7 +213,7 @@ describe('languageHandler', function() {
describe('_t', () => { describe('_t', () => {
it.each(testCasesEn)( it.each(testCasesEn)(
"%s and translates with fallback locale", "%s and translates with fallback locale",
async (_d, translationString, variables, tags, result) => { (_d, translationString, variables, tags, result) => {
expect(_t(translationString, variables, tags)).toEqual(result); expect(_t(translationString, variables, tags)).toEqual(result);
}, },
); );
@ -212,7 +222,7 @@ describe('languageHandler', function() {
describe('_tDom()', () => { describe('_tDom()', () => {
it.each(testCasesEn)( it.each(testCasesEn)(
"%s and translates with fallback locale, attributes fallback locale", "%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(<span lang="en">{ result }</span>); expect(_tDom(translationString, variables, tags)).toEqual(<span lang="en">{ result }</span>);
}, },
); );
@ -221,12 +231,12 @@ describe('languageHandler', function() {
}); });
describe('when languages dont load', () => { 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"; 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); 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"; const STRING_NOT_IN_THE_DICTIONARY = "a string that isn't in the translations dictionary";
expect(_tDom(STRING_NOT_IN_THE_DICTIONARY, {}, undefined)).toEqual( expect(_tDom(STRING_NOT_IN_THE_DICTIONARY, {}, undefined)).toEqual(
<span lang="en">{ STRING_NOT_IN_THE_DICTIONARY }</span>); <span lang="en">{ STRING_NOT_IN_THE_DICTIONARY }</span>);