Skip to content

Commit

Permalink
test(explorer): remove property based testing
Browse files Browse the repository at this point in the history
There were usefull when developping, but:
- they are more than 100 times slower than classic jest tests
- the part covered don't justify such slowdown
  • Loading branch information
Alenar committed Feb 3, 2025
1 parent 6f0f5cd commit 366d6ef
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 93 deletions.
2 changes: 0 additions & 2 deletions mithril-explorer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,10 @@ upgrade: clean install
react-chartjs-2@latest \
react-dom@latest \
react-redux@latest \
@fast-check/jest@latest \
@testing-library/jest-dom@latest \
@testing-library/react@latest \
eslint@latest \
eslint-config-next@latest \
@fast-check/jest@latest \
fantasticon@latest \
jest@latest \
jest-environment-jsdom@latest \
Expand Down
58 changes: 25 additions & 33 deletions mithril-explorer/__tests__/DownloadImmutableFormInput.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { fireEvent, render, screen } from "@testing-library/react";
import { test, fc } from "@fast-check/jest";
import "@testing-library/jest-dom";
import { DownloadImmutableFormInput } from "#/Artifacts/CardanoDbV2SnapshotsList/DownloadButton";

const maxImmutable = 100000;
const maxImmutable = 100_000;

function setup(max) {
const utils = render(<DownloadImmutableFormInput max={max} />);
return {
// Note: in `fast-check` tests `screen.getByRole("spinbutton")` find two elements for a reason I don't understand, so
// I'm using getAllByRole and selecting the first one to avoid the error.
input: screen.getAllByRole("spinbutton")[0],
input: screen.getByRole("spinbutton"),
...utils,
};
}
Expand All @@ -29,35 +26,32 @@ describe("DownloadImmutableFormInput", () => {
expect(input.value).toBe("");
});

test.prop({
immutable_file_number: fc.nat({
max: maxImmutable,
}),
})("Immutable below or equal to max allowed", ({ immutable_file_number }) => {
const { input } = setup(maxImmutable);
fireEvent.change(input, {
// target: { value: `${immutable_file_number}` },
target: { value: immutable_file_number },
});
it.each([0, 123, 67_782, maxImmutable])(
"Immutable below or equal to max allowed: %d",
(immutable_file_number) => {
const { input } = setup(maxImmutable);
fireEvent.change(input, {
target: { value: immutable_file_number },
});

expect(input.checkValidity()).toBeTruthy();
expect(input.value).toBe(`${immutable_file_number}`);
});
expect(input.checkValidity()).toBeTruthy();
expect(input.value).toBe(`${immutable_file_number}`);
},
);

test.prop({
immutable_file_number: fc.oneof(fc.integer({ max: -1 }), fc.integer({ min: maxImmutable + 1 })),
})("Immutable above max or below 0 is invalid", ({ immutable_file_number }) => {
const { input } = setup(maxImmutable);
fireEvent.change(input, {
target: { value: immutable_file_number },
});
it.each([-4328, -1, maxImmutable + 1, 528_432])(
"Immutable above max or below 0 is invalid: %d",
(immutable_file_number) => {
const { input } = setup(maxImmutable);
fireEvent.change(input, {
target: { value: immutable_file_number },
});

expect(input.checkValidity()).toBeFalsy();
});
expect(input.checkValidity()).toBeFalsy();
},
);

test.prop({
immutable_file_number: fc.string({ minLength: 1 }).filter((s) => isNaN(parseInt(s))),
})("Non-number is invalid", ({ immutable_file_number }) => {
it.each(["@124", "⚠️", "text"])("Non-number is invalid: %s", (immutable_file_number) => {
const { input } = setup({ maxImmutable });
fireEvent.change(input, {
target: { value: immutable_file_number },
Expand All @@ -66,9 +60,7 @@ describe("DownloadImmutableFormInput", () => {
expect(input.checkValidity()).toBeFalsy();
});

test.prop({
immutable_file_number: fc.float({ noInteger: true }),
})("Float is invalid", ({ immutable_file_number }) => {
it.each([0.1, 1.432, 67_782.32])("Float is invalid: %f", ({ immutable_file_number }) => {
const { input } = setup({ maxImmutable });
fireEvent.change(input, {
target: { value: immutable_file_number },
Expand Down
56 changes: 0 additions & 56 deletions mithril-explorer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions mithril-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@
"react-redux": "^9.2.0"
},
"devDependencies": {
"@fast-check/jest": "^2.0.3",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.2.0",
"eslint": "^9.18.0",
"eslint-config-next": "^15.1.5",
"fantasticon": "^3.0.0",
"fast-check": "^3.23.2",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"next-router-mock": "^0.9.13",
Expand Down

0 comments on commit 366d6ef

Please sign in to comment.