Skip to content

Commit b8a1a8f

Browse files
mmustafa-tsealexs-mparticle
authored andcommitted
fix: Check for uploader to prevent errors when calling upload() too early (#938)
1 parent 3b1aa42 commit b8a1a8f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/mp-instance.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ export default function mParticleInstance(instanceName) {
591591
Constants.NativeSdkPaths.Upload
592592
);
593593
} else {
594-
self._APIClient.uploader.prepareAndUpload(false, false);
594+
self._APIClient?.uploader?.prepareAndUpload(false, false);
595595
}
596596
}
597597
};

test/src/tests-batchUploader.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,20 @@ describe('batch uploader', () => {
329329
})
330330
});
331331

332+
it('should not throw an error when upload is called while storage has not been created yet', async () => {
333+
window.localStorage.clear();
334+
window.sessionStorage.clear();
335+
336+
window.mParticle.init(apiKey, window.mParticle.config);
337+
338+
const mpInstance = window.mParticle.getInstance();
339+
const uploader = mpInstance._APIClient.uploader;
340+
341+
expect(uploader).to.equal(null)
342+
343+
expect(() => { window.mParticle.upload() }).to.not.throw(TypeError, /Cannot read properties of null \(reading 'prepareAndUpload'\)/)
344+
});
345+
332346
it('should return batches that fail to unknown HTTP errors', async () => {
333347
window.mParticle.init(apiKey, window.mParticle.config);
334348

0 commit comments

Comments
 (0)