Skip to content

Commit

Permalink
Tests for getNavigationTree (#110)
Browse files Browse the repository at this point in the history
1. Rewrote the test for `parseComponentPath`
2. Added test for `getNavigationTree`
3. renamed `get-nav-tree.ts` to `get-navigation-tree.ts` 

Co-authored-by: Preston Bourne <[email protected]>
  • Loading branch information
prestonbourne and prestonbourne authored Jun 21, 2023
1 parent 39d7737 commit 7d3935f
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
"prerelease:canary": "turbo build",
"release": "changeset publish",
"release:canary": "changeset publish --tag canary",
"test": "jest"
"test": "npx turbo run test --concurrency 1 --continue"
}
}
101 changes: 101 additions & 0 deletions packages/swingset/__tests__/get-navigation-tree.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { describe, it } from 'vitest'
import { getNavigationTree } from '../src/get-navigation-tree'
import { ComponentEntity, NavigationTree } from '../src/types'

describe(getNavigationTree.name, () => {
it('Builds the Navigation Tree', ({ expect }) => {
const input: ComponentEntity[] = [
{
__type: 'component',
category: 'default',
componentPath: '../../..',
filepath: '../../..',
frontmatter: {},
normalizedPath: '',
relativePath: '',
slug: '',
title: '',
load: '',
},
]

const expectation: NavigationTree = [
{
children: [
{
__type: 'component',
componentPath: '../../..',
slug: '',
title: '',
},
],
title: 'default',
__type: 'category',
},
]

const result = getNavigationTree(input)

expect(result).toEqual(expectation)
})
it('Supports duplicate entities', ({ expect }) => {
const input: ComponentEntity[] = [
{
__type: 'component',
category: 'default',
componentPath: '../../..',
filepath: '../../..',
frontmatter: {},
normalizedPath: '',
relativePath: '',
slug: '',
title: '',
load: '',
},
{
__type: 'component',
category: 'default',
componentPath: '../../..',
filepath: '../../..',
frontmatter: {},
normalizedPath: '',
relativePath: '',
slug: '',
title: '',
load: '',
},
]

const expectation: NavigationTree = [
{
children: [
{
__type: 'component',
componentPath: '../../..',
slug: '',
title: '',
},
{
__type: 'component',
componentPath: '../../..',
slug: '',
title: '',
},
],
title: 'default',
__type: 'category',
},
]

const result = getNavigationTree(input)

expect(result).toEqual(expectation)
})
it('Returns an empty array if it receives one', ({ expect }) => {
const input: ComponentEntity[] = []
const expectation: NavigationTree = []
const result = getNavigationTree(input)

expect(result).toEqual(expectation)
})
})
7 changes: 0 additions & 7 deletions packages/swingset/__tests__/index.test.ts

This file was deleted.

21 changes: 13 additions & 8 deletions packages/swingset/__tests__/parse-component-path.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, it } from 'vitest'
import { parseComponentPath } from '../src/parse-component-path'

describe('parseComponentPath', () => {
it('converts the front matter string to an object', ({ expect }) => {
describe(parseComponentPath.name, () => {
it('Converts a 3 segment path to a navigationData object', ({ expect }) => {
const expectation = {
category: 'Components',
folder: 'Forms',
Expand All @@ -13,7 +13,7 @@ describe('parseComponentPath', () => {

expect(result).toEqual(expectation)
})
it('converts the front matter string to an object', ({ expect }) => {
it('Converts a 2 segment path to a navigationData object', ({ expect }) => {
const expectation = {
category: 'Components',
page: 'Button',
Expand All @@ -23,19 +23,24 @@ describe('parseComponentPath', () => {

expect(result).toEqual(expectation)
})
it('converts the front matter string to an object', ({ expect }) => {
it('Converts a 1 segment path to a navigationData object', ({ expect }) => {
const expectation = {
category: 'default',
page: 'Button',
}

const result = parseComponentPath('Button')

expect(result).toEqual(expectation)
})
it('Throws an error when invalid input is received', ({expect}) => {
it('Throws an error when too many segments are received', ({ expect }) => {
const input = 'edibles/fruits/berries/blueberries'

expect(() => parseComponentPath('edibles/fruits/berries/blueberries')).toThrowError()


const errorSnapshot =
"\"Received Component path with more than 3 segments: 'edibles/fruits/berries/blueberries'. Remove the extra segments. Expected format: '[Category]/[Folder]/[Page]'\""

expect(() => parseComponentPath(input)).toThrowErrorMatchingInlineSnapshot(
errorSnapshot
)
})
})
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/swingset/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { matter } from 'vfile-matter'
import { type LoaderContext } from 'webpack'
import { resolveComponents } from './resolvers/component.js'
import { stringifyEntity } from './resolvers/stringify-entity.js'
import { getNavigationTree } from './get-nav-tree.js'
import { getNavigationTree } from './get-navigation-tree.js'
import { resolveDocs } from './resolvers/doc.js'
import { NEXT_MDX_COMPONENTS_ALIAS } from './constants.js'
import { type SwingsetConfig } from './config.js'
Expand Down

0 comments on commit 7d3935f

Please sign in to comment.