Skip to content

Commit 3b74ecb

Browse files
jdaltonclaude
andcommitted
test: add unit tests for lang and helpers utilities
Add explicit unit tests for isNullishOrEmptyString and createHelpersNamespaceObject functions that were previously only tested indirectly. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 11f07ab commit 3b74ecb

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

test/helpers.test.mts

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
import { createHelpersNamespaceObject } from '../src/helpers.js'
4+
5+
describe('Helpers utilities', () => {
6+
describe('createHelpersNamespaceObject', () => {
7+
it('should create namespace object with basic helpers', () => {
8+
const helpers = {
9+
helper1: { prop1: 'value1', prop2: 'value2' },
10+
helper2: { prop1: 'value3', prop3: 'value4' },
11+
}
12+
const result = createHelpersNamespaceObject(helpers)
13+
14+
expect(result['prop1']).toEqual({
15+
helper1: 'value1',
16+
helper2: 'value3',
17+
})
18+
expect(result['prop2']).toEqual({ helper1: 'value2' })
19+
expect(result['prop3']).toEqual({ helper2: 'value4' })
20+
})
21+
22+
it('should handle empty helpers object', () => {
23+
const helpers = {}
24+
const result = createHelpersNamespaceObject(helpers)
25+
expect(result).toEqual({})
26+
})
27+
28+
it('should use defaults when property not found', () => {
29+
const helpers = {
30+
helper1: { prop1: 'value1' },
31+
helper2: { prop2: 'value2' },
32+
}
33+
const defaults = { helper1: 'default1', helper2: 'default2' }
34+
const result = createHelpersNamespaceObject(helpers, defaults)
35+
36+
expect(result['prop1']).toEqual({
37+
helper1: 'value1',
38+
helper2: 'default2',
39+
})
40+
expect(result['prop2']).toEqual({
41+
helper1: 'default1',
42+
helper2: 'value2',
43+
})
44+
})
45+
46+
it('should skip undefined defaults', () => {
47+
const helpers = {
48+
helper1: { prop1: 'value1' },
49+
}
50+
const defaults = { helper1: undefined }
51+
const result = createHelpersNamespaceObject(helpers, defaults)
52+
53+
expect(result['prop1']).toEqual({ helper1: 'value1' })
54+
})
55+
56+
it('should sort property names alphabetically by default', () => {
57+
const helpers = {
58+
helper1: { zProp: 'z', aProp: 'a', mProp: 'm' },
59+
}
60+
const result = createHelpersNamespaceObject(helpers)
61+
62+
const keys = Object.keys(result)
63+
expect(keys).toEqual(['aProp', 'mProp', 'zProp'])
64+
})
65+
66+
it('should use custom comparator for sorting', () => {
67+
const helpers = {
68+
helper1: { prop1: 'value1', prop2: 'value2', prop3: 'value3' },
69+
}
70+
// Reverse alphabetical order
71+
const comparator = (a: string, b: string) => b.localeCompare(a)
72+
const result = createHelpersNamespaceObject(helpers, { comparator })
73+
74+
const keys = Object.keys(result)
75+
expect(keys).toEqual(['prop3', 'prop2', 'prop1'])
76+
})
77+
78+
it('should handle helpers with null prototype', () => {
79+
const helper1 = Object.create(null)
80+
helper1.prop1 = 'value1'
81+
82+
const helpers = { helper1 }
83+
const result = createHelpersNamespaceObject(helpers)
84+
85+
expect(result['prop1']).toEqual({ helper1: 'value1' })
86+
})
87+
88+
it('should collect unique property names from all helpers', () => {
89+
const helpers = {
90+
helper1: { prop1: 'v1', prop2: 'v2' },
91+
helper2: { prop2: 'v3', prop3: 'v4' },
92+
helper3: { prop1: 'v5', prop3: 'v6', prop4: 'v7' },
93+
}
94+
const result = createHelpersNamespaceObject(helpers)
95+
96+
expect(Object.keys(result).sort()).toEqual([
97+
'prop1',
98+
'prop2',
99+
'prop3',
100+
'prop4',
101+
])
102+
})
103+
104+
it('should handle helpers with undefined values', () => {
105+
const helpers = {
106+
helper1: { prop1: 'value1', prop2: undefined },
107+
helper2: { prop1: undefined, prop2: 'value2' },
108+
}
109+
const result = createHelpersNamespaceObject(helpers)
110+
111+
expect(result['prop1']).toEqual({ helper1: 'value1' })
112+
expect(result['prop2']).toEqual({ helper2: 'value2' })
113+
})
114+
115+
it('should handle multiple helpers with overlapping properties', () => {
116+
const helpers = {
117+
helper1: { shared: 'value1', unique1: 'u1' },
118+
helper2: { shared: 'value2', unique2: 'u2' },
119+
helper3: { shared: 'value3', unique3: 'u3' },
120+
}
121+
const result = createHelpersNamespaceObject(helpers)
122+
123+
expect(result['shared']).toEqual({
124+
helper1: 'value1',
125+
helper2: 'value2',
126+
helper3: 'value3',
127+
})
128+
expect(result['unique1']).toEqual({ helper1: 'u1' })
129+
expect(result['unique2']).toEqual({ helper2: 'u2' })
130+
expect(result['unique3']).toEqual({ helper3: 'u3' })
131+
})
132+
})
133+
})

test/lang.test.mts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
import { isNullishOrEmptyString } from '../src/lang.js'
4+
5+
describe('Language utilities', () => {
6+
describe('isNullishOrEmptyString', () => {
7+
it('should return true for null', () => {
8+
expect(isNullishOrEmptyString(null)).toBe(true)
9+
})
10+
11+
it('should return true for undefined', () => {
12+
expect(isNullishOrEmptyString(undefined)).toBe(true)
13+
})
14+
15+
it('should return true for empty string', () => {
16+
expect(isNullishOrEmptyString('')).toBe(true)
17+
})
18+
19+
it('should return false for non-empty strings', () => {
20+
expect(isNullishOrEmptyString('test')).toBe(false)
21+
expect(isNullishOrEmptyString(' ')).toBe(false)
22+
expect(isNullishOrEmptyString('0')).toBe(false)
23+
})
24+
25+
it('should return false for numbers', () => {
26+
expect(isNullishOrEmptyString(0)).toBe(false)
27+
expect(isNullishOrEmptyString(123)).toBe(false)
28+
expect(isNullishOrEmptyString(-1)).toBe(false)
29+
})
30+
31+
it('should return false for booleans', () => {
32+
expect(isNullishOrEmptyString(true)).toBe(false)
33+
expect(isNullishOrEmptyString(false)).toBe(false)
34+
})
35+
36+
it('should return false for objects', () => {
37+
expect(isNullishOrEmptyString({})).toBe(false)
38+
expect(isNullishOrEmptyString([])).toBe(false)
39+
expect(isNullishOrEmptyString({ key: 'value' })).toBe(false)
40+
})
41+
42+
it('should return false for functions', () => {
43+
expect(isNullishOrEmptyString(() => {})).toBe(false)
44+
// biome-ignore lint: testing function
45+
expect(isNullishOrEmptyString(function () {})).toBe(false)
46+
})
47+
})
48+
})

0 commit comments

Comments
 (0)