forked from alexbepple/kiss-and-type
-
Notifications
You must be signed in to change notification settings - Fork 0
/
living.doc.test.js
112 lines (102 loc) · 3.58 KB
/
living.doc.test.js
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
import * as r from 'ramda'
import * as __ from 'hamjest'
import { createType } from './create-type'
/*
* The intent of this spec is to be an exhaustive, yet brief API documentation.
*
* Edge cases and other tests that do not add to a user’s high-level
* understanding of the API, go somewhere else.
*/
const thing = { foo: 1, bar: 2, baz: 3 }
describe('Defining types: Basic and extended forms', () => {
it('The most basic way to define props is to use strings.', () => {
const type = createType(['foo', 'bar', 'baz'])
__.assertThat(type.get.foo(thing), __.is(1))
__.assertThat(type.get.bar(thing), __.is(2))
__.assertThat(type.get.baz(thing), __.is(3))
__.assertThat(() => type.get.xyz({}), __.throws())
})
it('Every basic definition can be converted into an extended form, which allows for advanced features.', () => {
const type = createType(['foo', { bar: { alias: 'barAlias' } }, 'baz'])
__.assertThat(type.get.barAlias(thing), __.is(2))
})
it('There can be multiple extended forms next to each other.', () => {
const type = createType([
'foo',
{ bar: { alias: 'barAlias' } },
{ baz: { alias: 'bazAlias' } }
])
__.assertThat(type.get.bazAlias(thing), __.is(3))
})
it('In such a case, they can be merged.', () => {
const type = createType([
'foo',
{ bar: { alias: 'barAlias' }, baz: { alias: 'bazAlias' } }
])
__.assertThat(type.get.barAlias(thing), __.is(2))
__.assertThat(type.get.bazAlias(thing), __.is(3))
})
})
describe('Lingo inspired by Ramda', () => {
const type = createType(['foo'])
it('#pick', () => {
__.assertThat(type.pick.foo({ foo: 0, sthElse: null }), __.is({ foo: 0 }))
})
it('#pickAll', () => {
__.assertThat(type.pickAll({ foo: 0, bar: 1 }), __.is({ foo: 0 }))
})
it('#pluck', () => {
__.assertThat(
type.pluck.foo([type.objOf.foo(0), type.objOf.foo(1)]),
__.is([0, 1])
)
})
it('#has', () => {
__.assertThat(type.has.foo({}), __.is(false))
__.assertThat(type.has.foo(type.objOf.foo(null)), __.is(false))
__.assertThat(type.has.foo(type.objOf.foo(0)), __.is(true))
})
it('#eq', () => {
const isFoo0 = type.eq.foo(0)
__.assertThat(isFoo0(type.objOf.foo(0)), __.is(true))
__.assertThat(isFoo0(type.objOf.foo(1)), __.is(false))
})
it('#objOf', () => {
__.assertThat(type.objOf.foo(0), __.is(type.set.foo(0)({})))
})
it('#over', () => {
__.assertThat(
type.get.foo(type.over.foo(r.inc)(type.objOf.foo(0))),
__.is(1)
)
})
})
describe('Aliases', () => {
it('Yes, they alias.', () => {
const type = createType([{ foo: { alias: 'fooAlias' } }])
__.assertThat(type.get.fooAlias(thing), __.is(type.get.foo(thing)))
})
it('There can be many.', () => {
const type = createType([{ foo: { alias: ['alias1', 'alias2'] } }])
__.assertThat(type.get.alias1(thing), __.is(type.get.foo(thing)))
__.assertThat(type.get.alias2(thing), __.is(type.get.foo(thing)))
})
})
describe('Getter enhancers', () => {
const type = createType([{ foo: { get: r.defaultTo(0) } }])
it('enhance simple `get`', () => {
__.assertThat(type.get.foo({}), __.is(0))
})
it('enhance other prop retrievals, e.g. #pluck', () => {
__.assertThat(type.pluck.foo([{}, null]), __.is([0, 0]))
})
})
describe('Setter enhancers', () => {
const type = createType([{ foo: { set: Math.floor } }])
it('enhance simple `set`', () => {
__.assertThat(type.get.foo(type.set.foo(0.5)(null)), __.is(0))
})
it('enhance other prop modifiers, e.g. #objOf', () => {
__.assertThat(type.get.foo(type.objOf.foo(0.5)), __.is(0))
})
})