|
1 | 1 | import { describe, expect, test } from "vitest";
|
| 2 | +import { coreMetas } from "@webstudio-is/sdk"; |
2 | 3 | import * as baseMetas from "@webstudio-is/sdk-components-react/metas";
|
3 | 4 | import { createDefaultPages } from "@webstudio-is/project-build";
|
4 | 5 | import { $, renderData, ws } from "@webstudio-is/template";
|
5 | 6 | import {
|
6 | 7 | $instances,
|
7 | 8 | $pages,
|
| 9 | + $props, |
8 | 10 | $registeredComponentMetas,
|
9 | 11 | } from "~/shared/nano-states";
|
10 | 12 | import { registerContainers } from "~/shared/sync";
|
11 | 13 | import { $awareness, selectInstance } from "~/shared/awareness";
|
12 |
| -import { deleteSelectedInstance, unwrap, wrapIn } from "./commands"; |
| 14 | +import { |
| 15 | + deleteSelectedInstance, |
| 16 | + replaceWith, |
| 17 | + unwrap, |
| 18 | + wrapIn, |
| 19 | +} from "./commands"; |
13 | 20 | import { elementComponent } from "@webstudio-is/sdk";
|
14 | 21 |
|
15 | 22 | registerContainers();
|
16 | 23 |
|
17 |
| -const metas = new Map(Object.entries(baseMetas)); |
| 24 | +const metas = new Map(Object.entries({ ...coreMetas, ...baseMetas })); |
18 | 25 | $registeredComponentMetas.set(metas);
|
19 | 26 | $pages.set(createDefaultPages({ rootInstanceId: "" }));
|
20 | 27 | $awareness.set({ pageId: "" });
|
@@ -167,6 +174,117 @@ describe("wrap in", () => {
|
167 | 174 | });
|
168 | 175 | });
|
169 | 176 |
|
| 177 | +describe("replace with", () => { |
| 178 | + test("replace legacy tag with element", () => { |
| 179 | + const { instances, props } = renderData( |
| 180 | + <ws.element ws:tag="body" ws:id="bodyId"> |
| 181 | + <$.Box tag="article" ws:id="articleId"></$.Box> |
| 182 | + </ws.element> |
| 183 | + ); |
| 184 | + $instances.set(instances); |
| 185 | + $props.set(props); |
| 186 | + selectInstance(["articleId", "bodyId"]); |
| 187 | + replaceWith(elementComponent); |
| 188 | + const { instances: newInstances, props: newProps } = renderData( |
| 189 | + <ws.element ws:tag="body" ws:id="bodyId"> |
| 190 | + <ws.element ws:tag="article" ws:id="articleId"></ws.element> |
| 191 | + </ws.element> |
| 192 | + ); |
| 193 | + expect({ instances: $instances.get(), props: $props.get() }).toEqual({ |
| 194 | + instances: newInstances, |
| 195 | + props: newProps, |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
| 199 | + test("migrate legacy properties as well", () => { |
| 200 | + const { instances, props } = renderData( |
| 201 | + <ws.element ws:tag="body" ws:id="bodyId"> |
| 202 | + <$.Box |
| 203 | + ws:tag="div" |
| 204 | + ws:id="divId" |
| 205 | + className="my-class" |
| 206 | + htmlFor="my-id" |
| 207 | + ></$.Box> |
| 208 | + </ws.element> |
| 209 | + ); |
| 210 | + $instances.set(instances); |
| 211 | + $props.set(props); |
| 212 | + selectInstance(["divId", "bodyId"]); |
| 213 | + replaceWith(elementComponent); |
| 214 | + const { instances: newInstances, props: newProps } = renderData( |
| 215 | + <ws.element ws:tag="body" ws:id="bodyId"> |
| 216 | + <ws.element |
| 217 | + ws:tag="div" |
| 218 | + ws:id="divId" |
| 219 | + class="my-class" |
| 220 | + for="my-id" |
| 221 | + ></ws.element> |
| 222 | + </ws.element> |
| 223 | + ); |
| 224 | + expect({ instances: $instances.get(), props: $props.get() }).toEqual({ |
| 225 | + instances: newInstances, |
| 226 | + props: newProps, |
| 227 | + }); |
| 228 | + }); |
| 229 | + |
| 230 | + test("preserve currently specified tag", () => { |
| 231 | + $instances.set( |
| 232 | + renderData( |
| 233 | + <ws.element ws:tag="body" ws:id="bodyId"> |
| 234 | + <$.Box ws:tag="article" ws:id="articleId"></$.Box> |
| 235 | + </ws.element> |
| 236 | + ).instances |
| 237 | + ); |
| 238 | + selectInstance(["articleId", "bodyId"]); |
| 239 | + replaceWith(elementComponent); |
| 240 | + expect($instances.get()).toEqual( |
| 241 | + renderData( |
| 242 | + <ws.element ws:tag="body" ws:id="bodyId"> |
| 243 | + <ws.element ws:tag="article" ws:id="articleId"></ws.element> |
| 244 | + </ws.element> |
| 245 | + ).instances |
| 246 | + ); |
| 247 | + }); |
| 248 | + |
| 249 | + test("replace with first tag from presets", () => { |
| 250 | + $instances.set( |
| 251 | + renderData( |
| 252 | + <ws.element ws:tag="body" ws:id="bodyId"> |
| 253 | + <$.Heading ws:id="headingId"></$.Heading> |
| 254 | + </ws.element> |
| 255 | + ).instances |
| 256 | + ); |
| 257 | + selectInstance(["headingId", "bodyId"]); |
| 258 | + replaceWith(elementComponent); |
| 259 | + expect($instances.get()).toEqual( |
| 260 | + renderData( |
| 261 | + <ws.element ws:tag="body" ws:id="bodyId"> |
| 262 | + <ws.element ws:tag="h1" ws:id="headingId"></ws.element> |
| 263 | + </ws.element> |
| 264 | + ).instances |
| 265 | + ); |
| 266 | + }); |
| 267 | + |
| 268 | + test("fallback to div", () => { |
| 269 | + $instances.set( |
| 270 | + renderData( |
| 271 | + <ws.element ws:tag="body" ws:id="bodyId"> |
| 272 | + <$.Box ws:id="divId"></$.Box> |
| 273 | + </ws.element> |
| 274 | + ).instances |
| 275 | + ); |
| 276 | + selectInstance(["divId", "bodyId"]); |
| 277 | + replaceWith(elementComponent); |
| 278 | + expect($instances.get()).toEqual( |
| 279 | + renderData( |
| 280 | + <ws.element ws:tag="body" ws:id="bodyId"> |
| 281 | + <ws.element ws:tag="div" ws:id="divId"></ws.element> |
| 282 | + </ws.element> |
| 283 | + ).instances |
| 284 | + ); |
| 285 | + }); |
| 286 | +}); |
| 287 | + |
170 | 288 | describe("unwrap", () => {
|
171 | 289 | test("unwrap instance", () => {
|
172 | 290 | $instances.set(
|
|
0 commit comments