-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#500] chore: fix frontend package tests
- Loading branch information
1 parent
22448dd
commit 6e6c294
Showing
14 changed files
with
3,247 additions
and
3,382 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,5 +101,6 @@ module.exports = { | |
".vite/", | ||
"dist/", | ||
"node_modules/", | ||
"vite.config.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
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 was deleted.
Oops, something went wrong.
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
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,24 @@ | ||
import { basicReducer } from "../basicReducer"; | ||
|
||
describe("basicReducer", () => { | ||
it("should merge the previous state with the new state", () => { | ||
const prevState = { count: 0, name: "John" }; | ||
const newState = { count: 1 }; | ||
const expectedState = { count: 1, name: "John" }; | ||
|
||
const result = basicReducer(prevState, newState); | ||
|
||
expect(result).toEqual(expectedState); | ||
}); | ||
|
||
it("should not modify the previous state if the new state is empty", () => { | ||
const prevState = { count: 0, name: "John" }; | ||
const newState = {}; | ||
const expectedState = { count: 0, name: "John" }; | ||
|
||
const result = basicReducer(prevState, newState); | ||
|
||
expect(result).toEqual(expectedState); | ||
expect(result).toStrictEqual(prevState); | ||
}); | ||
}); |
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,16 @@ | ||
import { bech32 } from "bech32"; | ||
|
||
import { bech32Validation } from ".."; | ||
|
||
describe("bech32 validation", () => { | ||
it("should return true for valid bech32 value", async () => { | ||
const result = await bech32Validation(bech32.encode("test", [1, 2, 3])); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return an error message for invalid bech32 value", async () => { | ||
const value = "invalidBech32Value"; | ||
const result = await bech32Validation(value); | ||
expect(result).toBe("Invalid bech32 address"); | ||
}); | ||
}); |
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,27 @@ | ||
import { vi } from "vitest"; | ||
|
||
import { callAll } from ".."; | ||
|
||
describe("callAll", () => { | ||
it("should call all functions with provided arguments", () => { | ||
const fn1 = vi.fn(); | ||
const fn2 = vi.fn(); | ||
const fn3 = vi.fn(); | ||
|
||
const combinedFn = callAll(fn1, fn2, fn3); | ||
combinedFn("arg1", "arg2"); | ||
|
||
expect(fn1).toHaveBeenCalledWith("arg1", "arg2"); | ||
expect(fn2).toHaveBeenCalledWith("arg1", "arg2"); | ||
expect(fn3).toHaveBeenCalledWith("arg1", "arg2"); | ||
}); | ||
|
||
it("should not throw an error if any function is undefined", () => { | ||
const fn1 = vi.fn(); | ||
const fn2 = undefined; | ||
const fn3 = vi.fn(); | ||
|
||
const combinedFn = callAll(fn1, fn2, fn3); | ||
expect(() => combinedFn("arg1", "arg2")).not.toThrow(); | ||
}); | ||
}); |
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,42 @@ | ||
import { vi } from "vitest"; | ||
import { downloadJson } from ".."; | ||
|
||
describe("downloadJson", () => { | ||
it("downloads JSON with default file name", () => { | ||
const json = { name: "John Doe", age: 30 }; | ||
const linkMock = document.createElement("a"); | ||
const clickMock = vi.fn(); | ||
linkMock.click = clickMock; | ||
|
||
vi.spyOn(document, "createElement").mockReturnValue(linkMock); | ||
|
||
downloadJson(json); | ||
|
||
expect(linkMock.href).toBe( | ||
"data:text/jsonld;charset=utf-8,%7B%0A%20%20%22name%22%3A%20%22John%20Doe%22%2C%0A%20%20%22age%22%3A%2030%0A%7D", | ||
); | ||
expect(linkMock.download).toBe("data.jsonld"); | ||
expect(clickMock).toHaveBeenCalled(); | ||
|
||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it("downloads JSON with custom file name", () => { | ||
const json = { name: "John Doe", age: 30 }; | ||
const linkMock = document.createElement("a"); | ||
const clickMock = vi.fn(); | ||
linkMock.click = clickMock; | ||
|
||
vi.spyOn(document, "createElement").mockReturnValue(linkMock); | ||
|
||
downloadJson(json, "custom"); | ||
|
||
expect(linkMock.href).toBe( | ||
"data:text/jsonld;charset=utf-8,%7B%0A%20%20%22name%22%3A%20%22John%20Doe%22%2C%0A%20%20%22age%22%3A%2030%0A%7D", | ||
); | ||
expect(linkMock.download).toBe("custom.jsonld"); | ||
expect(clickMock).toHaveBeenCalled(); | ||
|
||
vi.restoreAllMocks(); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
govtool/frontend/src/utils/tests/validateMetadataHash.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,24 @@ | ||
import { vi } from "vitest"; | ||
import { API } from "@/services"; | ||
import { validateMetadataHash } from ".."; | ||
|
||
describe("validateMetadataHash", () => { | ||
const mockHash = "abcdef1234567890"; | ||
|
||
it("should throw an error for invalid URL", async () => { | ||
const invalidURL = "invalid-url"; | ||
await expect(validateMetadataHash(invalidURL, mockHash)).rejects.toThrow( | ||
"Invalid URL", | ||
); | ||
}); | ||
|
||
it("should throw an error for invalid JSON", async () => { | ||
const invalidJSONURL = "https://example.com/invalid-json"; | ||
vi.spyOn(API, "get").mockRejectedValueOnce(new Error("Invalid JSON")); | ||
await expect( | ||
validateMetadataHash(invalidJSONURL, mockHash), | ||
).rejects.toThrow("Invalid JSON"); | ||
}); | ||
|
||
// TODO: Provide tests for invalid hash | ||
}); |
Oops, something went wrong.