schlechtenburg/packages/rich-text/lib/test/to-dom.test.ts
2024-10-09 16:47:31 +02:00

106 lines
2.8 KiB
TypeScript

import { describe, expect, it, beforeAll } from 'vitest'
import { mount } from '@vue/test-utils';
import { defineComponent, ComponentInstance, Component } from 'vue'
import { useFormatTypes } from '../use-format-types';
import { toDom, applyValue } from '../to-dom';
import { createElement } from '../create-element';
import { spec } from './helpers';
describe( 'recordToDom', () => {
let TestComponent: Component;
let wrapper: ComponentInstance<typeof TestComponent>;
beforeAll(async () => {
TestComponent = defineComponent({
setup () {
return useFormatTypes();
}
});
wrapper = mount(TestComponent);
});
spec.forEach( ( { description, record, startPath, endPath } ) => {
// eslint-disable-next-line jest/valid-title
it( description, () => {
const { body, selection } = toDom({ value: record }, wrapper.componentVM);
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: '<span data-1="">b</span>',
future: '<span>b</span>',
movedCount: 0,
description: 'should remove attribute',
},
{
current: '<span data-1="" data-2="">b</span>',
future: '<span>b</span>',
movedCount: 0,
description: 'should remove attributes',
},
{
current: '<span>a</span>',
future: '<span data-1="">c</span>',
movedCount: 0,
description: 'should add attribute',
},
{
current: '<span>a</span>',
future: '<span data-1="" data-2="">c</span>',
movedCount: 0,
description: 'should add attributes',
},
{
current: '<span data-1="i">a</span>',
future: '<span data-1="ii">a</span>',
movedCount: 0,
description: 'should update attribute',
},
{
current: '<span data-1="i" data-2="ii">a</span>',
future: '<span data-1="ii" data-2="i">a</span>',
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 );
} );
} );
} );