@platform/state
Version:
A small, simple, strongly typed, [rx/observable] state-machine.
129 lines (128 loc) • 6.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var _1 = require(".");
var test_1 = require("../test");
describe('Patch', function () {
describe('toPatchSet', function () {
it('empty', function () {
var test = function (forward, backward) {
var res = _1.Patch.toPatchSet(forward, backward);
(0, test_1.expect)(res.prev).to.eql([]);
(0, test_1.expect)(res.next).to.eql([]);
};
test();
test([], []);
test(undefined, []);
test(undefined, []);
test(undefined, [undefined]);
});
it('converts paths to strings', function () {
var p1 = { op: 'add', path: ['foo', 'bar'], value: 123 };
var p2 = { op: 'remove', path: ['foo', 'bar'], value: 123 };
var test = function (res) {
(0, test_1.expect)(res.next[0].op).to.eql('add');
(0, test_1.expect)(res.prev[0].op).to.eql('remove');
(0, test_1.expect)(res.next[0].path).to.eql('foo/bar');
(0, test_1.expect)(res.prev[0].path).to.eql('foo/bar');
};
test(_1.Patch.toPatchSet([p1], [p2]));
test(_1.Patch.toPatchSet(p1, p2));
});
it('throw: when property name contains "/"', function () {
var patch = { op: 'add', path: ['foo', 'bar/baz'], value: 123 };
var err = /Property names cannot contain the "\/" character/;
(0, test_1.expect)(function () { return _1.Patch.toPatchSet(patch); }).to.throw(err);
(0, test_1.expect)(function () { return _1.Patch.toPatchSet([], patch); }).to.throw(err);
});
});
describe('isEmpty', function () {
var test = function (input, expected) {
var res = _1.Patch.isEmpty(input);
(0, test_1.expect)(res).to.eql(expected);
};
it('is empty', function () {
test(undefined, true);
test(null, true);
test({}, true);
test(' ', true);
test({ next: [], prev: [] }, true);
});
it('is not empty', function () {
var p1 = { op: 'add', path: ['foo', 'bar'], value: 123 };
var p2 = { op: 'remove', path: ['foo', 'bar'], value: 123 };
var patches = _1.Patch.toPatchSet([p1, p2], [p2, p1]);
test(patches, false);
});
});
describe('change (aka "produce")', function () {
it('change (op: "update" change)', function () {
var obj = { msg: 'hello', child: { foo: [123] } };
var res = _1.Patch.change(obj, function (draft) {
draft.msg = 'foobar';
draft.child.foo.push(456);
});
(0, test_1.expect)(res.to.msg).to.eql('foobar');
(0, test_1.expect)(res.to.child.foo).to.eql([123, 456]);
(0, test_1.expect)(res.op).to.eql('update');
(0, test_1.expect)(res.patches.prev.map(function (c) { return c.path; })).to.eql(['child/foo/length', 'msg']);
(0, test_1.expect)(res.patches.next.map(function (c) { return c.path; })).to.eql(['child/foo/1', 'msg']);
});
it('change (op: "replace" change)', function () {
var obj1 = { child: { msg: 'one' } };
var obj2 = { child: { msg: 'two' } };
var res = _1.Patch.change(obj1, obj2);
(0, test_1.expect)(res.op).to.eql('replace');
(0, test_1.expect)(res.to).to.eql(obj2);
(0, test_1.expect)(res.patches.prev).to.eql([{ op: 'replace', path: '', value: obj1 }]);
(0, test_1.expect)(res.patches.next).to.eql([{ op: 'replace', path: '', value: obj2 }]);
});
it('changeAsync', function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var obj, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
obj = { msg: 'hello', child: { foo: [123] } };
return [4, _1.Patch.changeAsync(obj, function (draft) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, test_1.time.wait(10)];
case 1:
_a.sent();
draft.msg = 'foobar';
draft.child.foo.push(456);
return [2];
}
});
}); })];
case 1:
res = _a.sent();
(0, test_1.expect)(res.to.msg).to.eql('foobar');
(0, test_1.expect)(res.to.child.foo).to.eql([123, 456]);
(0, test_1.expect)(res.op).to.eql('update');
(0, test_1.expect)(res.patches.prev.map(function (c) { return c.path; })).to.eql(['child/foo/length', 'msg']);
(0, test_1.expect)(res.patches.next.map(function (c) { return c.path; })).to.eql(['child/foo/1', 'msg']);
return [2];
}
});
}); });
});
describe('apply', function () {
it('applies patches forward (next)', function () {
var obj = { child: { foo: [123] } };
var res = _1.Patch.change(obj, function (draft) { return draft.child.foo.push(456); });
(0, test_1.expect)(obj.child.foo).to.eql([123]);
(0, test_1.expect)(res.op).to.eql('update');
(0, test_1.expect)(res.to.child.foo).to.eql([123, 456]);
(0, test_1.expect)(_1.Patch.apply(obj, res.patches).child.foo).to.eql([123, 456]);
});
it('applies patches backward (prev)', function () {
var obj = { child: { foo: [123] } };
var res = _1.Patch.change(obj, function (draft) { return draft.child.foo.push(456); });
var next = _1.Patch.apply(obj, res.patches.next);
var prev = _1.Patch.apply(next, res.patches.prev);
(0, test_1.expect)(next.child.foo).to.eql([123, 456]);
(0, test_1.expect)(prev.child.foo).to.eql([123]);
});
});
});