Skip to content

Commit 68caa89

Browse files
committed
Don't consider strings as primitive
1 parent 779314b commit 68caa89

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/components/Json/Json.test.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ describe('Json Component', () => {
2222
})
2323

2424
it.for([
25-
['foo', 'bar'],
2625
[],
2726
[1, 2, 3],
28-
[1, 'foo', null],
2927
Array.from({ length: 101 }, (_, i) => i),
30-
])('collapses any array', (array) => {
28+
])('collapses any array with primitives', (array) => {
3129
const { getByRole } = render(<Json json={array} />)
3230
expect(getByRole('treeitem').ariaExpanded).toBe('false')
3331
})
@@ -68,14 +66,22 @@ describe('Json Component', () => {
6866
it.for([
6967
[1, 'foo', [1, 2, 3]],
7068
[1, 'foo', { nested: true }],
71-
])('expands short arrays with non-primitive values', (arr) => {
69+
])('expands short arrays with inner arrays or objects', (arr) => {
7270
const { getAllByRole } = render(<Json json={arr} />)
7371
const treeItems = getAllByRole('treeitem')
7472
expect(treeItems.length).toBe(2)
7573
expect(treeItems[0]?.getAttribute('aria-expanded')).toBe('true') // the root
7674
expect(treeItems[1]?.getAttribute('aria-expanded')).toBe('false') // the non-primitive value (object/array)
7775
})
7876

77+
it.for([
78+
['foo', 'bar'],
79+
[1, 'foo', null],
80+
])('expands short arrays with strings', (arr) => {
81+
const { getByRole } = render(<Json json={arr} />)
82+
expect(getByRole('treeitem').getAttribute('aria-expanded')).toBe('true')
83+
})
84+
7985
it.for([
8086
{ obj: [314, null] },
8187
{ obj: { nested: true } },

src/components/Json/helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export function isPrimitive(value: unknown): boolean {
44
value === null ||
55
!Array.isArray(value) &&
66
typeof value !== 'object' &&
7-
typeof value !== 'function'
7+
typeof value !== 'function' &&
8+
typeof value !== 'string' // exception: don't consider strings as primitive here
89
)
910
}
1011

0 commit comments

Comments
 (0)