From c885405e10c9d1ec15968c42d410ce4f56dfbf04 Mon Sep 17 00:00:00 2001 From: Corey Date: Wed, 25 Sep 2024 04:30:23 -0700 Subject: [PATCH] feat: Add fields `subtitleLocKey`, `subtitleLocArgs` for localized subtitle and arguments in notifications (#154) --- doc/notification.markdown | 2 + index.d.ts | 4 ++ lib/notification/apsProperties.js | 10 +++ lib/notification/index.js | 2 + test/notification/apsProperties.js | 103 +++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+) diff --git a/doc/notification.markdown b/doc/notification.markdown index 681727a3..f232ce99 100644 --- a/doc/notification.markdown +++ b/doc/notification.markdown @@ -95,6 +95,8 @@ This table shows the name of the setter, with the key-path of the underlying pro | `title` | `aps.alert.title` | `String` | | `titleLocKey` | `aps.alert.title-loc-key` | `String` | | `titleLocArgs` | `aps.alert.title-loc-args` | `Array` | +| `subtitleLocKey` | `aps.alert.subtitle-loc-key` | `String` | +| `subtitleLocArgs` | `aps.alert.subtitle-loc-args` | `Array` | | `action` | `aps.alert.action` | `String` | | `actionLocKey` | `aps.alert.action-loc-key` | `String` | | `launchImage` | `aps.launch-image` | `String` | diff --git a/index.d.ts b/index.d.ts index 331b8f2d..a9df517e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -78,6 +78,8 @@ interface ApsAlert { title?: string "title-loc-key"?: string "title-loc-args"?: any[] + "subtitle-loc-key"?: string + "subtitle-loc-args"?: any[] action?: string "action-loc-key"?: string } @@ -173,6 +175,8 @@ export interface NotificationAlertOptions { body: string; "title-loc-key"?: string; "title-loc-args"?: string[]; + "subtitle-loc-key"?: string; + "subtitle-loc-args"?: string[]; "action-loc-key"?: string; "loc-key"?: string; "loc-args"?: string[]; diff --git a/lib/notification/apsProperties.js b/lib/notification/apsProperties.js index 393e65ed..44fdb735 100644 --- a/lib/notification/apsProperties.js +++ b/lib/notification/apsProperties.js @@ -49,6 +49,16 @@ module.exports = { this.aps.alert['title-loc-args'] = value; }, + set subtitleLocKey(value) { + this.prepareAlert(); + this.aps.alert['subtitle-loc-key'] = value; + }, + + set subtitleLocArgs(value) { + this.prepareAlert(); + this.aps.alert['subtitle-loc-args'] = value; + }, + set action(value) { this.prepareAlert(); this.aps.alert.action = value; diff --git a/lib/notification/index.js b/lib/notification/index.js index 957502ac..8651cee4 100644 --- a/lib/notification/index.js +++ b/lib/notification/index.js @@ -36,6 +36,8 @@ Notification.prototype = require('./apsProperties'); 'subtitle', 'titleLocKey', 'titleLocArgs', + 'subtitleLocKey', + 'subtitleLocArgs', 'action', 'actionLocKey', 'launchImage', diff --git a/test/notification/apsProperties.js b/test/notification/apsProperties.js index c3267321..c3f65ceb 100644 --- a/test/notification/apsProperties.js +++ b/test/notification/apsProperties.js @@ -261,6 +261,7 @@ describe('Notification', function () { }); }); }); + describe('titleLocKey', function () { it('sets the aps.alert.title-loc-key property', function () { note.titleLocKey = 'Warning'; @@ -363,6 +364,108 @@ describe('Notification', function () { }); }); + describe('subtitleLocKey', function () { + it('sets the aps.alert.subtitle-loc-key property', function () { + note.subtitleLocKey = 'Warning'; + expect(compiledOutput()).to.have.nested.deep.property('aps.alert.subtitle-loc-key', 'Warning'); + }); + + context('alert is already an object', function () { + beforeEach(function () { + note.alert = { body: 'Test', 'launch-image': 'test.png' }; + note.subtitleLocKey = 'Warning'; + }); + + it('contains all expected properties', function () { + expect(compiledOutput()).to.have.nested.deep.property('aps.alert').that.deep.equals({ + body: 'Test', + 'launch-image': 'test.png', + 'subtitle-loc-key': 'Warning', + }); + }); + }); + + context('alert is already a string', function () { + beforeEach(function () { + note.alert = 'Hello, world'; + note.subtitleLocKey = 'Warning'; + }); + + it('retains the alert body correctly', function () { + expect(compiledOutput()).to.have.nested.deep.property('aps.alert.body', 'Hello, world'); + }); + + it('sets the aps.alert.subtitle-loc-key property', function () { + expect(compiledOutput()).to.have.nested.deep.property( + 'aps.alert.subtitle-loc-key', + 'Warning' + ); + }); + }); + + describe('setAlert', function () { + it('is chainable', function () { + expect(note.setSubtitleLocKey('greeting')).to.equal(note); + expect(compiledOutput()).to.have.nested.deep.property( + 'aps.alert.subtitle-loc-key', + 'greeting' + ); + }); + }); + }); + + describe('subtitleLocArgs', function () { + it('sets the aps.alert.subtitle-loc-args property', function () { + note.subtitleLocArgs = ['arg1', 'arg2']; + expect(compiledOutput()) + .to.have.nested.deep.property('aps.alert.subtitle-loc-args') + .that.deep.equals(['arg1', 'arg2']); + }); + + context('alert is already an object', function () { + beforeEach(function () { + note.alert = { body: 'Test', 'launch-image': 'test.png' }; + note.subtitleLocArgs = ['Hi there']; + }); + + it('contains all expected properties', function () { + expect(compiledOutput()) + .to.have.nested.deep.property('aps.alert') + .that.deep.equals({ + body: 'Test', + 'launch-image': 'test.png', + 'subtitle-loc-args': ['Hi there'], + }); + }); + }); + + context('alert is already a string', function () { + beforeEach(function () { + note.alert = 'Hello, world'; + note.subtitleLocArgs = ['Hi there']; + }); + + it('retains the alert body', function () { + expect(compiledOutput()).to.have.nested.deep.property('aps.alert.body', 'Hello, world'); + }); + + it('sets the aps.alert.subtitle-loc-args property', function () { + expect(compiledOutput()) + .to.have.nested.deep.property('aps.alert.subtitle-loc-args') + .that.deep.equals(['Hi there']); + }); + }); + + describe('setTitleLocArgs', function () { + it('is chainable', function () { + expect(note.setTitleLocArgs(['iPhone 6s'])).to.equal(note); + expect(compiledOutput()) + .to.have.nested.deep.property('aps.alert.title-loc-args') + .that.deep.equals(['iPhone 6s']); + }); + }); + }); + describe('action', function () { it('sets the aps.alert.action property', function () { note.action = 'View';