Skip to content

Commit aca8ea9

Browse files
joostmekara
authored andcommitted
refactor(service-worker): Format comments and add additional test (angular#25860)
- Format JSDoc for notificationClicks - Add comment on why handleClick does not use hasOwnProperty - Add additional test that uses handleClick without action PR Close angular#25860
1 parent 4a01ada commit aca8ea9

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

packages/service-worker/src/push.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ export class SwPush {
2727

2828
/**
2929
* Emits the payloads of the received push notification messages as well as the action the user
30-
* interacted with.
31-
* If no action was used the action property will be an empty string `''`.
30+
* interacted with. If no action was used the action property will be an empty string `''`.
3231
*
33-
* Note that the `notification` property is **not** a
34-
* [Notification](https://developer.mozilla.org/en-US/docs/Web/API/Notification) object but rather
35-
* a
32+
* Note that the `notification` property is **not** a [Notification][Mozilla Notification] object
33+
* but rather a
3634
* [NotificationOptions](https://notifications.spec.whatwg.org/#dictdef-notificationoptions)
37-
* object that also includes the `title` of the
38-
* [Notification](https://developer.mozilla.org/en-US/docs/Web/API/Notification) object.
35+
* object that also includes the `title` of the [Notification][Mozilla Notification] object.
36+
*
37+
* [Mozilla Notification]: https://developer.mozilla.org/en-US/docs/Web/API/Notification
3938
*/
4039
readonly notificationClicks: Observable < {
4140
action: string;

packages/service-worker/worker/src/driver.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ export class Driver implements Debuggable, UpdateSource {
309309
notification.close();
310310

311311
const options: {-readonly[K in keyof Notification]?: Notification[K]} = {};
312+
// The filter uses `name in notification` because the properties are on the prototype so
313+
// hasOwnProperty does not work here
312314
NOTIFICATION_OPTION_NAMES.filter(name => name in notification)
313315
.forEach(name => options[name] = notification[name]);
314316

packages/service-worker/worker/test/happy_spec.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,16 +589,30 @@ const manifestUpdateHash = sha1(JSON.stringify(manifestUpdate));
589589
}]);
590590
});
591591

592-
async_it('broadcasts notification click events', async() => {
592+
async_it('broadcasts notification click events with action', async() => {
593593
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
594594
await driver.initialized;
595-
await scope.handleClick({title: 'This is a test', body: 'Test body'}, 'button');
595+
await scope.handleClick(
596+
{title: 'This is a test with action', body: 'Test body with action'}, 'button');
596597
const message: any = scope.clients.getMock('default') !.messages[0];
597598

598599
expect(message.type).toEqual('NOTIFICATION_CLICK');
599600
expect(message.data.action).toEqual('button');
600-
expect(message.data.notification.title).toEqual('This is a test');
601-
expect(message.data.notification.body).toEqual('Test body');
601+
expect(message.data.notification.title).toEqual('This is a test with action');
602+
expect(message.data.notification.body).toEqual('Test body with action');
603+
});
604+
605+
async_it('broadcasts notification click events without action', async() => {
606+
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');
607+
await driver.initialized;
608+
await scope.handleClick(
609+
{title: 'This is a test without action', body: 'Test body without action'});
610+
const message: any = scope.clients.getMock('default') !.messages[0];
611+
612+
expect(message.type).toEqual('NOTIFICATION_CLICK');
613+
expect(message.data.action).toBeUndefined();
614+
expect(message.data.notification.title).toEqual('This is a test without action');
615+
expect(message.data.notification.body).toEqual('Test body without action');
602616
});
603617

604618
async_it('prefetches updates to lazy cache when set', async() => {

0 commit comments

Comments
 (0)