Skip to content

Commit d7ff9fe

Browse files
committed
feat: add tests for undo after delete/duplicate operations
1 parent 9d53684 commit d7ff9fe

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

test/spec/LiveDevelopmentMultiBrowser-test.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,6 +2504,171 @@ define(function (require, exports, module) {
25042504

25052505
await endEditModePreviewSession();
25062506
}, 30000);
2507+
2508+
it("should restore deleted element when undo is pressed after delete operation", async function () {
2509+
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
2510+
"SpecRunnerUtils.openProjectFiles simple1.html");
2511+
2512+
await waitsForLiveDevelopmentToOpenWithEditMode('hover');
2513+
2514+
// Get original source code content
2515+
const originalContent = DocumentManager.getCurrentDocument().getText();
2516+
expect(originalContent).toContain('id="testId"'); // Ensure test element exists
2517+
2518+
// Store original element text for verification
2519+
const originalElementText = await forRemoteExec(`document.getElementById('testId').textContent.trim()`);
2520+
2521+
// Click on the test element to show more options box
2522+
await forRemoteExec(`document.getElementById('testId').click()`);
2523+
2524+
// Wait for more options box to appear
2525+
await waitForMoreOptionsBox(true);
2526+
2527+
// Click the delete button in the shadow DOM
2528+
await forRemoteExec(`
2529+
const shadowHosts = Array.from(document.body.children).filter(el => el.shadowRoot);
2530+
let deleteButton = null;
2531+
2532+
shadowHosts.forEach(host => {
2533+
if (host.shadowRoot && host.shadowRoot.innerHTML.includes('phoenix-more-options-box')) {
2534+
deleteButton = host.shadowRoot.querySelector('span[data-action="delete"]');
2535+
}
2536+
});
2537+
2538+
if (deleteButton) {
2539+
deleteButton.click();
2540+
}
2541+
`);
2542+
2543+
// Wait for the delete operation to complete
2544+
await awaits(1000);
2545+
2546+
// Verify the element is removed from source code
2547+
const deletedContent = DocumentManager.getCurrentDocument().getText();
2548+
expect(deletedContent).not.toContain('id="testId"');
2549+
expect(deletedContent.length).toBeLessThan(originalContent.length);
2550+
2551+
// Verify the element is also removed from DOM
2552+
await forRemoteExec(`!!document.getElementById('testId')`, (result) => {
2553+
return result === false;
2554+
});
2555+
2556+
// Now perform undo operation using Ctrl+Z
2557+
await forRemoteExec(`
2558+
const event = new KeyboardEvent('keydown', {
2559+
key: 'z',
2560+
ctrlKey: true,
2561+
metaKey: false, // Use false for Windows/Linux, true for Mac
2562+
bubbles: true,
2563+
cancelable: true
2564+
});
2565+
document.dispatchEvent(event);
2566+
`);
2567+
2568+
// Wait for the undo operation to complete
2569+
await awaits(1500);
2570+
2571+
// Verify the element is restored in source code
2572+
const restoredContent = DocumentManager.getCurrentDocument().getText();
2573+
expect(restoredContent).toContain('id="testId"');
2574+
expect(restoredContent.length).toBe(originalContent.length);
2575+
2576+
// Verify the element is restored in DOM
2577+
await forRemoteExec(`!!document.getElementById('testId')`, (result) => {
2578+
return result === true;
2579+
});
2580+
2581+
// Verify the restored element has the same text content
2582+
await forRemoteExec(`document.getElementById('testId').textContent.trim()`, (result) => {
2583+
return result === originalElementText;
2584+
});
2585+
2586+
await endEditModePreviewSession();
2587+
}, 30000);
2588+
2589+
it("should restore original state when undo is pressed after duplicate operation", async function () {
2590+
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["simple1.html"]),
2591+
"SpecRunnerUtils.openProjectFiles simple1.html");
2592+
2593+
await waitsForLiveDevelopmentToOpenWithEditMode('hover');
2594+
2595+
// Get original source code content
2596+
const originalContent = DocumentManager.getCurrentDocument().getText();
2597+
expect(originalContent).toContain('id="testId"'); // Ensure test element exists
2598+
2599+
// Count initial occurrences of the test element
2600+
const originalTestIdCount = (originalContent.match(/id="testId"/g) || []).length;
2601+
expect(originalTestIdCount).toBe(1); // Should have exactly one initially
2602+
2603+
// Click on the test element to show more options box
2604+
await forRemoteExec(`document.getElementById('testId').click()`);
2605+
2606+
// Wait for more options box to appear
2607+
await waitForMoreOptionsBox(true);
2608+
2609+
// Click the duplicate button in the shadow DOM
2610+
await forRemoteExec(`
2611+
const shadowHosts = Array.from(document.body.children).filter(el => el.shadowRoot);
2612+
let duplicateButton = null;
2613+
2614+
shadowHosts.forEach(host => {
2615+
if (host.shadowRoot && host.shadowRoot.innerHTML.includes('phoenix-more-options-box')) {
2616+
duplicateButton = host.shadowRoot.querySelector('span[data-action="duplicate"]');
2617+
}
2618+
});
2619+
2620+
if (duplicateButton) {
2621+
duplicateButton.click();
2622+
}
2623+
`);
2624+
2625+
// Wait for the duplicate operation to complete
2626+
await awaits(1000);
2627+
2628+
// Verify the element is duplicated in source code
2629+
const duplicatedContent = DocumentManager.getCurrentDocument().getText();
2630+
const newTestIdCount = (duplicatedContent.match(/id="testId"/g) || []).length;
2631+
expect(newTestIdCount).toBe(2); // Should now have two instances
2632+
expect(duplicatedContent.length).toBeGreaterThan(originalContent.length);
2633+
2634+
// Verify both elements exist in the DOM
2635+
await forRemoteExec(`document.querySelectorAll('[id="testId"]').length`, (result) => {
2636+
return result === 2;
2637+
});
2638+
2639+
// Now perform undo operation using Ctrl+Z
2640+
await forRemoteExec(`
2641+
const event = new KeyboardEvent('keydown', {
2642+
key: 'z',
2643+
ctrlKey: true,
2644+
metaKey: false, // Use false for Windows/Linux, true for Mac
2645+
bubbles: true,
2646+
cancelable: true
2647+
});
2648+
document.dispatchEvent(event);
2649+
`);
2650+
2651+
// Wait for the undo operation to complete
2652+
await awaits(1500);
2653+
2654+
// Verify the duplicate is removed and we're back to original state
2655+
const restoredContent = DocumentManager.getCurrentDocument().getText();
2656+
const restoredTestIdCount = (restoredContent.match(/id="testId"/g) || []).length;
2657+
expect(restoredTestIdCount).toBe(1); // Should be back to one instance
2658+
expect(restoredContent.length).toBe(originalContent.length);
2659+
2660+
// Verify only one element exists in DOM
2661+
await forRemoteExec(`document.querySelectorAll('[id="testId"]').length`, (result) => {
2662+
return result === 1;
2663+
});
2664+
2665+
// Verify the remaining element still has correct content
2666+
await forRemoteExec(`!!document.getElementById('testId')`, (result) => {
2667+
return result === true;
2668+
});
2669+
2670+
await endEditModePreviewSession();
2671+
}, 30000);
25072672
});
25082673
});
25092674
});

0 commit comments

Comments
 (0)