Skip to content

Commit

Permalink
refactor: rename buildMenu to buildMenuSection
Browse files Browse the repository at this point in the history
  • Loading branch information
isqua committed Oct 24, 2023
1 parent d11b242 commit c422640
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`toc/buildMenu > should build a menu and highlight current page 1`] = `
exports[`toc/buildMenuSection > should build a menu and highlight current page 1`] = `
[
{
"defaultOpenState": false,
Expand Down Expand Up @@ -35,7 +35,7 @@ exports[`toc/buildMenu > should build a menu and highlight current page 1`] = `
]
`;

exports[`toc/buildMenu > should build a menu even if there are no current page 1`] = `
exports[`toc/buildMenuSection > should build a menu even if there are no current page 1`] = `
[
{
"defaultOpenState": false,
Expand Down Expand Up @@ -70,7 +70,7 @@ exports[`toc/buildMenu > should build a menu even if there are no current page 1
]
`;

exports[`toc/buildMenu > should build a nested menu 1`] = `
exports[`toc/buildMenuSection > should build a nested menu 1`] = `
[
{
"defaultOpenState": false,
Expand All @@ -95,9 +95,9 @@ exports[`toc/buildMenu > should build a nested menu 1`] = `
]
`;

exports[`toc/buildMenu > should build an empty menu if there are no top level ids 1`] = `[]`;
exports[`toc/buildMenuSection > should build an empty menu if there are no top level ids 1`] = `[]`;

exports[`toc/buildMenu > should show page children 1`] = `
exports[`toc/buildMenuSection > should show page children 1`] = `
[
{
"defaultOpenState": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { describe, expect, it } from 'vitest'

import type { TableOfContent } from '../types'
import { buildMenu } from './buildMenu'
import { buildMenuSection } from './buildMenuSection'
import { getBreadCrumbs } from './getBreadCrumbs'

import tocFlat from '../../../test/fixtures/toc/flat.json'
import tocTwoLevels from '../../../test/fixtures/toc/two-levels.json'

describe('toc/buildMenu', () => {
describe('toc/buildMenuSection', () => {
it('should build a menu and highlight current page', () => {
const toc: TableOfContent = tocFlat
const currentUrl = 'bar.html'
const breadcrumbs = getBreadCrumbs(toc, currentUrl)
const menu = buildMenu(toc, { url: currentUrl, breadcrumbs })
const menu = buildMenuSection(toc, { url: currentUrl, breadcrumbs })

expect(menu).toMatchSnapshot()
})
Expand All @@ -21,7 +21,7 @@ describe('toc/buildMenu', () => {
const toc: TableOfContent = tocFlat
const currentUrl = ''
const breadcrumbs = getBreadCrumbs(toc, currentUrl)
const menu = buildMenu(toc, { url: currentUrl, breadcrumbs })
const menu = buildMenuSection(toc, { url: currentUrl, breadcrumbs })

expect(menu).toMatchSnapshot()
})
Expand All @@ -31,7 +31,7 @@ describe('toc/buildMenu', () => {
const currentUrl = ''
const parentId = 'this-id-does-not-exist'
const breadcrumbs = getBreadCrumbs(toc, currentUrl)
const menu = buildMenu(toc, { url: currentUrl, breadcrumbs, parentId })
const menu = buildMenuSection(toc, { url: currentUrl, breadcrumbs, parentId })

expect(menu).toEqual([])
})
Expand All @@ -43,7 +43,7 @@ describe('toc/buildMenu', () => {
}
const currentUrl = ''
const breadcrumbs = getBreadCrumbs(toc, currentUrl)
const menu = buildMenu(toc, { url: currentUrl, breadcrumbs })
const menu = buildMenuSection(toc, { url: currentUrl, breadcrumbs })

expect(menu).toMatchSnapshot()
})
Expand All @@ -52,7 +52,7 @@ describe('toc/buildMenu', () => {
const toc: TableOfContent = tocTwoLevels
const currentUrl = 'bar.html'
const breadcrumbs = getBreadCrumbs(toc, currentUrl)
const menu = buildMenu(toc, { url: currentUrl, breadcrumbs })
const menu = buildMenuSection(toc, { url: currentUrl, breadcrumbs })

expect(menu).toMatchSnapshot()
})
Expand All @@ -61,7 +61,7 @@ describe('toc/buildMenu', () => {
const toc: TableOfContent = tocTwoLevels
const currentUrl = 'bar-features.html'
const breadcrumbs = getBreadCrumbs(toc, currentUrl)
const menu = buildMenu(toc, { url: currentUrl, parentId: 'bar', breadcrumbs })
const menu = buildMenuSection(toc, { url: currentUrl, parentId: 'bar', breadcrumbs })

expect(menu).toMatchSnapshot()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const getPagesForParent = (toc: TableOfContent, parentId?: PageId): PageId[] =>
return toc.entities.pages[parentId]?.pages ?? []
}

export const buildMenu = (toc: TableOfContent, options: BuildMenuOptions): MenuItem[] => {
export const buildMenuSection = (toc: TableOfContent, options: BuildMenuOptions): MenuItem[] => {
const {
url,
parentId,
Expand Down
2 changes: 1 addition & 1 deletion src/features/toc/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { useGetTocQuery } from './api/useGetTocQuery'
export { buildMenu } from './core/buildMenu'
export { buildMenuSection } from './core/buildMenuSection'
export { getBreadCrumbs } from './core/getBreadCrumbs'
export { getCurrentPage } from './core/getCurrentPage'
export * from './types'
Expand Down
6 changes: 3 additions & 3 deletions src/features/toc/ui/Menu/Context/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useContext } from 'react'

import { buildMenu } from '../../../core/buildMenu'
import { buildMenuSection } from '../../../core/buildMenuSection'
import type { PageId, SectionHighlight } from '../../../types'
import { LocationContext, TocContext } from './contexts'

export const useMenuItems = (parentId: PageId = '', level: number = 0, highlight: SectionHighlight) => {
export const useSectionItems = (parentId: PageId = '', level: number = 0, highlight: SectionHighlight) => {
const { toc } = useContext(TocContext)
const currentLocation = useContext(LocationContext)

const items = buildMenu(toc, {
const items = buildMenuSection(toc, {
url: currentLocation.url,
breadcrumbs: currentLocation.breadcrumbs,
parentId,
Expand Down
4 changes: 2 additions & 2 deletions src/features/toc/ui/Menu/Section/Section.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PageId, SectionHighlight } from '../../../types'
import { useMenuItems } from '../Context/hooks'
import { useSectionItems } from '../Context/hooks'
import { Item, ItemToggle } from '../Item/Item'

type SectionProps = {
Expand All @@ -9,7 +9,7 @@ type SectionProps = {
}

export function Section({ parentId, level, highlight }: SectionProps): JSX.Element {
const items = useMenuItems(parentId, level, highlight)
const items = useSectionItems(parentId, level, highlight)

return (
<>
Expand Down

0 comments on commit c422640

Please sign in to comment.