From be4d907efb4e2fa658baa7c9b276ed282eb920db Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 14 Nov 2023 19:07:14 +0100 Subject: refactor(components, hooks): rewrite ToC and useHeadingsTree * replace TableOfContents component with TocWidget to keep the name of widget components coherent * replace `wrapper` prop with `tree` prop (the component no longer uses the hook, it is up to the consumer to provide the headings tree) * let consumer handle the widget title * add options to useHeadingsTree hook to retrieve only the wanted headings (and do not assume that h1 is unwanted) * expect an ref object instead of an element in useHeadingsTree hook * rename most of the types involved --- .../use-headings-tree/use-headings-tree.test.ts | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/utils/hooks/use-headings-tree/use-headings-tree.test.ts (limited to 'src/utils/hooks/use-headings-tree/use-headings-tree.test.ts') diff --git a/src/utils/hooks/use-headings-tree/use-headings-tree.test.ts b/src/utils/hooks/use-headings-tree/use-headings-tree.test.ts new file mode 100644 index 0000000..ad30a4f --- /dev/null +++ b/src/utils/hooks/use-headings-tree/use-headings-tree.test.ts @@ -0,0 +1,79 @@ +import { describe, expect, it } from '@jest/globals'; +import { renderHook } from '@testing-library/react'; +import { useHeadingsTree } from './use-headings-tree'; + +const labels = { + h1: 'Title 1', + firstH2: 'First subtitle', + secondH2: 'Second subtitle', +}; + +describe('useHeadingsTree', () => { + it('returns a ref object and the headings tree', () => { + const wrapper = document.createElement('div'); + + wrapper.innerHTML = ` +

${labels.h1}

+

${labels.firstH2}

+

Expedita et necessitatibus qui numquam sunt et ut et. Earum nostrum esse nemo nisi qui. Ab in iure qui repellat voluptatibus nostrum odit aut qui. Architecto eum fugit quod excepturi numquam qui maxime accusantium. Fugit ipsam harum tempora.

+

${labels.secondH2}

+

Totam cumque aut ipsum. Necessitatibus magnam necessitatibus. Qui illo nulla non ab. Accusamus voluptatem ab fugiat voluptas aspernatur velit dolore reprehenderit. Voluptatem quod minima asperiores voluptatum distinctio cumque quo.

`; + + const wrapperRef = { current: wrapper }; + const { result } = renderHook(() => useHeadingsTree(wrapperRef)); + + expect(result.current.length).toBe(1); + expect(result.current[0].label).toBe(labels.h1); + expect(result.current[0].children.length).toBe(2); + }); + + it('can return a headings tree starting at the specified level', () => { + const wrapper = document.createElement('div'); + + wrapper.innerHTML = ` +

${labels.h1}

+

${labels.firstH2}

+

Expedita et necessitatibus qui numquam sunt et ut et. Earum nostrum esse nemo nisi qui. Ab in iure qui repellat voluptatibus nostrum odit aut qui. Architecto eum fugit quod excepturi numquam qui maxime accusantium. Fugit ipsam harum tempora.

+

${labels.secondH2}

+

Totam cumque aut ipsum. Necessitatibus magnam necessitatibus. Qui illo nulla non ab. Accusamus voluptatem ab fugiat voluptas aspernatur velit dolore reprehenderit. Voluptatem quod minima asperiores voluptatum distinctio cumque quo.

`; + + const wrapperRef = { current: wrapper }; + const { result } = renderHook(() => + useHeadingsTree(wrapperRef, { fromLevel: 2 }) + ); + + expect(result.current.length).toBe(2); + expect(result.current[0].label).toBe(labels.firstH2); + expect(result.current[1].label).toBe(labels.secondH2); + }); + + it('can return a headings tree stopping at the specified level', () => { + const wrapper = document.createElement('div'); + + wrapper.innerHTML = ` +

${labels.h1}

+

${labels.firstH2}

+

Expedita et necessitatibus qui numquam sunt et ut et. Earum nostrum esse nemo nisi qui. Ab in iure qui repellat voluptatibus nostrum odit aut qui. Architecto eum fugit quod excepturi numquam qui maxime accusantium. Fugit ipsam harum tempora.

+

${labels.secondH2}

+

Totam cumque aut ipsum. Necessitatibus magnam necessitatibus. Qui illo nulla non ab. Accusamus voluptatem ab fugiat voluptas aspernatur velit dolore reprehenderit. Voluptatem quod minima asperiores voluptatum distinctio cumque quo.

`; + + const wrapperRef = { current: wrapper }; + const { result } = renderHook(() => + useHeadingsTree(wrapperRef, { toLevel: 1 }) + ); + + expect(result.current.length).toBe(1); + expect(result.current[0].label).toBe(labels.h1); + expect(result.current[0].children).toStrictEqual([]); + }); + + it('throws an error if the options are invalid', () => { + const wrapperRef = { current: null }; + + expect(() => + useHeadingsTree(wrapperRef, { fromLevel: 2, toLevel: 1 }) + ).toThrowError( + 'Invalid options: `fromLevel` must be lower or equal to `toLevel`.' + ); + }); +}); -- cgit v1.2.3