-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#81] configure unit tests with vitest
- Loading branch information
1 parent
1273673
commit 8c09303
Showing
15 changed files
with
1,156 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "@testing-library/jest-dom"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
govtool/frontend/src/utils/tests/getProposalTypeLabel.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
78
govtool/frontend/src/utils/tests/removeDuplicatedProposals.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.