-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathpush.test.js
93 lines (75 loc) · 2.92 KB
/
push.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'use strict';
define(['ably', 'shared_helper', 'chai', 'push'], function (Ably, Helper, chai, PushPlugin) {
const expect = chai.expect;
const swUrl = '/push_sw.js';
let rest;
const persistKeys = {
deviceId: 'ably.push.deviceId',
deviceSecret: 'ably.push.deviceSecret',
deviceIdentityToken: 'ably.push.deviceIdentityToken',
pushRecipient: 'ably.push.pushRecipient',
activationState: 'ably.push.activationState',
};
const messageChannel = new MessageChannel();
/**
* These tests don't work in CI for various reasons (below) but should work when running locally via `npm run test:webserver`, provided
* that you have enabled notification permissions for the origin serving the test ui.
*
* chromium, firefox - don't support push notifications in incognito
* webkit - doesn't have a way to launch programatically with notification permissions granted
*/
if (Notification.permission === 'granted') {
describe('browser/push', function () {
this.timeout(60 * 1000);
before(function (done) {
const helper = Helper.forHook(this);
helper.setupApp(function () {
rest = helper.AblyRest({
pushServiceWorkerUrl: swUrl,
plugins: { Push: PushPlugin },
});
done();
});
});
beforeEach(async function () {
Object.values(persistKeys).forEach((key) => {
Ably.Realtime.Platform.Config.push.storage.remove(key);
});
const worker = await navigator.serviceWorker.getRegistration(swUrl);
if (worker) {
await worker.unregister();
}
});
afterEach(async function () {
await rest.push.deactivate();
});
/** @spec RSH2a */
it('push_activation_succeeds', async function () {
await rest.push.activate();
expect(rest.device.deviceIdentityToken).to.be.ok;
});
/** @nospec */
it('direct_publish_device_id', async function () {
await rest.push.activate();
const pushRecipient = {
deviceId: rest.device.id,
};
const pushPayload = {
notification: { title: 'Test message', body: 'Test message body' },
data: { foo: 'bar' },
};
const sw = await navigator.serviceWorker.getRegistration(swUrl);
sw.active.postMessage({ type: 'INIT_PORT' }, [messageChannel.port2]);
const receivedPushPayload = await new Promise((resolve, reject) => {
messageChannel.port1.onmessage = function (event) {
resolve(event.data.payload);
};
rest.push.admin.publish(pushRecipient, pushPayload).catch(reject);
});
expect(receivedPushPayload.data).to.deep.equal(pushPayload.data);
expect(receivedPushPayload.notification.title).to.equal(pushPayload.notification.title);
expect(receivedPushPayload.notification.body).to.equal(pushPayload.notification.body);
});
});
}
});