|
1
|
// @vitest-environment jsdom |
|
2
|
import React from "react" |
|
3
|
import { render, screen, fireEvent, cleanup } from "@testing-library/react" |
|
4
|
import { describe, expect, it, vi, afterEach } from "vitest" |
|
5
|
import { z } from "zod" |
|
6
|
import { TieredPriceForm } from "./tiered-price-form" |
|
7
|
import { CurrencyInfo } from "../../../lib/data/currencies" |
|
8
|
|
|
9
|
afterEach(() => { |
|
10
|
cleanup() |
|
11
|
}) |
|
12
|
|
|
13
|
vi.mock("react-i18next", () => ({ |
|
14
|
useTranslation: () => ({ |
|
15
|
t: (key: string) => key, |
|
16
|
}), |
|
17
|
})) |
|
18
|
|
|
19
|
vi.mock("@medusajs/icons", () => ({ |
|
20
|
TriangleDownMini: () => <svg data-testid="triangle-down-mini" />, |
|
21
|
XMarkMini: () => <svg data-testid="x-mark-mini" />, |
|
22
|
})) |
|
23
|
|
|
24
|
let closeSpy: any |
|
25
|
vi.mock("../../../components/modals", () => { |
|
26
|
const MockComponent = ({ children }: { children: React.ReactNode }) => ( |
|
27
|
<div>{children}</div> |
|
28
|
) |
|
29
|
return { |
|
30
|
StackedFocusModal: { |
|
31
|
Content: MockComponent, |
|
32
|
Header: MockComponent, |
|
33
|
Body: MockComponent, |
|
34
|
Footer: MockComponent, |
|
35
|
Title: MockComponent, |
|
36
|
Description: MockComponent, |
|
37
|
Close: ({ children }: { children: React.ReactNode }) => ( |
|
38
|
<div onClick={() => closeSpy?.()}>{children}</div> |
|
39
|
), |
|
40
|
}, |
|
41
|
} |
|
42
|
}) |
|
43
|
|
|
44
|
describe("TieredPriceForm", () => { |
|
45
|
const mockCurrency: CurrencyInfo = { |
|
46
|
code: "usd", |
|
47
|
name: "US Dollar", |
|
48
|
symbol_native: "$", |
|
49
|
decimal_digits: 2, |
|
50
|
} |
|
51
|
|
|
52
|
const schema = z.object({ |
|
53
|
prices: z.array( |
|
54
|
z.object({ |
|
55
|
amount: z.string().min(1, "Amount is required"), |
|
56
|
min: z.string().min(1, "Min is required"), |
|
57
|
max: z.string().optional(), |
|
58
|
}) |
|
59
|
), |
|
60
|
}) |
|
61
|
|
|
62
|
const defaultProps = { |
|
63
|
schema, |
|
64
|
initialValues: [{ amount: "10", min: "1", max: "10" }], |
|
65
|
onSubmit: vi.fn(), |
|
66
|
onClose: vi.fn(), |
|
67
|
currency: mockCurrency, |
|
68
|
header: "Tiered Price Header", |
|
69
|
description: "Tiered Price Description", |
|
70
|
addPriceLabel: "Add Price", |
|
71
|
fieldConfig: { |
|
72
|
min: "min", |
|
73
|
max: "max", |
|
74
|
minLabel: "Min", |
|
75
|
maxLabel: "Max", |
|
76
|
}, |
|
77
|
renderConditionItem: vi.fn(({ index }) => ( |
|
78
|
<div data-testid={`condition-item-${index}`}>Condition Item {index}</div> |
|
79
|
)), |
|
80
|
renderConditionTrigger: vi.fn(({ index }) => ( |
|
81
|
<div data-testid={`condition-trigger-${index}`}> |
|
82
|
Condition Trigger {index} |
|
83
|
</div> |
|
84
|
)), |
|
85
|
} |
|
86
|
|
|
87
|
it("should render with initial values", () => { |
|
88
|
render(<TieredPriceForm {...defaultProps} />) |
|
89
|
|
|
90
|
expect(screen.getByText("Tiered Price Header")).toBeTruthy() |
|
91
|
expect(screen.getByText("Tiered Price Description")).toBeTruthy() |
|
92
|
expect(screen.getByText("Add Price")).toBeTruthy() |
|
93
|
expect(screen.getByTestId("condition-trigger-0")).toBeTruthy() |
|
94
|
expect(screen.getByTestId("condition-item-0")).toBeTruthy() |
|
95
|
}) |
|
96
|
|
|
97
|
it("should add a new price tier when add button is clicked", () => { |
|
98
|
render(<TieredPriceForm {...defaultProps} />) |
|
99
|
|
|
100
|
const addButton = screen.getByText("Add Price") |
|
101
|
fireEvent.click(addButton) |
|
102
|
|
|
103
|
expect(screen.getByTestId("condition-trigger-1")).toBeTruthy() |
|
104
|
expect(screen.getByTestId("condition-item-1")).toBeTruthy() |
|
105
|
}) |
|
106
|
|
|
107
|
it("should remove a price tier when remove button is clicked", () => { |
|
108
|
render(<TieredPriceForm {...defaultProps} />) |
|
109
|
|
|
110
|
const removeButton = screen.getByTestId("x-mark-mini").closest("button") |
|
111
|
if (!removeButton) { |
|
112
|
throw new Error("Remove button not found") |
|
113
|
} |
|
114
|
|
|
115
|
fireEvent.click(removeButton) |
|
116
|
|
|
117
|
expect(screen.queryByTestId("condition-trigger-0")).toBeNull() |
|
118
|
}) |
|
119
|
|
|
120
|
it("should call onSubmit with valid data", async () => { |
|
121
|
const onSubmit = vi.fn() |
|
122
|
render(<TieredPriceForm {...defaultProps} onSubmit={onSubmit} />) |
|
123
|
|
|
124
|
const saveButton = screen.getByText("actions.save") |
|
125
|
fireEvent.click(saveButton) |
|
126
|
|
|
127
|
// Since handleSubmit is async and uses react-hook-form, we might need to wait |
|
128
|
// But in this case, initialValues are already valid according to the schema |
|
129
|
await vi.waitFor(() => { |
|
130
|
expect(onSubmit).toHaveBeenCalledWith({ |
|
131
|
prices: [{ amount: "10", min: "1", max: "10" }], |
|
132
|
}) |
|
133
|
}) |
|
134
|
expect(defaultProps.onClose).toHaveBeenCalled() |
|
135
|
}) |
|
136
|
|
|
137
|
it("should handle validation errors on submit", async () => { |
|
138
|
const onSubmit = vi.fn() |
|
139
|
// Initial values that are invalid (amount is empty) |
|
140
|
const initialValues = [{ amount: "", min: "1", max: "10" }] |
|
141
|
|
|
142
|
render( |
|
143
|
<TieredPriceForm |
|
144
|
{...defaultProps} |
|
145
|
onSubmit={onSubmit} |
|
146
|
initialValues={initialValues} |
|
147
|
/> |
|
148
|
) |
|
149
|
|
|
150
|
const saveButton = screen.getByText("actions.save") |
|
151
|
fireEvent.click(saveButton) |
|
152
|
|
|
153
|
await vi.waitFor(() => { |
|
154
|
expect(onSubmit).not.toHaveBeenCalled() |
|
155
|
}) |
|
156
|
}) |
|
157
|
|
|
158
|
it("should trigger submit on Ctrl+Enter / Cmd+Enter", async () => { |
|
159
|
const onSubmit = vi.fn() |
|
160
|
render(<TieredPriceForm {...defaultProps} onSubmit={onSubmit} />) |
|
161
|
|
|
162
|
const form = document.querySelector("form") |
|
163
|
if (!form) { |
|
164
|
throw new Error("Form not found") |
|
165
|
} |
|
166
|
|
|
167
|
fireEvent.keyDown(form, { |
|
168
|
key: "Enter", |
|
169
|
ctrlKey: true, |
|
170
|
}) |
|
171
|
|
|
172
|
await vi.waitFor(() => { |
|
173
|
expect(onSubmit).toHaveBeenCalled() |
|
174
|
}) |
|
175
|
}) |
|
176
|
|
|
177
|
it("should call onClose when cancel is clicked", () => { |
|
178
|
closeSpy = vi.fn() |
|
179
|
render(<TieredPriceForm {...defaultProps} />) |
|
180
|
|
|
181
|
const cancelButton = screen.getByText("actions.cancel") |
|
182
|
fireEvent.click(cancelButton) |
|
183
|
|
|
184
|
expect(closeSpy).toHaveBeenCalled() |
|
185
|
}) |
|
186
|
}) |