From af1ec76149e7e7ac6746ed56337a9ba9b5e276b9 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 7 Mar 2023 13:58:10 +0000 Subject: [PATCH] Fix jest/no-conditional-expect lint and enable it (#10307) --- .eslintrc.js | 3 - test/SlidingSyncManager-test.ts | 2 + .../views/rooms/MessageComposer-test.tsx | 4 + .../components/WysiwygComposer-test.tsx | 1 + test/editor/model-test.ts | 105 ++++++------------ test/editor/operations-test.ts | 7 +- test/utils/FixedRollingArray-test.ts | 2 + test/utils/Singleflight-test.ts | 8 +- 8 files changed, 49 insertions(+), 83 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index d133a3c0051..f448c78cd49 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -179,9 +179,6 @@ module.exports = { // TODO: There are many tests with invalid expects that should be fixed, // https://github.com/vector-im/element-web/issues/24709 "jest/valid-expect": "off", - // TODO: There are many cases to refactor away, - // https://github.com/vector-im/element-web/issues/24710 - "jest/no-conditional-expect": "off", // Also treat "oldBackendOnly" as a test function. // Used in some crypto tests. "jest/no-standalone-expect": [ diff --git a/test/SlidingSyncManager-test.ts b/test/SlidingSyncManager-test.ts index 3b49e7ee2a7..76ebd8f15c3 100644 --- a/test/SlidingSyncManager-test.ts +++ b/test/SlidingSyncManager-test.ts @@ -168,8 +168,10 @@ describe("SlidingSyncManager", () => { expect(slidingSync.setListRanges).toHaveBeenCalledTimes(wantWindows.length - 1); wantWindows.forEach((range, i) => { if (i === 0) { + // eslint-disable-next-line jest/no-conditional-expect expect(slidingSync.setList).toHaveBeenCalledWith( SlidingSyncManager.ListSearch, + // eslint-disable-next-line jest/no-conditional-expect expect.objectContaining({ ranges: [[0, batchSize - 1], range], }), diff --git a/test/components/views/rooms/MessageComposer-test.tsx b/test/components/views/rooms/MessageComposer-test.tsx index 48feec7a8f8..3ffe68424d2 100644 --- a/test/components/views/rooms/MessageComposer-test.tsx +++ b/test/components/views/rooms/MessageComposer-test.tsx @@ -220,8 +220,10 @@ describe("MessageComposer", () => { it(`should${value || "not"} display the button`, () => { if (value) { + // eslint-disable-next-line jest/no-conditional-expect expect(screen.getByLabelText(buttonLabel)).toBeInTheDocument(); } else { + // eslint-disable-next-line jest/no-conditional-expect expect(screen.queryByLabelText(buttonLabel)).not.toBeInTheDocument(); } }); @@ -242,8 +244,10 @@ describe("MessageComposer", () => { it(`should${!value || "not"} display the button`, () => { if (!value) { + // eslint-disable-next-line jest/no-conditional-expect expect(screen.getByLabelText(buttonLabel)).toBeInTheDocument(); } else { + // eslint-disable-next-line jest/no-conditional-expect expect(screen.queryByLabelText(buttonLabel)).not.toBeInTheDocument(); } }); diff --git a/test/components/views/rooms/wysiwyg_composer/components/WysiwygComposer-test.tsx b/test/components/views/rooms/wysiwyg_composer/components/WysiwygComposer-test.tsx index aa5c0bd7399..3ab7d768d64 100644 --- a/test/components/views/rooms/wysiwyg_composer/components/WysiwygComposer-test.tsx +++ b/test/components/views/rooms/wysiwyg_composer/components/WysiwygComposer-test.tsx @@ -186,6 +186,7 @@ describe("WysiwygComposer", () => { await waitFor(() => { const selection = document.getSelection(); if (selection) { + // eslint-disable-next-line jest/no-conditional-expect expect(selection.focusNode?.textContent).toEqual("other"); } }); diff --git a/test/editor/model-test.ts b/test/editor/model-test.ts index 04d2667f191..cb475f696b2 100644 --- a/test/editor/model-test.ts +++ b/test/editor/model-test.ts @@ -15,9 +15,10 @@ limitations under the License. */ import EditorModel from "../../src/editor/model"; -import { createPartCreator, createRenderer, isDocumentPosition } from "./mock"; +import { createPartCreator, createRenderer } from "./mock"; import DocumentOffset from "../../src/editor/offset"; import { PillPart } from "../../src/editor/parts"; +import DocumentPosition from "../../src/editor/position"; describe("editor/model", function () { describe("plain text manipulation", function () { @@ -26,10 +27,8 @@ describe("editor/model", function () { const model = new EditorModel([], createPartCreator(), renderer); model.update("hello", "insertText", new DocumentOffset(5, true)); expect(renderer.count).toBe(1); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(0); - expect(renderer.caret.offset).toBe(5); - } + expect((renderer.caret as DocumentPosition).index).toBe(0); + expect((renderer.caret as DocumentPosition).offset).toBe(5); expect(model.parts.length).toBe(1); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("hello"); @@ -40,10 +39,8 @@ describe("editor/model", function () { const model = new EditorModel([pc.plain("hello")], pc, renderer); model.update("hello world", "insertText", new DocumentOffset(11, true)); expect(renderer.count).toBe(1); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(0); - expect(renderer.caret.offset).toBe(11); - } + expect((renderer.caret as DocumentPosition).index).toBe(0); + expect((renderer.caret as DocumentPosition).offset).toBe(11); expect(model.parts.length).toBe(1); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("hello world"); @@ -54,10 +51,8 @@ describe("editor/model", function () { const model = new EditorModel([pc.plain("world")], pc, renderer); model.update("hello world", "insertText", new DocumentOffset(6, false)); expect(renderer.count).toBe(1); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(0); - expect(renderer.caret.offset).toBe(6); - } + expect((renderer.caret as DocumentPosition).index).toBe(0); + expect((renderer.caret as DocumentPosition).offset).toBe(6); expect(model.parts.length).toBe(1); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("hello world"); @@ -70,10 +65,8 @@ describe("editor/model", function () { const model = new EditorModel([pc.plain("hello")], pc, renderer); model.update("hello\n", "insertText", new DocumentOffset(6, true)); expect(renderer.count).toBe(1); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(1); - expect(renderer.caret.offset).toBe(1); - } + expect((renderer.caret as DocumentPosition).index).toBe(1); + expect((renderer.caret as DocumentPosition).offset).toBe(1); expect(model.parts.length).toBe(2); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("hello"); @@ -86,10 +79,8 @@ describe("editor/model", function () { const model = new EditorModel([pc.plain("hello")], pc, renderer); model.update("hello\n\n\nworld!", "insertText", new DocumentOffset(14, true)); expect(renderer.count).toBe(1); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(4); - expect(renderer.caret.offset).toBe(6); - } + expect((renderer.caret as DocumentPosition).index).toBe(4); + expect((renderer.caret as DocumentPosition).offset).toBe(6); expect(model.parts.length).toBe(5); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("hello"); @@ -112,10 +103,8 @@ describe("editor/model", function () { ); model.update("hello\nwarm\nworld", "insertText", new DocumentOffset(10, true)); expect(renderer.count).toBe(1); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(2); - expect(renderer.caret.offset).toBe(4); - } + expect((renderer.caret as DocumentPosition).index).toBe(2); + expect((renderer.caret as DocumentPosition).offset).toBe(4); expect(model.parts.length).toBe(5); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("hello"); @@ -135,10 +124,8 @@ describe("editor/model", function () { const pc = createPartCreator(); const model = new EditorModel([pc.plain("try "), pc.roomPill("#someroom")], pc, renderer); model.update("try foo#someroom", "insertText", new DocumentOffset(7, false)); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(0); - expect(renderer.caret.offset).toBe(7); - } + expect((renderer.caret as DocumentPosition).index).toBe(0); + expect((renderer.caret as DocumentPosition).offset).toBe(7); expect(model.parts.length).toBe(2); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("try foo"); @@ -150,10 +137,8 @@ describe("editor/model", function () { const pc = createPartCreator(); const model = new EditorModel([pc.plain("try "), pc.roomPill("#someroom"), pc.plain("?")], pc, renderer); model.update("try #some perhapsroom?", "insertText", new DocumentOffset(17, false)); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(2); - expect(renderer.caret.offset).toBe(8); - } + expect((renderer.caret as DocumentPosition).index).toBe(2); + expect((renderer.caret as DocumentPosition).offset).toBe(8); expect(model.parts.length).toBe(3); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("try "); @@ -168,10 +153,8 @@ describe("editor/model", function () { const model = new EditorModel([pc.roomPill("#someroom")], pc, renderer); model.update("#someroo", "deleteContentBackward", new DocumentOffset(8, true)); expect(renderer.count).toBe(1); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(-1); - expect(renderer.caret.offset).toBe(0); - } + expect((renderer.caret as DocumentPosition).index).toBe(-1); + expect((renderer.caret as DocumentPosition).offset).toBe(0); expect(model.parts.length).toBe(0); }); it("remove non-editable part with delete", function () { @@ -180,10 +163,8 @@ describe("editor/model", function () { const model = new EditorModel([pc.roomPill("#someroom")], pc, renderer); model.update("someroom", "deleteContentForward", new DocumentOffset(0, false)); expect(renderer.count).toBe(1); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(-1); - expect(renderer.caret.offset).toBe(0); - } + expect((renderer.caret as DocumentPosition).index).toBe(-1); + expect((renderer.caret as DocumentPosition).offset).toBe(0); expect(model.parts.length).toBe(0); }); }); @@ -196,10 +177,8 @@ describe("editor/model", function () { model.update("hello @a", "insertText", new DocumentOffset(8, true)); expect(renderer.count).toBe(1); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(1); - expect(renderer.caret.offset).toBe(2); - } + expect((renderer.caret as DocumentPosition).index).toBe(1); + expect((renderer.caret as DocumentPosition).offset).toBe(2); expect(model.parts.length).toBe(2); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("hello "); @@ -211,10 +190,8 @@ describe("editor/model", function () { model.autoComplete.tryComplete(); // see MockAutoComplete expect(renderer.count).toBe(2); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(1); - expect(renderer.caret.offset).toBe(5); - } + expect((renderer.caret as DocumentPosition).index).toBe(1); + expect((renderer.caret as DocumentPosition).offset).toBe(5); expect(model.parts.length).toBe(2); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("hello "); @@ -230,10 +207,8 @@ describe("editor/model", function () { model.update("hello #r", "insertText", new DocumentOffset(8, true)); expect(renderer.count).toBe(1); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(1); - expect(renderer.caret.offset).toBe(2); - } + expect((renderer.caret as DocumentPosition).index).toBe(1); + expect((renderer.caret as DocumentPosition).offset).toBe(2); expect(model.parts.length).toBe(2); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("hello "); @@ -245,10 +220,8 @@ describe("editor/model", function () { model.autoComplete.tryComplete(); // see MockAutoComplete expect(renderer.count).toBe(2); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(1); - expect(renderer.caret.offset).toBe(9); - } + expect((renderer.caret as DocumentPosition).index).toBe(1); + expect((renderer.caret as DocumentPosition).offset).toBe(9); expect(model.parts.length).toBe(2); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("hello "); @@ -268,10 +241,8 @@ describe("editor/model", function () { model.update("hello #riot-dev!!", "insertText", new DocumentOffset(17, true)); expect(renderer.count).toBe(3); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(2); - expect(renderer.caret.offset).toBe(2); - } + expect((renderer.caret as DocumentPosition).index).toBe(2); + expect((renderer.caret as DocumentPosition).offset).toBe(2); expect(model.parts.length).toBe(3); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("hello "); @@ -289,10 +260,8 @@ describe("editor/model", function () { model.update("try #define", "insertFromPaste", new DocumentOffset(11, true)); expect(model.autoComplete).toBeFalsy(); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(0); - expect(renderer.caret.offset).toBe(11); - } + expect((renderer.caret as DocumentPosition).index).toBe(0); + expect((renderer.caret as DocumentPosition).offset).toBe(11); expect(model.parts.length).toBe(1); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("try #define"); @@ -306,10 +275,8 @@ describe("editor/model", function () { model.update("try #define", "insertFromDrop", new DocumentOffset(11, true)); expect(model.autoComplete).toBeFalsy(); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.index).toBe(0); - expect(renderer.caret.offset).toBe(11); - } + expect((renderer.caret as DocumentPosition).index).toBe(0); + expect((renderer.caret as DocumentPosition).offset).toBe(11); expect(model.parts.length).toBe(1); expect(model.parts[0].type).toBe("plain"); expect(model.parts[0].text).toBe("try #define"); diff --git a/test/editor/operations-test.ts b/test/editor/operations-test.ts index dc18168b42d..2a456e46833 100644 --- a/test/editor/operations-test.ts +++ b/test/editor/operations-test.ts @@ -15,7 +15,7 @@ limitations under the License. */ import EditorModel from "../../src/editor/model"; -import { createPartCreator, createRenderer, isDocumentPosition } from "./mock"; +import { createPartCreator, createRenderer } from "./mock"; import { formatRange, formatRangeAsCode, @@ -25,6 +25,7 @@ import { } from "../../src/editor/operations"; import { Formatting } from "../../src/components/views/rooms/MessageComposerFormatBar"; import { longestBacktickSequence } from "../../src/editor/deserialize"; +import DocumentPosition from "../../src/editor/position"; const SERIALIZED_NEWLINE = { text: "\n", type: "newline" }; @@ -86,9 +87,7 @@ describe("editor/operations: formatting operations", () => { expect(range.parts[0].text).toBe(input); formatRangeAsLink(range, text); - if (isDocumentPosition(renderer.caret)) { - expect(renderer.caret.offset).toBe(4 + expectation.indexOf("|")); - } + expect((renderer.caret as DocumentPosition).offset).toBe(4 + expectation.indexOf("|")); expect(model.parts[0].text).toBe("foo " + expectation.replace("|", "") + " bar"); }); }); diff --git a/test/utils/FixedRollingArray-test.ts b/test/utils/FixedRollingArray-test.ts index f810d46d229..335182322db 100644 --- a/test/utils/FixedRollingArray-test.ts +++ b/test/utils/FixedRollingArray-test.ts @@ -56,8 +56,10 @@ describe("FixedRollingArray", () => { expect(previous - current).toBe(1); if (i === 1) { + // eslint-disable-next-line jest/no-conditional-expect expect(previous).toBe(maxValue); } else if (i === width) { + // eslint-disable-next-line jest/no-conditional-expect expect(current).toBe(minValue); } } diff --git a/test/utils/Singleflight-test.ts b/test/utils/Singleflight-test.ts index be758c85125..1e4b6f00f4f 100644 --- a/test/utils/Singleflight-test.ts +++ b/test/utils/Singleflight-test.ts @@ -28,13 +28,7 @@ describe("Singleflight", () => { [null, "test"], ]; for (const p of permutations) { - try { - Singleflight.for(p[0], p[1]); - // noinspection ExceptionCaughtLocallyJS - throw new Error("failed to fail: " + JSON.stringify(p)); - } catch (e) { - expect(e.message).toBe("An instance and key must be supplied"); - } + expect(() => Singleflight.for(p[0], p[1])).toThrow("An instance and key must be supplied"); } });