Skip to content

Commit 9975f7d

Browse files
committed
test: convert .then to async/await for beaconUpload test
1 parent ca4a655 commit 9975f7d

1 file changed

Lines changed: 15 additions & 36 deletions

File tree

test/src/tests-beaconUpload.ts

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,63 +45,52 @@ describe('Beacon Upload', () => {
4545
fetchMock.restore();
4646
});
4747

48-
it('should trigger beacon on page visibilitychange events', function(done) {
48+
// http://go/j-SDKE-301
49+
it('should trigger beacon on page visibilitychange events', async () => {
4950
window.mParticle._resetForTests(MPConfig);
5051

5152
const bond = sinon.spy(navigator, 'sendBeacon');
5253
window.mParticle.init(apiKey, window.mParticle.config);
5354

54-
waitForCondition(hasIdentifyReturned)
55-
.then(() => {
55+
await waitForCondition(hasIdentifyReturned);
5656

5757
// visibility change is a document property, not window
5858
document.dispatchEvent(new Event('visibilitychange'));
5959

6060
bond.called.should.eql(true);
6161
bond.lastCall.args[0].should.eql(urls.events);
62-
63-
done();
64-
})
6562
});
6663

67-
it('should trigger beacon on page beforeunload events', function(done) {
64+
it('should trigger beacon on page beforeunload events', async () => {
6865
window.mParticle._resetForTests(MPConfig);
6966

7067
const bond = sinon.spy(navigator, 'sendBeacon');
7168
window.mParticle.init(apiKey, window.mParticle.config);
7269

73-
waitForCondition(hasIdentifyReturned)
74-
.then(() => {
70+
await waitForCondition(hasIdentifyReturned);
7571

7672
// karma fails if onbeforeunload is not set to null
7773
window.onbeforeunload = null;
7874
window.dispatchEvent(new Event('beforeunload'));
7975

8076
bond.called.should.eql(true);
8177
bond.getCalls()[0].args[0].should.eql(urls.events);
82-
83-
done();
84-
});
8578
});
8679

87-
it('should trigger beacon on pagehide events', function(done) {
80+
it('should trigger beacon on pagehide events', async () => {
8881
window.mParticle._resetForTests(MPConfig);
8982

9083
const bond = sinon.spy(navigator, 'sendBeacon');
9184
window.mParticle.init(apiKey, window.mParticle.config);
9285

93-
waitForCondition(hasIdentifyReturned)
94-
.then(() => {
86+
await waitForCondition(hasIdentifyReturned);
9587

9688
window.dispatchEvent(new Event('pagehide'));
9789

9890
bond.called.should.eql(true);
9991
bond.getCalls()[0].args[0].should.eql(urls.events);
10092

10193
(typeof bond.getCalls()[0].args[1]).should.eql('object');
102-
103-
done();
104-
});
10594
});
10695

10796
describe('Offline Storage Enabled', () => {
@@ -124,13 +113,14 @@ describe('Beacon Upload', () => {
124113
fetchMock.restore();
125114
});
126115

127-
it('`visibilitychange` should purge events and batches from Offline Storage after dispatch', function(done) {
116+
// http://go/j-SDKE-301
117+
it('`visibilitychange` should purge events and batches from Offline Storage after dispatch', async () => {
128118
const eventStorageKey = 'mprtcl-v4_abcdef-events';
129119
const batchStorageKey = 'mprtcl-v4_abcdef-batches';
130120
window.mParticle._resetForTests(MPConfig);
131121
window.mParticle.init(apiKey, window.mParticle.config);
132-
waitForCondition(hasIdentifyReturned)
133-
.then(() => {
122+
await waitForCondition(hasIdentifyReturned);
123+
134124
const mpInstance = window.mParticle.getInstance();
135125
const uploader = mpInstance._APIClient.uploader;
136126

@@ -174,19 +164,15 @@ describe('Beacon Upload', () => {
174164
uploader.batchesQueuedForProcessing.length,
175165
'Batch Queue should be empty after dispatch'
176166
).to.equal(0);
177-
178-
done();
179-
});
180167
});
181168

182-
it('`beforeunload` should purge events and batches from Offline Storage after dispatch', function(done) {
169+
it('`beforeunload` should purge events and batches from Offline Storage after dispatch', async () => {
183170
const eventStorageKey = 'mprtcl-v4_abcdef-events';
184171
const batchStorageKey = 'mprtcl-v4_abcdef-batches';
185172

186173
window.mParticle._resetForTests(MPConfig);
187174
window.mParticle.init(apiKey, window.mParticle.config);
188-
waitForCondition(hasIdentifyReturned)
189-
.then(() => {
175+
await waitForCondition(hasIdentifyReturned);
190176

191177
const mpInstance = window.mParticle.getInstance();
192178
const uploader = mpInstance._APIClient.uploader;
@@ -231,19 +217,15 @@ describe('Beacon Upload', () => {
231217
uploader.batchesQueuedForProcessing.length,
232218
'Batch Queue should be empty after dispatch'
233219
).to.equal(0);
234-
235-
done();
236-
});
237220
});
238221

239-
it('`pagehide` should purge events and batches from Offline Storage after dispatch', function(done) {
222+
it('`pagehide` should purge events and batches from Offline Storage after dispatch', async () => {
240223
const eventStorageKey = 'mprtcl-v4_abcdef-events';
241224
const batchStorageKey = 'mprtcl-v4_abcdef-batches';
242225

243226
window.mParticle._resetForTests(MPConfig);
244227
window.mParticle.init(apiKey, window.mParticle.config);
245-
waitForCondition(hasIdentifyReturned)
246-
.then(() => {
228+
await waitForCondition(hasIdentifyReturned);
247229

248230
const mpInstance = window.mParticle.getInstance();
249231
const uploader = mpInstance._APIClient.uploader;
@@ -288,9 +270,6 @@ describe('Beacon Upload', () => {
288270
uploader.batchesQueuedForProcessing.length,
289271
'Batch Queue should be empty after dispatch'
290272
).to.equal(0);
291-
292-
done();
293-
});
294273
});
295274
});
296275
});

0 commit comments

Comments
 (0)