|
| 1 | +import { mount } from "@vue/test-utils"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import Textarea from "../Textarea.vue"; |
| 4 | + |
| 5 | +describe("Textarea", () => { |
| 6 | + it("renders with default props", () => { |
| 7 | + const wrapper = mount(Textarea); |
| 8 | + expect(wrapper.find(".control").exists()).toBe(true); |
| 9 | + expect(wrapper.find("textarea").exists()).toBe(true); |
| 10 | + }); |
| 11 | + |
| 12 | + it("handles v-model correctly", async () => { |
| 13 | + const wrapper = mount(Textarea, { |
| 14 | + props: { |
| 15 | + modelValue: "Initial text", |
| 16 | + }, |
| 17 | + }); |
| 18 | + expect(wrapper.find("textarea").element.value).toBe("Initial text"); |
| 19 | + |
| 20 | + await wrapper.find("textarea").setValue("New text"); |
| 21 | + const emitted = wrapper.emitted("update:modelValue"); |
| 22 | + expect(emitted).toBeTruthy(); |
| 23 | + expect(emitted?.[0]).toEqual(["Initial text"]); |
| 24 | + expect(emitted?.[1]).toEqual(["New text"]); |
| 25 | + }); |
| 26 | + |
| 27 | + it("applies style props correctly", () => { |
| 28 | + const wrapper = mount(Textarea, { |
| 29 | + props: { |
| 30 | + color: "is-primary", |
| 31 | + size: "is-large", |
| 32 | + }, |
| 33 | + }); |
| 34 | + const textarea = wrapper.find("textarea"); |
| 35 | + expect(textarea.classes()).toContain("is-primary"); |
| 36 | + expect(textarea.classes()).toContain("is-large"); |
| 37 | + expect(wrapper.find(".control").classes()).toContain("is-large"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("shows loading state", () => { |
| 41 | + const wrapper = mount(Textarea, { |
| 42 | + props: { |
| 43 | + loading: true, |
| 44 | + }, |
| 45 | + }); |
| 46 | + expect(wrapper.find(".control").classes()).toContain("is-loading"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("applies hover state", () => { |
| 50 | + const wrapper = mount(Textarea, { |
| 51 | + props: { |
| 52 | + hovered: true, |
| 53 | + }, |
| 54 | + }); |
| 55 | + expect(wrapper.find("textarea").classes()).toContain("is-hovered"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("applies focus state", () => { |
| 59 | + const wrapper = mount(Textarea, { |
| 60 | + props: { |
| 61 | + focused: true, |
| 62 | + }, |
| 63 | + }); |
| 64 | + expect(wrapper.find("textarea").classes()).toContain("is-focused"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("handles focus and blur events", async () => { |
| 68 | + const wrapper = mount(Textarea); |
| 69 | + const textarea = wrapper.find("textarea"); |
| 70 | + |
| 71 | + await textarea.trigger("focus"); |
| 72 | + expect(wrapper.vm.isFocused).toBe(true); |
| 73 | + |
| 74 | + await textarea.trigger("blur"); |
| 75 | + expect(wrapper.vm.isFocused).toBe(false); |
| 76 | + }); |
| 77 | + |
| 78 | + it("shows character counter when hasCounter and maxlength are set", () => { |
| 79 | + const wrapper = mount(Textarea, { |
| 80 | + props: { |
| 81 | + hasCounter: true, |
| 82 | + maxlength: "100", |
| 83 | + modelValue: "Hello", |
| 84 | + }, |
| 85 | + }); |
| 86 | + expect(wrapper.find("small").exists()).toBe(true); |
| 87 | + expect(wrapper.find("small").text()).toBe("5 / 100"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("hides counter when not focused", async () => { |
| 91 | + const wrapper = mount(Textarea, { |
| 92 | + props: { |
| 93 | + hasCounter: true, |
| 94 | + maxlength: "100", |
| 95 | + modelValue: "Hello", |
| 96 | + }, |
| 97 | + }); |
| 98 | + expect(wrapper.find("small").classes()).toContain("is-invisible"); |
| 99 | + |
| 100 | + await wrapper.find("textarea").trigger("focus"); |
| 101 | + expect(wrapper.find("small").classes()).not.toContain("is-invisible"); |
| 102 | + |
| 103 | + await wrapper.find("textarea").trigger("blur"); |
| 104 | + expect(wrapper.find("small").classes()).toContain("is-invisible"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("inherits HTML attributes", () => { |
| 108 | + const wrapper = mount(Textarea, { |
| 109 | + attrs: { |
| 110 | + placeholder: "Enter text here", |
| 111 | + rows: "5", |
| 112 | + name: "description", |
| 113 | + }, |
| 114 | + }); |
| 115 | + const textarea = wrapper.find("textarea"); |
| 116 | + expect(textarea.attributes("placeholder")).toBe("Enter text here"); |
| 117 | + expect(textarea.attributes("rows")).toBe("5"); |
| 118 | + expect(textarea.attributes("name")).toBe("description"); |
| 119 | + }); |
| 120 | + |
| 121 | + it("updates value when modelValue prop changes", async () => { |
| 122 | + const wrapper = mount(Textarea, { |
| 123 | + props: { |
| 124 | + modelValue: "Initial", |
| 125 | + }, |
| 126 | + }); |
| 127 | + // @ts-expect-error |
| 128 | + await wrapper.setProps({ modelValue: "Updated" }); |
| 129 | + expect(wrapper.find("textarea").element.value).toBe("Updated"); |
| 130 | + }); |
| 131 | +}); |
0 commit comments