-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-sync.test.ts
107 lines (88 loc) · 3.65 KB
/
custom-sync.test.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
import { GraphSynchronizer, IContinueSmartSync, ICustomSync, IMakeRdo, IRdoNodeWrapper, IGraphSyncOptions, comparerUtils } from '@ablestack/rdo';
import { Logger } from '@ablestack/rdo/infrastructure/logger';
const logger = Logger.make('custom-sync.test.ts');
// --------------------------------------------------------------
// CONFIG
// --------------------------------------------------------------
const config: IGraphSyncOptions = {
targetedNodeOptions: [
{ sourceNodeMatcher: { nodePath: 'bar/mapOfBaz' }, makeRdo: (sourceNode: Baz) => new BazRdo() },
{ sourceNodeMatcher: { nodePath: 'bar/baz/mapOfFred' }, makeRdo: (sourceNode: Fred) => new FredRdo() },
{ sourceNodeMatcher: { nodePath: 'bar/mapOfBaz/mapOfFred' }, makeRdo: (sourceNode: Fred) => new FredRdo() },
],
};
// --------------------------------------------------------------
// TEST
// --------------------------------------------------------------
test('Custom Sync Demo', () => {
const fooRdo = new FooRdo();
const graphSynchronizer = new GraphSynchronizer(config);
// POSTURE VERIFICATION
expect(fooRdo.bar.mapOfBazzz.size).toBeFalsy();
// EXECUTE
graphSynchronizer.smartSync({ rootRdo: fooRdo, rootSourceNode: fooSourceJson });
// RESULTS VERIFICATION
expect(fooRdo.id).toEqual(fooSourceJson.id);
expect(fooRdo.bar.id).toEqual(`custom-id-${fooSourceJson.bar.id}`);
expect(fooRdo.bar.mapOfBazzz.size).toEqual(fooSourceJson.bar.mapOfBaz.length);
expect(Array.from(fooRdo.bar.mapOfBazzz.values())[0].id).toEqual(fooSourceJson.bar.mapOfBaz[0].id);
expect(Array.from(fooRdo.bar.mapOfBazzz.values())[0].mapOfFred.size).toEqual(fooSourceJson.bar.mapOfBaz[0].mapOfFred.length);
expect(Array.from(Array.from(fooRdo.bar.mapOfBazzz.values())[0].mapOfFred.values())[0].id).toEqual(fooSourceJson.bar.mapOfBaz[0].mapOfFred[0].id);
});
// --------------------------------------------------------------
// MODELS & DATA
// --------------------------------------------------------------
//
// Source Data Models
type Foo = { id: string; bar: Bar };
type Bar = { id: string; baz: Baz; mapOfBaz: Map<string, Baz> };
type Baz = { id: string; fred: Fred; mapOfFred: Map<string, Fred> };
type Fred = { id: string };
//
// Source Data
export const fooSourceJson = {
id: 'foo-0',
bar: {
id: 'bar-0',
baz: {
id: 'bar-0-baz-0',
fred: { id: 'bar-0-baz-0-fred-0' },
mapOfFred: [{ id: 'bar-0-baz-0-fred-1' }, { id: 'bar-0-baz-0-fred-2' }],
},
mapOfBaz: [
{
id: 'bar-0-baz-1',
fred: { id: 'bar-0-baz-1-fred-0' },
mapOfFred: [{ id: 'bar-0-baz-1-fred-1' }, { id: 'bar-0-baz-1-fred-2' }],
},
],
},
};
//
// RDO Graphs
export class FooRdo {
public id = '';
public bar = new CustomSyncBarRDO();
}
export class CustomSyncBarRDO implements ICustomSync<Bar> {
public id: string = '';
public bazzz = new BazRdo();
public mapOfBazzz = new Map<string, BazRdo>();
public synchronizeState({ sourceObject, continueSmartSync }: { sourceObject: Bar; continueSmartSync: IContinueSmartSync }) {
let changed = false;
changed = comparerUtils.valueGraph.updateIfNotEqual(this.id, `custom-id-${sourceObject.id}`, (val) => this.id = val) || changed;
// sync bazzz
changed = continueSmartSync({ sourceNode: sourceObject, sourceNodeItemKey: 'baz', rdoNode: this, rdoNodeItemKey: 'bazzz' }) || changed;
// sync mapOfBazz
continueSmartSync({ sourceNode: sourceObject, sourceNodeItemKey: 'mapOfBaz', rdoNode: this, rdoNodeItemKey: 'mapOfBazzz' }) || changed;
return changed;
}
}
export class BazRdo {
public id = '';
public fred = new FredRdo();
public mapOfFred = new Map<string, FredRdo>();
}
export class FredRdo {
public id = '';
}