|
1 | 1 | import { render, fireEvent, screen } from "@testing-library/preact"; |
2 | | -import StartEditingButtonComponent, { getEditButtonPosition } from "../startEditingButton"; |
| 2 | +import StartEditingButtonComponent, { |
| 3 | + getEditButtonPosition, |
| 4 | +} from "../startEditingButton"; |
3 | 5 | import Config from "../../../configManager/configManager"; |
4 | 6 | import { asyncRender } from "../../../__test__/utils"; |
5 | 7 |
|
6 | 8 | describe("StartEditingButtonComponent", () => { |
7 | 9 | let visualBuilderContainer: HTMLDivElement; |
8 | 10 |
|
9 | 11 | beforeEach(() => { |
10 | | - document.getElementsByTagName('html')[0].innerHTML = ''; |
| 12 | + document.getElementsByTagName("html")[0].innerHTML = ""; |
11 | 13 | Config.reset(); |
12 | 14 | Config.set("stackDetails.apiKey", "bltapikey"); |
13 | 15 | Config.set("stackDetails.environment", "bltenvironment"); |
@@ -38,103 +40,120 @@ describe("StartEditingButtonComponent", () => { |
38 | 40 | }); |
39 | 41 |
|
40 | 42 | test("should update the href when clicked", async () => { |
41 | | - const { getByTestId } = await asyncRender(<StartEditingButtonComponent />); |
| 43 | + const { getByTestId } = await asyncRender( |
| 44 | + <StartEditingButtonComponent /> |
| 45 | + ); |
42 | 46 | const button = getByTestId("vcms-start-editing-btn"); |
43 | 47 |
|
44 | 48 | expect(button?.getAttribute("href")).toBe( |
45 | | - "https://app.contentstack.com/#!/stack/bltapikey/visual-builder?branch=main&environment=bltenvironment&target-url=http%3A%2F%2Flocalhost%3A3000%2F&locale=en-us" |
| 49 | + "https://app.contentstack.com/#!/stack/bltapikey/visual-editor?branch=main&environment=bltenvironment&target-url=http%3A%2F%2Flocalhost%3A3000%2F&locale=en-us" |
46 | 50 | ); |
47 | 51 | }); |
48 | 52 |
|
49 | 53 | test("should not render when enable is false", async () => { |
50 | 54 | Config.set("editInVisualBuilderButton.enable", false); |
51 | | - const { container } = await asyncRender(<StartEditingButtonComponent />); |
| 55 | + const { container } = await asyncRender( |
| 56 | + <StartEditingButtonComponent /> |
| 57 | + ); |
52 | 58 | expect(container).toBeEmptyDOMElement(); |
53 | 59 | }); |
54 | 60 |
|
55 | 61 | test("should render when enable is true", async () => { |
56 | 62 | Config.set("editInVisualBuilderButton.enable", true); |
57 | | - const { getByTestId } = await asyncRender(<StartEditingButtonComponent />); |
| 63 | + const { getByTestId } = await asyncRender( |
| 64 | + <StartEditingButtonComponent /> |
| 65 | + ); |
58 | 66 | const button = getByTestId("vcms-start-editing-btn"); |
59 | 67 | expect(button).toBeInTheDocument(); |
60 | 68 | }); |
61 | 69 |
|
62 | | - test.each([ |
63 | | - 'bottom-right', |
64 | | - 'bottom-left', |
65 | | - 'top-left', |
66 | | - 'top-right' |
67 | | - ])('should return valid position %s', (position) => { |
68 | | - expect(getEditButtonPosition(position)).toBe(position); |
69 | | - }); |
| 70 | + test.each(["bottom-right", "bottom-left", "top-left", "top-right"])( |
| 71 | + "should return valid position %s", |
| 72 | + (position) => { |
| 73 | + expect(getEditButtonPosition(position)).toBe(position); |
| 74 | + } |
| 75 | + ); |
70 | 76 |
|
71 | 77 | test.each([ |
72 | | - 'invalid-position', |
73 | | - 'center', |
74 | | - '', |
| 78 | + "invalid-position", |
| 79 | + "center", |
| 80 | + "", |
75 | 81 | undefined, |
76 | 82 | null, |
77 | 83 | 123, |
78 | 84 | {}, |
79 | 85 | [], |
80 | 86 | false, |
81 | | - ])('should return bottom-right for invalid input: %s', (invalidPosition) => { |
82 | | - expect(getEditButtonPosition(invalidPosition)).toBe('bottom-right'); |
83 | | - }); |
84 | | - |
| 87 | + ])( |
| 88 | + "should return bottom-right for invalid input: %s", |
| 89 | + (invalidPosition) => { |
| 90 | + expect(getEditButtonPosition(invalidPosition)).toBe("bottom-right"); |
| 91 | + } |
| 92 | + ); |
| 93 | + |
85 | 94 | test("should render with default values when editInVisualBuilderButton config is missing", async () => { |
86 | 95 | Config.reset(); |
87 | 96 | Config.set("stackDetails.apiKey", "bltapikey"); |
88 | 97 | Config.set("stackDetails.environment", "bltenvironment"); |
89 | 98 |
|
90 | | - const { getByTestId } = await asyncRender(<StartEditingButtonComponent />); |
| 99 | + const { getByTestId } = await asyncRender( |
| 100 | + <StartEditingButtonComponent /> |
| 101 | + ); |
91 | 102 | const button = getByTestId("vcms-start-editing-btn"); |
92 | | - |
93 | | - expect(Config.get().editInVisualBuilderButton.position).toBe("bottom-right") |
| 103 | + |
| 104 | + expect(Config.get().editInVisualBuilderButton.position).toBe( |
| 105 | + "bottom-right" |
| 106 | + ); |
94 | 107 | expect(button).toBeInTheDocument(); |
95 | 108 | }); |
96 | 109 |
|
97 | 110 | test("should update href with current URL when mouse enters button", async () => { |
98 | | - Object.defineProperty(window, 'location', { |
99 | | - value: new URL('http://localhost:3000'), |
| 111 | + Object.defineProperty(window, "location", { |
| 112 | + value: new URL("http://localhost:3000"), |
100 | 113 | }); |
101 | | - |
102 | | - const { getByTestId } = await asyncRender(<StartEditingButtonComponent />); |
| 114 | + |
| 115 | + const { getByTestId } = await asyncRender( |
| 116 | + <StartEditingButtonComponent /> |
| 117 | + ); |
103 | 118 | const button = getByTestId("vcms-start-editing-btn"); |
104 | 119 | const initialHref = button.getAttribute("href"); |
105 | | - |
106 | | - Object.defineProperty(window, 'location', { |
107 | | - value: new URL('http://localhost:3000/about'), |
108 | | - writable: true |
| 120 | + |
| 121 | + Object.defineProperty(window, "location", { |
| 122 | + value: new URL("http://localhost:3000/about"), |
| 123 | + writable: true, |
109 | 124 | }); |
110 | 125 |
|
111 | 126 | fireEvent.mouseEnter(button); |
112 | | - |
| 127 | + |
113 | 128 | const updatedHref = button.getAttribute("href"); |
114 | 129 | expect(updatedHref).not.toBe(initialHref); |
115 | | - expect(updatedHref).toContain(encodeURIComponent("http://localhost:3000/about")); |
| 130 | + expect(updatedHref).toContain( |
| 131 | + encodeURIComponent("http://localhost:3000/about") |
| 132 | + ); |
116 | 133 | }); |
117 | 134 |
|
118 | 135 | test("should update href with current URL when button is focused", async () => { |
119 | | - Object.defineProperty(window, 'location', { |
120 | | - value: new URL('http://localhost:3000'), |
| 136 | + Object.defineProperty(window, "location", { |
| 137 | + value: new URL("http://localhost:3000"), |
121 | 138 | }); |
122 | | - |
123 | | - const { getByTestId } = await asyncRender(<StartEditingButtonComponent />); |
| 139 | + |
| 140 | + const { getByTestId } = await asyncRender( |
| 141 | + <StartEditingButtonComponent /> |
| 142 | + ); |
124 | 143 | const button = getByTestId("vcms-start-editing-btn"); |
125 | 144 | const initialHref = button.getAttribute("href"); |
126 | | - |
127 | | - Object.defineProperty(window, 'location', { |
128 | | - value: new URL('http://localhost:3000/contact'), |
129 | | - writable: true |
| 145 | + |
| 146 | + Object.defineProperty(window, "location", { |
| 147 | + value: new URL("http://localhost:3000/contact"), |
| 148 | + writable: true, |
130 | 149 | }); |
131 | 150 |
|
132 | 151 | fireEvent.focus(button); |
133 | | - |
| 152 | + |
134 | 153 | const updatedHref = button.getAttribute("href"); |
135 | 154 | expect(updatedHref).not.toBe(initialHref); |
136 | | - expect(updatedHref).toContain(encodeURIComponent("http://localhost:3000/contact")); |
| 155 | + expect(updatedHref).toContain( |
| 156 | + encodeURIComponent("http://localhost:3000/contact") |
| 157 | + ); |
137 | 158 | }); |
138 | | - |
139 | | - |
140 | 159 | }); |
0 commit comments