-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.tsx
110 lines (90 loc) · 2.36 KB
/
index.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React from 'react'
import { render } from 'ink-testing-library'
import { test, expect } from 'vitest'
import timers from 'node:timers/promises'
import FilterList from './src/index.jsx'
import { Box, Text } from 'ink'
const ARROW_UP = '\u001B[A'
const ARROW_DOWN = '\u001B[B'
const BACKSPACE = '\u007F'
const input = async (write: (data: string) => void, input: string) => {
await timers.setTimeout(100)
write(input)
}
test('Arrow keys and filtering', async () => {
const { lastFrame, stdin } = render(
<FilterList
items={[
{
label: 'First',
value: { id: 'first' },
},
{
label: 'Second',
value: { id: 'second' },
},
{
label: 'Third',
value: { id: 'third' },
},
{
label: 'Fourth',
value: { id: 'fourth' },
},
{
label: 'Fifth',
value: { id: 'fifth' },
},
]}
/>,
)
expect(lastFrame()).toMatchSnapshot()
await input(stdin.write, ARROW_DOWN)
expect(lastFrame()).toMatchSnapshot()
await input(stdin.write, ARROW_DOWN)
expect(lastFrame()).toMatchSnapshot()
await input(stdin.write, ARROW_UP)
expect(lastFrame()).toMatchSnapshot()
await input(stdin.write, 'second')
expect(lastFrame()).toMatchSnapshot()
// Clear the input
await input(stdin.write, BACKSPACE)
await input(stdin.write, BACKSPACE)
await input(stdin.write, BACKSPACE)
await input(stdin.write, BACKSPACE)
await input(stdin.write, BACKSPACE)
await input(stdin.write, BACKSPACE)
await input(stdin.write, 'does not exist')
expect(lastFrame()).toMatchSnapshot()
})
test('renderItem', () => {
const { lastFrame } = render(
<FilterList
height={10}
items={[
{
label: 'First item',
value: { id: 'first' },
},
{
label: 'Second item',
value: { id: 'second' },
},
{
label: 'Third item',
value: { id: 'third' },
},
]}
renderItem={(item, isSelected) => {
const color = isSelected ? 'blue' : 'white'
return (
<Box flexDirection={'column'}>
<Text color={color}>{item.value.id}</Text>
<Text color={color}>{item.label}</Text>
</Box>
)
}}
/>,
)
expect(lastFrame()).toMatchSnapshot()
})