Skip to content

Commit 2b1eb16

Browse files
author
Ramesh VK
committed
Add more tests and clean lint..
1 parent 8674f20 commit 2b1eb16

File tree

7 files changed

+90
-32
lines changed

7 files changed

+90
-32
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -779,10 +779,9 @@ readable ISO string. `Unwrap` returns this value too (though
779779
- ~streams.wrap support~
780780
- ~PathChange change type~
781781
- ~fields accessible using dot notation~
782-
6. Collections
782+
6. ~Collections~
783783
- ~map, order, orderBy, filter, groupBy~
784-
- comprehensive tests
785-
7. Composition
784+
7. ~Composition~
786785
- ~watch, object, merge~
787786
8. ~Collaboration~
788787
- ~merge support in change types~
@@ -792,10 +791,12 @@ readable ISO string. `Unwrap` returns this value too (though
792791
- multiple tabs support
793792
- add merge tests
794793
9. Branch merge support
795-
10. Server DB support
796-
11. Mutable collections support
797-
- map
798-
- filter
794+
10. Server support
795+
- snapshots
796+
- db storage
797+
- expose writeable streams via store API
798+
11. ~Mutable collections support~
799+
- ~order, orderBy, filter, groupBy~
799800
12. ~Mutable composition support~
800801
- ~object~
801802
- ~merge~

es6/changes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function buildChanges(types) {
1616
}
1717

1818
merge(other, older) {
19-
const reuslt = [];
19+
const result = [];
2020
for (let kk = 0; kk < this.changes.length; kk++) {
2121
const c = this.changes[kk];
2222
if (!c) continue;

es6/child_stream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export function buildChildStream(types) {
77
this.key = key;
88
}
99

10-
withRef(r) {
11-
throw new Error("Cannot set ref on a derived stream");
10+
withRef(_r) {
11+
throw new Error("NYI");
1212
}
1313

1414
ref() {

es6/group.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export function buildGroup(types) {
66
return types.wrap(stream.groups, stream);
77
};
88

9-
const valueOf = x => x && x.valueOf();
109
function group(v, fn) {
1110
const result = {};
1211
v = (v && v.valueOf()) || {};
@@ -54,7 +53,8 @@ export function buildGroup(types) {
5453
append(c, older) {
5554
if (c === null) return this;
5655

57-
// TODO: handle keycounts cleanups
56+
// TODO: handle case when last elt of group is deleted
57+
// (the whole group should also be deleted)
5858
const changes = new types.ChangeBuilder();
5959
c.visit([], {
6060
replace: (path, cx) => {
@@ -91,7 +91,6 @@ export function buildGroup(types) {
9191
let current = this.s.valueOf();
9292
let keycounts = counts(this.groups);
9393

94-
console.log("Counts", keycounts);
9594
c.visit([], {
9695
replace: (path, cx) => {
9796
const updated = new types.PathChange(path, cx).apply(current);
@@ -109,11 +108,9 @@ export function buildGroup(types) {
109108
const changed = "" + beforeGroup !== "" + afterGroup;
110109
if (existed && (!exists || changed)) {
111110
keycounts[beforeGroup]--;
112-
console.log("Decrementing", beforeGroup);
113111
const r = new types.Replace(current[key], null);
114112
changes.replace(["" + beforeGroup, key], r);
115113
if (keycounts[beforeGroup] === 0) {
116-
console.log("Removing", beforeGroup, key);
117114
const rx = new types.Replace({}, null);
118115
changes.replace(["" + beforeGroup], rx);
119116
}

es6/test/filter_test.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,44 @@ import { wrap, filter } from "../main.js";
77

88
describe("filter", function() {
99
it("filters", () => {
10-
const s = {};
11-
const size = 1000;
12-
for (let kk = 0; kk < size; kk++) {
13-
s[kk] = kk;
14-
}
15-
const filtered = filter(wrap(s), v => v < 10);
16-
let count = 0;
17-
filtered.forEachKey(key => {
18-
count++;
19-
});
20-
expect(count).to.equal(10);
10+
const s = wrap({ hello: 4, world: 3, ok: 2 });
11+
const f = filter(s, v => v % 2 === 0);
12+
13+
expect(f.valueOf()).to.deep.equal({ hello: 4, ok: 2 });
14+
});
15+
16+
it("tracks changes", () => {
17+
const s = wrap({ hello: 4, world: 3, ok: 2 });
18+
let f = filter(s, v => v % 2 === 0);
19+
20+
expect(f.valueOf()).to.deep.equal({ hello: 4, ok: 2 });
21+
22+
s.hello.replace(9);
23+
f = f.latest();
24+
expect(f.valueOf()).to.deep.equal({ ok: 2 });
25+
});
26+
27+
it("updates", () => {
28+
let s = wrap({ hello: 4, world: 3, ok: 2 });
29+
let f = filter(s, v => v % 2 === 0);
30+
31+
expect(f.valueOf()).to.deep.equal({ hello: 4, ok: 2 });
32+
f.hello.replace(9);
33+
34+
f = f.latest();
35+
s = s.latest();
36+
expect(f.valueOf()).to.deep.equal({ ok: 2 });
37+
expect(s.valueOf()).to.deep.equal({ hello: 9, world: 3, ok: 2 });
38+
});
39+
40+
it("updates #2", () => {
41+
let s = wrap({ hello: 4, world: 3, ok: 2 });
42+
let f = filter(s, v => v % 2 === 0);
43+
44+
expect(f.valueOf()).to.deep.equal({ hello: 4, ok: 2 });
45+
f.replace(null);
46+
47+
s = s.latest();
48+
expect(s.valueOf()).to.deep.equal({ world: 3 });
2149
});
2250
});

es6/test/map_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,13 @@ describe("map", () => {
7575
const c = new PathChange(["last"], new Replace("SCHMOE", "DOE"));
7676
expect(mapped.nextChange()).to.deep.equal(c);
7777
});
78+
79+
it("handles deep changes", () => {
80+
let x = wrap({ first: { second: 5 } });
81+
let mapped = map(x, val => val.second);
82+
83+
expect(JSON.stringify(mapped)).to.equal(`{"first":5}`);
84+
x.first.second.replace(7);
85+
expect(JSON.stringify(mapped.latest())).to.equal(`{"first":7}`);
86+
});
7887
});

es6/test/order_test.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,34 @@
33
/* eslint-env mocha, browser */
44

55
import { expect } from "./expect.js";
6-
import { wrap, order, PathChange, Replace } from "../main.js";
6+
import { wrap, order, orderBy } from "../main.js";
77

88
describe("order", () => {
9+
function keys(s) {
10+
const result = [];
11+
s.forEachKey(key => result.push(key) && false);
12+
return result;
13+
}
14+
915
it("wraps dict", () => {
1016
let s = wrap({ hello: 42, ok: 33 });
11-
let o = order(s, (list, x, y) => list[x] - list[y]);
17+
let o = order(s, (x, y) => x - y);
1218

1319
expect(JSON.stringify(s)).to.equal(JSON.stringify(o));
1420
expect(o.exists("hello")).to.equal(true);
1521
expect(o.withRef(["boo"]).ref()).to.deep.equal(["boo"]);
22+
23+
expect(keys(o)).to.deep.equal(["ok", "hello"]);
1624
});
1725

1826
it("tracks dict", () => {
1927
let s = wrap({ hello: 42, ok: 33 });
20-
let o = order(s, (list, x, y) => list[x] - list[y]);
28+
let o = order(s, (x, y) => x - y);
2129

2230
s.hello.replace(22);
2331

2432
expect(s.nextChange()).to.deep.equal(o.nextChange());
33+
expect(keys(o.latest())).to.deep.equal(["hello", "ok"]);
2534

2635
s = s.next();
2736
o = o.next();
@@ -31,11 +40,12 @@ describe("order", () => {
3140

3241
it("writes through changes #1", () => {
3342
let s = wrap({ hello: 42, ok: 33 });
34-
let o = order(s, (list, x, y) => list[x] - list[y]);
43+
let o = order(s, (x, y) => x - y);
3544

3645
o.hello.replace(22);
3746

3847
expect(s.nextChange()).to.deep.equal(o.nextChange());
48+
expect(keys(o.latest())).to.deep.equal(["hello", "ok"]);
3949

4050
s = s.next();
4151
o = o.next();
@@ -45,11 +55,12 @@ describe("order", () => {
4555

4656
it("writes through changes #2", () => {
4757
let s = wrap({ hello: 42, ok: 33 });
48-
let o = order(s, (list, x, y) => list[x] - list[y]);
58+
let o = order(s, (x, y) => x - y);
4959

5060
o.replace({ hello: 22, ok: 33 });
5161

5262
expect(s.nextChange()).to.deep.equal(o.nextChange());
63+
expect(keys(o.latest())).to.deep.equal(["hello", "ok"]);
5364

5465
s = s.next();
5566
o = o.next();
@@ -59,15 +70,27 @@ describe("order", () => {
5970

6071
it("writes through changes #3", () => {
6172
let s = wrap({ hello: 42, ok: 33 });
62-
let o = order(s, (list, x, y) => list[x] - list[y]);
73+
let o = order(s, (x, y) => x - y);
6374

6475
o.replacePath(["hello"], 22);
6576

6677
expect(s.nextChange()).to.deep.equal(o.nextChange());
78+
expect(keys(o.latest())).to.deep.equal(["hello", "ok"]);
6779

6880
s = s.next();
6981
o = o.next();
7082

7183
expect(JSON.stringify(s)).to.equal(JSON.stringify(o));
7284
});
85+
86+
it("sorts with orderBy", () => {
87+
let s = wrap({ hello: 42, ok: 33 });
88+
let o = orderBy(s, x => x);
89+
90+
expect(JSON.stringify(s)).to.equal(JSON.stringify(o));
91+
expect(o.exists("hello")).to.equal(true);
92+
expect(o.withRef(["boo"]).ref()).to.deep.equal(["boo"]);
93+
94+
expect(keys(o)).to.deep.equal(["ok", "hello"]);
95+
});
7396
});

0 commit comments

Comments
 (0)