Skip to content

Commit

Permalink
✅ - test: add tests for field selection lib
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvandescheur committed Nov 25, 2024
1 parent bfb667c commit 82a416d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
103 changes: 103 additions & 0 deletions frontend/src/lib/fieldSelection/fieldSelection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { TypedField } from "@maykin-ui/admin-ui";

import {
addToFieldSelection,
clearFieldSelection,
getFieldSelection,
isFieldActive,
removeFromFieldSelection,
setFieldSelection,
} from "./fieldSelection";

describe("fieldSelection", () => {
const testKey = "testKey";
const mockFields: TypedField[] = [
{ name: "field1", type: "boolean" },
{ name: "field2", type: "number" },
{ name: "field3", type: "string" },
];

beforeEach(() => {
sessionStorage.clear();
});

test("should add fields to selection", async () => {
await addToFieldSelection(testKey, mockFields);

const result = await getFieldSelection(testKey);
expect(result).toEqual({
field1: true,
field2: true,
field3: true,
});
});

test("should remove fields from selection", async () => {
await addToFieldSelection(testKey, mockFields);
await removeFromFieldSelection(testKey, [mockFields[0]]);

const result = await getFieldSelection(testKey);
expect(result).toEqual({
field1: false,
field2: true,
field3: true,
});
});

test("should retrieve field selection", async () => {
const fieldSelection = { field1: true, field2: false };
await setFieldSelection(testKey, fieldSelection);

const result = await getFieldSelection(testKey);
expect(result).toEqual(fieldSelection);
});

test("should clear field selection", async () => {
const fieldSelection = { field1: true, field2: true };
await setFieldSelection(testKey, fieldSelection);

await clearFieldSelection(testKey);

const result = await getFieldSelection(testKey);
expect(result).toEqual({});
});

test("should check if a field is active", async () => {
const fieldSelection = { field1: true, field2: false };
await setFieldSelection(testKey, fieldSelection);

const isActive1 = await isFieldActive(testKey, mockFields[0]);
const isActive2 = await isFieldActive(testKey, mockFields[1]);

expect(isActive1).toBe(true);
expect(isActive2).toBe(false);
});

test("should handle non-existent keys gracefully", async () => {
const result = await getFieldSelection("nonExistentKey");
expect(result).toEqual({});
});

test("should update existing field selection when adding new fields", async () => {
await addToFieldSelection(testKey, [mockFields[0]]);
await addToFieldSelection(testKey, [mockFields[1]]);

const result = await getFieldSelection(testKey);
expect(result).toEqual({
field1: true,
field2: true,
});
});

test("should update existing field selection when removing fields", async () => {
await addToFieldSelection(testKey, mockFields);
await removeFromFieldSelection(testKey, [mockFields[1]]);

const result = await getFieldSelection(testKey);
expect(result).toEqual({
field1: true,
field2: false,
field3: true,
});
});
});
2 changes: 1 addition & 1 deletion frontend/src/lib/zaakSelection/zaakSelection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const mockZaak1: Zaak = { url: "https://example.com/zaak1" } as Zaak;
const mockZaak2 = "https://example.com/zaak2";
const mockKey = "testKey";

describe("Zaak Selection Functions", () => {
describe("zaakSelection", () => {
beforeEach(() => {
sessionStorage.clear();
});
Expand Down

0 comments on commit 82a416d

Please sign in to comment.