|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { deepClone } from '../src/core/clone.js'; |
| 3 | + |
| 4 | +describe('deepClone', () => { |
| 5 | + it('clones a simple div', () => { |
| 6 | + const el = document.createElement('div'); |
| 7 | + el.textContent = 'hello'; |
| 8 | + const styleMap = new Map(); |
| 9 | + const styleCache = new WeakMap(); |
| 10 | + const nodeMap = new Map(); |
| 11 | + const clone = deepClone(el, styleMap, styleCache, nodeMap, false); |
| 12 | + expect(clone).not.toBe(el); |
| 13 | + expect(clone.textContent).toBe('hello'); |
| 14 | + }); |
| 15 | + |
| 16 | + it('clones canvas as an image', () => { |
| 17 | + const canvas = document.createElement('canvas'); |
| 18 | + canvas.width = 10; |
| 19 | + canvas.height = 10; |
| 20 | + const ctx = canvas.getContext('2d'); |
| 21 | + ctx.fillStyle = 'red'; |
| 22 | + ctx.fillRect(0,0,10,10); |
| 23 | + const styleMap = new Map(); |
| 24 | + const styleCache = new WeakMap(); |
| 25 | + const nodeMap = new Map(); |
| 26 | + const clone = deepClone(canvas, styleMap, styleCache, nodeMap, false); |
| 27 | + expect(clone.tagName).toBe('IMG'); |
| 28 | + expect(clone.src.startsWith('data:image/')).toBe(true); |
| 29 | + }); |
| 30 | + |
| 31 | + it('deepClone handles data-capture="exclude"', () => { |
| 32 | + const el = document.createElement('div'); |
| 33 | + el.setAttribute('data-capture', 'exclude'); |
| 34 | + const clone = deepClone(el, new Map(), new WeakMap(), new Map(), false); |
| 35 | + expect(clone).not.toBeNull(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('deepClone handles data-capture="placeholder"', () => { |
| 39 | + const el = document.createElement('div'); |
| 40 | + el.setAttribute('data-capture', 'placeholder'); |
| 41 | + el.setAttribute('data-placeholder-text', 'Placeholder!'); |
| 42 | + const clone = deepClone(el, new Map(), new WeakMap(), new Map(), false); |
| 43 | + expect(clone.textContent).toContain('Placeholder!'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('deepClone handles iframe', () => { |
| 47 | + const iframe = document.createElement('iframe'); |
| 48 | + iframe.width = 100; |
| 49 | + iframe.height = 50; |
| 50 | + const clone = deepClone(iframe, new Map(), new WeakMap(), new Map(), false); |
| 51 | + expect(clone.tagName).toBe('DIV'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('deepClone handles input, textarea, select', () => { |
| 55 | + const input = document.createElement('input'); |
| 56 | + input.value = 'foo'; |
| 57 | + input.checked = true; |
| 58 | + const textarea = document.createElement('textarea'); |
| 59 | + textarea.value = 'bar'; |
| 60 | + const select = document.createElement('select'); |
| 61 | + const opt = document.createElement('option'); |
| 62 | + opt.value = 'baz'; |
| 63 | + select.appendChild(opt); |
| 64 | + select.value = 'baz'; |
| 65 | + [input, textarea, select].forEach(el => { |
| 66 | + const clone = deepClone(el, new Map(), new WeakMap(), new Map(), false); |
| 67 | + expect(clone.value).toBe(el.value); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('deepClone handles shadow DOM', () => { |
| 72 | + const el = document.createElement('div'); |
| 73 | + const shadow = el.attachShadow({mode:'open'}); |
| 74 | + const span = document.createElement('span'); |
| 75 | + span.textContent = 'shadow'; |
| 76 | + shadow.appendChild(span); |
| 77 | + const clone = deepClone(el, new Map(), new WeakMap(), new Map(), false); |
| 78 | + expect(clone).not.toBeNull(); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('deepClone edge cases', () => { |
| 83 | + it('clones unsupported node (Comment) as a new Comment', () => { |
| 84 | + const fake = document.createComment('not supported'); |
| 85 | + const result = deepClone(fake, new Map(), new WeakMap(), new Map(), false); |
| 86 | + expect(result.nodeType).toBe(Node.COMMENT_NODE); |
| 87 | + expect(result.textContent).toBe('not supported'); |
| 88 | + expect(result).not.toBe(fake); // Es un clon, no el mismo objeto |
| 89 | + }); |
| 90 | + it('handles error in internal logic', () => { |
| 91 | + const el = document.createElement('div'); |
| 92 | + vi.spyOn(el, 'cloneNode').mockImplementation(() => { throw new Error('fail'); }); |
| 93 | + expect(() => deepClone(el, new Map(), new WeakMap(), new Map(), false)).toThrow('fail'); |
| 94 | + }); |
| 95 | + it('clones attributes and children', () => { |
| 96 | + const el = document.createElement('div'); |
| 97 | + el.setAttribute('data-test', '1'); |
| 98 | + const result = deepClone(el, new Map(), new WeakMap(), new Map(), false); |
| 99 | + expect(result.getAttribute('data-test')).toBe('1'); |
| 100 | + }); |
| 101 | +}); |
0 commit comments