Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#500] chore: fix frontend package tests #538

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/code_check_frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
node-version-file: "govtool/frontend/.nvmrc"

- name: 🧪 Test
env:
NODE_OPTIONS: "--max_old_space_size=4096"
run: |
npm install
npm run test
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ changes.
- i18next library added to FE [Issue 80](https://github.com/IntersectMBO/govtool/issues/80)
- Add possibility to vote on behalf of myself - Sole Voter [Issue 119](https://github.com/IntersectMBO/govtool/issues/119)
- Added Nix Flakes configurtion to handle unified developers setup [Issue 526](https://github.com/IntersectMBO/govtool/issues/526)
- Add missing test to utils [Issue 500](https://github.com/IntersectMBO/govtool/issues/500).

### Fixed

Expand All @@ -53,6 +54,7 @@ changes.
- Fixed deployment scripts to address [Issue 171](https://github.com/IntersectMBO/govtool/issues/171).
- Fixed get drep voting power incorrectly executed endpoint [Issue 280](https://github.com/IntersectMBO/govtool/issues/280).
- Fixed CSP settings to allow error reports with Sentry [Issue 291](https://github.com/IntersectMBO/govtool/issues/291).
- Fix frontend package tests [Issue 500](https://github.com/IntersectMBO/govtool/issues/500).

### Changed

Expand Down
1 change: 1 addition & 0 deletions govtool/frontend/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@ module.exports = {
".vite/",
"dist/",
"node_modules/",
"vite.config.ts",
],
};
7 changes: 3 additions & 4 deletions govtool/frontend/src/consts/governanceAction/fields.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Validations } from "@utils";

import { bech32Validation, numberValidation } from "@utils";
import I18n from "@/i18n";
import {
GovernanceActionType,
Expand Down Expand Up @@ -96,7 +95,7 @@ export const GOVERNANCE_ACTION_FIELDS: GovernanceActionFields = {
placeholderI18nKey:
"createGovernanceAction.fields.declarations.receivingAddress.placeholder",
rules: {
validate: Validations.bech32,
validate: bech32Validation,
},
},
amount: {
Expand All @@ -109,7 +108,7 @@ export const GOVERNANCE_ACTION_FIELDS: GovernanceActionFields = {
value: true,
message: I18n.t("createGovernanceAction.fields.validations.required"),
},
validate: Validations.number,
validate: numberValidation,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import I18n from "@/i18n";
* @param value - The value to be validated.
* @returns A boolean indicating whether the value is valid or an error message if it is not valid.
*/
export default async (value: string) => {
export const bech32Validation = async (value: string) => {
try {
const decoded = await bech32.decode(value);
if (decoded.words.length) {
Expand Down
7 changes: 0 additions & 7 deletions govtool/frontend/src/utils/govActionValidations/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions govtool/frontend/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./adaFormat";
export * from "./basicReducer";
export * from "./bech32Validation";
export * from "./callAll";
export * from "./canonizeJSON";
export * from "./checkIsMaintenanceOn";
Expand All @@ -11,12 +12,11 @@ export * from "./getDRepID";
export * from "./getGovActionId";
export * from "./getLengthInBytes";
export * from "./getProposalTypeLabel";
export * from "./govActionValidations";
export * from "./isValidFormat";
export * from "./jsonUtils";
export * from "./localStorage";
export * from "./numberValidation";
export * from "./openInNewTab";
export * from "./removeDuplicatedProposals";
export * from "./validateMetadataHash";
export * from "./wait";
export * from "./generateJsonld";
2 changes: 1 addition & 1 deletion govtool/frontend/src/utils/jsonUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NodeObject } from "jsonld";

export const downloadJson = (json: NodeObject, fileName?: string) => {
const jsonString = `data:text/jsonld;chatset=utf-8,${encodeURIComponent(
const jsonString = `data:text/jsonld;charset=utf-8,${encodeURIComponent(
JSON.stringify(json, null, 2),
)}`;
const link = document.createElement("a");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import I18n from "@/i18n";
* @param value - The string value to be validated.
* @returns Either an error message or `true` if the value is a valid number.
*/
export default (value: string) => {
export const numberValidation = (value: string) => {
const parsedValue = Number(
value.includes(",") ? value.replace(",", ".") : value,
);
Expand Down
24 changes: 24 additions & 0 deletions govtool/frontend/src/utils/tests/basicReducer.test.ts
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);
});
});
16 changes: 16 additions & 0 deletions govtool/frontend/src/utils/tests/bech32Validation.test.ts
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");
});
});
27 changes: 27 additions & 0 deletions govtool/frontend/src/utils/tests/callAll.test.ts
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();
});
});
42 changes: 42 additions & 0 deletions govtool/frontend/src/utils/tests/jsonUtils.test.ts
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 govtool/frontend/src/utils/tests/validateMetadataHash.test.ts
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
});
Loading