Skip to content

Commit

Permalink
test(Event): Apple Location tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbo2002 committed Apr 13, 2020
1 parent aa840b8 commit d8b176f
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
116 changes: 116 additions & 0 deletions test/test_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit d8b176f

Please sign in to comment.