From 32dbcbea26e897f69e86a165f26c7986f11f36ce Mon Sep 17 00:00:00 2001 From: Nils Date: Fri, 29 Nov 2024 08:57:02 +0100 Subject: [PATCH 1/2] refactor: move button to composition api --- .../form/mt-button/mt-button.spec.ts | 87 ++++++++++ .../components/form/mt-button/mt-button.vue | 164 +++++------------- 2 files changed, 133 insertions(+), 118 deletions(-) create mode 100644 packages/component-library/src/components/form/mt-button/mt-button.spec.ts diff --git a/packages/component-library/src/components/form/mt-button/mt-button.spec.ts b/packages/component-library/src/components/form/mt-button/mt-button.spec.ts new file mode 100644 index 000000000..91c746270 --- /dev/null +++ b/packages/component-library/src/components/form/mt-button/mt-button.spec.ts @@ -0,0 +1,87 @@ +import { render, screen } from "@testing-library/vue"; +import MtButton from "./mt-button.vue"; +import { vi } from "vitest"; +import { userEvent } from "@storybook/test"; + +describe("mt-button", () => { + it("performs an action when clicking on a button", async () => { + // ARRANGE + const handler = vi.fn(); + + render(MtButton, { + props: { + // @ts-expect-error -- Event handler is not typed because they are inherited + onClick: handler, + }, + }); + + // ACT + await userEvent.click(screen.getByRole("button")); + + // ASSERT + expect(handler).toHaveBeenCalledOnce(); + }); + + it.each(["{Enter}", " "])('performs an action when pressing "%s" on a button', async (key) => { + // ARRANGE + const handler = vi.fn(); + + render(MtButton, { + props: { + // @ts-expect-error -- Event handler is not typed because they are inherited + onClick: handler, + }, + }); + + await userEvent.tab(); + + // ACT + await userEvent.keyboard(key); + + // ASSERT + expect(handler).toHaveBeenCalledOnce(); + }); + + it("does not perform an action when clicking on a disabled button", async () => { + // ARRANGE + const handler = vi.fn(); + + render(MtButton, { + props: { + // @ts-expect-error -- Event handler is not typed because they are inherited + onClick: handler, + disabled: true, + }, + }); + + // ACT + await userEvent.click(screen.getByRole("button")); + + // ASSERT + expect(handler).not.toHaveBeenCalled(); + }); + + it.each(["{Enter}", " "])( + 'does not perform an action when pressing "%s" on a disabled button', + async (key) => { + // ARRANGE + const handler = vi.fn(); + + render(MtButton, { + props: { + // @ts-expect-error -- Event handler is not typed because they are inherited + onClick: handler, + disabled: true, + }, + }); + + await userEvent.tab(); + + // ACT + await userEvent.keyboard(key); + + // ASSERT + expect(handler).not.toHaveBeenCalled(); + }, + ); +}); diff --git a/packages/component-library/src/components/form/mt-button/mt-button.vue b/packages/component-library/src/components/form/mt-button/mt-button.vue index c9c09ed01..17523affa 100644 --- a/packages/component-library/src/components/form/mt-button/mt-button.vue +++ b/packages/component-library/src/components/form/mt-button/mt-button.vue @@ -21,133 +21,61 @@ v-bind="$attrs" > - + - + -