diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-11-17 17:27:54 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-18 19:30:47 +0100 |
| commit | 6ab9635a22d69186c8a24181ad5df7736e288577 (patch) | |
| tree | 855c74af9a336d9130c40f07fa8d51acb42dc701 /src/utils/hooks/use-headings-tree/use-headings-tree.test.ts | |
| parent | dcd2a7ab382fece8e0ae2979aad4a180b6a105e1 (diff) | |
fix: generate an id for each headings in the page main contents
Since #be4d907 the ids was no longer addded to headings in
useHeadingsTree hook. It was a bad practice to manipulate the DOM
that way. However, I did not move the implementation elsewhere...
To fix this, I now use rehype-slug on both markdown contents and
html string coming from WordPress.
I'm not sure the dynamic imports are really useful here since the
table of contents is on almost all pages but Jest was failing with
regular import because of ESM. It is the only thing that makes the
tests functional again so... However if we want to test the
`updateContentTree` function, Jest fails for the same reason. So I
decided to not test this function. I've already spend too much time
on this issue.
Another problem: the ToC on projects page. Currently we use the ref
on the body but the page contents are imported dynamically so the
hook is executed before the contents are loaded. It makes the ToC
empty... We should refactor the pages so we can use the ref
directly on the imported contents.
Diffstat (limited to 'src/utils/hooks/use-headings-tree/use-headings-tree.test.ts')
| -rw-r--r-- | src/utils/hooks/use-headings-tree/use-headings-tree.test.ts | 47 |
1 files changed, 21 insertions, 26 deletions
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 index ad30a4f..2c8ff2d 100644 --- a/src/utils/hooks/use-headings-tree/use-headings-tree.test.ts +++ b/src/utils/hooks/use-headings-tree/use-headings-tree.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from '@jest/globals'; -import { renderHook } from '@testing-library/react'; +import { act, renderHook } from '@testing-library/react'; import { useHeadingsTree } from './use-headings-tree'; const labels = { @@ -9,7 +9,7 @@ const labels = { }; describe('useHeadingsTree', () => { - it('returns a ref object and the headings tree', () => { + it('returns a ref callback and the headings tree', () => { const wrapper = document.createElement('div'); wrapper.innerHTML = ` @@ -19,12 +19,13 @@ describe('useHeadingsTree', () => { <h2>${labels.secondH2}</h2> <p>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.</p>`; - const wrapperRef = { current: wrapper }; - const { result } = renderHook(() => useHeadingsTree(wrapperRef)); + const { result } = renderHook(() => useHeadingsTree()); - expect(result.current.length).toBe(1); - expect(result.current[0].label).toBe(labels.h1); - expect(result.current[0].children.length).toBe(2); + act(() => result.current.ref(wrapper)); + + expect(result.current.tree.length).toBe(1); + expect(result.current.tree[0].label).toBe(labels.h1); + expect(result.current.tree[0].children.length).toBe(2); }); it('can return a headings tree starting at the specified level', () => { @@ -37,14 +38,13 @@ describe('useHeadingsTree', () => { <h2>${labels.secondH2}</h2> <p>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.</p>`; - const wrapperRef = { current: wrapper }; - const { result } = renderHook(() => - useHeadingsTree(wrapperRef, { fromLevel: 2 }) - ); + const { result } = renderHook(() => useHeadingsTree({ fromLevel: 2 })); + + act(() => result.current.ref(wrapper)); - expect(result.current.length).toBe(2); - expect(result.current[0].label).toBe(labels.firstH2); - expect(result.current[1].label).toBe(labels.secondH2); + expect(result.current.tree.length).toBe(2); + expect(result.current.tree[0].label).toBe(labels.firstH2); + expect(result.current.tree[1].label).toBe(labels.secondH2); }); it('can return a headings tree stopping at the specified level', () => { @@ -57,22 +57,17 @@ describe('useHeadingsTree', () => { <h2>${labels.secondH2}</h2> <p>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.</p>`; - const wrapperRef = { current: wrapper }; - const { result } = renderHook(() => - useHeadingsTree(wrapperRef, { toLevel: 1 }) - ); + const { result } = renderHook(() => useHeadingsTree({ toLevel: 1 })); + + act(() => result.current.ref(wrapper)); - expect(result.current.length).toBe(1); - expect(result.current[0].label).toBe(labels.h1); - expect(result.current[0].children).toStrictEqual([]); + expect(result.current.tree.length).toBe(1); + expect(result.current.tree[0].label).toBe(labels.h1); + expect(result.current.tree[0].children).toStrictEqual([]); }); it('throws an error if the options are invalid', () => { - const wrapperRef = { current: null }; - - expect(() => - useHeadingsTree(wrapperRef, { fromLevel: 2, toLevel: 1 }) - ).toThrowError( + expect(() => useHeadingsTree({ fromLevel: 2, toLevel: 1 })).toThrowError( 'Invalid options: `fromLevel` must be lower or equal to `toLevel`.' ); }); |
