-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap-and-array-sync.test.ts
79 lines (60 loc) · 2.57 KB
/
map-and-array-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
import { GraphSynchronizer, IGraphSyncOptions } from '@ablestack/rdo';
import { Logger } from '@ablestack/rdo/infrastructure/logger';
const logger = Logger.make('map-sync.test.ts');
// --------------------------------------------------------------
// CONFIG
// --------------------------------------------------------------
const config: IGraphSyncOptions = {
targetedNodeOptions: [
{ sourceNodeMatcher: { nodePath: 'arrayOfBar' }, makeRdo: (sourceNode: Bar) => new BarWithNotesRDO() },
{ sourceNodeMatcher: { nodePath: 'mapOfBar' }, makeRdo: (sourceNode: Bar) => new BarWithNotesRDO() },
],
};
// --------------------------------------------------------------
// TEST
// --------------------------------------------------------------
test('Simple usage demo with notes', () => {
const fooWithNotesRDO = new FooWithNotesRDO();
const graphSynchronizer = new GraphSynchronizer(config);
// POSTURE VERIFICATION
expect(fooWithNotesRDO.mapOfBar.size).toBeFalsy();
// EXECUTE
graphSynchronizer.smartSync({ rootRdo: fooWithNotesRDO, rootSourceNode: fooWithNotesSourceJSON });
// RESULTS VERIFICATION
expect(fooWithNotesRDO.arrayOfBar.length).toEqual(fooWithNotesSourceJSON.arrayOfBar.length);
expect(fooWithNotesRDO.arrayOfBar[0].id).toEqual(fooWithNotesSourceJSON.arrayOfBar[0].id);
expect(fooWithNotesRDO.mapOfBar.size).toEqual(fooWithNotesSourceJSON.mapOfBar.length);
expect(fooWithNotesRDO.mapOfBar.values().next().value.id).toEqual(fooWithNotesSourceJSON.mapOfBar[0].id);
});
// --------------------------------------------------------------
// MODELS & DATA
// --------------------------------------------------------------
//
// Source Data Models
type Bar = { id: string; name: string };
//
// Source Data
export const fooWithNotesSourceJSON = {
id: 'foo-0',
bar: { id: 'bar-0' },
arrayOfBar: [{ id: 'bar-1' }],
mapOfBar: [{ id: 'bar-2' }],
};
//
// RDO Graphs
export class FooWithNotesRDO {
// Public properties. Getters and Setters will work fine also
public id = '';
// Child RDOs
public bar = new BarWithNotesRDO();
// Collections of Child RDOs
public arrayOfBar = new Array<BarWithNotesRDO>();
public mapOfBar = new Map<string, BarWithNotesRDO>();
/* Any other domain-specific properties and methods here */
// Note, all values must be initialized to a default value on instantiation, or they will be removed by the TS -> JS compilation step and will not sync
}
export class BarWithNotesRDO {
public id: string = '';
/* Further nesting of RDOs or collections of RDOs here */
/* Any other domain-specific properties and methods here */
}