Skip to content

Commit

Permalink
feat: Add fields subtitleLocKey, subtitleLocArgs for localized su…
Browse files Browse the repository at this point in the history
…btitle and arguments in notifications (#154)
  • Loading branch information
cbaker6 authored Sep 25, 2024
1 parent b886c88 commit c885405
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/notification.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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[];
Expand Down
10 changes: 10 additions & 0 deletions lib/notification/apsProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions lib/notification/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Notification.prototype = require('./apsProperties');
'subtitle',
'titleLocKey',
'titleLocArgs',
'subtitleLocKey',
'subtitleLocArgs',
'action',
'actionLocKey',
'launchImage',
Expand Down
103 changes: 103 additions & 0 deletions test/notification/apsProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ describe('Notification', function () {
});
});
});

describe('titleLocKey', function () {
it('sets the aps.alert.title-loc-key property', function () {
note.titleLocKey = 'Warning';
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit c885405

Please sign in to comment.