diff --git a/src/mp-instance.js b/src/mp-instance.js index 1736ce1d..b1815833 100644 --- a/src/mp-instance.js +++ b/src/mp-instance.js @@ -591,7 +591,7 @@ export default function mParticleInstance(instanceName) { Constants.NativeSdkPaths.Upload ); } else { - self._APIClient.uploader.prepareAndUpload(false, false); + self._APIClient?.uploader?.prepareAndUpload(false, false); } } }; diff --git a/test/src/tests-batchUploader.ts b/test/src/tests-batchUploader.ts index 5981fc6b..5554ce3d 100644 --- a/test/src/tests-batchUploader.ts +++ b/test/src/tests-batchUploader.ts @@ -329,6 +329,20 @@ describe('batch uploader', () => { }) }); + it('should not throw an error when upload is called while storage has not been created yet', async () => { + window.localStorage.clear(); + window.sessionStorage.clear(); + + window.mParticle.init(apiKey, window.mParticle.config); + + const mpInstance = window.mParticle.getInstance(); + const uploader = mpInstance._APIClient.uploader; + + expect(uploader).to.equal(null) + + expect(() => { window.mParticle.upload() }).to.not.throw(TypeError, /Cannot read properties of null \(reading 'prepareAndUpload'\)/) + }); + it('should return batches that fail to unknown HTTP errors', async () => { window.mParticle.init(apiKey, window.mParticle.config);