@@ -11,6 +11,7 @@ var defineModelTestsWithDataSource = require('./util/model-tests');
1111var PersistedModel = loopback . PersistedModel ;
1212var expect = require ( 'chai' ) . expect ;
1313var debug = require ( 'debug' ) ( 'test' ) ;
14+ var runtime = require ( './../lib/runtime' ) ;
1415
1516describe ( 'Replication / Change APIs' , function ( ) {
1617 this . timeout ( 10000 ) ;
@@ -60,6 +61,77 @@ describe('Replication / Change APIs', function() {
6061 } ;
6162 } ) ;
6263
64+ describe ( 'cleanup check for enableChangeTracking' , function ( ) {
65+ describe ( 'when no changeCleanupInterval set' , function ( ) {
66+ it ( 'should call rectifyAllChanges if running on server' , function ( done ) {
67+ var calls = mockRectifyAllChanges ( SourceModel ) ;
68+ SourceModel . enableChangeTracking ( ) ;
69+
70+ if ( runtime . isServer ) {
71+ expect ( calls ) . to . eql ( [ 'rectifyAllChanges' ] ) ;
72+ } else {
73+ expect ( calls ) . to . eql ( [ ] ) ;
74+ }
75+
76+ done ( ) ;
77+ } ) ;
78+ } ) ;
79+
80+ describe ( 'when changeCleanupInterval set to -1' , function ( ) {
81+ var Model ;
82+ beforeEach ( function ( ) {
83+ Model = this . Model = PersistedModel . extend (
84+ 'Model-' + tid ,
85+ { id : { id : true , type : String , defaultFn : 'guid' } } ,
86+ { trackChanges : true , changeCleanupInterval : - 1 } ) ;
87+
88+ Model . attachTo ( dataSource ) ;
89+ } ) ;
90+
91+ it ( 'should not call rectifyAllChanges' , function ( done ) {
92+ var calls = mockRectifyAllChanges ( Model ) ;
93+ Model . enableChangeTracking ( ) ;
94+ expect ( calls ) . to . eql ( [ ] ) ;
95+ done ( ) ;
96+ } ) ;
97+ } ) ;
98+
99+ describe ( 'when changeCleanupInterval set to 10000' , function ( ) {
100+ var Model ;
101+ beforeEach ( function ( ) {
102+ Model = this . Model = PersistedModel . extend (
103+ 'Model-' + tid ,
104+ { id : { id : true , type : String , defaultFn : 'guid' } } ,
105+ { trackChanges : true , changeCleanupInterval : 10000 } ) ;
106+
107+ Model . attachTo ( dataSource ) ;
108+ } ) ;
109+
110+ it ( 'should call rectifyAllChanges if running on server' , function ( done ) {
111+ var calls = mockRectifyAllChanges ( Model ) ;
112+ Model . enableChangeTracking ( ) ;
113+ if ( runtime . isServer ) {
114+ expect ( calls ) . to . eql ( [ 'rectifyAllChanges' ] ) ;
115+ } else {
116+ expect ( calls ) . to . eql ( [ ] ) ;
117+ }
118+
119+ done ( ) ;
120+ } ) ;
121+ } ) ;
122+
123+ function mockRectifyAllChanges ( Model ) {
124+ var calls = [ ] ;
125+
126+ Model . rectifyAllChanges = function ( cb ) {
127+ calls . push ( 'rectifyAllChanges' ) ;
128+ process . nextTick ( cb ) ;
129+ } ;
130+
131+ return calls ;
132+ }
133+ } ) ;
134+
63135 describe ( 'optimization check rectifyChange Vs rectifyAllChanges' , function ( ) {
64136 beforeEach ( function initialData ( done ) {
65137 var data = [ { name : 'John' , surname : 'Doe' } , { name : 'Jane' , surname : 'Roe' } ] ;
0 commit comments