-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
selectKeys.test.ts
68 lines (56 loc) 路 2.15 KB
/
selectKeys.test.ts
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
import { expectType } from 'ts-expect'
import { D, pipe } from '../..'
const obj = { x: 123, y: 'abc', z: false }
const rec: Record<string, number> = { a: 1, b: 2, c: 3 }
describe('selectKeys', () => {
it('provides correct types', () => {
const keys = ['x', 'y'] as const
expectType<Pick<typeof obj, 'x' | 'y'>>(D.selectKeys(obj, ['x', 'y']))
expectType<typeof rec>(D.selectKeys(rec, ['a', 'b']))
expectType<typeof rec>(D.selectKeys(rec, ['y', 'z']))
expectType<typeof rec>(D.selectKeys(rec, keys))
})
it('selects the right keys', () => {
expect(D.selectKeys(obj, ['x', 'y'])).toEqual({ x: 123, y: 'abc' })
expect(D.selectKeys(rec, ['a', 'b'])).toEqual({ a: 1, b: 2 })
expect(D.selectKeys(rec, ['y', 'z'])).toEqual({})
})
it('*', () => {
expect(
D.selectKeys({ name: 'Joe', location: 'Warsaw', age: 42 }, [
'name',
'age',
]),
).toEqual({ name: 'Joe', age: 42 })
const obj: Record<string, string> = { name: 'Joe', location: 'Warsaw' }
expect(
// 猬囷笍 const obj: Record<string, string> = { name: 'Joe', location: 'Warsaw' }
D.selectKeys(obj, ['name', 'age']),
).toEqual({ name: 'Joe' })
})
})
describe('selectKeys (pipe)', () => {
it('provides correct types', () => {
expectType<Pick<typeof obj, 'x' | 'y'>>(pipe(obj, D.selectKeys(['x', 'y'])))
expectType<typeof rec>(pipe(rec, D.selectKeys(['a', 'b'])))
expectType<typeof rec>(pipe(rec, D.selectKeys(['y', 'z'])))
})
it('selects the right keys', () => {
expect(pipe(obj, D.selectKeys(['x', 'y']))).toEqual({ x: 123, y: 'abc' })
expect(pipe(rec, D.selectKeys(['a', 'b']))).toEqual({ a: 1, b: 2 })
expect(pipe(rec, D.selectKeys(['y', 'z']))).toEqual({})
})
it('*', () => {
expect(
pipe(
{ name: 'Joe', location: 'Warsaw', age: 42 },
D.selectKeys(['name', 'age']),
),
).toEqual({ name: 'Joe', age: 42 })
const obj: Record<string, string> = { name: 'Joe', location: 'Warsaw' }
expect(
// 猬囷笍 const obj: Record<string, string> = { name: 'Joe', location: 'Warsaw' }
pipe(obj, D.selectKeys(['name', 'age'])),
).toEqual({ name: 'Joe' })
})
})