1+ var Promise = require ( 'bluebird' ) ;
2+
13var _Raven = require ( '../../src/raven' ) ;
24var reactNativePlugin = require ( '../../plugins/react-native' ) ;
35
@@ -8,6 +10,10 @@ describe('React Native plugin', function () {
810 beforeEach ( function ( ) {
911 Raven = new _Raven ( ) ;
1012 Raven . config ( 'http://abc@example.com:80/2' ) ;
13+
14+ reactNativePlugin . _persistPayload = self . sinon . stub ( ) . returns ( Promise . resolve ( ) ) ;
15+ reactNativePlugin . _restorePayload = self . sinon . stub ( ) . returns ( Promise . resolve ( ) ) ;
16+ reactNativePlugin . _clearPayload = self . sinon . stub ( ) . returns ( Promise . resolve ( ) ) ;
1117 } ) ;
1218
1319 describe ( '_normalizeData()' , function ( ) {
@@ -135,16 +141,96 @@ describe('React Native plugin', function () {
135141 }
136142 } ) ;
137143
138- it ( 'should call the default React Native handler and Raven.captureException' , function ( ) {
144+ it ( 'checks for persisted errors when starting' , function ( ) {
145+ var onInit = self . sinon . stub ( ) ;
146+ reactNativePlugin ( Raven , { onInitialize : onInit } ) ;
147+ assert . isTrue ( reactNativePlugin . _restorePayload . calledOnce ) ;
148+
149+ return Promise . resolve ( ) . then ( function ( ) {
150+ assert . isTrue ( onInit . calledOnce ) ;
151+ } ) ;
152+ } ) ;
153+
154+ it ( 'reports persisted errors' , function ( ) {
155+ var payload = { abc : 123 } ;
156+ self . sinon . stub ( Raven , '_sendProcessedPayload' ) ;
157+ reactNativePlugin . _restorePayload = self . sinon . stub ( ) . returns ( Promise . resolve ( payload ) ) ;
158+ var onInit = self . sinon . stub ( ) ;
159+ reactNativePlugin ( Raven , { onInitialize : onInit } ) ;
160+
161+ return Promise . resolve ( ) . then ( function ( ) {
162+ assert . isTrue ( onInit . calledOnce ) ;
163+ assert . equal ( onInit . getCall ( 0 ) . args [ 0 ] , payload ) ;
164+ assert . isTrue ( Raven . _sendProcessedPayload . calledOnce ) ;
165+ assert . equal ( Raven . _sendProcessedPayload . getCall ( 0 ) . args [ 0 ] , payload ) ;
166+ } ) ;
167+ } ) ;
168+
169+ it ( 'clears persisted errors after they are reported' , function ( ) {
170+ var payload = { abc : 123 } ;
171+ var callback ;
172+ self . sinon . stub ( Raven , '_sendProcessedPayload' , function ( p , cb ) { callback = cb ; } ) ;
173+ reactNativePlugin . _restorePayload = self . sinon . stub ( ) . returns ( Promise . resolve ( payload ) ) ;
174+
175+ reactNativePlugin ( Raven ) ;
176+
177+ return Promise . resolve ( ) . then ( function ( ) {
178+ assert . isFalse ( reactNativePlugin . _clearPayload . called ) ;
179+ callback ( ) ;
180+ assert . isTrue ( reactNativePlugin . _clearPayload . called ) ;
181+ } ) ;
182+ } ) ;
183+
184+ it ( 'does not clear persisted errors if there is an error reporting' , function ( ) {
185+ var payload = { abc : 123 } ;
186+ var callback ;
187+ self . sinon . stub ( Raven , '_sendProcessedPayload' , function ( p , cb ) { callback = cb ; } ) ;
188+ reactNativePlugin . _restorePayload = self . sinon . stub ( ) . returns ( Promise . resolve ( payload ) ) ;
189+
139190 reactNativePlugin ( Raven ) ;
140- var err = new Error ( ) ;
141- this . sinon . stub ( Raven , 'captureException' ) ;
142191
143- this . globalErrorHandler ( err ) ;
192+ return Promise . resolve ( ) . then ( function ( ) {
193+ assert . isFalse ( reactNativePlugin . _clearPayload . called ) ;
194+ callback ( new Error ( 'nope' ) ) ;
195+ assert . isFalse ( reactNativePlugin . _clearPayload . called ) ;
196+ } ) ;
197+ } ) ;
198+
199+ describe ( 'in development mode' , function ( ) {
200+ beforeEach ( function ( ) {
201+ global . __DEV__ = true ;
202+ } ) ;
203+
204+ it ( 'should call the default React Native handler and Raven.captureException' , function ( ) {
205+ reactNativePlugin ( Raven ) ;
206+ var err = new Error ( ) ;
207+ this . sinon . stub ( Raven , 'captureException' ) ;
144208
145- assert . isTrue ( this . defaultErrorHandler . calledOnce ) ;
146- assert . isTrue ( Raven . captureException . calledOnce ) ;
147- assert . equal ( Raven . captureException . getCall ( 0 ) . args [ 0 ] , err ) ;
209+ this . globalErrorHandler ( err , true ) ;
210+
211+ assert . isTrue ( this . defaultErrorHandler . calledOnce ) ;
212+ assert . isTrue ( Raven . captureException . calledOnce ) ;
213+ assert . equal ( Raven . captureException . getCall ( 0 ) . args [ 0 ] , err ) ;
214+ } ) ;
215+ } ) ;
216+
217+ describe ( 'in production mode' , function ( ) {
218+ beforeEach ( function ( ) {
219+ global . __DEV__ = false ;
220+ } ) ;
221+
222+ it ( 'should call the default React Native handler after persisting the error' , function ( ) {
223+ reactNativePlugin ( Raven ) ;
224+ var err = new Error ( ) ;
225+ this . globalErrorHandler ( err , true ) ;
226+
227+ assert . isTrue ( reactNativePlugin . _persistPayload . calledOnce ) ;
228+
229+ var defaultErrorHandler = this . defaultErrorHandler ;
230+ return Promise . resolve ( ) . then ( function ( ) {
231+ assert . isTrue ( defaultErrorHandler . calledOnce ) ;
232+ } ) ;
233+ } ) ;
148234 } ) ;
149235 } ) ;
150236} ) ;
0 commit comments