cronofy-elements
Advanced tools
Comparing version 1.26.1 to 1.27.0
{ | ||
"name": "cronofy-elements", | ||
"version": "1.26.1", | ||
"version": "1.27.0", | ||
"description": "Fast track scheduling with Cronofy's embeddable UI Elements", | ||
@@ -5,0 +5,0 @@ "main": "build/npm/CronofyElements.js", |
@@ -71,3 +71,4 @@ import React, { useState, useEffect } from "react"; | ||
callback: options.callback ? options.callback : cb => {}, | ||
startDay: options.config.startDay | ||
startDay: options.config.startDay, | ||
defaultWeeklyPeriods: options.config.defaultWeeklyPeriods | ||
})); | ||
@@ -204,3 +205,6 @@ const [extras, setExtras] = useState(() => parseExtras(options)); | ||
const rulesResponse = isNewRule | ||
? buildRuleTemplate(options.tzid) | ||
? buildRuleTemplate( | ||
options.config.defaultWeeklyPeriods, | ||
options.tzid | ||
) | ||
: res[0]; | ||
@@ -248,3 +252,3 @@ | ||
active: rulesResponse.availability_rule.calendar_ids || [], | ||
all: calsResponse.calendars, | ||
all: calsResponse.parsed, | ||
profiles: calsResponse.profiles | ||
@@ -251,0 +255,0 @@ }); |
@@ -93,14 +93,6 @@ import * as mocks from "./mocks"; | ||
export const parseRulesCalendars = response => { | ||
// Filter deleted calendars | ||
const profiles = response["cronofy.data"].profiles.map(profile => { | ||
const profile_calendars = profile.profile_calendars.filter( | ||
cal => !cal.calendar_deleted | ||
); | ||
return { ...profile, profile_calendars }; | ||
}); | ||
// Format and flatten calendars into 1D array | ||
const calendars = profiles | ||
.map(profile => | ||
profile.profile_calendars.map(cal => ({ | ||
const parsed = response["cronofy.data"].profiles.map(profile => { | ||
return profile.profile_calendars | ||
.filter(cal => !cal.calendar_deleted) | ||
.map(cal => ({ | ||
name: cal.calendar_name, | ||
@@ -110,9 +102,8 @@ id: cal.calendar_id, | ||
provider_name: profile.provider_name | ||
})) | ||
) | ||
.reduce((acc, curr) => [...acc, ...curr], []); | ||
})); | ||
}); | ||
return { | ||
calendars, | ||
profiles | ||
parsed: parsed.reduce((acc, curr) => [...acc, ...curr], []), | ||
profiles: response["cronofy.data"].profiles | ||
}; | ||
@@ -119,0 +110,0 @@ }; |
@@ -9,2 +9,3 @@ import { | ||
import { logConstructor } from "./logging"; | ||
import { default_availability_rules } from "./mocks"; | ||
@@ -24,3 +25,3 @@ export const parseAvailabilityRulesOptions = (options = {}) => { | ||
const validStartDays = [ | ||
const validDays = [ | ||
"sunday", | ||
@@ -34,3 +35,3 @@ "monday", | ||
]; | ||
const validStartDay = validStartDays.includes(startDay); | ||
const validStartDay = validDays.includes(startDay); | ||
@@ -47,2 +48,79 @@ if (!validStartDay) { | ||
const defaultWeeklyPeriods = | ||
typeof config.default_weekly_periods === "undefined" | ||
? default_availability_rules | ||
: config.default_weekly_periods; | ||
const validateDefaultWeeklyPeriods = (default_weekly_periods, log) => { | ||
if (!Array.isArray(default_weekly_periods)) { | ||
return false; | ||
} | ||
if (default_weekly_periods.length < 1) { | ||
return false; | ||
} | ||
let errors = {}; | ||
default_weekly_periods.forEach((dwp, index) => { | ||
if ( | ||
dwp === null || | ||
typeof dwp !== "object" || | ||
!("day" in dwp) || | ||
!("start_time" in dwp) || | ||
!("end_time" in dwp) | ||
) { | ||
errors[`default_weekly_period[${index}]`] = [ | ||
"default weekly period must be an object with day, start_time and end_time properties" | ||
]; | ||
return false; | ||
} | ||
let out = []; | ||
if (!validDays.includes(dwp.day)) { | ||
out.push( | ||
"Please provide a valid day. '" + | ||
`${dwp.day}` + | ||
"' is not a supported value." | ||
); | ||
} | ||
if (!moment(dwp.start_time, "HH:mm", true).isValid()) { | ||
out.push("start_time must be a valid time string"); | ||
} | ||
if (!moment(dwp.end_time, "HH:mm", true).isValid()) { | ||
out.push("end_time must be a valid time string"); | ||
} | ||
if (dwp.start_time >= dwp.end_time) { | ||
out.push("end must be after start"); | ||
} | ||
if (out.length > 0) errors[`default_weekly_period[${index}]`] = out; | ||
}); | ||
if (Object.keys(errors).length > 0) { | ||
log.error( | ||
"Some of the provided `default_weekly_periods` option have errors.", | ||
{ | ||
docsSlug: "availability-viewer/#default_weekly_periods", | ||
errorObject: errors | ||
} | ||
); | ||
return false; | ||
} | ||
return true; | ||
}; | ||
const validDefaultWeeklyPeriods = validateDefaultWeeklyPeriods( | ||
defaultWeeklyPeriods, | ||
log | ||
); | ||
if (!validDefaultWeeklyPeriods) { | ||
log.error("The provided default weekly periods are invalid"); | ||
return false; | ||
} | ||
options.error = false; | ||
@@ -88,2 +166,3 @@ const demoMode = typeof options.demo === "undefined" ? false : options.demo; | ||
delete config.week_start_day; | ||
delete config.default_weekly_periods; | ||
@@ -96,5 +175,5 @@ return { | ||
domains, | ||
config: { ...config, logs, startDay }, | ||
config: { ...config, logs, startDay, defaultWeeklyPeriods }, | ||
translations | ||
}; | ||
}; |
import moment from "moment-timezone"; | ||
import { default_availability_rules } from "./mocks"; | ||
@@ -101,3 +100,3 @@ export const buildSlotID = (dayIndex, slotIndex) => { | ||
export const buildRuleTemplate = tzid => ({ | ||
export const buildRuleTemplate = (default_weekly_periods, tzid) => ({ | ||
availability_rule: { | ||
@@ -107,4 +106,4 @@ availability_rule_id: "default", | ||
calendar_ids: [], | ||
weekly_periods: default_availability_rules | ||
weekly_periods: default_weekly_periods | ||
} | ||
}); |
@@ -1,10 +0,3 @@ | ||
import moment from "moment-timezone"; | ||
import { parseQuery } from "../src/js/helpers/init"; | ||
import { parseAgendaOptions } from "../src/js/helpers/init.Agenda"; | ||
import { parseAvailabilityRulesOptions } from "../src/js/helpers/init.AvailabilityRules"; | ||
import { parseAvailabilityViewerOptions } from "../src/js/helpers/init.AvailabilityViewer"; | ||
import { parseCalendarSyncOptions } from "../src/js/helpers/init.CalendarSync"; | ||
import { parseSlotPickerOptions } from "../src/js/helpers/init.SlotPicker"; | ||
import { parseQuery, parseGridTime } from "../src/js/helpers/init"; | ||
// The parsers rely on console warning and errors to | ||
@@ -20,1290 +13,2 @@ // comunicate problems to the users | ||
const generic_query_periods = [ | ||
{ | ||
start: moment().toISOString(), | ||
end: moment().add(1, "days").toISOString() | ||
} | ||
]; | ||
describe("Parsing Agenda options", () => { | ||
it("works when all options are declared", () => { | ||
const input = { | ||
data_center: "de", | ||
target_id: "TARGET", | ||
element_token: "TOKEN" | ||
}; | ||
const expectedOutput = { | ||
config: { logs: "warn", mode: "default" }, | ||
data_center: "de", | ||
domains: { | ||
apiDomain: "https://api-de.cronofy.com", | ||
appDomain: "https://app-de.cronofy.com" | ||
}, | ||
target: "TARGET", | ||
token: "TOKEN" | ||
}; | ||
const result = parseAgendaOptions(input); | ||
expect(result.data_center).toEqual(expectedOutput.data_center); | ||
expect(result.domains).toEqual(expectedOutput.domains); | ||
expect(result.target).toEqual(expectedOutput.target); | ||
expect(result.token).toEqual(expectedOutput.token); | ||
expect(result.config).toEqual(expectedOutput.config); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
}); | ||
it("supports valid modes", () => { | ||
const input = { | ||
config: { mode: "free_busy" }, | ||
target_id: "TARGET", | ||
element_token: "TOKEN" | ||
}; | ||
const expectedConfig = { logs: "warn", mode: "free_busy" }; | ||
const result = parseAgendaOptions(input); | ||
expect(result.config).toEqual(expectedConfig); | ||
expect(result.config.mode).toEqual("free_busy"); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
}); | ||
it("errors when given an invalid mode", () => { | ||
const input = { | ||
config: { mode: "INVALID_MODE" }, | ||
target_id: "TARGET", | ||
element_token: "TOKEN" | ||
}; | ||
const result = parseAgendaOptions(input); | ||
expect(result).toEqual(false); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
}); | ||
it("fails if there is no token declared", () => { | ||
expect(parseAgendaOptions({ target_id: "TARGET" })).toEqual(false); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
it("continues if there is no token declared, but we're in demo mode", () => { | ||
const expectedOutput = { | ||
demo: true, | ||
domains: { | ||
apiDomain: "https://api.cronofy.com", | ||
appDomain: "https://app.cronofy.com" | ||
}, | ||
target: "cronofy-element-agenda", | ||
token: false | ||
}; | ||
const result = parseAgendaOptions({ demo: true }); | ||
expect(result.demo).toEqual(expectedOutput.demo); | ||
expect(result.token).toEqual(expectedOutput.token); | ||
expect(console.warn).toHaveBeenCalledTimes(2); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("applies the default target if there is no target option", () => { | ||
const expectedOutput = { | ||
target: "cronofy-element-agenda", | ||
token: "TOKEN", | ||
domains: { | ||
apiDomain: "https://api.cronofy.com", | ||
appDomain: "https://app.cronofy.com" | ||
} | ||
}; | ||
const result = parseAgendaOptions({ element_token: "TOKEN" }); | ||
expect(result.target).toEqual(expectedOutput.target); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("applies the default api_domain if there is no api_domain option", () => { | ||
const expectedOutput = { | ||
domains: { | ||
apiDomain: "https://api.cronofy.com", | ||
appDomain: "https://app.cronofy.com" | ||
}, | ||
target: "TARGET", | ||
token: "TOKEN" | ||
}; | ||
const result = parseAgendaOptions({ | ||
element_token: "TOKEN", | ||
target_id: "TARGET" | ||
}); | ||
expect(result.domains).toEqual(expectedOutput.domains); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
describe("Parsing SlotPicker options", () => { | ||
it("works when all options are declared", () => { | ||
const input = { | ||
target_id: "TARGET", | ||
element_token: "TOKEN", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
} | ||
}; | ||
const result = parseSlotPickerOptions(input); | ||
expect(result.config).toEqual({ | ||
mode: "confirm", | ||
logs: "warn" | ||
}); | ||
expect(result.domains).toEqual({ | ||
apiDomain: "https://api.cronofy.com", | ||
appDomain: "https://app.cronofy.com" | ||
}); | ||
expect(result.query).toEqual({ | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}); | ||
expect(result.target).toEqual("TARGET"); | ||
expect(result.token).toEqual("TOKEN"); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
}); | ||
it("fails if there is no token declared", () => { | ||
expect(parseSlotPickerOptions()).toEqual(false); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
it("continues if there is no token declared, but we're in demo mode", () => { | ||
const expectedOutput = { | ||
config: { | ||
mode: "confirm", | ||
logs: "warn" | ||
}, | ||
demo: true, | ||
domains: { | ||
apiDomain: "https://api.cronofy.com", | ||
appDomain: "https://app.cronofy.com" | ||
}, | ||
query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
target: "cronofy-element-slot-picker", | ||
token: false | ||
}; | ||
const result = parseSlotPickerOptions({ | ||
demo: true, | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
} | ||
}); | ||
expect(result.config).toEqual(expectedOutput.config); | ||
expect(result.domains).toEqual(expectedOutput.domains); | ||
expect(result.target).toEqual(expectedOutput.target); | ||
expect(console.warn).toHaveBeenCalledTimes(2); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("applies the default target if there is no target option", () => { | ||
expect( | ||
parseSlotPickerOptions({ | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
element_token: "TOKEN" | ||
}).target | ||
).toEqual("cronofy-element-slot-picker"); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("applies the default domains if there is no data_center option", () => { | ||
expect( | ||
parseSlotPickerOptions({ | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
element_token: "TOKEN", | ||
target_id: "TARGET" | ||
}).domains | ||
).toEqual({ | ||
apiDomain: "https://api.cronofy.com", | ||
appDomain: "https://app.cronofy.com" | ||
}); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("creates correct domains if there is a data_center option", () => { | ||
expect( | ||
parseSlotPickerOptions({ | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
data_center: "de", | ||
element_token: "TOKEN", | ||
target_id: "TARGET" | ||
}).domains | ||
).toEqual({ | ||
apiDomain: "https://api-de.cronofy.com", | ||
appDomain: "https://app-de.cronofy.com" | ||
}); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
describe("Parsing AvailabilityViewer options", () => { | ||
const sniffedTimezone = moment.tz.guess(); | ||
it("works when all options are declared", () => { | ||
const input = { | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { | ||
bounds_control: true, | ||
start_time: "01:00", | ||
end_time: "02:00", | ||
interval: 60, | ||
week_start_day: "monday" | ||
}, | ||
data_center: "DATA_CENTER", | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
tzid: sniffedTimezone | ||
}; | ||
const expectedOutput = { | ||
config: { | ||
boundsControl: true, | ||
end_time: "02:00", | ||
interval: 60, | ||
logs: "warn", | ||
mode: "confirm", | ||
slot_selection: "available", | ||
startDay: "monday", | ||
start_time: "01:00" | ||
}, | ||
customtzid: true, | ||
data_center: "DATA_CENTER", | ||
domains: { | ||
apiDomain: "https://api-DATA_CENTER.cronofy.com", | ||
appDomain: "https://app-DATA_CENTER.cronofy.com" | ||
}, | ||
error: false, | ||
query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
target: "TARGET", | ||
token: "TOKEN", | ||
tzid: sniffedTimezone | ||
}; | ||
const result = parseAvailabilityViewerOptions(input); | ||
expect(result.config).toEqual(expectedOutput.config); | ||
expect(result.customtzid).toEqual(expectedOutput.customtzid); | ||
expect(result.data_center).toEqual(expectedOutput.data_center); | ||
expect(result.domains).toEqual(expectedOutput.domains); | ||
expect(result.error).toEqual(expectedOutput.error); | ||
expect(result.query).toEqual(expectedOutput.query); | ||
expect(result.target).toEqual(expectedOutput.target); | ||
expect(result.token).toEqual(expectedOutput.token); | ||
expect(result.tzid).toEqual(expectedOutput.tzid); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
}); | ||
it("returns an error if there is no query present when all other required options are declared", () => { | ||
const input = { | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
tzid: sniffedTimezone, | ||
config: { start_time: "01:00", end_time: "02:00" } | ||
}; | ||
const output = parseAvailabilityViewerOptions(input); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
}); | ||
it("fails if there is no token declared", () => { | ||
expect(parseAvailabilityViewerOptions()).toEqual(false); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
it("continues if there is no token declared, but we're in demo mode", () => { | ||
const output = parseAvailabilityViewerOptions({ | ||
demo: true, | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { start_time: "01:00", end_time: "02:00" } | ||
}); | ||
expect(output.query).toEqual({ | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}); | ||
expect(output.token).toEqual(false); | ||
expect(output.target).toEqual("cronofy-element-availability-viewer"); | ||
expect(output.tzid).toEqual(sniffedTimezone); | ||
expect(console.warn).toHaveBeenCalledTimes(2); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("applies the default target if there is no target option", () => { | ||
const output = parseAvailabilityViewerOptions({ | ||
element_token: "TOKEN", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { start_time: "01:00", end_time: "02:00" } | ||
}); | ||
expect(output.token).toEqual("TOKEN"); | ||
expect(output.query).toEqual({ | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}); | ||
expect(output.target).toEqual("cronofy-element-availability-viewer"); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("applies the default domains if there is no data_center option", () => { | ||
const expectedDomains = { | ||
apiDomain: "https://api.cronofy.com", | ||
appDomain: "https://app.cronofy.com" | ||
}; | ||
const output = parseAvailabilityViewerOptions({ | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { start_time: "01:00", end_time: "02:00" } | ||
}); | ||
expect(output.token).toEqual("TOKEN"); | ||
expect(output.target).toEqual("TARGET"); | ||
expect(output.query).toEqual({ | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}); | ||
expect(output.domains).toEqual(expectedDomains); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("creates correct domains if there is a data_center option", () => { | ||
const expectedDomains = { | ||
apiDomain: "https://api-de.cronofy.com", | ||
appDomain: "https://app-de.cronofy.com" | ||
}; | ||
const output = parseAvailabilityViewerOptions({ | ||
data_center: "de", | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { start_time: "01:00", end_time: "02:00" } | ||
}); | ||
expect(output.token).toEqual("TOKEN"); | ||
expect(output.target).toEqual("TARGET"); | ||
expect(output.query).toEqual({ | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}); | ||
expect(output.domains).toEqual(expectedDomains); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly parses the config.mode", () => { | ||
const output = parseAvailabilityViewerOptions({ | ||
data_center: "de", | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
config: { | ||
mode: "multi_select", | ||
start_time: "01:00", | ||
end_time: "02:00" | ||
}, | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
} | ||
}); | ||
expect(output.token).toEqual("TOKEN"); | ||
expect(output.target).toEqual("TARGET"); | ||
expect(output.query).toEqual({ | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}); | ||
expect(output.config.mode).toEqual("multi_select"); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly parses an empty config.mode", () => { | ||
const output = parseAvailabilityViewerOptions({ | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { start_time: "01:00", end_time: "02:00" } | ||
}); | ||
expect(output.token).toEqual("TOKEN"); | ||
expect(output.target).toEqual("TARGET"); | ||
expect(output.query).toEqual({ | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}); | ||
expect(output.config.mode).toEqual("confirm"); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly parses the config.customBounds", () => { | ||
const output = parseAvailabilityViewerOptions({ | ||
data_center: "de", | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
config: { | ||
bounds_control: true, | ||
start_time: "01:00", | ||
end_time: "02:00" | ||
}, | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
} | ||
}); | ||
expect(output.token).toEqual("TOKEN"); | ||
expect(output.target).toEqual("TARGET"); | ||
expect(output.query).toEqual({ | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}); | ||
expect(output.config.boundsControl).toEqual(true); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly parses an empty config.customBounds", () => { | ||
const output = parseAvailabilityViewerOptions({ | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { start_time: "01:00", end_time: "02:00" } | ||
}); | ||
expect(output.token).toEqual("TOKEN"); | ||
expect(output.target).toEqual("TARGET"); | ||
expect(output.query).toEqual({ | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}); | ||
expect(output.config.boundsControl).toEqual(false); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly parses default config.slotSelection", () => { | ||
const defaultOutput = parseAvailabilityViewerOptions({ | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
config: { | ||
slot_selection: "availability", | ||
start_time: "01:00", | ||
end_time: "02:00" | ||
}, | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
} | ||
}); | ||
expect(defaultOutput.config.slot_selection).toEqual("availability"); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly parses empty config.slotSelection", () => { | ||
const withoutSelection = parseAvailabilityViewerOptions({ | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { | ||
start_time: "01:00", | ||
end_time: "02:00" | ||
} | ||
}); | ||
expect(withoutSelection.config.slot_selection).toEqual("available"); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly parses config.slotSelection", () => { | ||
const withSelection = parseAvailabilityViewerOptions({ | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
config: { | ||
slot_selection: "restricted", | ||
start_time: "01:00", | ||
end_time: "02:00" | ||
}, | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
} | ||
}); | ||
expect(withSelection.config.slot_selection).toEqual("restricted"); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly parses tzid option", () => { | ||
const input = { | ||
data_center: "DATA_CENTER", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { | ||
start_time: "01:00", | ||
end_time: "02:00" | ||
}, | ||
target_id: "TARGET", | ||
element_token: "TOKEN", | ||
tzid: sniffedTimezone | ||
}; | ||
const result = parseAvailabilityViewerOptions(input); | ||
expect(result.customtzid).toEqual(true); | ||
expect(result.tzid).toEqual(sniffedTimezone); | ||
}); | ||
it("correctly falls back when given an invalid tzid option", () => { | ||
const input = { | ||
data_center: "DATA_CENTER", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { | ||
start_time: "01:00", | ||
end_time: "02:00" | ||
}, | ||
target_id: "TARGET", | ||
element_token: "TOKEN", | ||
tzid: "A_NON_IANA_TZID" | ||
}; | ||
const result = parseAvailabilityViewerOptions(input); | ||
expect(result.customtzid).toEqual(true); | ||
expect(result.tzid).toEqual(sniffedTimezone); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
}); | ||
it("correctly adds a fallback tzid option", () => { | ||
const input = { | ||
data_center: "DATA_CENTER", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { | ||
start_time: "01:00", | ||
end_time: "02:00" | ||
}, | ||
target_id: "TARGET", | ||
element_token: "TOKEN" | ||
}; | ||
const result = parseAvailabilityViewerOptions(input); | ||
expect(result.customtzid).toEqual(false); | ||
expect(result.tzid).toEqual(sniffedTimezone); | ||
}); | ||
it("correctly adds fallback config.start_time and config.end_time", () => { | ||
const input = { | ||
data_center: "DATA_CENTER", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
target_id: "TARGET", | ||
element_token: "TOKEN" | ||
}; | ||
const result = parseAvailabilityViewerOptions(input); | ||
expect(result.config.start_time).toEqual("09:00"); | ||
expect(result.config.end_time).toEqual("17:00"); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly fallbacks when config.start_time is invalid", () => { | ||
const input = { | ||
data_center: "DATA_CENTER", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { | ||
start_time: "A string that is not a time", | ||
end_time: "13:00" | ||
}, | ||
target_id: "TARGET", | ||
element_token: "TOKEN" | ||
}; | ||
const result = parseAvailabilityViewerOptions(input); | ||
expect(result.config.start_time).toEqual("09:00"); | ||
expect(result.config.end_time).toEqual("13:00"); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.warn.mock.calls[0][0]).toContain( | ||
"config.start_time: using default value of 09:00" | ||
); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly fallbacks when config.end_time is invalid", () => { | ||
const input = { | ||
data_center: "DATA_CENTER", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { | ||
start_time: "08:00", | ||
end_time: 1234 | ||
}, | ||
target_id: "TARGET", | ||
element_token: "TOKEN" | ||
}; | ||
const result = parseAvailabilityViewerOptions(input); | ||
expect(result.config.start_time).toEqual("08:00"); | ||
expect(result.config.end_time).toEqual("17:00"); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.warn.mock.calls[0][0]).toContain( | ||
"config.end_time: using default value of 17:00" | ||
); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly rounds config.start_time to valid interval", () => { | ||
const input = { | ||
data_center: "DATA_CENTER", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
config: { | ||
start_time: "08:25" | ||
}, | ||
target_id: "TARGET", | ||
element_token: "TOKEN" | ||
}; | ||
const result = parseAvailabilityViewerOptions(input); | ||
expect(result.config.start_time).toEqual("08:15"); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.warn.mock.calls[0][0]).toContain( | ||
"config.start_time: rounded from 08:25 to 08:15" | ||
); | ||
}); | ||
it("correctly rounds config.start_time to valid interval", () => { | ||
const input = { | ||
data_center: "DATA_CENTER", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
target_id: "TARGET", | ||
element_token: "TOKEN" | ||
}; | ||
const config_1 = { | ||
config: { | ||
end_time: "17:25" | ||
} | ||
}; | ||
const result_1 = parseAvailabilityViewerOptions({ | ||
...input, | ||
...config_1 | ||
}); | ||
expect(result_1.config.end_time).toEqual("17:30"); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.warn.mock.calls[0][0]).toContain( | ||
"config.end_time: rounded from 17:25 to 17:30" | ||
); | ||
console.warn.mockClear(); | ||
const config_2 = { | ||
config: { | ||
start_time: "08:38" | ||
} | ||
}; | ||
const result_2 = parseAvailabilityViewerOptions({ | ||
...input, | ||
...config_2 | ||
}); | ||
expect(result_2.config.start_time).toEqual("08:30"); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.warn.mock.calls[0][0]).toContain( | ||
"config.start_time: rounded from 08:38 to 08:30" | ||
); | ||
}); | ||
it("adjusts config.interval to match config.start_time when needed", () => { | ||
const input = { | ||
data_center: "DATA_CENTER", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
target_id: "TARGET", | ||
element_token: "TOKEN" | ||
}; | ||
const config_1 = { | ||
config: { | ||
start_time: "08:30", | ||
interval: 60 | ||
} | ||
}; | ||
const result_1 = parseAvailabilityViewerOptions({ | ||
...input, | ||
...config_1 | ||
}); | ||
expect(result_1.config.interval).toEqual(30); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.warn.mock.calls[0][0]).toContain( | ||
"config.interval of 60 minutes has been shortened to 30 minutes for optimal tessellation of results in the grid." | ||
); | ||
console.warn.mockClear(); | ||
const config_2 = { | ||
config: { | ||
start_time: "08:15", | ||
interval: 60 | ||
} | ||
}; | ||
const result_2 = parseAvailabilityViewerOptions({ | ||
...input, | ||
...config_2 | ||
}); | ||
expect(result_2.config.interval).toEqual(15); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.warn.mock.calls[0][0]).toContain( | ||
"config.interval of 60 minutes has been shortened to 15 minutes for optimal tessellation of results in the grid." | ||
); | ||
console.warn.mockClear(); | ||
const config_3 = { | ||
config: { | ||
start_time: "08:45", | ||
interval: 60 | ||
} | ||
}; | ||
const result_3 = parseAvailabilityViewerOptions({ | ||
...input, | ||
...config_3 | ||
}); | ||
expect(result_3.config.interval).toEqual(15); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.warn.mock.calls[0][0]).toContain( | ||
"config.interval of 60 minutes has been shortened to 15 minutes for optimal tessellation of results in the grid." | ||
); | ||
}); | ||
it("fails if start_time is after end_time", () => { | ||
const input = { | ||
data_center: "DATA_CENTER", | ||
availability_query: { | ||
query_periods: generic_query_periods, | ||
required_duration: "TEST" | ||
}, | ||
target_id: "TARGET", | ||
element_token: "TOKEN", | ||
config: { | ||
start_time: "08:00", | ||
end_time: "06:00", | ||
interval: 60 | ||
} | ||
}; | ||
const result = parseAvailabilityViewerOptions(input); | ||
expect(result).toEqual(false); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
expect(console.error.mock.calls[0][0]).toContain( | ||
"must be earlier than" | ||
); | ||
}); | ||
it("correctly parses grid times", () => { | ||
const simpleTime = parseGridTime("08:00"); | ||
expect(simpleTime.time).toBe("08:00"); | ||
expect(simpleTime.offset).toBe(0); | ||
expect(simpleTime.message).toBe(false); | ||
const offsetTime = parseGridTime("08:30"); | ||
expect(offsetTime.time).toBe("08:30"); | ||
expect(offsetTime.offset).toBe(30); | ||
expect(offsetTime.message).toBe(false); | ||
const invalidTime = parseGridTime("INVALID"); | ||
expect(invalidTime.time).toBe("09:00"); | ||
expect(invalidTime.offset).toBe(0); | ||
expect(invalidTime.message).toContain("using default value"); | ||
const roundedDown = parseGridTime("08:42"); | ||
expect(roundedDown.time).toBe("08:30"); | ||
expect(roundedDown.offset).toBe(30); | ||
expect(roundedDown.message).toContain("rounded"); | ||
const roundedUp = parseGridTime("08:42", "", true); | ||
expect(roundedUp.time).toBe("08:45"); | ||
expect(roundedUp.offset).toBe(45); | ||
expect(roundedUp.message).toContain("rounded"); | ||
const noLeadingZeroTime = parseGridTime("8:00"); | ||
expect(noLeadingZeroTime.time).toBe("09:00"); | ||
expect(noLeadingZeroTime.offset).toBe(0); | ||
expect(noLeadingZeroTime.message).toContain("using default value"); | ||
const emptyTime = parseGridTime(); | ||
expect(emptyTime.time).toBe("09:00"); | ||
expect(emptyTime.offset).toBe(0); | ||
expect(emptyTime.message).toBe(false); | ||
const customFallbackTime = parseGridTime("INVALID", "06:00"); | ||
expect(customFallbackTime.time).toBe("06:00"); | ||
expect(customFallbackTime.offset).toBe(0); | ||
expect(customFallbackTime.message).toContain("using default value"); | ||
}); | ||
}); | ||
describe("Parsing AvailabilityRules options", () => { | ||
const sniffedTimezone = moment.tz.guess(); | ||
it("works when all options are declared", () => { | ||
const input = { | ||
config: { | ||
logs: "info", | ||
week_start_day: "monday" | ||
}, | ||
data_center: "de", | ||
target_id: "TARGET", | ||
element_token: "TOKEN", | ||
availability_rule_id: "RULE_ID", | ||
tzid: sniffedTimezone | ||
}; | ||
const expectedOutput = { | ||
availability_rule_id: "RULE_ID", | ||
config: { logs: "info", startDay: "monday" }, | ||
data_center: "de", | ||
domains: { | ||
apiDomain: "https://api-de.cronofy.com", | ||
appDomain: "https://app-de.cronofy.com" | ||
}, | ||
error: false, | ||
target: "TARGET", | ||
token: "TOKEN", | ||
tzid: sniffedTimezone | ||
}; | ||
const result = parseAvailabilityRulesOptions(input); | ||
expect(result.availability_rule_id).toEqual( | ||
expectedOutput.availability_rule_id | ||
); | ||
expect(result.config).toEqual(expectedOutput.config); | ||
expect(result.data_center).toEqual(expectedOutput.data_center); | ||
expect(result.domains).toEqual(expectedOutput.domains); | ||
expect(result.error).toEqual(expectedOutput.error); | ||
expect(result.target).toEqual(expectedOutput.target); | ||
expect(result.token).toEqual(expectedOutput.token); | ||
expect(result.tzid).toEqual(expectedOutput.tzid); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
}); | ||
it("shows a warning if there is no availability_rule_id present when all other options are declared", () => { | ||
const input = { | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
tzid: sniffedTimezone | ||
}; | ||
parseAvailabilityRulesOptions(input); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
}); | ||
it("fails if there is no token declared", () => { | ||
expect(parseAvailabilityRulesOptions()).toEqual(false); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
it("continues if there is no token declared, but we're in demo mode", () => { | ||
const expectedOutput = { | ||
availability_rule_id: "RULE_ID", | ||
config: {}, | ||
demo: true, | ||
domains: { | ||
apiDomain: "https://api.cronofy.com", | ||
appDomain: "https://app.cronofy.com" | ||
}, | ||
error: false, | ||
target: "cronofy-element-availability-rules", | ||
token: false, | ||
tzid: sniffedTimezone | ||
}; | ||
const result = parseAvailabilityRulesOptions({ | ||
demo: true, | ||
availability_rule_id: "RULE_ID", | ||
tzid: sniffedTimezone | ||
}); | ||
expect(result.demo).toEqual(expectedOutput.demo); | ||
expect(result.token).toEqual(expectedOutput.token); | ||
expect(console.warn).toHaveBeenCalledTimes(2); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("applies the default target if there is no target option", () => { | ||
const expectedOutput = { | ||
availability_rule_id: "RULE_ID", | ||
config: {}, | ||
domains: { | ||
apiDomain: "https://api.cronofy.com", | ||
appDomain: "https://app.cronofy.com" | ||
}, | ||
error: false, | ||
target: "cronofy-element-availability-rules", | ||
token: "TOKEN", | ||
tzid: sniffedTimezone | ||
}; | ||
const result = parseAvailabilityRulesOptions({ | ||
element_token: "TOKEN", | ||
availability_rule_id: "RULE_ID", | ||
tzid: sniffedTimezone | ||
}); | ||
expect(result.target).toEqual(expectedOutput.target); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("applies the default domains if there is no data_center option", () => { | ||
const expectedOutput = { | ||
availability_rule_id: "RULE_ID", | ||
config: {}, | ||
domains: { | ||
apiDomain: "https://api.cronofy.com", | ||
appDomain: "https://app.cronofy.com" | ||
}, | ||
error: false, | ||
target: "TARGET", | ||
token: "TOKEN", | ||
tzid: sniffedTimezone | ||
}; | ||
const result = parseAvailabilityRulesOptions({ | ||
availability_rule_id: "RULE_ID", | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
tzid: sniffedTimezone | ||
}); | ||
expect(result.domains).toEqual(expectedOutput.domains); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("creates correct domains if there is a data_center option", () => { | ||
const expectedOutput = { | ||
availability_rule_id: "RULE_ID", | ||
config: {}, | ||
data_center: "de", | ||
domains: { | ||
apiDomain: "https://api-de.cronofy.com", | ||
appDomain: "https://app-de.cronofy.com" | ||
}, | ||
error: false, | ||
target: "TARGET", | ||
token: "TOKEN", | ||
tzid: sniffedTimezone | ||
}; | ||
const result = parseAvailabilityRulesOptions({ | ||
availability_rule_id: "RULE_ID", | ||
data_center: "de", | ||
element_token: "TOKEN", | ||
target_id: "TARGET", | ||
tzid: sniffedTimezone | ||
}); | ||
expect(result.domains).toEqual(expectedOutput.domains); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
describe("Parsing CalendarSync options", () => { | ||
it("works when all required options are declared", () => { | ||
const input = { | ||
authorization: { | ||
redirect_uri: "REDIRECT_URI", | ||
client_id: "CLIENT_ID", | ||
scope: "SCOPE" | ||
}, | ||
data_center: "de", | ||
element_token: "TOKEN", | ||
target_id: "TARGET" | ||
}; | ||
const expectedOutput = { | ||
authorization: { | ||
redirect_uri: "REDIRECT_URI", | ||
client_id: "CLIENT_ID", | ||
scope: "SCOPE" | ||
}, | ||
data_center: "de", | ||
domains: { | ||
apiDomain: "https://api-de.cronofy.com", | ||
appDomain: "https://app-de.cronofy.com" | ||
}, | ||
error: false, | ||
target: "TARGET", | ||
token: "TOKEN" | ||
}; | ||
const output = parseCalendarSyncOptions(input); | ||
expect(output.authorization).toEqual(expectedOutput.authorization); | ||
expect(output.domains).toEqual(expectedOutput.domains); | ||
expect(output.error).toEqual(false); | ||
expect(output.target).toEqual("TARGET"); | ||
expect(output.token).toEqual("TOKEN"); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
}); | ||
it("does not fail even if there is no token declared", () => { | ||
const expectedOutput = { | ||
authorization: { | ||
client_id: "CLIENT_ID", | ||
redirect_uri: "REDIRECT_URI", | ||
scope: "SCOPE" | ||
}, | ||
domains: { | ||
apiDomain: "https://api.cronofy.com", | ||
appDomain: "https://app.cronofy.com" | ||
}, | ||
error: false, | ||
target: "TARGET", | ||
token: false | ||
}; | ||
const output = parseCalendarSyncOptions({ | ||
authorization: { | ||
redirect_uri: "REDIRECT_URI", | ||
client_id: "CLIENT_ID", | ||
scope: "SCOPE" | ||
}, | ||
target_id: "TARGET" | ||
}); | ||
expect(output.authorization).toEqual(expectedOutput.authorization); | ||
expect(output.domains).toEqual(expectedOutput.domains); | ||
expect(output.error).toEqual(false); | ||
expect(output.target).toEqual("TARGET"); | ||
expect(output.token).toEqual(false); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
}); | ||
it("fails if there is no authorization option", () => { | ||
expect( | ||
parseCalendarSyncOptions({ | ||
target: "TARGET", | ||
token: "TOKEN" | ||
}) | ||
).toEqual(false); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
it("fails if there is no authorization.redirect_uri option", () => { | ||
expect( | ||
parseCalendarSyncOptions({ | ||
target: "TARGET", | ||
token: "TOKEN", | ||
authorization: { | ||
client_id: "CLIENT_ID", | ||
scope: "SCOPE" | ||
} | ||
}) | ||
).toEqual(false); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
it("fails if there is no authorization.client_id option", () => { | ||
expect( | ||
parseCalendarSyncOptions({ | ||
target: "TARGET", | ||
token: "TOKEN", | ||
authorization: { | ||
redirect_uri: "REDIRECT_URI", | ||
scope: "SCOPE" | ||
} | ||
}) | ||
).toEqual(false); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
it("fails if there is no authorization.scope option", () => { | ||
expect( | ||
parseCalendarSyncOptions({ | ||
authorization: { | ||
redirect_uri: "REDIRECT_URI", | ||
client_id: "CLIENT_ID" | ||
}, | ||
target: "TARGET", | ||
token: "TOKEN" | ||
}) | ||
).toEqual(false); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
it("continues if there is no token declared, but we're in demo mode", () => { | ||
const expectedAuthorization = { | ||
redirect_uri: "REDIRECT_URI", | ||
client_id: "CLIENT_ID", | ||
scope: "SCOPE" | ||
}; | ||
const output = parseCalendarSyncOptions({ | ||
demo: true | ||
}); | ||
expect(output.authorization).toEqual(expectedAuthorization); | ||
expect(output.demo).toEqual(true); | ||
expect(console.warn).toHaveBeenCalledTimes(2); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("applies the default target if there is no target option", () => { | ||
const output = parseCalendarSyncOptions({ | ||
authorization: { | ||
redirect_uri: "REDIRECT_URI", | ||
client_id: "CLIENT_ID", | ||
scope: "SCOPE" | ||
}, | ||
element_token: "TOKEN" | ||
}); | ||
expect(output.target).toEqual("cronofy-element-calendar-sync"); | ||
expect(console.warn).toHaveBeenCalledTimes(1); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("applies the default domains if there is no data_center option", () => { | ||
const expectedDomains = { | ||
apiDomain: "https://api.cronofy.com", | ||
appDomain: "https://app.cronofy.com" | ||
}; | ||
const output = parseCalendarSyncOptions({ | ||
authorization: { | ||
redirect_uri: "REDIRECT_URI", | ||
client_id: "CLIENT_ID", | ||
scope: "SCOPE" | ||
}, | ||
element_token: "TOKEN", | ||
target_id: "TARGET" | ||
}); | ||
expect(output.domains).toEqual(expectedDomains); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("creates correct domains if there is a data_center option", () => { | ||
const expectedDomains = { | ||
apiDomain: "https://api-de.cronofy.com", | ||
appDomain: "https://app-de.cronofy.com" | ||
}; | ||
const output = parseCalendarSyncOptions({ | ||
authorization: { | ||
redirect_uri: "REDIRECT_URI", | ||
client_id: "CLIENT_ID", | ||
scope: "SCOPE" | ||
}, | ||
data_center: "de", | ||
element_token: "TOKEN", | ||
target_id: "TARGET" | ||
}); | ||
expect(output.domains).toEqual(expectedDomains); | ||
expect(output.data_center).toEqual("de"); | ||
expect(output.error).toEqual(false); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly parses singleProfile option", () => { | ||
const output = parseCalendarSyncOptions({ | ||
authorization: { | ||
redirect_uri: "REDIRECT_URI", | ||
client_id: "CLIENT_ID", | ||
scope: "SCOPE" | ||
}, | ||
single_profile: true, | ||
element_token: "TOKEN", | ||
target_id: "TARGET" | ||
}); | ||
expect(output.single_profile).toEqual(true); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
it("correctly applies default for singleProfile option", () => { | ||
const output = parseCalendarSyncOptions({ | ||
authorization: { | ||
redirect_uri: "REDIRECT_URI", | ||
client_id: "CLIENT_ID", | ||
scope: "SCOPE" | ||
}, | ||
element_token: "TOKEN", | ||
target_id: "TARGET" | ||
}); | ||
expect(output.single_profile).toEqual(false); | ||
expect(console.warn).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
describe("Parsing query_periods", () => { | ||
@@ -1310,0 +15,0 @@ let log = { |
@@ -91,2 +91,29 @@ import * as utils from "../src/js/helpers/utils.AvailabilityRules"; | ||
const tzid = "TEST_TIMEZONE"; | ||
const default_weekly_periods = [ | ||
{ | ||
day: "monday", | ||
end_time: "17:00", | ||
start_time: "09:00" | ||
}, | ||
{ | ||
day: "tuesday", | ||
end_time: "17:00", | ||
start_time: "09:00" | ||
}, | ||
{ | ||
day: "wednesday", | ||
end_time: "17:00", | ||
start_time: "09:00" | ||
}, | ||
{ | ||
day: "thursday", | ||
end_time: "17:00", | ||
start_time: "09:00" | ||
}, | ||
{ | ||
day: "friday", | ||
end_time: "17:00", | ||
start_time: "09:00" | ||
} | ||
]; | ||
const expectedOutput = { | ||
@@ -126,3 +153,5 @@ availability_rule: { | ||
}; | ||
expect(utils.buildRuleTemplate(tzid)).toEqual(expectedOutput); | ||
expect(utils.buildRuleTemplate(default_weekly_periods, tzid)).toEqual( | ||
expectedOutput | ||
); | ||
}); | ||
@@ -132,2 +161,29 @@ | ||
it("builds a rules template when no tzid is provided", () => { | ||
const default_weekly_periods = [ | ||
{ | ||
day: "monday", | ||
end_time: "17:00", | ||
start_time: "09:00" | ||
}, | ||
{ | ||
day: "tuesday", | ||
end_time: "17:00", | ||
start_time: "09:00" | ||
}, | ||
{ | ||
day: "wednesday", | ||
end_time: "17:00", | ||
start_time: "09:00" | ||
}, | ||
{ | ||
day: "thursday", | ||
end_time: "17:00", | ||
start_time: "09:00" | ||
}, | ||
{ | ||
day: "friday", | ||
end_time: "17:00", | ||
start_time: "09:00" | ||
} | ||
]; | ||
const expectedOutput = { | ||
@@ -167,4 +223,6 @@ availability_rule: { | ||
}; | ||
expect(utils.buildRuleTemplate()).toEqual(expectedOutput); | ||
expect(utils.buildRuleTemplate(default_weekly_periods)).toEqual( | ||
expectedOutput | ||
); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
2910732
242
27432