git.fiddlerwoaroof.com
Browse code

lenses fully tested

Ed Langley authored on 26/05/2018 06:19:08
Showing 2 changed files
... ...
@@ -121,8 +121,8 @@ export default function StateContainer(data = {}) {
121 121
                         return createLens([...keyPath, ...subPath]);
122 122
                     },
123 123
 
124
-                    withValue(cb) {
125
-                        return cb(this.get());
124
+                    withValue(cb, ...rest) {
125
+                        return cb(this.get(), ...rest);
126 126
                     },
127 127
 
128 128
                     swap(cb) {
... ...
@@ -1,5 +1,6 @@
1 1
 import StateContainer, {lensTransformer} from '../src/state_container';
2 2
 import sinon from 'sinon';
3
+import * as R from 'ramda';
3 4
 
4 5
 test("initial state setting works", () => {
5 6
    const container = new StateContainer({foo: 'bar'});
... ...
@@ -199,9 +200,9 @@ test("recorder from recorder also records - infinite turtles", () => {
199 200
 });
200 201
 
201 202
 test("lenses work", () => {
202
-    const container = new StateContainer({foo: {bar: 1, baz: 2}});
203
-    let lens, sublens;
203
+    let container, lens, sublens;
204 204
 
205
+    container = new StateContainer({ foo: { bar: 1, baz: 2 } });
205 206
     lens = container.lensFor('foo');
206 207
     expect(lens.get()).toEqual({bar:1, baz: 2});
207 208
 
... ...
@@ -220,7 +221,40 @@ test("lenses work", () => {
220 221
 
221 222
     lens.set(3);
222 223
     expect(lens.get()).toBe(3);
224
+    expect(lens.withValue(R.identity)).toBe(3);
225
+    expect(lens.withValue((value,prop) => R.assoc(prop, value, {b: 2, a:1}), 'a')).toEqual({a:3, b:2});
223 226
     expect(container.getState()).toEqual({foo: 3});
227
+
228
+    expect(lens.swap(R.assoc('a', R.__, {b: 2, a:1}))).toEqual({a:3, b:2});
229
+    expect(lens.get()).toEqual({a:3, b:2});
230
+
231
+    container = new StateContainer({ foo: { bar: 1, baz: 2 } });
232
+    const recorder = container.getRecorder();
233
+
234
+    lens = recorder.lensFor('foo');
235
+    expect(lens.get()).toEqual({bar:1, baz: 2});
236
+
237
+    sublens = lens.lensFor('bar');
238
+    expect(sublens.get()).toBe(1);
239
+
240
+    sublens.set(2);
241
+    expect(sublens.get()).toBe(2, 'changing a nested lens updates its value');
242
+    expect(lens.get()).toEqual({bar:2, baz: 2}, 'changing a nested lens updates its parent\'s value');
243
+    expect(recorder.getState()).toEqual({foo: {bar:2, baz: 2}}, 'changing a nested lens updates recorder state');
244
+
245
+    lens.set({bar: 3});
246
+    expect(sublens.get()).toBe(3, 'changing parent lens to complex value updates nested lens value');
247
+    expect(lens.get()).toEqual({bar:3}, 'changing parent lens to complex value updates itself');
248
+    expect(recorder.getState()).toEqual({foo: {bar:3}}, 'changing parent lens to complex value updates recorder');
249
+
250
+    lens.set(3);
251
+    expect(lens.get()).toBe(3);
252
+    expect(lens.withValue(R.identity)).toBe(3);
253
+    expect(lens.withValue((value,prop) => R.assoc(prop, value, {b: 2, a:1}), 'a')).toEqual({a:3, b:2});
254
+    expect(recorder.getState()).toEqual({foo: 3});
255
+
256
+    expect(lens.swap(R.assoc('a', R.__, {b: 2, a:1}))).toEqual({a:3, b:2});
257
+    expect(lens.get()).toEqual({a:3, b:2});
224 258
 });
225 259
 
226 260
 test("lenses treat undefined properly", () => {