Skip to content

Commit

Permalink
SLVUU-117 add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vferraro-scottlogic committed Jan 11, 2024
1 parent 21f2303 commit 7cd36c6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion vuu-ui/packages/vuu-layout/src/utils/typeOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export function typeOf(element?: LayoutModel | WithType): string | undefined {

export const isTypeOf = (element: ReactElement, type: string) => typeOf(element) === type;

export const isLayoutJSON = (layout: LayoutJSON): layout is LayoutJSON=> "type" in layout;
export const isLayoutJSON = (layout: LayoutJSON): layout is LayoutJSON =>
layout !== undefined && "type" in layout;
30 changes: 30 additions & 0 deletions vuu-ui/packages/vuu-layout/test/typeOf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, it } from "vitest";
import { LayoutJSON, isLayoutJSON } from "../src";

const validLayout: LayoutJSON = {
type: "Stack",
};

const invalidLayout: unknown = {
name: "invalid-layout-test",
};

describe("isLayoutJSON", () => {
it("returns true when layout is valid", () => {
const result = isLayoutJSON(validLayout);

expect(result).toBe(true);
});

it("returns false when layout is not valid", () => {
const result = isLayoutJSON(invalidLayout as LayoutJSON);

expect(result).toBe(false);
});

it("returns false when layout is undefined", () => {
const result = isLayoutJSON(undefined as unknown as LayoutJSON);

expect(result).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const defaultLayout = vi.hoisted(() => ({

const newLayout: LayoutJSON = {
type: "Stack",
title: "test-layout-edited",
props: {
title: "test-layout-edited",
},
};

const metadata: LayoutMetadata = {
Expand Down Expand Up @@ -160,7 +162,10 @@ describe("LayoutManagementProvider", () => {

result.current.saveLayout(metadata);

expect(persistence.createLayout).toHaveBeenCalled();
expect(persistence.createLayout).toHaveBeenCalledWith(
metadata,
newLayout
);
});

it("doesn't call createLayout, triggers error notification and logs error when layout path can't be resolved ", () => {
Expand All @@ -169,6 +174,7 @@ describe("LayoutManagementProvider", () => {

vi.mocked(persistence.createLayout).mockResolvedValueOnce(metadata);
vi.mocked(resolveJSONPath).mockReturnValue(undefined);
vi.mocked(isLayoutJSON).mockReturnValue(true);

result.current.saveLayout(metadata);

Expand Down

0 comments on commit 7cd36c6

Please sign in to comment.