From 4342b0a6f9089d8694686afa48e1f46906398997 Mon Sep 17 00:00:00 2001 From: Mo Mustafa <96316894+mmustafa-tse@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:56:02 -0800 Subject: [PATCH] fix: Check for uploader to prevent errors when calling upload() too early (#938) --- src/mp-instance.js | 2 +- test/src/tests-batchUploader.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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);