import { describe, expect, it, beforeAll } from 'vitest' import { toDom, applyValue } from '../to-dom'; import { createElement } from '../create-element'; import { spec } from './helpers'; describe( 'recordToDom', () => { spec.forEach( ( { description, record, startPath, endPath } ) => { // eslint-disable-next-line jest/valid-title it( description, () => { const { body, selection } = toDom( { value: record, } ); expect( body ).toMatchSnapshot(); expect( selection ).toEqual( { startPath, endPath } ); } ); } ); } ); describe( 'applyValue', () => { const cases = [ { current: 'test', future: '', movedCount: 0, description: 'should remove nodes', }, { current: '', future: 'test', movedCount: 1, description: 'should add nodes', }, { current: 'test', future: 'test', movedCount: 0, description: 'should not modify', }, { current: 'b', future: 'b', movedCount: 0, description: 'should remove attribute', }, { current: 'b', future: 'b', movedCount: 0, description: 'should remove attributes', }, { current: 'a', future: 'c', movedCount: 0, description: 'should add attribute', }, { current: 'a', future: 'c', movedCount: 0, description: 'should add attributes', }, { current: 'a', future: 'a', movedCount: 0, description: 'should update attribute', }, { current: 'a', future: 'a', movedCount: 0, description: 'should update attributes', }, ]; cases.forEach( ( { current, future, description, movedCount } ) => { // eslint-disable-next-line jest/valid-title it( description, () => { const body = createElement( document, current ).cloneNode( true ); const futureBody = createElement( document, future ).cloneNode( true ); const childNodes = Array.from( futureBody.childNodes ); applyValue( futureBody, body ); const count = childNodes.reduce( ( acc, { parentNode } ) => { return parentNode === body ? acc + 1 : acc; }, 0 ); expect( body.innerHTML ).toEqual( future ); expect( count ).toEqual( movedCount ); } ); } ); } );