-
-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathuseStoreMap.test.tsx
122 lines (119 loc) · 3.14 KB
/
useStoreMap.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
111
112
113
114
115
116
117
118
119
120
121
122
/* eslint-disable no-unused-vars */
import React from 'react'
import {createStore} from 'effector'
import {useStoreMap} from 'effector-react'
const typecheck = '{global}'
type User = {
username: string
email: string
bio: string
}
type Props = {
id: number
field: keyof User
}
describe('useStoreMap', () => {
const users = createStore<User[]>([
{
username: 'alice',
email: 'alice@example.com',
bio: '. . .',
},
{
username: 'bob',
email: 'bob@example.com',
bio: '~/ - /~',
},
{
username: 'carol',
email: 'carol@example.com',
bio: '- - -',
},
])
describe('infer type with `as const`', () => {
it('should pass typecheck', () => {
const UserProperty = ({id, field}: Props) => {
const value = useStoreMap({
store: users,
keys: [id, field] as const,
fn: (users, [id, field]) => users[id][field] || null,
})
return <div>{value}</div>
}
expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
no errors
"
`)
})
it('should fail typecheck', () => {
const UserProperty = ({id, field}: Props) => {
const value = useStoreMap({
store: users,
keys: [id] as const,
fn: (users, [id, field]) => null,
})
return <div>{value}</div>
}
expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
Tuple type 'readonly [number]' of length '1' has no element at index '1'.
"
`)
})
})
describe('infer type with `as [explicit, tuple]`', () => {
it('should pass typecheck', () => {
const UserProperty = ({id, field}: Props) => {
const value = useStoreMap({
store: users,
keys: [id, field] as [number, keyof User],
fn: (users, [id, field]) => users[id][field] || null,
})
return <div>{value}</div>
}
expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
no errors
"
`)
})
it('should fail typecheck', () => {
const UserProperty = ({id, field}: Props) => {
const value = useStoreMap({
store: users,
keys: [id, field] as [number, keyof User],
fn: (users, [id, field]: [number, number]) => null,
})
return <div>{value}</div>
}
expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
Type '[number, \\"email\\" | \\"username\\" | \\"bio\\"]' is not assignable to type '[number, number]'.
Type '\\"email\\" | \\"username\\" | \\"bio\\"' is not assignable to type 'number'.
Type '\\"email\\"' is not assignable to type 'number'.
"
`)
})
})
it('can infer tuple type without `as`', () => {
const UserProperty = ({id, field}: Props) => {
const value = useStoreMap({
store: users,
keys: [id, field],
fn: (users, [id, field]) => users[id][field] || null,
})
return <div>{value}</div>
}
expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
no errors
"
`)
})
})