schlechtenburg/packages/rich-text/lib/test/slice.test.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-10-08 07:15:26 +00:00
import { describe, expect, it } from 'vitest'
import deepFreeze from 'deep-freeze';
import { slice } from '../slice';
import { getSparseArrayLength } from './helpers';
describe( 'slice', () => {
const em = { type: 'em' };
it( 'should slice', () => {
const record = {
formats: [ , , , , [ em ], [ em ], [ em ], , , , , , , ],
replacements: [ , , , , , , , , , , , , , ],
text: 'one two three',
};
const expected = {
formats: [ , [ em ], [ em ] ],
replacements: [ , , , ],
text: ' tw',
};
const result = slice( deepFreeze( record ), 3, 6 );
expect( result ).toEqual( expected );
expect( result ).not.toBe( record );
expect( getSparseArrayLength( result.formats ) ).toBe( 2 );
} );
it( 'should slice record', () => {
const record = {
formats: [ , , , , [ em ], [ em ], [ em ], , , , , , , ],
replacements: [ , , , , , , , , , , , , , ],
text: 'one two three',
start: 3,
end: 6,
};
const expected = {
formats: [ , [ em ], [ em ] ],
replacements: [ , , , ],
text: ' tw',
};
const result = slice( deepFreeze( record ) );
expect( result ).toEqual( expected );
expect( result ).not.toBe( record );
expect( getSparseArrayLength( result.formats ) ).toBe( 2 );
} );
} );