@@ -12,6 +12,7 @@ jest.dontMock('../TaskQueue');
1212jest . dontMock ( '../SingleInstanceStateController' ) ;
1313jest . dontMock ( '../UniqueInstanceStateController' ) ;
1414
15+ const ParseError = require ( '../ParseError' ) . default ;
1516const LocalDatastore = require ( '../LocalDatastore' ) ;
1617const ParseInstallation = require ( '../ParseInstallation' ) ;
1718const CoreManager = require ( '../CoreManager' ) ;
@@ -84,6 +85,67 @@ describe('ParseInstallation', () => {
8485 expect ( InstallationController . updateInstallationOnDisk ) . toHaveBeenCalledTimes ( 1 ) ;
8586 } ) ;
8687
88+ it ( 'can save if object not found' , async ( ) => {
89+ const InstallationController = {
90+ async updateInstallationOnDisk ( ) { } ,
91+ async currentInstallationId ( ) { } ,
92+ async currentInstallation ( ) { } ,
93+ } ;
94+ let once = true ; // save will be called twice first time will reject
95+ CoreManager . setInstallationController ( InstallationController ) ;
96+ CoreManager . setRESTController ( {
97+ request ( ) {
98+ if ( ! once ) {
99+ return Promise . resolve ( { } , 200 ) ;
100+ }
101+ once = false ;
102+ const parseError = new ParseError (
103+ ParseError . OBJECT_NOT_FOUND ,
104+ 'Object not found.'
105+ ) ;
106+ return Promise . reject ( parseError ) ;
107+ } ,
108+ ajax ( ) { } ,
109+ } ) ;
110+ CoreManager . setLocalDatastore ( LocalDatastore ) ;
111+ jest . spyOn ( InstallationController , 'updateInstallationOnDisk' ) . mockImplementationOnce ( ( ) => { } ) ;
112+ const installation = new ParseInstallation ( ) ;
113+ installation . set ( 'deviceToken' , '1234' ) ;
114+ jest . spyOn ( installation , '_markAllFieldsDirty' ) ;
115+ await installation . save ( ) ;
116+ expect ( installation . _markAllFieldsDirty ) . toHaveBeenCalledTimes ( 1 ) ;
117+ expect ( InstallationController . updateInstallationOnDisk ) . toHaveBeenCalledTimes ( 1 ) ;
118+ } ) ;
119+
120+ it ( 'can save and handle errors' , async ( ) => {
121+ const InstallationController = {
122+ async updateInstallationOnDisk ( ) { } ,
123+ async currentInstallationId ( ) { } ,
124+ async currentInstallation ( ) { } ,
125+ } ;
126+ CoreManager . setInstallationController ( InstallationController ) ;
127+ CoreManager . setRESTController ( {
128+ request ( ) {
129+ const parseError = new ParseError (
130+ ParseError . INTERNAL_SERVER_ERROR ,
131+ 'Cannot save installation on client.'
132+ ) ;
133+ return Promise . reject ( parseError ) ;
134+ } ,
135+ ajax ( ) { } ,
136+ } ) ;
137+ CoreManager . setLocalDatastore ( LocalDatastore ) ;
138+ jest . spyOn ( InstallationController , 'updateInstallationOnDisk' ) . mockImplementationOnce ( ( ) => { } ) ;
139+ const installation = new ParseInstallation ( ) ;
140+ installation . set ( 'deviceToken' , '1234' ) ;
141+ try {
142+ await installation . save ( ) ;
143+ } catch ( e ) {
144+ expect ( e . message ) . toEqual ( 'Cannot save installation on client.' ) ;
145+ }
146+ expect ( InstallationController . updateInstallationOnDisk ) . toHaveBeenCalledTimes ( 0 ) ;
147+ } ) ;
148+
87149 it ( 'can get current installation' , async ( ) => {
88150 const InstallationController = {
89151 async updateInstallationOnDisk ( ) { } ,
@@ -100,4 +162,76 @@ describe('ParseInstallation', () => {
100162 expect ( installation . deviceType ) . toEqual ( 'web' ) ;
101163 expect ( installation . installationId ) . toEqual ( '1234' ) ;
102164 } ) ;
165+
166+ it ( 'can fetch and save to disk' , async ( ) => {
167+ const InstallationController = {
168+ async updateInstallationOnDisk ( ) { } ,
169+ async currentInstallationId ( ) { } ,
170+ async currentInstallation ( ) { } ,
171+ } ;
172+ CoreManager . setInstallationController ( InstallationController ) ;
173+ CoreManager . setRESTController ( {
174+ request ( ) {
175+ return Promise . resolve ( { } , 200 ) ;
176+ } ,
177+ ajax ( ) { } ,
178+ } ) ;
179+ CoreManager . setLocalDatastore ( LocalDatastore ) ;
180+ jest . spyOn ( InstallationController , 'updateInstallationOnDisk' ) . mockImplementationOnce ( ( ) => { } ) ;
181+ const installation = new ParseInstallation ( ) ;
182+ installation . id = 'abc' ;
183+ await installation . fetch ( ) ;
184+ expect ( InstallationController . updateInstallationOnDisk ) . toHaveBeenCalledTimes ( 1 ) ;
185+ } ) ;
186+
187+ it ( 'can fetch if object not found' , async ( ) => {
188+ const InstallationController = {
189+ async updateInstallationOnDisk ( ) { } ,
190+ async currentInstallationId ( ) { } ,
191+ async currentInstallation ( ) { } ,
192+ } ;
193+ let once = true ;
194+ CoreManager . setInstallationController ( InstallationController ) ;
195+ CoreManager . setRESTController ( {
196+ request ( ) {
197+ if ( ! once ) {
198+ // save() results
199+ return Promise . resolve ( { } , 200 ) ;
200+ }
201+ once = false ;
202+ // fetch() results
203+ const parseError = new ParseError (
204+ ParseError . OBJECT_NOT_FOUND ,
205+ 'Object not found.'
206+ ) ;
207+ return Promise . reject ( parseError ) ;
208+ } ,
209+ ajax ( ) { } ,
210+ } ) ;
211+ CoreManager . setLocalDatastore ( LocalDatastore ) ;
212+ jest . spyOn ( InstallationController , 'updateInstallationOnDisk' ) . mockImplementationOnce ( ( ) => { } ) ;
213+ const installation = new ParseInstallation ( ) ;
214+ installation . id = '1234' ;
215+ jest . spyOn ( installation , '_markAllFieldsDirty' ) ;
216+ await installation . fetch ( ) ;
217+ expect ( installation . _markAllFieldsDirty ) . toHaveBeenCalledTimes ( 1 ) ;
218+ expect ( InstallationController . updateInstallationOnDisk ) . toHaveBeenCalledTimes ( 1 ) ;
219+ } ) ;
220+
221+ it ( 'can fetch and handle errors' , async ( ) => {
222+ const InstallationController = {
223+ async updateInstallationOnDisk ( ) { } ,
224+ async currentInstallationId ( ) { } ,
225+ async currentInstallation ( ) { } ,
226+ } ;
227+ CoreManager . setInstallationController ( InstallationController ) ;
228+ jest . spyOn ( InstallationController , 'updateInstallationOnDisk' ) . mockImplementationOnce ( ( ) => { } ) ;
229+ const installation = new ParseInstallation ( ) ;
230+ try {
231+ await installation . fetch ( ) ;
232+ } catch ( e ) {
233+ expect ( e . message ) . toEqual ( 'Object does not have an ID' ) ;
234+ }
235+ expect ( InstallationController . updateInstallationOnDisk ) . toHaveBeenCalledTimes ( 0 ) ;
236+ } ) ;
103237} ) ;
0 commit comments