This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
382b8c9
commit fe782e3
Showing
6 changed files
with
596 additions
and
6 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
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,35 @@ | ||
import { | ||
TimeRangePOJO, | ||
ContactInfoPOJO, | ||
AddressPOJO, | ||
WeightPOJO, | ||
DimensionsPOJO, | ||
PickupCancellationReason | ||
} from "@shipengine/connect-sdk"; | ||
import { BaseTestConfigOptions } from "./base-test-config-options"; | ||
|
||
|
||
export type PickupPackageConfig = { | ||
packagingName: string; | ||
dimensions?: DimensionsPOJO; | ||
weight?: WeightPOJO; | ||
}; | ||
|
||
export type PickupShipmentConfig = { | ||
deliveryServiceName: string; | ||
packages: PickupPackageConfig[]; | ||
}; | ||
|
||
export interface CancelSameDayPickupTestParams { | ||
pickupServiceName: string; | ||
deliveryServiceName: string; | ||
address: AddressPOJO; | ||
contact: ContactInfoPOJO; | ||
timeWindow: TimeRangePOJO; | ||
shipments: PickupShipmentConfig[]; | ||
cancellationReason: PickupCancellationReason; | ||
} | ||
|
||
export interface CancelSameDayPickupConfigOptions | ||
extends CancelSameDayPickupTestParams, | ||
BaseTestConfigOptions {} |
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,211 @@ | ||
import { DeliveryService, PickupService, LengthUnit, WeightUnit, PickupCancellationReason } from "@shipengine/connect-sdk"; | ||
import { CarrierApp, PickupRequestPOJO, PickupShipmentPOJO, PickupPackagePOJO, PickupCancellationPOJO } from "@shipengine/connect-sdk/lib/internal"; | ||
import Suite from "../runner/suite"; | ||
import { initializeTimeStamps } from "../../utils/time-stamps"; | ||
import { CancelSameDayPickupTestParams, CancelSameDayPickupConfigOptions } from "../runner/config/cancel-same-day-pickup"; | ||
import reduceDefaultsWithConfig from "../utils/reduce-defaults-with-config"; | ||
import objectToTestTitle from "../utils/object-to-test-title"; | ||
import useShipmentAddresses from '../utils/use-shipment-addresses'; | ||
import findDeliveryServiceByName from '../utils/find-delivery-service-by-name'; | ||
import findPickupServiceByName from '../utils/find-pickup-service-by-name'; | ||
import { findDomesticDeliveryService } from '../utils/find-domestic-delivery-service'; | ||
import Test from '../runner/test'; | ||
import { buildAddress } from '../factories/address'; | ||
import findPackagingByName from '../utils/find-packaging-by-name'; | ||
import { v4 } from "uuid"; | ||
import { expect } from "chai"; | ||
|
||
interface TestArgs { | ||
title: string; | ||
methodArgs: PickupRequestPOJO; | ||
config: unknown; | ||
testParams: CancelSameDayPickupTestParams; | ||
} | ||
|
||
export class CancelSameDayPickup extends Suite { | ||
title = "cancelPickups_same_day"; | ||
|
||
private deliveryService: DeliveryService | undefined; | ||
|
||
private pickupService: PickupService | undefined; | ||
|
||
private setDeliveryService( | ||
config: CancelSameDayPickupConfigOptions, | ||
): void { | ||
const carrierApp = this.app as CarrierApp; | ||
|
||
if (config.deliveryServiceName) { | ||
this.deliveryService = findDeliveryServiceByName( | ||
config.deliveryServiceName, | ||
carrierApp, | ||
); | ||
} else { | ||
try { | ||
this.deliveryService = findDomesticDeliveryService(carrierApp); | ||
} catch { | ||
this.deliveryService = undefined; | ||
} | ||
} | ||
} | ||
|
||
private setPickupService(config: CancelSameDayPickupConfigOptions): void { | ||
const carrierApp = this.app as CarrierApp; | ||
|
||
if (config.pickupServiceName) { | ||
this.pickupService = findPickupServiceByName(config.pickupServiceName, carrierApp); | ||
} | ||
|
||
else if (carrierApp.pickupServices.length > 0) { | ||
this.pickupService = carrierApp.pickupServices[0]; | ||
} | ||
} | ||
|
||
buildTestArg(config: CancelSameDayPickupConfigOptions): TestArgs | undefined { | ||
const carrierApp = this.app as CarrierApp; | ||
this.setPickupService(config); | ||
this.setDeliveryService(config); | ||
|
||
if (!this.deliveryService || !this.pickupService) return undefined; | ||
const [shipFrom, shipTo] = useShipmentAddresses(this.deliveryService); | ||
|
||
if (!shipTo || !shipFrom) return undefined; | ||
|
||
const address = buildAddress(`${shipFrom.country}-from`); | ||
|
||
const { todayEarly, todayEvening } = initializeTimeStamps(); | ||
|
||
const defaults: CancelSameDayPickupTestParams = { | ||
pickupServiceName: this.pickupService.name, | ||
deliveryServiceName: this.deliveryService.name, | ||
address, | ||
contact: { name: "John Doe" }, | ||
timeWindow: { | ||
startDateTime: todayEarly, | ||
endDateTime: todayEvening | ||
}, | ||
shipments: [{ | ||
deliveryServiceName: this.deliveryService.name, | ||
packages: [ | ||
{ | ||
packagingName: this.deliveryService.packaging[0].name, | ||
dimensions: { | ||
length: 12, | ||
width: 12, | ||
height: 12, | ||
unit: LengthUnit.Inches | ||
}, | ||
weight: { | ||
unit: WeightUnit.Pounds, | ||
value: 5.0 | ||
} | ||
} | ||
] | ||
}], | ||
cancellationReason: PickupCancellationReason.NotReady | ||
}; | ||
|
||
const testParams = reduceDefaultsWithConfig< | ||
CancelSameDayPickupTestParams | ||
>(defaults, config); | ||
|
||
const shipments: PickupShipmentPOJO[] = testParams.shipments.map((shipmentParams) => { | ||
const shipment: PickupShipmentPOJO = { | ||
deliveryService: findDeliveryServiceByName(shipmentParams.deliveryServiceName, carrierApp), | ||
packages: shipmentParams.packages.map((pkgParams) => { | ||
const pkg: PickupPackagePOJO = { | ||
packaging: findPackagingByName(pkgParams.packagingName, carrierApp), | ||
dimensions: pkgParams.dimensions, | ||
weight: pkgParams.weight | ||
}; | ||
|
||
return pkg; | ||
}) | ||
} | ||
|
||
return shipment; | ||
}); | ||
|
||
const rateCriteriaPOJO: PickupRequestPOJO = { | ||
pickupService: findPickupServiceByName(testParams.pickupServiceName, carrierApp), | ||
address: testParams.address, | ||
timeWindow: testParams.timeWindow, | ||
contact: testParams.contact, | ||
shipments | ||
}; | ||
|
||
const title = config.expectedErrorMessage | ||
? `it raises an error when scheduling a pickup with ${objectToTestTitle( | ||
testParams, | ||
)}` | ||
: `it schedules a pickup with ${objectToTestTitle( | ||
testParams, | ||
)}`; | ||
|
||
return { | ||
title, | ||
methodArgs: rateCriteriaPOJO, | ||
config, | ||
testParams | ||
}; | ||
} | ||
|
||
buildTestArgs(): Array<TestArgs | undefined> { | ||
if (Array.isArray(this.config)) { | ||
return this.config.map((config: CancelSameDayPickupConfigOptions) => { | ||
return this.buildTestArg(config); | ||
}); | ||
} | ||
|
||
const config = this.config as CancelSameDayPickupConfigOptions; | ||
return [this.buildTestArg(config)]; | ||
} | ||
|
||
tests(): Test[] { | ||
const testArgs = this.buildTestArgs().filter(args => args !== undefined) as TestArgs[]; | ||
|
||
if (testArgs.length === 0) { | ||
return []; | ||
} | ||
return testArgs.map((testArg) => { | ||
return this.test( | ||
testArg.title, | ||
testArg.methodArgs, | ||
testArg.config, | ||
async () => { | ||
const carrierApp = this.app as CarrierApp; | ||
|
||
const transaction = await this.transaction(testArg.config); | ||
|
||
// This should never actually throw because we handle this case up stream. | ||
if (!carrierApp.schedulePickup) { | ||
throw new Error("schedulePickup is not implemented"); | ||
} | ||
|
||
// This should never actually throw because we handle this case up stream. | ||
if (!carrierApp.cancelPickups) { | ||
throw new Error("cancelPickups is not implemented"); | ||
} | ||
|
||
await carrierApp.schedulePickup(transaction, testArg.methodArgs); | ||
|
||
const cancellationID = v4(); | ||
|
||
const pickupCancellation: PickupCancellationPOJO[] = [{ | ||
cancellationID, | ||
id: v4(), | ||
pickupService: testArg.methodArgs.pickupService, | ||
reason: testArg.testParams.cancellationReason, | ||
address: testArg.methodArgs.address, | ||
contact: testArg.methodArgs.contact, | ||
timeWindows: [testArg.methodArgs.timeWindow], | ||
shipments: testArg.methodArgs.shipments, | ||
}] | ||
|
||
const pickupCancelOutcome = await carrierApp.cancelPickups(transaction, pickupCancellation); | ||
const customMsg = `The pickupCancellationOutcome cancellationID does not match the cancellationID that was included in the PickupCancellation parameter: ${cancellationID}`; | ||
expect(pickupCancelOutcome[0].cancellationID).to.equal(cancellationID, customMsg); | ||
} | ||
); | ||
}); | ||
} | ||
} |
Oops, something went wrong.