/* eslint-env jest */ import { headingsKey, readHeadings, type EditorBlock } from '../headings'; const block = ( name: string, attributes: Record, innerBlocks: EditorBlock[] = [] ): EditorBlock => ({ name, attributes, innerBlocks }); describe('the headings of the table of contents', () => { const blocks = [ block('core/heading', { content: 'Why cotton', level: 2 }), block('core/paragraph', { content: 'Soft.' }), block('core/group', {}, [ block('core/heading', { content: 'Care', level: 3 }), block('core/heading', { content: 'Deep', level: 4 }), ]), block('core/heading', { content: ' ', level: 2 }), ]; it('gives the same key for the same headings, whatever else changed', () => { const edited = [ ...blocks.slice(0, 1), block('core/paragraph', { content: 'Soft and warm.' }), ...blocks.slice(2), ]; expect(headingsKey(edited)).toBe(headingsKey(blocks)); expect( headingsKey([block('core/heading', { content: 'New', level: 2 })]) ).not.toBe(headingsKey(blocks)); }); it('reads the headings back down to the deepest level asked', () => { expect(readHeadings(headingsKey(blocks), 3)).toEqual([ { text: 'Why cotton', level: 2 }, { text: 'Care', level: 3 }, ]); }); });