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
1bc1534
commit 01d23c9
Showing
6 changed files
with
592 additions
and
2 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
35 changes: 35 additions & 0 deletions
35
src/core/test-app/runner/config/cancel-pickups-next-day.ts
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 CancelPickupsNextDayTestParams { | ||
pickupServiceName: string; | ||
deliveryServiceName: string; | ||
address: AddressPOJO; | ||
contact: ContactInfoPOJO; | ||
timeWindow: TimeRangePOJO; | ||
shipments: PickupShipmentConfig[]; | ||
cancellationReason: PickupCancellationReason; | ||
} | ||
|
||
export interface CancelPickupsNextDayConfigOptions | ||
extends CancelPickupsNextDayTestParams, | ||
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 { CancelPickupsNextDayTestParams, CancelPickupsNextDayConfigOptions } from "../runner/config/cancel-pickups-next-day"; | ||
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: CancelPickupsNextDayTestParams; | ||
} | ||
|
||
export class CancelPickupsNextDay extends Suite { | ||
title = "cancelPickups_next_day"; | ||
|
||
private deliveryService: DeliveryService | undefined; | ||
|
||
private pickupService: PickupService | undefined; | ||
|
||
private setDeliveryService( | ||
config: CancelPickupsNextDayConfigOptions, | ||
): 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: CancelPickupsNextDayConfigOptions): 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: CancelPickupsNextDayConfigOptions): 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 { tomorrowEarly, tomorrowEvening } = initializeTimeStamps(); | ||
|
||
const defaults: CancelPickupsNextDayTestParams = { | ||
pickupServiceName: this.pickupService.name, | ||
deliveryServiceName: this.deliveryService.name, | ||
address, | ||
contact: { name: "John Doe" }, | ||
timeWindow: { | ||
startDateTime: tomorrowEarly, | ||
endDateTime: tomorrowEvening | ||
}, | ||
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< | ||
CancelPickupsNextDayTestParams | ||
>(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: CancelPickupsNextDayConfigOptions) => { | ||
return this.buildTestArg(config); | ||
}); | ||
} | ||
|
||
const config = this.config as CancelPickupsNextDayConfigOptions; | ||
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); | ||
} | ||
); | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.