-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add extra unit test for coverage
- Loading branch information
1 parent
a4a79a4
commit a4c09af
Showing
6 changed files
with
149 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { act, render, screen } from "@testing-library/react"; | ||
import { expect, test, vi } from "vitest"; | ||
import { Board } from "../../../lib/components"; | ||
import { mockController } from "../../../lib/components/internal/dnd-controller/__mocks__/controller"; | ||
import { DragAndDropData } from "../../../lib/components/internal/dnd-controller/controller"; | ||
import { Coordinates } from "../../../lib/components/internal/utils/coordinates"; | ||
import { defaultProps } from "./utils"; | ||
|
||
vi.mock("../../../lib/components/internal/dnd-controller/controller"); | ||
|
||
test("renders acquired item", () => { | ||
render(<Board {...defaultProps} />); | ||
expect(screen.queryByTestId("acquired-item")).toBeNull(); | ||
const draggableItem = { id: "test", data: { title: "Test item" }, definition: {} }; | ||
|
||
act(() => | ||
mockController.start({ | ||
interactionType: "keyboard", | ||
operation: "insert", | ||
draggableItem, | ||
collisionRect: { top: 0, bottom: 0, left: 0, right: 0 }, | ||
coordinates: new Coordinates({ x: 0, y: 0 }), | ||
} as DragAndDropData) | ||
); | ||
|
||
act(() => | ||
mockController.acquire({ | ||
droppableId: "awsui-placeholder-1-0", | ||
draggableItem, | ||
renderAcquiredItem: () => <div data-testid="acquired-item"></div>, | ||
}) | ||
); | ||
expect(screen.queryByTestId("acquired-item")).toBeInTheDOM(); | ||
|
||
act(() => mockController.discard()); | ||
expect(screen.queryByTestId("acquired-item")).toBeNull(); | ||
}); |
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,63 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import Button from "@cloudscape-design/components/button"; | ||
import { BoardProps } from "../../../lib/components/board"; | ||
import BoardItem from "../../../lib/components/board-item"; | ||
|
||
interface ItemData { | ||
title: string; | ||
} | ||
|
||
const i18nStrings: BoardProps.I18nStrings<ItemData> = { | ||
liveAnnouncementDndStarted() { | ||
return "Operation started"; | ||
}, | ||
liveAnnouncementDndItemReordered() { | ||
return "Reorder performed"; | ||
}, | ||
liveAnnouncementDndItemResized() { | ||
return "Resize performed"; | ||
}, | ||
liveAnnouncementDndItemInserted() { | ||
return "Insert performed"; | ||
}, | ||
liveAnnouncementDndCommitted() { | ||
return "Operation committed"; | ||
}, | ||
liveAnnouncementDndDiscarded() { | ||
return "Operation discarded"; | ||
}, | ||
liveAnnouncementItemRemoved() { | ||
return "Remove performed"; | ||
}, | ||
navigationAriaLabel: "Board navigation", | ||
navigationAriaDescription: "Click on non-empty item to move focus over", | ||
navigationItemAriaLabel: (item) => (item ? item.data.title : "Empty"), | ||
}; | ||
|
||
const itemI18nStrings = { | ||
dragHandleAriaLabel: "Drag handle", | ||
resizeHandleAriaLabel: "Resize handle", | ||
}; | ||
|
||
export const defaultProps: BoardProps<{ title: string }> = { | ||
items: [ | ||
{ id: "1", data: { title: "Item 1" } }, | ||
{ id: "2", data: { title: "Item 2" } }, | ||
], | ||
renderItem: (item, actions) => ( | ||
<BoardItem | ||
i18nStrings={itemI18nStrings} | ||
settings={ | ||
<Button data-testid="remove-button" onClick={() => actions.removeItem()}> | ||
Remove | ||
</Button> | ||
} | ||
> | ||
{item.data.title} | ||
</BoardItem> | ||
), | ||
onItemsChange: () => undefined, | ||
i18nStrings: i18nStrings, | ||
empty: "No items", | ||
}; |
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
37 changes: 37 additions & 0 deletions
37
src/internal/item-container/__tests__/get-next-droppable.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,37 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { expect, test } from "vitest"; | ||
import { Droppable } from "../../../../lib/components/internal/dnd-controller/controller"; | ||
import { Rect } from "../../../../lib/components/internal/interfaces"; | ||
import { getNextDroppable } from "../../../../lib/components/internal/item-container/get-next-droppable"; | ||
|
||
function getMockElement({ left, right, top, bottom }: Rect) { | ||
return { | ||
getBoundingClientRect: () => ({ left, right, top, bottom }), | ||
ownerDocument: { | ||
defaultView: { | ||
pageXOffset: 0, | ||
pageYOffset: 0, | ||
}, | ||
}, | ||
} as HTMLElement; | ||
} | ||
|
||
test("returns null if there are no droppables", () => { | ||
const elementMock = getMockElement({ left: 0, right: 0, top: 0, bottom: 0 }); | ||
expect(getNextDroppable(elementMock, [], "left")).toBe(null); | ||
}); | ||
|
||
test("returns next droppable matching the direction", () => { | ||
const elementMock = getMockElement({ left: 6, right: 4, top: 0, bottom: 0 }); | ||
const next = getNextDroppable( | ||
elementMock, | ||
[ | ||
["1", { element: getMockElement({ left: 0, right: 10, top: 0, bottom: 0 }) } as Droppable], | ||
["2", { element: getMockElement({ left: 5, right: 5, top: 0, bottom: 0 }) } as Droppable], | ||
["3", { element: getMockElement({ left: 10, right: 0, top: 0, bottom: 0 }) } as Droppable], | ||
], | ||
"right" | ||
); | ||
expect(next).toBe("2"); | ||
}); |