Skip to content

Commit 346528c

Browse files
committed
Don't consider strings as primitive
1 parent ee56a1c commit 346528c

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
@@ -21,12 +21,10 @@ describe('Json Component', () => {
2121
})
2222

2323
it.for([
24-
['foo', 'bar'],
2524
[],
2625
[1, 2, 3],
27-
[1, 'foo', null],
2826
Array.from({ length: 101 }, (_, i) => i),
29-
])('collapses any array', (array) => {
27+
])('collapses any array with primitives', (array) => {
3028
const { getByRole } = render(<Json json={array} />)
3129
expect(getByRole('treeitem').ariaExpanded).toBe('false')
3230
})
@@ -67,14 +65,22 @@ describe('Json Component', () => {
6765
it.for([
6866
[1, 'foo', [1, 2, 3]],
6967
[1, 'foo', { nested: true }],
70-
])('expands short arrays with non-primitive values', (arr) => {
68+
])('expands short arrays with inner arrays or objects', (arr) => {
7169
const { getAllByRole } = render(<Json json={arr} />)
7270
const treeItems = getAllByRole('treeitem')
7371
expect(treeItems.length).toBe(2)
7472
expect(treeItems[0]?.getAttribute('aria-expanded')).toBe('true') // the root
7573
expect(treeItems[1]?.getAttribute('aria-expanded')).toBe('false') // the non-primitive value (object/array)
7674
})
7775

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