mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Implement first meaningful unit tests for a complete behavior
Note: in this case, the behavior is even asynchronous, and thus a tool "asyncFn" is used to keep the test as simple as possible. Co-authored-by: Janne Savolainen <janne.savolainen@live.fi> Signed-off-by: Mikko Aspiala <mikko.aspiala@gmail.com>
This commit is contained in:
1 parent
90ec6809d7
commit
78a989a72a
8 files changed
+235
-72
No files matched your search
@@ -140,6 +140,7 @@ export class ConfirmDialog extends React.Component<ConfirmDialogProps> {
|
||||
isOpen={dialogState.isOpen}
|
||||
onClose={this.onClose}
|
||||
close={this.close}
|
||||
{...(dialogState.isOpen ? { "data-testid":"confirmation-dialog" } : {})}
|
||||
>
|
||||
<div className="confirm-content">
|
||||
{icon} {message}
|
||||
@@ -158,6 +159,7 @@ export class ConfirmDialog extends React.Component<ConfirmDialogProps> {
|
||||
label={labelOk}
|
||||
onClick={prevDefault(this.ok)}
|
||||
waiting={this.isSaving}
|
||||
data-testid="confirm"
|
||||
{...okButtonProps}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -41,6 +41,7 @@ export interface DialogProps {
|
||||
modal?: boolean;
|
||||
pinned?: boolean;
|
||||
animated?: boolean;
|
||||
"data-testid"?: string;
|
||||
}
|
||||
|
||||
interface DialogState {
|
||||
@@ -149,7 +150,7 @@ export class Dialog extends React.PureComponent<DialogProps, DialogState> {
|
||||
};
|
||||
|
||||
render() {
|
||||
const { modal, animated, pinned } = this.props;
|
||||
const { modal, animated, pinned, "data-testid": testId } = this.props;
|
||||
let { className } = this.props;
|
||||
|
||||
className = cssNames("Dialog flex center", className, { modal, pinned });
|
||||
@@ -158,6 +159,7 @@ export class Dialog extends React.PureComponent<DialogProps, DialogState> {
|
||||
className={className}
|
||||
onClick={stopPropagation}
|
||||
ref={this.ref}
|
||||
data-testid={testId}
|
||||
>
|
||||
<div className="box" ref={e => this.contentElem = e}>
|
||||
{this.props.children}
|
||||
|
||||
+137
-6
@@ -1,15 +1,146 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`kube-object-menu given kube object, renders 1`] = `
|
||||
exports[`kube-object-menu given kube object renders 1`] = `
|
||||
<body>
|
||||
<div>
|
||||
<ul
|
||||
class="Animate opacity Menu MenuActions flex KubeObjectMenu toolbar gaps right bottom"
|
||||
<div>
|
||||
<ul
|
||||
class="Animate opacity Menu MenuActions flex KubeObjectMenu toolbar gaps right bottom"
|
||||
>
|
||||
<div>
|
||||
Some menu item
|
||||
</div>
|
||||
<li
|
||||
class="MenuItem"
|
||||
data-menu-action-remove-test="true"
|
||||
data-testid="menu-action-remove"
|
||||
tabindex="0"
|
||||
>
|
||||
<i
|
||||
class="Icon material interactive focusable"
|
||||
id="tooltip_target_3"
|
||||
tabindex="0"
|
||||
>
|
||||
<span
|
||||
class="icon"
|
||||
data-icon-name="delete"
|
||||
>
|
||||
delete
|
||||
</span>
|
||||
<div />
|
||||
</i>
|
||||
<span
|
||||
class="title"
|
||||
>
|
||||
Remove
|
||||
</span>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
class="Animate opacity-scale Dialog flex center ConfirmDialog modal"
|
||||
/>
|
||||
<div
|
||||
class="Tooltip narrow invisible formatter"
|
||||
>
|
||||
Delete
|
||||
</div>
|
||||
</body>
|
||||
`;
|
||||
|
||||
exports[`kube-object-menu given kube object when removing kube object renders 1`] = `
|
||||
<body>
|
||||
<div>
|
||||
<div>
|
||||
<ul
|
||||
class="Animate opacity Menu MenuActions flex KubeObjectMenu toolbar gaps right bottom"
|
||||
>
|
||||
<div>
|
||||
Some menu item
|
||||
</div>
|
||||
<li
|
||||
class="MenuItem"
|
||||
data-menu-action-remove-test="true"
|
||||
data-testid="menu-action-remove"
|
||||
tabindex="0"
|
||||
>
|
||||
<i
|
||||
class="Icon material interactive focusable"
|
||||
id="tooltip_target_7"
|
||||
tabindex="0"
|
||||
>
|
||||
<span
|
||||
class="icon"
|
||||
data-icon-name="delete"
|
||||
>
|
||||
delete
|
||||
</span>
|
||||
<div />
|
||||
</i>
|
||||
<span
|
||||
class="title"
|
||||
>
|
||||
Remove
|
||||
</span>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
class="Animate opacity-scale Dialog flex center ConfirmDialog modal"
|
||||
data-testid="confirmation-dialog"
|
||||
>
|
||||
<div
|
||||
class="box"
|
||||
>
|
||||
<div>
|
||||
Some menu item
|
||||
<div
|
||||
class="confirm-content"
|
||||
>
|
||||
<i
|
||||
class="Icon material focusable big"
|
||||
>
|
||||
<span
|
||||
class="icon"
|
||||
data-icon-name="warning"
|
||||
>
|
||||
warning
|
||||
</span>
|
||||
</i>
|
||||
|
||||
<div>
|
||||
<p>
|
||||
Remove
|
||||
<b>
|
||||
Some cluster name/some-kind/some-name
|
||||
</b>
|
||||
?
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</ul>
|
||||
<div
|
||||
class="confirm-buttons"
|
||||
>
|
||||
<button
|
||||
class="Button cancel plain"
|
||||
type="button"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
class="Button ok primary"
|
||||
data-testid="confirm"
|
||||
type="button"
|
||||
>
|
||||
Remove
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
class="Tooltip narrow invisible formatter"
|
||||
>
|
||||
Delete
|
||||
</div>
|
||||
</body>
|
||||
`;
|
||||
|
||||
@@ -24,6 +24,7 @@ import { render } from "@testing-library/react";
|
||||
import "@testing-library/jest-dom/extend-expect";
|
||||
import { KubeObjectMenu } from "./kube-object-menu";
|
||||
import { KubeObject } from "../../../common/k8s-api/kube-object";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import type { KubeApi } from "../../../common/k8s-api/kube-api";
|
||||
import type {
|
||||
IHasGettableItemsForKind,
|
||||
@@ -32,6 +33,8 @@ import type {
|
||||
import type { IGettableStore } from "../../../common/k8s-api/api-manager";
|
||||
import type { KubeObjectStore } from "../../../common/k8s-api/kube-object.store";
|
||||
import type { IHasName } from "../../../main/cluster";
|
||||
import { ConfirmDialog } from "../confirm-dialog";
|
||||
import asyncFn from "@async-fn/jest";
|
||||
|
||||
describe("kube-object-menu", () => {
|
||||
let hideDetailsStub: () => void;
|
||||
@@ -64,7 +67,7 @@ describe("kube-object-menu", () => {
|
||||
|
||||
const MenuItemComponentStub: React.FC = () => <div>Some menu item</div>;
|
||||
|
||||
const menuItemStub: KubeObjectMenuRegistration = {
|
||||
const dynamicMenuItemStub: KubeObjectMenuRegistration = {
|
||||
apiVersions: ["irrelevant"],
|
||||
components: { MenuItem: MenuItemComponentStub },
|
||||
kind: "irrelevant",
|
||||
@@ -73,7 +76,7 @@ describe("kube-object-menu", () => {
|
||||
kubeObjectMenuRegistryStub = {
|
||||
// eslint-disable-next-line unused-imports/no-unused-vars-ts
|
||||
getItemsForKind: (kind: string, apiVersion: string): any => [
|
||||
menuItemStub,
|
||||
dynamicMenuItemStub,
|
||||
],
|
||||
};
|
||||
|
||||
@@ -100,29 +103,86 @@ describe("kube-object-menu", () => {
|
||||
expect(baseElement).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it("given kube object, renders", () => {
|
||||
objectStub = KubeObject.create({
|
||||
apiVersion: "some-api-version",
|
||||
kind: "some-kind",
|
||||
metadata: {
|
||||
uid: "some-uid",
|
||||
name: "some-name",
|
||||
resourceVersion: "some-resource-version",
|
||||
},
|
||||
describe("given kube object", () => {
|
||||
let baseElement: Element;
|
||||
let removeActionMock: any;
|
||||
let getByTestId: (arg0: string) => any;
|
||||
let queryByTestId: (arg0: string) => any;
|
||||
|
||||
beforeEach(async () => {
|
||||
objectStub = KubeObject.create({
|
||||
apiVersion: "some-api-version",
|
||||
kind: "some-kind",
|
||||
metadata: {
|
||||
uid: "some-uid",
|
||||
name: "some-name",
|
||||
resourceVersion: "some-resource-version",
|
||||
},
|
||||
});
|
||||
|
||||
removeActionMock = asyncFn();
|
||||
|
||||
({ baseElement, getByTestId, queryByTestId } = render(
|
||||
<div>
|
||||
<ConfirmDialog/>
|
||||
|
||||
<KubeObjectMenu
|
||||
object={objectStub}
|
||||
apiManager={apiManagerStub}
|
||||
cluster={clusterStub}
|
||||
kubeObjectMenuRegistry={kubeObjectMenuRegistryStub}
|
||||
hideDetails={hideDetailsStub}
|
||||
editResourceTab={editResourceTabStub}
|
||||
toolbar={true}
|
||||
removeAction={removeActionMock}
|
||||
/>
|
||||
</div>,
|
||||
));
|
||||
});
|
||||
|
||||
const { baseElement } = render(
|
||||
<KubeObjectMenu
|
||||
object={objectStub}
|
||||
apiManager={apiManagerStub}
|
||||
cluster={clusterStub}
|
||||
kubeObjectMenuRegistry={kubeObjectMenuRegistryStub}
|
||||
hideDetails={hideDetailsStub}
|
||||
editResourceTab={editResourceTabStub}
|
||||
toolbar={true}
|
||||
/>,
|
||||
);
|
||||
it("renders", () => {
|
||||
expect(baseElement).toMatchSnapshot();
|
||||
});
|
||||
|
||||
expect(baseElement).toMatchSnapshot();
|
||||
it("does not open a confirmation dialog yet", () => {
|
||||
expect(queryByTestId("confirmation-dialog")).toBeNull();
|
||||
});
|
||||
|
||||
describe("when removing kube object", () => {
|
||||
beforeEach(() => {
|
||||
const menuItem = getByTestId("menu-action-remove");
|
||||
|
||||
userEvent.click(menuItem);
|
||||
});
|
||||
|
||||
it("renders", () => {
|
||||
expect(baseElement).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it("opens a confirmation dialog", () => {
|
||||
getByTestId("confirmation-dialog");
|
||||
});
|
||||
|
||||
describe("when remove is confirmed", () => {
|
||||
beforeEach(() => {
|
||||
const confirmRemovalButton = getByTestId("confirm");
|
||||
|
||||
userEvent.click(confirmRemovalButton);
|
||||
});
|
||||
|
||||
it("calls for removal of the kube object", () => {
|
||||
expect(removeActionMock).toHaveBeenCalledWith();
|
||||
});
|
||||
|
||||
it("does not close the confirmation dialog yet", () => {
|
||||
getByTestId("confirmation-dialog");
|
||||
});
|
||||
|
||||
it("when removal resolves, closes the confirmation dialog", async () => {
|
||||
await removeActionMock.resolve();
|
||||
|
||||
expect(queryByTestId("confirmation-dialog")).toBeNull(); });
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -137,7 +137,7 @@ export class KubeObjectMenu<
|
||||
}: { components: { MenuItem: React.ReactType<any> }},
|
||||
index: number,
|
||||
) => (
|
||||
<MenuItem
|
||||
<MenuItem
|
||||
object={object}
|
||||
toolbar={toolbar}
|
||||
// TODO: Fix misuse of index in key
|
||||
|
||||
@@ -144,7 +144,7 @@ export class MenuActions extends React.Component<MenuActionsProps> {
|
||||
</MenuItem>
|
||||
)}
|
||||
{removeAction && (
|
||||
<MenuItem onClick={this.remove}>
|
||||
<MenuItem onClick={this.remove} data-menu-action-remove-test data-testid="menu-action-remove">
|
||||
<Icon material="delete" interactive={toolbar} tooltip="Delete"/>
|
||||
<span className="title">Delete</span>
|
||||
</MenuItem>
|
||||
|
||||
Reference in new issue
Block a user