this repo has no description
1import assert from 'node:assert/strict'
2import { describe, test } from 'node:test'
3import { treeify } from './index.js'
4import type { TreeInput } from './index.js'
5
6describe('treeify types', () => {
7 test('input values', () => {
8 const inputWithValues: TreeInput = ['root', ['child1', 'child2', 'child3']]
9 let result = treeify(inputWithValues)
10 assert.ok(result)
11
12 const emptyInput: TreeInput = []
13 result = treeify(emptyInput)
14 assert.equal(result, '') // empty string
15
16 const inputBuildUp: TreeInput = []
17 inputBuildUp.push('root')
18 inputBuildUp.push(['child1', 'child2', 'child3'])
19 inputBuildUp.push('root2', ['cousin1', 'cousin2', 'cousin3'])
20 result = treeify(inputBuildUp)
21 assert.ok(result)
22
23 // @ts-expect-error
24 const inputWithoutRootString: TreeInput = [{ bad: 'root' }, 'root2']
25 assert.throws(() => treeify(inputWithoutRootString), {
26 message: 'First element must be a string',
27 })
28
29 const inputWithRootStringAndInvalidValues: TreeInput = ['root']
30 // @ts-expect-error
31 inputWithRootStringAndInvalidValues.push(1)
32 // @ts-expect-error
33 inputWithRootStringAndInvalidValues.push({}, [], Number.POSITIVE_INFINITY)
34 result = treeify(inputWithRootStringAndInvalidValues)
35 assert.ok(result) // non-strings are ignored
36 })
37
38 test('generated inputs', () => {
39 function generateInput(): TreeInput {
40 const out: TreeInput = []
41 out.push('root')
42 out.push(['child1', 'child2', 'child3'])
43 return out
44 }
45
46 const input: TreeInput = ['root', generateInput()]
47 input.push(generateInput())
48
49 assert.ok(treeify(input))
50 })
51})