diff --git a/src/event.js b/src/event.js index 9cd340083..0e1892473 100755 --- a/src/event.js +++ b/src/event.js @@ -547,6 +547,9 @@ class ICalEvent { if (location === undefined) { return this._data.location; } + if(this._data.appleLocation && location) { + this._data.appleLocation = null; + } this._data.location = location ? location.toString() : null; return this; @@ -555,17 +558,27 @@ class ICalEvent { /** * Set/Get the Apple event's location * - * @param {object} [location] - * @since 0.2.0 + * @param {object|null} [appleLocation] + * @param {string} [appleLocation.title] + * @param {string} [appleLocation.address] + * @param {number} [appleLocation.radius] + * @param {object} [appleLocation.geo] + * @param {string|number} [appleLocation.lat] + * @param {string|number} [appleLocation.lon] + * @since 1.10.0 * @returns {ICalEvent|String} */ appleLocation (appleLocation) { if (appleLocation === undefined) { return this._data.appleLocation; } + if (appleLocation === null) { + this._data.location = null; + return this; + } if (!appleLocation.title || !appleLocation.address || !appleLocation.radius || !appleLocation.geo || !appleLocation.geo.lat || !appleLocation.geo.lon) { - throw new Error('`appleLocation` isn\'t formatted correctly.'); + throw new Error('`appleLocation` isn\'t formatted correctly. See https://github.com/sebbo2002/ical-generator#applelocationobject-applelocation'); } this._data.appleLocation = appleLocation; diff --git a/test/test_event.js b/test/test_event.js index b7b6e055f..1c2b974a3 100644 --- a/test/test_event.js +++ b/test/test_event.js @@ -1038,6 +1038,122 @@ describe('ical-generator Event', function () { event.location('Europa-Park'); assert.strictEqual(event._data.location, 'Europa-Park'); }); + + it('should reset appleLocation', function () { + const event = new ICalEvent({ + start: moment(), + summary: 'Example Event', + appleLocation: { + title: 'My Title', + address: 'My Address', + radius: 40, + geo: { + lat: '52.063921', + lon: '5.128511' + } + } + }, new ICalCalendar()); + + event.location('Europa-Park'); + assert.strictEqual(event._data.appleLocation, null); + }); + }); + + describe('appleLocation()', function () { + it('getter should return value', function () { + const e = new ICalEvent(null, new ICalCalendar()); + assert.strictEqual(e.appleLocation(), null); + + e._data.appleLocation = { + title: 'My Title', + address: 'My Address', + radius: 40, + geo: { + lat: '52.063921', + lon: '5.128511', + } + }; + assert.deepEqual(e.appleLocation(), { + title: 'My Title', + address: 'My Address', + radius: 40, + geo: { + lat: '52.063921', + lon: '5.128511', + } + }); + + e._data.appleLocation = null; + assert.strictEqual(e.appleLocation(), null); + }); + + it('setter should return this', function () { + const e = new ICalEvent(null, new ICalCalendar()); + assert.deepStrictEqual(e, e.appleLocation(null)); + assert.deepStrictEqual(e, e.appleLocation({ + title: 'My Title', + address: 'My Address', + radius: 40, + geo: { + lat: '52.063921', + lon: '5.128511', + } + })); + }); + + it('should update appleLocation', function () { + const event = new ICalEvent({ + start: moment(), + summary: 'Example Event' + }, new ICalCalendar()); + + event.appleLocation({ + title: 'My Title', + address: 'My Address', + radius: 40, + geo: { + lat: '52.063921', + lon: '5.128511', + } + }); + assert.deepEqual(event._data.appleLocation, { + title: 'My Title', + address: 'My Address', + radius: 40, + geo: { + lat: '52.063921', + lon: '5.128511', + } + }); + }); + + it('should reset location', function () { + const event = new ICalEvent({ + start: moment(), + summary: 'Example Event', + location: 'Batman Cave' + }, new ICalCalendar()); + + event.appleLocation({ + title: 'My Title', + address: 'My Address', + radius: 40, + geo: { + lat: '52.063921', + lon: '5.128511', + } + }); + assert.ok(event._data.location !== 'Batman Cave', null); + }); + + it('should throw error when string is not valid', function () { + const event = new ICalEvent({ + start: moment(), + summary: 'Example Event' + }, new ICalCalendar()); + + assert.throws(() => event.appleLocation({}), /`appleLocation` isn't formatted correctly/i); + }); }); describe('description()', function () {