Skip to content

Commit

Permalink
[#81] configure unit tests with vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzalowski committed Feb 6, 2024
1 parent 1273673 commit bf1834d
Show file tree
Hide file tree
Showing 15 changed files with 1,156 additions and 33 deletions.
14 changes: 1 addition & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,7 @@ As a minor extension, we also keep a semantic version for the `UNRELEASED`
changes.

## [Unreleased]

### Added
-

### Fixed
- Fixed vote calculation problems related to NoConfidence DRep [Issue 59](https://github.com/IntersectMBO/govtool/issues/59)
- Fixed ada-holder/get-current-delegation error when delegated to NoConfidence or AlwaysAbstain dreps. [Issue 82](https://github.com/IntersectMBO/govtool/issues/82)

### Changed
- Changed and improved working conventions docs, PR template and codeowners file, addressing [Issue 88](https://github.com/IntersectMBO/govtool/issues/88).

### Removed
-
- Vitest unit tests added for utils functions [Issue 81](https://github.com/IntersectMBO/govtool/issues/81)

## [sancho-v1.0.1](https://github.com/IntersectMBO/govtool/releases/tag/sancho-v1.0.1) 2023-12-XX

Expand Down
12 changes: 10 additions & 2 deletions govtool/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build",
"test-storybook": "test-storybook",
"chromatic": "chromatic"
"chromatic": "chromatic",
"test": "vitest",
"test:watch": "vitest watch"
},
"dependencies": {
"@emotion/react": "^11.11.1",
Expand Down Expand Up @@ -52,19 +54,25 @@
"@storybook/react-vite": "^7.4.5",
"@storybook/test-runner": "^0.16.0",
"@storybook/testing-library": "^0.2.2",
"@testing-library/jest-dom": "^6.1.6",
"@testing-library/react": "^14.1.2",
"@testing-library/user-event": "^14.5.2",
"@types/node": "^20.4.8",
"@types/react-dom": "^18.0.11",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"@vitejs/plugin-react": "^4.0.0",
"@vitest/ui": "^1.1.0",
"chromatic": "^10.0.0",
"eslint": "^8.38.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.4",
"eslint-plugin-storybook": "^0.6.14",
"jsdom": "^23.0.1",
"storybook": "^7.4.5",
"typescript": "^5.0.2",
"vite": "^4.3.9"
"vite": "^4.3.9",
"vitest": "^1.1.0"
},
"eslintConfig": {
"extends": [
Expand Down
1 change: 1 addition & 0 deletions govtool/frontend/src/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@testing-library/jest-dom";
8 changes: 4 additions & 4 deletions govtool/frontend/src/utils/adaFormat.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const LOVELANCE = 1000000;
const LOVELACE = 1000000;
const DECIMALS = 6;

export const correctAdaFormat = (lovelance: number | undefined) => {
return lovelance
? Number.parseFloat((lovelance / LOVELANCE).toFixed(DECIMALS))
export const correctAdaFormat = (lovelace: number | undefined) => {
return lovelace
? Number.parseFloat((lovelace / LOVELACE).toFixed(DECIMALS))
: 0;
};
4 changes: 4 additions & 0 deletions govtool/frontend/src/utils/getGovActionId.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const getShortenedGovActionId = (txHash: string, index: number) => {
if (txHash.length <= 6) {
return `${txHash}#${index}`;
}

const firstPart = txHash.slice(0, 4);
const lastPart = txHash.slice(-4);

Expand Down
36 changes: 36 additions & 0 deletions govtool/frontend/src/utils/tests/adaFormat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { correctAdaFormat } from "..";

describe("correctAdaFormat", () => {
const LOVELACE = 1000000;
const DECIMALS = 6;

it("converts lovelace to ADA for a given number", () => {
const lovelace = 15000000;
const expectedAda = 15;
expect(correctAdaFormat(lovelace)).toBe(expectedAda);
});

it("returns 0 for undefined lovelace value", () => {
const lovelace = undefined;
expect(correctAdaFormat(lovelace)).toBe(0);
});

it("handles large lovelace values correctly", () => {
const lovelace = 123456789012345;
const expectedAda = lovelace / LOVELACE;
expect(correctAdaFormat(lovelace)).toBe(expectedAda);
});

it("handles small lovelace values with correct rounding", () => {
const lovelace = 123;
const expectedAda = Number.parseFloat(
(lovelace / LOVELACE).toFixed(DECIMALS)
);
expect(correctAdaFormat(lovelace)).toBe(expectedAda);
});

it("returns 0 for zero lovelace value", () => {
const lovelace = 0;
expect(correctAdaFormat(lovelace)).toBe(0);
});
});
22 changes: 22 additions & 0 deletions govtool/frontend/src/utils/tests/formatDate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { formatDisplayDate } from "..";

describe("formatDisplayDate", () => {
it("formats a date object correctly", () => {
const date = new Date("2023-01-01T00:00:00Z");
const formatted = formatDisplayDate(date);
expect(formatted).toBe("1st Jan 2023");
});

it("formats a date string correctly", () => {
const dateString = "2023-01-01";
const formatted = formatDisplayDate(dateString);
expect(formatted).toBe("1st Jan 2023");
});

it("handles custom format strings", () => {
const date = new Date("2023-12-25T00:00:00Z");
const formatString = "yyyy-MM-dd";
const formatted = formatDisplayDate(date, formatString);
expect(formatted).toBe("2023-12-25");
});
});
40 changes: 40 additions & 0 deletions govtool/frontend/src/utils/tests/getGovActionId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { getShortenedGovActionId, getFullGovActionId } from "..";

describe("getShortenedGovActionId", () => {
it("should return the correct shortened id for long hashes", () => {
const txHash = "1234567890abcdef1234567890abcdef";
const index = 5;
const result = getShortenedGovActionId(txHash, index);
expect(result).toBe("1234...cdef#5");
});

it("should handle hashes shorter than 6 characters correctly", () => {
const txHash = "12345";
const index = 2;
const result = getShortenedGovActionId(txHash, index);
expect(result).toBe("12345#2");
});

it("should handle hashes exactly 6 characters long by not shortening", () => {
const txHash = "123456";
const index = 3;
const result = getShortenedGovActionId(txHash, index);
expect(result).toBe("123456#3");
});

it("should handle an empty string for txHash correctly", () => {
const txHash = "";
const index = 1;
const result = getShortenedGovActionId(txHash, index);
expect(result).toBe("#1");
});
});

describe("getFullGovActionId", () => {
it("should return the full id with index", () => {
const txHash = "1234567890abcdef1234567890abcdef";
const index = 10;
const result = getFullGovActionId(txHash, index);
expect(result).toBe("1234567890abcdef1234567890abcdef#10");
});
});
23 changes: 23 additions & 0 deletions govtool/frontend/src/utils/tests/getLengthInBytes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getLengthInBytes } from "..";

describe("getLengthInBytes", () => {
it("returns correct byte length for ASCII characters", () => {
const asciiStr = "Hello";
expect(getLengthInBytes(asciiStr)).toBe(5);
});

it("returns correct byte length for multibyte characters", () => {
const multibyteStr = "𩸽";
expect(getLengthInBytes(multibyteStr)).toBe(4);
});

it("returns correct byte length for mixed characters", () => {
const mixedStr = "Hello, 世界! 👋";
expect(getLengthInBytes(mixedStr)).toBe(19);
});

it("returns 0 for an empty string", () => {
const emptyStr = "";
expect(getLengthInBytes(emptyStr)).toBe(0);
});
});
25 changes: 25 additions & 0 deletions govtool/frontend/src/utils/tests/getProposalTypeLabel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getProposalTypeLabel } from "..";

describe("getProposalTypeLabel", () => {
it("returns correct label for a known type", () => {
const type = "NoConfidence";
const expectedLabel = "No Confidence";
expect(getProposalTypeLabel(type)).toBe(expectedLabel);
});

it("returns correct label for another known type", () => {
const type = "ParameterChange";
const expectedLabel = "Protocol Parameter Changes";
expect(getProposalTypeLabel(type)).toBe(expectedLabel);
});

it("returns the type itself when no matching key is found", () => {
const type = "UnknownType";
expect(getProposalTypeLabel(type)).toBe(type);
});

it("returns the type itself when given an empty string", () => {
const type = "";
expect(getProposalTypeLabel(type)).toBe(type);
});
});
71 changes: 71 additions & 0 deletions govtool/frontend/src/utils/tests/isValidFormat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { isValidURLFormat, isValidHashFormat } from "..";

describe("isValidURLFormat", () => {
it("returns true for valid HTTP URLs", () => {
const validHttpUrl = "http://example.com";
expect(isValidURLFormat(validHttpUrl)).toBe(true);
});

it("returns true for valid HTTPS URLs", () => {
const validHttpsUrl = "https://example.com";
expect(isValidURLFormat(validHttpsUrl)).toBe(true);
});

it("returns true for valid HTTPS URLs with IP", () => {
const validHttpsUrl = "http://192.168.0.1/resoruce";
expect(isValidURLFormat(validHttpsUrl)).toBe(true);
});

it("returns true for valid HTTPS URLs with IP", () => {
const validHttpsUrl = "http://192.168.0.1";
expect(isValidURLFormat(validHttpsUrl)).toBe(true);
});

it("returns true for valid IPFS URLs", () => {
const validIpfsUrl =
"ipfs://c94ae10c7bbc2632f051cadcec61e24a954aa1d61173597b21c03534";
expect(isValidURLFormat(validIpfsUrl)).toBe(true);
});

it("returns false for invalid URLs", () => {
const invalidUrl = "htp:/example.com";
expect(isValidURLFormat(invalidUrl)).toBe(false);
});

it("returns false for invalid URLs without domain", () => {
const invalidUrl = "https://invalid";
expect(isValidURLFormat(invalidUrl)).toBe(false);
});

it("returns false for strings that are not URLs", () => {
const notUrl = "Just a string";
expect(isValidURLFormat(notUrl)).toBe(false);
});

it("returns true for empty string", () => {
const empty = "";
expect(isValidURLFormat(empty)).toBe(true);
});
});

describe("isValidHashFormat", () => {
it("returns true for valid hexadecimal strings", () => {
const validHash = "1a3B";
expect(isValidHashFormat(validHash)).toBe(true);
});

it("returns false for non-hexadecimal strings", () => {
const invalidHash = "GHIJKLMNOP";
expect(isValidHashFormat(invalidHash)).toBe(false);
});

it("returns false for hexadecimal strings with spaces", () => {
const invalidHash = "1a 3B";
expect(isValidHashFormat(invalidHash)).toBe(false);
});

it("returns true for empty string", () => {
const empty = "";
expect(isValidHashFormat(empty)).toBe(true);
});
});
78 changes: 78 additions & 0 deletions govtool/frontend/src/utils/tests/removeDuplicatedProposals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { removeDuplicatedProposals } from "..";

describe("remove duplicated proposals", () => {
it("returns all proposals when all are unique", () => {
expect(removeDuplicatedProposals(uniqueProposals).length).toBe(
uniqueProposals.length
);
});

it("removes duplicate proposals based on txHash and index", () => {
expect(removeDuplicatedProposals(duplicatedProposals).length).toBe(
uniqueProposals.length
);
});

it("returns empty array if input is empty", () => {
const proposals: ActionType[] = [];
expect(removeDuplicatedProposals(proposals).length).toBe(0);
});
});

const uniqueProposals = [
{
id: "1322",
txHash: "2bca7756ba6c998518c1bccbcdd5165e32d3c8e0bfdf930d34359c98354e85a0",
index: 0,
type: "InfoAction",
details: "InfoAction 1",
expiryDate: "2024-01-08T15:32:13.61165Z",
createdDate: "2023-12-29T23:04:41Z",
url: "https://bit.ly/3zCH2HL",
metadataHash:
"1111111111111111111111111111111111111111111111111111111111111111",
yesVotes: 0,
noVotes: 0,
abstainVotes: 81528377728,
},
{
id: "1338",
txHash: "5e37f4d48182c4d8ff8e8ee7472c066501459b7bc8aaf6ca2f93a522ae12b0ea",
index: 0,
type: "InfoAction",
details: "InfoAction 2",
expiryDate: "2024-01-15T15:16:04.8932Z",
createdDate: "2024-01-05T23:06:02Z",
url: "https://bit.ly/3zCH2HL",
metadataHash:
"2222222222222222222222222222222222222222222222222222222222222222",
yesVotes: 0,
noVotes: 0,
abstainVotes: 81528377728,
},
{
id: "1335",
txHash: "e88ddff921de8b7f6079a1c25a301c034de6b3ec8a906ad75463f0f5b3597672",
index: 0,
type: "InfoAction",
details: "InfoAction 3",
expiryDate: "2024-01-14T15:18:23.28155Z",
createdDate: "2024-01-04T23:06:36Z",
url: "https://bit.ly/3zCH2HL",
metadataHash:
"3333333333333333333333333333333333333333333333333333333333333333",
yesVotes: 3175400714,
noVotes: 0,
abstainVotes: 81528377728,
},
];

const duplicatedProposals = [
...uniqueProposals,
{
...uniqueProposals[0],
},
{
...uniqueProposals[1],
},
];
7 changes: 6 additions & 1 deletion govtool/frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
"@utils": ["./src/utils/index.ts"]
}
},
"include": ["src"],
"include": [
"src",
"vitest.config.ts",
"src/**/*.test.ts",
"src/**/*.test.tsx"
],
"references": [{ "path": "./tsconfig.node.json" }]
}
Loading

0 comments on commit bf1834d

Please sign in to comment.