|
| 1 | +/** |
| 2 | + * @vitest-environment happy-dom |
| 3 | + */ |
| 4 | +import { vi, MockInstance } from 'vitest'; |
| 5 | +import { |
| 6 | + NodeType as RRNodeType, |
| 7 | + createMirror, |
| 8 | + Mirror as NodeMirror, |
| 9 | + serializedNodeWithId, |
| 10 | +} from 'rrweb-snapshot'; |
| 11 | +import { RRDocument } from '../../src'; |
| 12 | +import { diff, ReplayerHandler } from '../../src/diff'; |
| 13 | + |
| 14 | +describe('diff algorithm for rrdom', () => { |
| 15 | + let mirror: NodeMirror; |
| 16 | + let replayer: ReplayerHandler; |
| 17 | + let warn: MockInstance; |
| 18 | + let elementSn: serializedNodeWithId; |
| 19 | + let elementSn2: serializedNodeWithId; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + mirror = createMirror(); |
| 23 | + replayer = { |
| 24 | + mirror, |
| 25 | + applyCanvas: () => {}, |
| 26 | + applyInput: () => {}, |
| 27 | + applyScroll: () => {}, |
| 28 | + applyStyleSheetMutation: () => {}, |
| 29 | + afterAppend: () => {}, |
| 30 | + }; |
| 31 | + document.write('<!DOCTYPE html><html><head></head><body></body></html>'); |
| 32 | + // Mock the original console.warn function to make the test fail once console.warn is called. |
| 33 | + warn = vi.spyOn(console, 'warn'); |
| 34 | + |
| 35 | + elementSn = { |
| 36 | + type: RRNodeType.Element, |
| 37 | + tagName: 'DIALOG', |
| 38 | + attributes: {}, |
| 39 | + childNodes: [], |
| 40 | + id: 1, |
| 41 | + }; |
| 42 | + |
| 43 | + elementSn2 = { |
| 44 | + ...elementSn, |
| 45 | + attributes: {}, |
| 46 | + }; |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + // Check that warn was not called (fail on warning) |
| 51 | + expect(warn).not.toBeCalled(); |
| 52 | + vi.resetAllMocks(); |
| 53 | + }); |
| 54 | + describe('diff dialog elements', () => { |
| 55 | + vi.setConfig({ testTimeout: 60_000 }); |
| 56 | + |
| 57 | + it('should trigger `showModal` on rr_open_mode:modal attributes', () => { |
| 58 | + const tagName = 'DIALOG'; |
| 59 | + const node = document.createElement(tagName) as HTMLDialogElement; |
| 60 | + vi.spyOn(node, 'matches').mockReturnValue(false); // matches is used to check if the dialog was opened with showModal |
| 61 | + const showModalFn = vi.spyOn(node, 'showModal'); |
| 62 | + |
| 63 | + const rrDocument = new RRDocument(); |
| 64 | + const rrNode = rrDocument.createElement(tagName); |
| 65 | + rrNode.attributes = { rr_open_mode: 'modal', open: '' }; |
| 66 | + |
| 67 | + mirror.add(node, elementSn); |
| 68 | + rrDocument.mirror.add(rrNode, elementSn); |
| 69 | + diff(node, rrNode, replayer); |
| 70 | + |
| 71 | + expect(showModalFn).toBeCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should trigger `close` on rr_open_mode removed', () => { |
| 75 | + const tagName = 'DIALOG'; |
| 76 | + const node = document.createElement(tagName) as HTMLDialogElement; |
| 77 | + node.showModal(); |
| 78 | + vi.spyOn(node, 'matches').mockReturnValue(true); // matches is used to check if the dialog was opened with showModal |
| 79 | + const closeFn = vi.spyOn(node, 'close'); |
| 80 | + |
| 81 | + const rrDocument = new RRDocument(); |
| 82 | + const rrNode = rrDocument.createElement(tagName); |
| 83 | + rrNode.attributes = {}; |
| 84 | + |
| 85 | + mirror.add(node, elementSn); |
| 86 | + rrDocument.mirror.add(rrNode, elementSn); |
| 87 | + diff(node, rrNode, replayer); |
| 88 | + |
| 89 | + expect(closeFn).toBeCalled(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should not trigger `close` on rr_open_mode is kept', () => { |
| 93 | + const tagName = 'DIALOG'; |
| 94 | + const node = document.createElement(tagName) as HTMLDialogElement; |
| 95 | + vi.spyOn(node, 'matches').mockReturnValue(true); // matches is used to check if the dialog was opened with showModal |
| 96 | + node.setAttribute('rr_open_mode', 'modal'); |
| 97 | + node.setAttribute('open', ''); |
| 98 | + const closeFn = vi.spyOn(node, 'close'); |
| 99 | + |
| 100 | + const rrDocument = new RRDocument(); |
| 101 | + const rrNode = rrDocument.createElement(tagName); |
| 102 | + rrNode.attributes = { rr_open_mode: 'modal', open: '' }; |
| 103 | + |
| 104 | + mirror.add(node, elementSn); |
| 105 | + rrDocument.mirror.add(rrNode, elementSn); |
| 106 | + diff(node, rrNode, replayer); |
| 107 | + |
| 108 | + expect(closeFn).not.toBeCalled(); |
| 109 | + expect(node.open).toBe(true); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments