Fix jest/no-conditional-expect lint and enable it (#10307)

This commit is contained in:
Michael Telatynski 2023-03-07 13:58:10 +00:00 committed by GitHub
parent 2c97cb4054
commit af1ec76149
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 49 additions and 83 deletions

View File

@ -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": [

View File

@ -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],
}),

View File

@ -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();
}
});

View File

@ -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");
}
});

View File

@ -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");

View File

@ -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");
});
});

View File

@ -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);
}
}

View File

@ -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");
}
});