-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlens.spec.ts
217 lines (175 loc) · 8.15 KB
/
lens.spec.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { L } from './lens';
interface Address { city?: string; street: string; neighbor?: House }
interface Person { type: 'Person', name?: string; address: Address }
interface Student extends Person { school: string }
interface Company { type: 'Company', title: string }
interface House { owner: Person | Company }
const isCompany = (c: Person | Company): c is Company => (c || <any>{}).type === 'Company';
const isPerson = (c:Person | Company): c is Person => (c || <any>{}).type === 'Person';
const duplicate = x => x + x;
describe('Mini Lens for TypeScript', () => {
describe('Dumb, no path', () => {
const dumbLens = L<string>().to();
it('can view', () => {
expect(dumbLens.view('foo')).toEqual('foo');
});
it('can set', () => {
expect(dumbLens.set('foo', 'bar')).toEqual('bar');
});
it('can override', () => {
expect(dumbLens.over('foo', duplicate)).toEqual('foofoo');
});
});
describe('Through nested objects', () => {
const lensPerson2Street = L<Person>().to('address', 'street');
it('can view', () => {
expect(lensPerson2Street.view({ address: { street: 'foo' }, type: 'Person' })).toEqual('foo');
});
it('view can short circuit null values', () => {
expect(lensPerson2Street.view(null)).toBeNull();
expect(lensPerson2Street.view({ address: null, type: 'Person' })).toBeNull();
});
it('can set', () => {
const withAddress: Person = { address: { street: 'foo' }, type: 'Person' };
const updated = lensPerson2Street.set(withAddress, 'bar');
expect(lensPerson2Street.view(updated)).toEqual('bar');
// but not reference equal
expect(updated).not.toBe(withAddress);
});
it('set can short-circuit null values', () => {
const noAddress: Person = { address: null, type: 'Person' };
const updated = lensPerson2Street.set(noAddress, 'bar');
expect(updated).toEqual(noAddress);
// but not reference equal
expect(updated).not.toBe(noAddress);
});
it('can override', () => {
const withAddress: Person = { address: { street: 'foo' }, type: 'Person' };
const updated = lensPerson2Street.over(withAddress, duplicate);
expect(lensPerson2Street.view(updated)).toEqual('foofoo');
// but not reference equal
expect(updated).not.toBe(withAddress);
});
});
describe('chain lens / cast through union type', () => {
const lens4CompanyTitle = L<House>().to('owner')
.castIf<Company>(isCompany)
.chain(L<Company>().to('title'));
it('can view', () => {
expect(lens4CompanyTitle.view({ owner: { title: 'title foo', type: 'Company' } })).toEqual('title foo');
});
it('can not view incorrect variant', () => {
expect(lens4CompanyTitle.view({ owner: { name: 'foo', address: null, type: 'Person' } })).toBeUndefined();
});
it('can set', () => {
const updated = lens4CompanyTitle.set({ owner: { title: 'title foo', type: 'Company' } }, 'title bar');
expect(lens4CompanyTitle.view(updated)).toEqual('title bar');
});
it('can set on null value', () => {
const house: House = { owner: undefined };
const person: Person = { address: { street: 'queen' }, type: 'Person' };
const withOwner = L<House>().to('owner').castIf(isPerson).set(house, person);
expect(withOwner).toEqual({ owner: person });
});
it('can not set incorrect variant', () => {
const notACompany = { name: 'foo', address: null, type: 'Person' };
const withoutCompany = { owner: <any>notACompany };
const updated = lens4CompanyTitle.set(withoutCompany, 'title bar');
expect(updated).toEqual(withoutCompany);
});
});
describe('chain and cast galore', () => {
const lensGalore = L<House>().to('owner').castIf(isPerson)
.chain(L<Person>().to('address', 'neighbor', 'owner').castIf(isCompany))
.chain(L<Company>().to('title'));
it('with valid data', () => {
const nested: House = {
owner: <Person>{
type: 'Person',
address: {
street: null,
neighbor: {
owner: {
type: 'Company',
title: 'bar'
}
}
}
}
};
expect(lensGalore.view(nested)).toEqual('bar');
expect(lensGalore.view(lensGalore.set(nested, 'bar'))).toEqual('bar');
});
it('with invalid data', () => {
const nested: House = {
owner: <Person>{
type: 'Person',
address: {
street: null
}
}
};
expect(lensGalore.view(nested)).toBeUndefined();
expect(lensGalore.view(lensGalore.set(nested, 'bar'))).toBeUndefined();
});
});
describe('chain with path', () => {
const lens4CompanyTitle = L<House>().to('owner')
.castIf<Company>(isCompany)
.then.to('title');
it('can view', () => {
expect(lens4CompanyTitle.view({ owner: { title: 'title foo', type: 'Company' } })).toEqual('title foo');
});
it('can not view incorrect variant', () => {
expect(lens4CompanyTitle.view({ owner: { name: 'foo', address: null, type: 'Person' } })).toBeUndefined();
});
it('can set', () => {
const updated = lens4CompanyTitle.set({ owner: { title: 'title foo', type: 'Company' } }, 'title bar');
expect(lens4CompanyTitle.view(updated)).toEqual('title bar');
});
it('can not set incorrect variant', () => {
const notACompany = { name: 'foo', address: null, type: 'Person' };
const withoutCompany = { owner: <any>notACompany };
const updated = lens4CompanyTitle.set(withoutCompany, 'title bar');
expect(updated).toEqual(withoutCompany);
});
});
describe('chain with path -- aliased', () => {
const lens4CompanyTitle = L<House>().to('owner')
.castIf<Company>(isCompany)
.then.to('title');
it('can view', () => {
expect(lens4CompanyTitle.view({ owner: { title: 'title foo', type: 'Company' } })).toEqual('title foo');
});
it('can not view incorrect variant', () => {
expect(lens4CompanyTitle.view({ owner: { name: 'foo', address: null, type: 'Person' } })).toBeUndefined();
});
it('can set', () => {
const updated = lens4CompanyTitle.set({ owner: { title: 'title foo', type: 'Company' } }, 'title bar');
expect(lens4CompanyTitle.view(updated)).toEqual('title bar');
});
it('can not set incorrect variant', () => {
const notACompany = { name: 'foo', address: null, type: 'Person' };
const withoutCompany = { owner: <any>notACompany };
const updated = lens4CompanyTitle.set(withoutCompany, 'title bar');
expect(updated).toEqual(withoutCompany);
});
});
describe('work with arrays', () => {
const l = L<string[]>().to(1);
const strings = [ 'aaa', 'bbb', 'ccc' ];
it('can view thru array', () => {
const oneChar = l.view(strings);
expect(oneChar).toEqual('bbb');
})
it('can set thru array', () => {
const actual = l.set(strings, 'zzz');
expect(actual).toEqual([ 'aaa', 'zzz', 'ccc' ]);
})
});
describe('works with extended types', () => {
const lStudentToName = L<Person>().to('name');
const student : Student = { 'address': null, 'school': 'Snakebite', 'type': 'Person' };
// this should compile
});
});