Skip to content

Commit 5df3de9

Browse files
author
Ilya Nevolin
committed
unit tests improved
1 parent fdd26da commit 5df3de9

File tree

2 files changed

+92
-48
lines changed

2 files changed

+92
-48
lines changed

spurwing.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/* Copyright Spurwing.io and Healthie Inc.
2+
* Released under the MIT license
3+
* https://www.spurwing.io/
4+
*/
5+
16
const Spurwing = (function() {
27
'use strict';
38

@@ -7,45 +12,42 @@ const Spurwing = (function() {
712
}
813

914
this.get_appointment_types = async function(provider_id, clients_can_book) {
10-
return await this.GET(this.API_URL + 'appointment_types.json', { provider_id, clients_can_book })
15+
return await this.HTTP('GET', this.API_URL + 'appointment_types.json', { provider_id, clients_can_book })
1116
}
1217
this.get_days_available = async function(provider_id, appointment_type_id, date_from_month, timezone, org_level) {
13-
return await this.GET(this.API_URL + 'bookings/days_available.json', { provider_id, appointment_type_id, date_from_month, timezone, org_level })
18+
return await this.HTTP('GET', this.API_URL + 'bookings/days_available.json', { provider_id, appointment_type_id, date_from_month, timezone, org_level })
1419
}
1520
this.get_slots_available = async function(provider_id, appointment_type_id, start_date, end_date, org_level) {
16-
return await this.GET(this.API_URL + 'bookings/slots_available.json', { provider_id, appointment_type_id, start_date, end_date, org_level })
21+
return await this.HTTP('GET', this.API_URL + 'bookings/slots_available.json', { provider_id, appointment_type_id, start_date, end_date, org_level })
1722
}
1823
this.complete_booking = async function(provider_id, appointment_type_id, date, timezone, first_name, last_name, email, phone_number, contact_type) {
19-
return await this.POST(this.API_URL + 'bookings/complete_booking.json', { provider_id, appointment_type_id, date, timezone, first_name, last_name, email, phone_number, contact_type })
24+
return await this.HTTP('POST', this.API_URL + 'bookings/complete_booking.json', { provider_id, appointment_type_id, date, timezone, first_name, last_name, email, phone_number, contact_type })
2025
}
21-
22-
this.GET = async function(url, params) {
23-
return new Promise((resolve, reject) => {
24-
this.ajax({
25-
method: 'GET',
26-
url: url,
27-
data: params,
28-
success: ((data) => resolve(data)),
29-
error: ((data) => reject(data)),
30-
});
31-
}).catch(error => {console.error('GET error:', error) ; throw error});
26+
this.list_appointments = async function(authorization, page_size, offset, provider_id) {
27+
return await this.HTTP('GET', this.API_URL + 'appointments', { page_size, offset, provider_id }, { authorization: 'Bearer ' + authorization })
28+
}
29+
this.delete_appointment = async function(appointment_id, authorization) {
30+
return await this.HTTP('DELETE', this.API_URL + 'appointments/' + appointment_id, {}, { authorization: 'Bearer ' + authorization })
3231
}
33-
this.POST = async function(url, data) {
32+
33+
this.HTTP = async function(method, url, data, headers) {
3434
return new Promise((resolve, reject) => {
3535
this.ajax({
36-
method: 'POST',
36+
method: method,
3737
url: url,
3838
data: data,
39+
headers: headers || {},
3940
success: ((data) => resolve(data)),
4041
error: ((data) => reject(data)),
4142
});
42-
}).catch(error =>{console.error('POST error:', error) ; throw error});
43+
}).catch(error =>{console.error(method + ' error:', error) ; throw error});
4344
}
45+
4446
this.ajax = function(option) {
4547
function isEmpty(obj) {
4648
return Object.keys(obj).length === 0;
4749
}
48-
let {method, url, success, error} = option;
50+
let {method, url, success, error, headers} = option;
4951
if (!method)
5052
return console.error('not set method');
5153
if (!url)
@@ -74,6 +76,9 @@ const Spurwing = (function() {
7476
xhr.open(method, url);
7577
xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
7678
xhr.setRequestHeader('Accept', 'application/json, text/javascript');
79+
for (const hdr in headers) {
80+
xhr.setRequestHeader(hdr, headers[hdr]);
81+
}
7782
xhr.send(data);
7883
xhr.onreadystatechange = function () {
7984
if (xhr.readyState === 4) {

tests.js

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,80 @@
1+
/* Copyright Spurwing.io and Healthie Inc.
2+
* Released under the MIT license
3+
* https://www.spurwing.io/
4+
*/
5+
6+
// chai testing framework: https://www.chaijs.com/guide/styles/
17
chai.should();
28
const expect = chai.expect;
3-
// https://www.chaijs.com/guide/styles/
49

5-
let sp = new Spurwing();
6-
const PID = 'your-provider-id';
10+
11+
const urlParams = new URLSearchParams(window.location.search);
12+
const PID = urlParams.get('pid'); // 'your spurwing provider-id';
13+
const KEY = urlParams.get('key') // 'your spurwing api-key'
14+
15+
async function runner(testname, func) {
16+
try {
17+
console.log(testname, 'STARTED')
18+
await func()
19+
console.log(testname, 'PASSED')
20+
} catch (err) {
21+
console.error(testname, 'FAILED')
22+
console.error(err)
23+
}
24+
}
25+
26+
(async () => {
27+
28+
await runner('TEST 1', (async() => {
29+
let sp = new Spurwing();
30+
let tz = "Europe/Brussels";
31+
32+
let A = await sp.get_appointment_types(PID, true)
33+
console.log(A)
34+
A.should.have.lengthOf(3) // default 3
35+
36+
let B = await sp.get_days_available(PID, A[0].id, dateNow(), tz, false)
37+
console.log(B)
38+
expect(B.days_available.length).to.be.at.least(1) // at least one day available this month
39+
40+
let C = await sp.get_slots_available(PID, A[0].id, dateNow(), dateTomorrow(), false)
41+
console.log(C)
42+
expect(C.slots_available.length).to.be.at.least(10) // each day has 96 15min slots (60*24 / 15 == 96)
43+
44+
let slot = C.slots_available[5].date
45+
let D = await sp.complete_booking(PID, A[0].id, slot, tz, 'Ilya', 'Nevolin', 'ilya2@nevolin.be', '111-111-7777', 'My Contact Type');
46+
console.log(D)
47+
D.should.have.property('appointment')
48+
expect(D.appointment.length).to.equal(60)
49+
50+
let apid = D.appointment.id;
51+
let F = await sp.delete_appointment(apid, KEY)
52+
console.log(F)
53+
F.should.have.property('data')
54+
F.data.should.have.property('appointment')
55+
F.data.appointment.id.should.equal(D.appointment.id)
56+
F.errors.should.have.lengthOf(0)
57+
}));
58+
59+
await runner('TEST 2', (async() => {
60+
let sp = new Spurwing();
61+
let E = await sp.list_appointments(KEY, 1000, 0, PID)
62+
console.log(E)
63+
}));
64+
65+
})();
66+
67+
//////////////////////////////
68+
////// helper functions //////
69+
//////////////////////////////
770

871
function dateNow() {
972
let d = new Date();
1073
return d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate();
1174
}
75+
1276
function dateTomorrow() {
1377
let d = new Date();
1478
d.setDate(d.getDate() + 1);
1579
return d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate();
16-
}
17-
18-
(async () => {
19-
console.log('UNIT TEST 1:')
20-
let tz = "Europe/Brussels";
21-
22-
let A = await sp.get_appointment_types(PID, true)
23-
console.log(A)
24-
A.should.have.lengthOf(3) // default 3
25-
26-
let B = await sp.get_days_available(PID, A[0].id, dateNow(), tz, false)
27-
console.log(B)
28-
expect(B.days_available.length).to.be.at.least(1) // at least one day available this month
29-
30-
let C = await sp.get_slots_available(PID, A[0].id, dateNow(), dateTomorrow(), false)
31-
console.log(C)
32-
expect(C.slots_available.length).to.be.at.least(96) // each day has 96 15min slots (60*24 / 15 == 96)
33-
34-
let slot = C.slots_available[5].date
35-
console.log(slot)
36-
37-
let D = await sp.complete_booking(PID, A[0].id, slot, tz, 'Ilya', 'Nevolin', 'ilya2@nevolin.be', '111-111-7777', 'My Contact Type');
38-
console.log(D)
39-
D.should.have.property('appointment')
40-
expect(D.appointment.length).to.equal(60)
41-
})();
80+
}

0 commit comments

Comments
 (0)