1 // @vitest-environment jsdom
2 import { render, screen, fireEvent, cleanup } from "@testing-library/react"
3 import { describe, expect, it, vi, afterEach } from "vitest"
4 import { TieredPriceControls } from "./tiered-price-controls"
5
6 afterEach(() => {
7 cleanup()
8 })
9
10 vi.mock("@medusajs/icons", () => ({
11 ArrowsPointingOut: () => <svg data-testid="arrows-pointing-out" />,
12 CircleSliders: () => <svg data-testid="circle-sliders" />,
13 }))
14
15 describe("TieredPriceCell", () => {
16 const defaultProps = {
17 isTiered: false,
18 isAnchor: false,
19 onOpenModal: vi.fn(),
20 symbolWidth: 20,
21 }
22
23 it("should not render CircleSliders when isTiered is false", () => {
24 render(<TieredPriceControls {...defaultProps} />)
25 expect(screen.queryByTestId("circle-sliders")).toBeNull()
26 expect(screen.getByRole("button")).toBeTruthy()
27 })
28
29 it("should render CircleSliders when isTiered is true and isAnchor is false", () => {
30 render(<TieredPriceControls {...defaultProps} isTiered={true} />)
31 expect(screen.getByTestId("circle-sliders")).toBeTruthy()
32 expect(screen.getByRole("button")).toBeTruthy()
33 })
34
35 it("should not render CircleSliders when isTiered is true but isAnchor is true", () => {
36 render(
37 <TieredPriceControls {...defaultProps} isTiered={true} isAnchor={true} />
38 )
39 expect(screen.queryByTestId("circle-sliders")).toBeNull()
40 expect(screen.getByRole("button")).toBeTruthy()
41 })
42
43 it("should call onOpenModal when the expansion button is clicked", () => {
44 const onOpenModal = vi.fn()
45 render(<TieredPriceControls {...defaultProps} onOpenModal={onOpenModal} />)
46 const button = screen.getByRole("button")
47 fireEvent.click(button)
48 expect(onOpenModal).toHaveBeenCalledTimes(1)
49 })
50
51 it("should trigger onOpenModal when Cmd+B is pressed and isAnchor is true", () => {
52 const onOpenModal = vi.fn()
53 render(
54 <TieredPriceControls
55 {...defaultProps}
56 onOpenModal={onOpenModal}
57 isAnchor={true}
58 />
59 )
60 fireEvent.keyDown(document, {
61 key: "b",
62 metaKey: true,
63 })
64 expect(onOpenModal).toHaveBeenCalledTimes(1)
65 })
66
67 it("should trigger onOpenModal when Ctrl+B is pressed and isAnchor is true", () => {
68 const onOpenModal = vi.fn()
69 render(
70 <TieredPriceControls
71 {...defaultProps}
72 onOpenModal={onOpenModal}
73 isAnchor={true}
74 />
75 )
76 fireEvent.keyDown(document, {
77 key: "b",
78 ctrlKey: true,
79 })
80 expect(onOpenModal).toHaveBeenCalledTimes(1)
81 })
82
83 it("should not trigger onOpenModal when Cmd+B is pressed and isAnchor is false", () => {
84 const onOpenModal = vi.fn()
85 render(
86 <TieredPriceControls
87 {...defaultProps}
88 onOpenModal={onOpenModal}
89 isAnchor={false}
90 />
91 )
92 fireEvent.keyDown(document, {
93 key: "b",
94 metaKey: true,
95 })
96 expect(onOpenModal).not.toHaveBeenCalled()
97 })
98 })