Skip to content

Commit 32f6c42

Browse files
authored
Merge pull request #6 from saw-jan/prepare-release-v1.1.0
Fix and refactor
2 parents 11ffffb + 4872e94 commit 32f6c42

File tree

10 files changed

+138
-93
lines changed

10 files changed

+138
-93
lines changed

__tests__/Events.spec.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { buildParams } = require('./helpers')
12
const Events = require('../lib/Events')
23
const Logger = require('../lib/Logger')
34

@@ -34,8 +35,11 @@ describe('Events class', function () {
3435
},
3536
],
3637
])('valid options value', function (options) {
38+
const urlParams = buildParams(options)
3739
API.listEvents(options)
40+
3841
expect(Request.get).toHaveBeenCalledTimes(1)
42+
expect(Request.get).toHaveBeenCalledWith(`/events?${urlParams}`)
3943
})
4044

4145
test.each([
@@ -44,50 +48,58 @@ describe('Events class', function () {
4448
[{ query: {} }],
4549
[{ subcalendarId: 'id' }],
4650
[{ format: 'text' }],
51+
[{ startDate: '' }],
52+
[{ endDate: undefined }],
53+
[{ subcalendarId: undefined }],
4754
])('invalid options value', function (options) {
4855
expect(() => API.listEvents(options)).toThrow()
4956
})
5057

51-
test.each([
52-
[{}],
53-
[{ startDate: '' }],
54-
[{ endDate: undefined }],
55-
[{ query: false }],
56-
[{ subcalendarId: null }],
57-
[{ format: 0 }],
58-
])('ignored options value', function (options) {
59-
API.listEvents(options)
60-
expect(Request.get).toHaveBeenCalledTimes(1)
61-
})
58+
test.each([[{}], [{ subcalendarId: null }]])(
59+
'ignored options value',
60+
function (options) {
61+
API.listEvents(options)
62+
expect(Request.get).toHaveBeenCalledTimes(1)
63+
expect(Request.get).toHaveBeenCalledWith(`/events`)
64+
}
65+
)
6266

6367
test('invalid option parameter', function () {
6468
expect(() => API.listEvents({ someKey: true })).toThrow()
6569
})
6670

67-
test.each([[], 'string', true, false, 123])(
71+
test.each([[], 'string', true, false, 123, null])(
6872
'invalid options',
6973
function (option) {
7074
expect(() => API.listEvents(option)).toThrow()
7175
}
7276
)
77+
78+
test('ignored option: undefined', function () {
79+
API.listEvents(undefined)
80+
expect(Request.get).toHaveBeenCalledTimes(1)
81+
expect(Request.get).toHaveBeenCalledWith(`/events`)
82+
})
7383
})
7484

7585
describe('method: listEvent', function () {
7686
test('with event id (number)', function () {
7787
API.listEvent(1234)
7888
expect(Request.get).toHaveBeenCalledTimes(1)
89+
expect(Request.get).toHaveBeenCalledWith(`/events/1234`)
7990
})
8091

8192
test('with event id (string)', function () {
8293
API.listEvent('1234')
8394
expect(Request.get).toHaveBeenCalledTimes(1)
95+
expect(Request.get).toHaveBeenCalledWith(`/events/1234`)
8496
})
8597

8698
test('without event id', function () {
8799
expect(() => API.listEvent()).toThrow()
88100
})
89101

90-
test.each([[], 'string', true, false, {}])(
102+
test.each([[], 'string', true, false, {}, null, undefined])(
91103
'invalid event id',
92104
function (id) {
93105
expect(() => API.listEvent(id)).toThrow()

__tests__/SubCalendar.spec.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { buildParams } = require('./helpers')
12
const SubCalendar = require('../lib/SubCalendar')
23
const Logger = require('../lib/Logger')
34

@@ -19,63 +20,77 @@ describe('SubCalendar class', function () {
1920
test.each([[{ includeInactive: true }], [{ includeInactive: false }]])(
2021
'valid options value',
2122
function (options) {
23+
const urlParams = buildParams(options)
2224
API.listSubCalendars(options)
2325

2426
expect(Request.get).toHaveBeenCalledTimes(1)
27+
expect(Request.get).toHaveBeenCalledWith(`/subcalendars?${urlParams}`)
2528
}
2629
)
2730

2831
test.each([
2932
[{ includeInactive: 'text' }],
3033
[{ includeInactive: [] }],
3134
[{ includeInactive: {} }],
35+
[{ includeInactive: undefined }],
36+
[{ includeInactive: 1234 }],
3237
])('invalid options value', function (options) {
3338
expect(() => API.listSubCalendars(options)).toThrow()
3439
})
3540

36-
test.each([
37-
[{}],
38-
[{ includeInactive: '' }],
39-
[{ includeInactive: null }],
40-
[{ includeInactive: undefined }],
41-
[{ includeInactive: 0 }],
42-
])('ignored options value', function (options) {
43-
API.listSubCalendars(options)
41+
test.each([[{}], [{ includeInactive: null }]])(
42+
'ignored options value',
43+
function (options) {
44+
API.listSubCalendars(options)
4445

45-
expect(Request.get).toHaveBeenCalledTimes(1)
46-
})
46+
expect(Request.get).toHaveBeenCalledTimes(1)
47+
expect(Request.get).toHaveBeenCalledWith('/subcalendars')
48+
}
49+
)
4750

4851
test('invalid option parameter', function () {
4952
expect(() => API.listSubCalendars({ someKey: true })).toThrow()
5053
})
5154

52-
test.each([([], 'string', true, false, 123)])(
55+
test.each([([], 'string', true, false, 123, null)])(
5356
'invalid options',
5457
function (option) {
5558
expect(() => API.listSubCalendars(option)).toThrow()
5659
}
5760
)
61+
62+
test('ignored option: undefined', function () {
63+
API.listSubCalendars(undefined)
64+
65+
expect(Request.get).toHaveBeenCalledTimes(1)
66+
expect(Request.get).toHaveBeenCalledWith('/subcalendars')
67+
})
5868
})
5969

6070
describe('method: listSubCalendar', function () {
6171
test('with id (number)', function () {
6272
API.listSubCalendar(1234)
6373

6474
expect(Request.get).toHaveBeenCalledTimes(1)
75+
expect(Request.get).toHaveBeenCalledWith('/subcalendars/1234')
6576
})
6677

6778
test('with id (string)', function () {
6879
API.listSubCalendar('1234')
6980

7081
expect(Request.get).toHaveBeenCalledTimes(1)
82+
expect(Request.get).toHaveBeenCalledWith('/subcalendars/1234')
7183
})
7284

7385
test('without id', function () {
7486
expect(() => API.listSubCalendar()).toThrow()
7587
})
7688

77-
test.each([[], {}, true, false, 'text'])('invalid id', function (id) {
78-
expect(() => API.listSubCalendar(id)).toThrow()
79-
})
89+
test.each([[], {}, true, false, 'text', null, undefined])(
90+
'invalid id',
91+
function (id) {
92+
expect(() => API.listSubCalendar(id)).toThrow()
93+
}
94+
)
8095
})
8196
})

__tests__/helpers.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
exports.buildParams = function (params) {
2+
let urlParams = ''
3+
Object.keys(params).forEach(function (key) {
4+
if (key === 'subcalendarId') {
5+
params[key].forEach((id) => {
6+
urlParams += `${key}[]=${id}&`
7+
})
8+
} else {
9+
urlParams += `${key}=${params[key]}&`
10+
}
11+
})
12+
return urlParams.substring(0, urlParams.length - 1)
13+
}

examples/example.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,25 @@ const { Events, SubCalendar } = new Client({
99
;(async function getLeaves() {
1010
// fetch id for leave calendar
1111
let leaveCalId
12-
await SubCalendar.listSubCalendars({ includeInactive: true }).then(
13-
({ data: { subcalendars } }) => {
12+
await SubCalendar.listSubCalendars()
13+
.then(({ data: { subcalendars } }) => {
1414
for (const subCal of subcalendars) {
1515
if (subCal.name === 'leave') {
1616
leaveCalId = subCal.id
1717
break
1818
}
1919
}
20-
}
21-
)
20+
})
21+
.catch((err) => console.log(err.response.data))
2222

23-
// get all events identified by leaveCalId
24-
const {
25-
data: { events },
26-
} = await Events.listEvents({
27-
startDate: '2022-06-06',
28-
endDate: '2022-06-10',
29-
subcalendarId: leaveCalId,
30-
})
31-
32-
// leave events
33-
console.log(events)
23+
if (leaveCalId) {
24+
// get all events identified by leaveCalId
25+
await Events.listEvents({
26+
startDate: '2022-06-06',
27+
endDate: '2022-06-10',
28+
subcalendarId: [leaveCalId],
29+
})
30+
.then(({ data: { events } }) => console.log(events))
31+
.catch((err) => console.log(err.response.data))
32+
}
3433
})()

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ module.exports = {
33
clearMocks: true,
44

55
// The glob patterns Jest uses to detect test files
6-
testMatch: ['**/__tests__/**/*.[jt]s'],
6+
testMatch: ['**/__tests__/**/*.(spec|test).[jt]s'],
77
}

lib/API.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,22 @@ module.exports = class API {
2929
this._Logger.error(msg)
3030
}
3131
}
32+
33+
_buildUrlParams(params) {
34+
let urlParams = ''
35+
36+
Object.keys(params).forEach((key) => {
37+
if (params[key] !== null && params[key] !== undefined) {
38+
if (params[key] instanceof Array) {
39+
params[key].forEach((value) => {
40+
urlParams += `${key}[]=${value}&`
41+
})
42+
} else {
43+
urlParams += `${key}=${params[key]}&`
44+
}
45+
}
46+
})
47+
48+
return urlParams.substring(0, urlParams.length - 1)
49+
}
3250
}

lib/Client.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ module.exports = class Client {
1717
}
1818

1919
getBaseUrl() {
20-
return join(this.#url, this.#calToken)
20+
return join(this.#url, this.#calToken).replace(
21+
/(?<=http(s)?):\/(?!\/)/,
22+
'://'
23+
)
2124
}
2225

2326
getAuthHeader() {

lib/Events.js

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ module.exports = class Events extends API {
2626
) {
2727
this._validateOptionType(params)
2828

29-
const urlParams = this.#buildParams(params)
30-
const path = urlParams
31-
? join(this.#ENDPOINT, `?${urlParams}`)
32-
: this.#ENDPOINT
29+
const urlParams = this.#getUrlParams(params)
30+
const path = urlParams ? `${this.#ENDPOINT}?${urlParams}` : this.#ENDPOINT
3331

3432
return this._Request.get(path)
3533
}
@@ -40,23 +38,10 @@ module.exports = class Events extends API {
4038
return this._Request.get(join(this.#ENDPOINT, eventId.toString()))
4139
}
4240

43-
#buildParams(params) {
41+
#getUrlParams(params) {
4442
this.#validateParams(params)
4543

46-
let urlParams = ''
47-
Object.keys(params).forEach((key) => {
48-
if (params[key]) {
49-
if (key === 'subcalendarId') {
50-
params[key].forEach((id) => {
51-
urlParams += `${key}[]=${id}&`
52-
})
53-
} else {
54-
urlParams += `${key}=${params[key]}&`
55-
}
56-
}
57-
})
58-
59-
return urlParams.substring(0, urlParams.length - 1)
44+
return this._buildUrlParams(params)
6045
}
6146

6247
#validateParams(params) {
@@ -65,29 +50,35 @@ module.exports = class Events extends API {
6550
this._Logger.error(`Invalid url param: '${key}'`)
6651
}
6752

68-
if (params[key]) {
69-
if (
70-
key === 'format' &&
71-
!this.#URL_PARAMS.format.split('|').includes(params[key])
72-
) {
73-
this._Logger.error(`Format can either be 'html' or 'markdown'`)
53+
if (params[key] !== null) {
54+
if (key === 'format') {
55+
if (!this.#URL_PARAMS.format.split('|').includes(params[key])) {
56+
this._Logger.error(`Format can either be 'html' or 'markdown'`)
57+
}
58+
return
7459
}
75-
76-
if (key !== 'format') {
77-
if (key === 'subcalendarId' && !params[key] instanceof Array) {
78-
this._Logger.error(
79-
`Parameter '${key}' must be of type '${this.#URL_PARAMS[key]}'`
80-
)
81-
} else if (
82-
key !== 'subcalendarId' &&
83-
typeof params[key] !== this.#URL_PARAMS[key]
60+
if (key === 'subcalendarId') {
61+
if (
62+
!(params[key] instanceof Array) ||
63+
typeof params[key] == 'string'
8464
) {
8565
this._Logger.error(
86-
`Parameter '${key}' must be of type '${
87-
this.#URL_PARAMS[key]
88-
}'. Given '${typeof params[key]}'`
66+
`Parameter '${key}' must be of type '${this.#URL_PARAMS[key]}'`
8967
)
9068
}
69+
return
70+
}
71+
if (typeof params[key] !== this.#URL_PARAMS[key]) {
72+
this._Logger.error(
73+
`Parameter '${key}' must be of type '${
74+
this.#URL_PARAMS[key]
75+
}'. Given '${typeof params[key]}'`
76+
)
77+
}
78+
if (key === 'startDate' || key === 'endDate') {
79+
if (params[key] === '') {
80+
this._Logger.error(`Invalid date for '${key}': ${params[key]}`)
81+
}
9182
}
9283
}
9384
})

0 commit comments

Comments
 (0)