|
1 | 1 | import {expect} from 'chai'
|
2 |
| -import {ClassorientedTeststate, ObjectArray, SimpleTeststate, SubTypeA} from './testobjects' |
| 2 | +import {Concert, ObjectArray, Rockband, SimpleBand} from './testobjects' |
3 | 3 | import {ReplicationBuilder} from '../replicator'
|
4 | 4 | import {deepFreeze, isDeepFrozen} from '../deepFreeze'
|
5 | 5 |
|
6 | 6 | describe('ReplicationBuilder', () => {
|
7 | 7 |
|
8 | 8 | it('should clone a property and execute function on the clone ', function () {
|
9 |
| - let rootState = new ClassorientedTeststate(); |
10 |
| - let someNewValue = 'someNewValue'; |
11 |
| - let manipulatedRoot = ReplicationBuilder.forObject(rootState).replaceProperty('subTypeA').andDo((clonedSubTypeA) => { |
12 |
| - clonedSubTypeA.setSubTypeAAttribute(someNewValue) |
| 9 | + let concert = new Concert(); |
| 10 | + let newBandName = 'someNewName'; |
| 11 | + let manipulatedRoot = ReplicationBuilder.forObject(concert).replaceValueOf('band').withCloneAndDo((band) => { |
| 12 | + band.changeNameTo(newBandName) |
13 | 13 | }).build();
|
14 |
| - expect(manipulatedRoot.subTypeA.subTypeAAttribute).to.equal(someNewValue); |
| 14 | + expect(manipulatedRoot.band.name).to.equal(newBandName); |
15 | 15 | });
|
16 | 16 |
|
17 | 17 | it('Inputstate must not be modified, output must be modified', () => {
|
18 |
| - let rootState = new ClassorientedTeststate(); |
19 |
| - let manipulatedRoot = ReplicationBuilder.forObject(rootState).getProperty('subTypeA').getProperty('subTypeB').replaceProperty('subTypeBAttribute').with('Test').build(); |
| 18 | + let concert = new Concert(); |
| 19 | + let manipulatedRoot = ReplicationBuilder.forObject(concert).property('band').property('homeland').replaceValueOf('name').with('Spain').build(); |
20 | 20 |
|
21 |
| - expect(rootState.subTypeA.subTypeB.subTypeBAttribute).to.null; |
22 |
| - expect(manipulatedRoot.subTypeA.subTypeB.subTypeBAttribute).to.equal('Test') |
| 21 | + expect(concert.band.homeland.name).to.null; |
| 22 | + expect(manipulatedRoot.band.homeland.name).to.equal('Spain') |
23 | 23 | });
|
24 | 24 |
|
25 | 25 | it('Inputstate must not be modified, output must be modified', () => {
|
26 |
| - let rootState = new ClassorientedTeststate(); |
27 |
| - let manipulatedRoot = ReplicationBuilder.forObject(rootState).getProperty('subTypeA').getProperty('subTypeB').replaceProperty('subTypeBAttribute').with('Test').build(); |
| 26 | + let rootState = new Concert(); |
| 27 | + let manipulatedRoot = ReplicationBuilder.forObject(rootState).property('band').property('homeland').replaceValueOf('name').with('Russia').build(); |
28 | 28 |
|
29 |
| - expect(rootState.subTypeA.subTypeB.subTypeBAttribute).to.null; |
30 |
| - expect(manipulatedRoot.subTypeA.subTypeB.subTypeBAttribute).to.equal('Test') |
| 29 | + expect(rootState.band.homeland.name).to.null; |
| 30 | + expect(manipulatedRoot.band.homeland.name).to.equal('Russia') |
31 | 31 | });
|
32 | 32 |
|
33 | 33 | it('Inputstate must not be modified, output node must be deleted', () => {
|
34 |
| - let rootState = new ClassorientedTeststate(); |
35 |
| - let manipulatedRoot = ReplicationBuilder.forObject(rootState).removeProperty('subTypeA').build(); |
| 34 | + let rootState = new Concert(); |
| 35 | + let manipulatedRoot = ReplicationBuilder.forObject(rootState).removeProperty('band').build(); |
36 | 36 |
|
37 |
| - expect(rootState.subTypeA).to.exist; |
38 |
| - expect(manipulatedRoot.subTypeA).to.not.exist |
| 37 | + expect(rootState.band).to.exist; |
| 38 | + expect(manipulatedRoot.band).to.not.exist |
39 | 39 | });
|
40 | 40 |
|
41 | 41 | it('Inputstate must not be modified, output child node must be deleted', () => {
|
42 |
| - let rootState = new ClassorientedTeststate(); |
43 |
| - let manipulatedRoot = ReplicationBuilder.forObject(rootState).getProperty('subTypeA').removeProperty('subTypeB').build(); |
| 42 | + let rootState = new Concert(); |
| 43 | + let manipulatedRoot = ReplicationBuilder.forObject(rootState).property('band').removeProperty('homeland').build(); |
44 | 44 |
|
45 |
| - expect(rootState.subTypeA.subTypeB).to.exist; |
46 |
| - expect(manipulatedRoot.subTypeA.subTypeB).to.not.exist |
| 45 | + expect(rootState.band.homeland).to.exist; |
| 46 | + expect(manipulatedRoot.band.homeland).to.not.exist |
47 | 47 | });
|
48 | 48 |
|
49 | 49 | it('with untyped structure: Inputstate must not be modified, output must be modified', () => {
|
50 |
| - let rootState = SimpleTeststate; |
51 |
| - let manipulatedRoot = ReplicationBuilder.forObject(rootState).getProperty('subTypeB').replaceProperty('subtypeBAttribute').with('Test').build(); |
| 50 | + let simpleBand = SimpleBand; |
| 51 | + let manipulatedRoot = ReplicationBuilder.forObject(simpleBand).property('genre').replaceValueOf('name').with('Test').build(); |
52 | 52 |
|
53 |
| - expect(rootState.subTypeB.subtypeBAttribute).to.equal('initial'); |
54 |
| - expect(manipulatedRoot.subTypeB.subtypeBAttribute).to.equal('Test') |
| 53 | + expect(simpleBand.genre.name).to.equal('initial'); |
| 54 | + expect(manipulatedRoot.genre.name).to.equal('Test') |
55 | 55 | });
|
56 | 56 |
|
57 | 57 | it('if input state is deep frozen --> output state must be deep frozen', () => {
|
58 |
| - let rootState = new ClassorientedTeststate(); |
59 |
| - deepFreeze(rootState); |
60 |
| - let manipulatedRoot = ReplicationBuilder.forObject(rootState).getProperty('subTypeA').getProperty('subTypeB').replaceProperty('subTypeBAttribute').with('Test').build(); |
| 58 | + let concert = new Concert(); |
| 59 | + deepFreeze(concert); |
| 60 | + let manipulatedRoot = ReplicationBuilder.forObject(concert).property('band').property('homeland').replaceValueOf('name').with('Test').build(); |
61 | 61 |
|
62 |
| - expect(rootState.subTypeA.subTypeB.subTypeBAttribute).to.null; |
63 |
| - expect(manipulatedRoot.subTypeA.subTypeB.subTypeBAttribute).to.equal('Test'); |
| 62 | + expect(concert.band.homeland.name).to.null; |
| 63 | + expect(manipulatedRoot.band.homeland.name).to.equal('Test'); |
64 | 64 | expect(isDeepFrozen(manipulatedRoot)).true
|
65 | 65 | });
|
66 | 66 |
|
67 | 67 | it('redux like root state --> if input state is deep frozen --> output state must be deep frozen', () => {
|
68 |
| - let rootState = { |
69 |
| - subTypeA: new SubTypeA() |
| 68 | + let festival = { |
| 69 | + band: new Rockband() |
70 | 70 | };
|
71 |
| - deepFreeze(rootState); |
72 |
| - let manipulatedRoot = ReplicationBuilder.forObject(rootState).getProperty('subTypeA').getProperty('subTypeB').replaceProperty('subTypeBArray') |
73 |
| - .by((oldArray) => [...oldArray, 'Test']).build(); |
| 71 | + deepFreeze(festival); |
| 72 | + let newFestival = ReplicationBuilder.forObject(festival).property('band').replaceValueOf('members') |
| 73 | + .by((oldMembers) => [...oldMembers, 'NewRocker']).build(); |
74 | 74 |
|
75 |
| - expect(rootState.subTypeA.subTypeB.subTypeBArray.length).to.equal(0); |
76 |
| - expect(manipulatedRoot.subTypeA.subTypeB.subTypeBArray[0]).to.equal('Test'); |
77 |
| - expect(isDeepFrozen(manipulatedRoot)).true |
| 75 | + expect(festival.band.members.length).to.equal(0); |
| 76 | + expect(newFestival.band.members[0]).to.equal('NewRocker'); |
| 77 | + expect(isDeepFrozen(newFestival)).true |
78 | 78 | });
|
79 | 79 |
|
80 | 80 | it('if input state is NOT frozen --> output state must NOT be frozen', () => {
|
81 |
| - let rootState = new ClassorientedTeststate(); |
82 |
| - let manipulatedRoot = ReplicationBuilder.forObject(rootState).getProperty('subTypeA').getProperty('subTypeB').replaceProperty('subTypeBAttribute').with('Test').build(); |
83 |
| - |
| 81 | + let concert = new Concert(); |
| 82 | + let manipulatedRoot = ReplicationBuilder.forObject(concert).property('band').property('homeland').replaceValueOf('name').with('USA').build(); |
84 | 83 | expect(Object.isFrozen(manipulatedRoot)).false
|
85 | 84 | });
|
86 | 85 |
|
|
0 commit comments