Skip to content

Commit

Permalink
[#500] chore: fix frontend package tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzalowski committed Mar 22, 2024
1 parent 22448dd commit 9065905
Show file tree
Hide file tree
Showing 14 changed files with 3,251 additions and 3,383 deletions.
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
6 changes: 5 additions & 1 deletion govtool/frontend/src/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
// eslint-disable-next-line import/no-extraneous-dependencies
/* eslint-disable import/no-extraneous-dependencies */
import "@testing-library/jest-dom";
import { vi } from "vitest";
import { Validations } from "./utils";

vi.stubGlobal("Validations", Validations);
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ 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);
console.log(decoded);

Check warning on line 13 in govtool/frontend/src/utils/bech32Validation.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
if (decoded.words.length) {
return true;
}
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

0 comments on commit 9065905

Please sign in to comment.