-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- set currentCarrier on initializing configBus - remove unused settings (priceSaturdayDelivery and priceMondayDelivery) - add test that runs daily against the real api - fix a few delivery options tests that marked the wrong results as correct
- Loading branch information
1 parent
e107b99
commit 0e50559
Showing
21 changed files
with
413 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Run test on the live API | ||
|
||
on: | ||
schedule: | ||
- cron: '0 12 * * *' | ||
|
||
jobs: | ||
test: | ||
name: Run test on live API | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 2 | ||
ref: 'master' | ||
- uses: bahmutov/npm-install@v1 | ||
- name: Run jest | ||
run: ./node_modules/.bin/cross-env npm test -- tests/unit/delivery-options/testLiveApi.spec.js --coverage=false | ||
env: | ||
NODE_ICU_DATA: node_modules/full-icu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as CONFIG from '@/data/keys/configKeys'; | ||
import { configBus as realConfigBus } from '@/delivery-options/config/configBus'; | ||
|
||
/** | ||
* @param {Number} dropOffDay | ||
* @param {import('@/delivery-options/config/configBus').configBus} configBus | ||
* @param {Number} day | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
export function checkIsDropOffDay(dropOffDay, configBus = realConfigBus, day = new Date().getDay()) { | ||
const dateMatches = day === dropOffDay; | ||
const dateIsDropOffDay = configBus.get(CONFIG.DROP_OFF_DAYS).includes(day); | ||
|
||
return dateMatches && dateIsDropOffDay; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,21 @@ | ||
import * as CONFIG from '@/data/keys/configKeys'; | ||
import { extraDeliveryConfig } from '@/config/extraDeliveryConfig'; | ||
import { checkIsDropOffDay } from '@/delivery-options/data/request/checkIsDropOffDay'; | ||
import { getExtraDropOffDay } from '@/delivery-options/data/request/getExtraDropOffDay'; | ||
import { configBus as realConfigBus } from '@/delivery-options/config/configBus'; | ||
|
||
/** | ||
* Get cutoff time for a special delivery day. Returns default cutoff time if the conditions for an extra delivery day | ||
* don't pass. | ||
* | ||
* @param {import('@/delivery-options/config/configBus')} configBus - Optional parameter for easier testing. | ||
* @param {import('@/delivery-options/config/configBus').configBus} configBus - Optional parameter for easier testing. | ||
* | ||
* @returns {MyParcelDeliveryOptions.Config.cutoffTime} | ||
* @returns {String} | ||
*/ | ||
export function getCutOffTime(configBus = realConfigBus) { | ||
const today = new Date().getDay(); | ||
const extraDropOffDay = getExtraDropOffDay(configBus); | ||
const todayIsExtraDropOffDay = extraDropOffDay && checkIsDropOffDay(extraDropOffDay.dropOffDay, configBus); | ||
|
||
const extraDropOffDay = extraDeliveryConfig.find((setting) => { | ||
const allowedForPlatform = setting.platforms.includes(configBus.get(CONFIG.PLATFORM)); | ||
const todayIsExtraDay = today === setting.dropOffDay; | ||
const requirementsFulfilled = setting.requires.every((requirement) => Boolean(configBus.get(requirement))); | ||
const extraDayIsDropOffDay = configBus.get(CONFIG.DROP_OFF_DAYS).includes(setting.dropOffDay); | ||
|
||
return todayIsExtraDay && allowedForPlatform && extraDayIsDropOffDay && requirementsFulfilled; | ||
}); | ||
|
||
return extraDropOffDay | ||
return todayIsExtraDropOffDay | ||
? configBus.get(extraDropOffDay.cutoffTime) | ||
: configBus.get(CONFIG.CUTOFF_TIME); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as CONFIG from '@/data/keys/configKeys'; | ||
import { CarrierConfigurationFactory } from '@/data/carriers/carrierConfigurationFactory'; | ||
import { extraDeliveryConfig } from '@/config/extraDeliveryConfig'; | ||
import { configBus as realConfigBus } from '@/delivery-options/config/configBus'; | ||
|
||
/** | ||
* @param {import('@/delivery-options/config/configBus').configBus} configBus | ||
* | ||
* @returns {Object} | ||
*/ | ||
export function getExtraDropOffDay(configBus = realConfigBus) { | ||
const platform = configBus.get(CONFIG.PLATFORM); | ||
const carrierConfiguration = CarrierConfigurationFactory.create(configBus.currentCarrier, platform); | ||
|
||
return extraDeliveryConfig.find((setting) => { | ||
const allowedForCarrierAndPlatform = carrierConfiguration.hasFeature(setting.requires); | ||
const requiredOptionsPresent = setting.requires.every(configBus.get); | ||
|
||
return allowedForCarrierAndPlatform && requiredOptionsPresent; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/__mocks__/@myparcel/js-sdk/dist/data/delivery-options/cutoffTimeHasPassed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { createCutOffTimeDate } from '@Tests/helpers/createCutOffTimeDate'; | ||
|
||
/** | ||
* @param {String} cutoffTime - Timestamp in HH:mm format. | ||
* @param {import('dayjs').Dayjs} date | ||
* | ||
* @returns {import('dayjs').Dayjs} | ||
*/ | ||
export function cutoffTimeHasPassed(cutoffTime, date) { | ||
const cutOffTimeDate = createCutOffTimeDate(cutoffTime, date); | ||
|
||
return !date.isBefore(cutOffTimeDate); | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/__mocks__/@myparcel/js-sdk/dist/data/delivery-options/findExtraDelivery.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { CarrierConfigurationFactory } from '@/data/carriers/carrierConfigurationFactory'; | ||
import { extraDeliveryConfig } from '@/config/extraDeliveryConfig'; | ||
import { platformCarrierMap } from '@/config/platform/platformCarrierMap'; | ||
|
||
/** | ||
* Use passed args to find a valid extra delivery day. | ||
* | ||
* @param {Object} args | ||
* @param {Number} dayOfWeek | ||
* | ||
* @returns {Object} | ||
*/ | ||
export function findExtraDelivery(args, dayOfWeek) { | ||
// Falls back to the first carrier for current platform. | ||
const carrier = args.carrier ?? platformCarrierMap[args.platform][0]; | ||
const carrierConfiguration = CarrierConfigurationFactory.create(carrier, args.platform); | ||
|
||
return extraDeliveryConfig.find((setting) => { | ||
const isToday = setting.deliveryDay === dayOfWeek; | ||
const hasDropOffDay = args.dropoff_days.includes(setting.dropOffDay); | ||
const allowedForCarrierAndPlatform = carrierConfiguration.hasFeature(setting.requires); | ||
|
||
return isToday && hasDropOffDay && allowedForCarrierAndPlatform; | ||
}); | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/__mocks__/@myparcel/js-sdk/dist/data/delivery-options/getDropOffDay.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Find the last possible dropoff day for a given delivery day. | ||
* | ||
* @param {import('dayjs').Dayjs} deliveryDay | ||
* @param {String} dropOffDays | ||
* @returns {import('dayjs').Dayjs} | ||
*/ | ||
export function getDropOffDay(deliveryDay, dropOffDays) { | ||
const dropOffDaysArray = dropOffDays.split(';').map(Number); | ||
|
||
let dropOffDay = deliveryDay.subtract(1, 'day'); | ||
|
||
while (!dropOffDaysArray.includes(dropOffDay.weekday())) { | ||
dropOffDay = dropOffDay.subtract(1, 'day'); | ||
} | ||
|
||
return dropOffDay; | ||
} |
53 changes: 24 additions & 29 deletions
53
tests/__mocks__/@myparcel/js-sdk/dist/data/delivery-options/getNextDeliveryOption.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,54 @@ | ||
import { MONDAY, SATURDAY, SUNDAY } from '@/config/extraDeliveryConfig'; | ||
import { MYPARCEL, SENDMYPARCEL } from '@/data/keys/platformKeys'; | ||
import { cutoffTimeHasPassed } from '@Mocks/@myparcel/js-sdk/dist/data/delivery-options/cutoffTimeHasPassed'; | ||
import { dayjs } from '@Tests/dayjs'; | ||
import { extraDeliveryConfig } from '@/config/extraDeliveryConfig'; | ||
import { findExtraDelivery } from '@Mocks/@myparcel/js-sdk/dist/data/delivery-options/findExtraDelivery'; | ||
import { getDeliveryOptionsEntry } from './getDeliveryOptionsEntry'; | ||
import { getDropOffDay } from '@Mocks/@myparcel/js-sdk/dist/data/delivery-options/getDropOffDay'; | ||
|
||
const disallowedDays = { | ||
[MYPARCEL]: [0, 1], | ||
[SENDMYPARCEL]: [0, 6], | ||
const daysWithoutDelivery = { | ||
[MYPARCEL]: [MONDAY, SUNDAY], | ||
[SENDMYPARCEL]: [SATURDAY, SUNDAY], | ||
}; | ||
|
||
/** | ||
* Returns the next available delivery date, very much like the actual responses from the API. This needs to be | ||
* quite precise because we can't mock the current date with real api responses. | ||
* quite precise because we can't mock the current date with real api responses. | ||
* | ||
* @param {Object} args | ||
* @param {Number} daysOffset | ||
* @param {import('dayjs').Dayjs} date | ||
* | ||
* @returns {Object} | ||
*/ | ||
export function getNextDeliveryOption(args, daysOffset = 1) { | ||
const next = () => getNextDeliveryOption(args, daysOffset + 1); | ||
export function getNextDeliveryOption(args, daysOffset = 1, date = dayjs()) { | ||
const next = () => getNextDeliveryOption(args, daysOffset + 1, date); | ||
|
||
const today = dayjs().add(daysOffset, 'day'); | ||
const dayOfWeek = today.weekday(); | ||
const todayIsDisallowed = disallowedDays[args.platform].includes(dayOfWeek); | ||
|
||
let dropOffDay = dayOfWeek - 1; | ||
|
||
const extraDelivery = extraDeliveryConfig.find((config) => { | ||
const isToday = config.deliveryDay === dayOfWeek; | ||
const allowedInPlatform = config.platforms.includes(args.platform); | ||
|
||
return isToday && allowedInPlatform; | ||
}); | ||
const currentDeliveryDate = date.add(daysOffset, 'day'); | ||
const dropOffDay = getDropOffDay(currentDeliveryDate, args.dropoff_days); | ||
const currentDayOfWeek = currentDeliveryDate.weekday(); | ||
const todayIsDisallowed = daysWithoutDelivery[args.platform].includes(currentDayOfWeek); | ||
const extraDelivery = findExtraDelivery(args, currentDayOfWeek); | ||
|
||
// Skip Saturday or Monday if its setting is not enabled. | ||
if (extraDelivery) { | ||
// Skip Saturday or Monday if its setting is not enabled. | ||
if (args[`${args.platform === MYPARCEL ? 'monday' : 'saturday'}_delivery`] !== 1) { | ||
const extraDeliveryEnabled = args[`${args.platform === MYPARCEL ? 'monday' : 'saturday'}_delivery`] === 1; | ||
const isExtraDropOffDay = dropOffDay.weekday() === extraDelivery.dropOffDay; | ||
|
||
if (!extraDeliveryEnabled || !isExtraDropOffDay) { | ||
return next(); | ||
} | ||
|
||
// With Monday delivery, for example, the dropoff day is Saturday instead of Sunday. | ||
dropOffDay = extraDelivery.dropOffDay; | ||
} else if (todayIsDisallowed) { | ||
return next(); | ||
} | ||
|
||
const dropOffDays = args.dropoff_days.split(';'); | ||
|
||
// If the drop off day for today is not enabled, skip. | ||
if (!dropOffDays.includes(dropOffDay.toString())) { | ||
// If today is the dropoff day, check if the cutoff time has passed. | ||
if (date.isSame(dropOffDay) && cutoffTimeHasPassed(args.cutoff_time, date)) { | ||
return next(); | ||
} | ||
|
||
return { | ||
index: daysOffset, | ||
data: getDeliveryOptionsEntry(today, !!extraDelivery), | ||
data: getDeliveryOptionsEntry(currentDeliveryDate, !!extraDelivery), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import customParseFormat from 'dayjs/plugin/customParseFormat'; | ||
import dayjs from 'dayjs'; | ||
import weekday from 'dayjs/plugin/weekday'; | ||
|
||
dayjs.extend(customParseFormat); | ||
dayjs.extend(weekday); | ||
|
||
export { dayjs }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import dayjs from 'dayjs'; | ||
|
||
/** | ||
* @param {String} cutoffTime - String in HH:mm format. | ||
* @param {import('dayjs').Dayjs} date | ||
* | ||
* @returns {import('dayjs').Dayjs} | ||
*/ | ||
export function createCutOffTimeDate(cutoffTime, date = dayjs()) { | ||
const [hour, minute] = cutoffTime.split(':'); | ||
|
||
return date.set('h', parseInt(hour)).set('m', parseInt(minute)); | ||
} |
Oops, something went wrong.