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.
Merge branch 'master' into DX-217-cancel-a-shipment
- Loading branch information
Showing
8 changed files
with
613 additions
and
3 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,26 @@ | ||
import { | ||
WeightUnit, | ||
DateTimeZonePOJO, | ||
AddressWithContactInfoPOJO | ||
} from "@shipengine/connect-sdk"; | ||
import { NewLabelPOJO } from "@shipengine/connect-sdk/lib/internal"; | ||
|
||
import { BaseTestConfigOptions } from "./base-test-config-options"; | ||
|
||
export interface CreateShipmentReturnTestParams { | ||
deliveryServiceName: string; | ||
label: NewLabelPOJO; | ||
shipFrom?: AddressWithContactInfoPOJO; | ||
returnTo?: AddressWithContactInfoPOJO; | ||
weight: { | ||
value: number; | ||
unit: WeightUnit; | ||
}; | ||
shipDateTime: DateTimeZonePOJO | Date | string; | ||
deliveryConfirmationName?: string; | ||
rmaNumber?: string; | ||
} | ||
|
||
export interface CreateShipmentReturnConfigOptions | ||
extends CreateShipmentReturnTestParams, | ||
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,214 @@ | ||
import { DeliveryService, WeightUnit, DeliveryConfirmation } from "@shipengine/connect-sdk"; | ||
import { CarrierApp, NewShipmentPOJO, NewPackagePOJO } from "@shipengine/connect-sdk/lib/internal"; | ||
import Suite from "../runner/suite"; | ||
import { | ||
CreateShipmentReturnConfigOptions, | ||
CreateShipmentReturnTestParams, | ||
} from "../runner/config/create-shipment-return"; | ||
import { initializeTimeStamps } from "../../utils/time-stamps"; | ||
import reduceDefaultsWithConfig from '../utils/reduce-defaults-with-config'; | ||
import objectToTestTitle from '../utils/object-to-test-title'; | ||
import useDomesticShippingAddress from '../utils/use-domestic-shipment-addresses'; | ||
|
||
import { expect } from "chai"; | ||
import findDeliveryServiceByName from '../utils/find-delivery-service-by-name'; | ||
import findDeliveryConfirmationByName from '../utils/find-delivery-confirmation-by-name'; | ||
|
||
interface TestArgs { | ||
title: string; | ||
methodArgs: NewShipmentPOJO; | ||
config: any; | ||
} | ||
|
||
export class CreateShipmentReturn extends Suite { | ||
title = "createShipment_return"; | ||
|
||
private deliveryService?: DeliveryService; | ||
|
||
private deliveryConfirmation?: DeliveryConfirmation; | ||
|
||
private setDeliveryService( | ||
config: CreateShipmentReturnConfigOptions, | ||
): void { | ||
const carrierApp = this.app as CarrierApp; | ||
|
||
if (config.deliveryServiceName) { | ||
this.deliveryService = findDeliveryServiceByName( | ||
config.deliveryServiceName, | ||
carrierApp, | ||
); | ||
} else { | ||
try { | ||
this.deliveryService = carrierApp.deliveryServices[0]; | ||
} catch { | ||
this.deliveryService = undefined; | ||
} | ||
} | ||
} | ||
|
||
private setDeliveryConfirmation( | ||
config: CreateShipmentReturnConfigOptions, | ||
): void { | ||
if (config.deliveryConfirmationName) { | ||
// We do not want to handle the exception here if this raises. It indicates issues w/ the config provided. | ||
this.deliveryConfirmation = findDeliveryConfirmationByName( | ||
config.deliveryConfirmationName, | ||
this.app as CarrierApp, | ||
); | ||
} else if ( | ||
this.deliveryService && | ||
this.deliveryService.deliveryConfirmations.length !== 0 && | ||
this.deliveryService.deliveryConfirmations[0] | ||
) { | ||
this.deliveryConfirmation = this.deliveryService.deliveryConfirmations[0]; | ||
} else { | ||
this.deliveryConfirmation = undefined; | ||
} | ||
} | ||
|
||
buildTestArg( | ||
config: CreateShipmentReturnConfigOptions, | ||
): TestArgs | undefined { | ||
this.setDeliveryService(config); | ||
this.setDeliveryConfirmation(config); | ||
|
||
if (!this.deliveryService) return undefined; | ||
|
||
let shipFrom; | ||
let returnTo; | ||
try { | ||
[shipFrom, returnTo] = useDomesticShippingAddress(this.deliveryService); | ||
} catch { } | ||
|
||
const { tomorrow } = initializeTimeStamps(); | ||
|
||
// Make a best guess at the defaults, need to resolve the default vs config based delivery service early | ||
// on since that determines what address and associated timezones get generated. | ||
const defaults: CreateShipmentReturnTestParams = { | ||
deliveryServiceName: this.deliveryService.name, | ||
label: { | ||
size: this.deliveryService.labelSizes[0], | ||
format: this.deliveryService.labelFormats[0] | ||
}, | ||
shipDateTime: tomorrow, | ||
shipFrom: shipFrom, | ||
returnTo: returnTo, | ||
weight: { | ||
unit: WeightUnit.Pounds, | ||
value: 50.0, | ||
}, | ||
rmaNumber: `RMA-${Buffer.from(new Date().toISOString()).toString("base64").toUpperCase()}` | ||
}; | ||
|
||
if (this.deliveryService.deliveryConfirmations.length > 0) { | ||
defaults.deliveryConfirmationName = this.deliveryService.deliveryConfirmations[0].name; | ||
} | ||
|
||
const testParams = reduceDefaultsWithConfig< | ||
CreateShipmentReturnTestParams | ||
>(defaults, config); | ||
|
||
if (!testParams.shipFrom || !testParams.returnTo) return undefined; | ||
|
||
const packagePOJO: NewPackagePOJO = { | ||
packaging: { | ||
id: this.deliveryService.packaging[0].id | ||
}, | ||
label: { | ||
size: testParams.label.size, | ||
format: testParams.label.format, | ||
}, | ||
weight: { | ||
value: testParams.weight.value, | ||
unit: testParams.weight.unit, | ||
}, | ||
}; | ||
|
||
const newShipmentPOJO: NewShipmentPOJO = { | ||
deliveryService: { | ||
id: this.deliveryService.id, | ||
}, | ||
shipFrom: testParams.shipFrom!, | ||
shipTo: testParams.returnTo!, | ||
shipDateTime: testParams.shipDateTime, | ||
packages: [packagePOJO], | ||
returns: { | ||
isReturn: true, | ||
rmaNumber: testParams.rmaNumber | ||
} | ||
}; | ||
|
||
const title = config.expectedErrorMessage | ||
? `it raises an error when creating a new return shipment with ${objectToTestTitle( | ||
testParams, | ||
)}` | ||
: `it creates a new return shipment with ${objectToTestTitle( | ||
testParams, | ||
)}`; | ||
|
||
if (this.deliveryConfirmation) { | ||
newShipmentPOJO.deliveryConfirmation = { | ||
id: this.deliveryConfirmation.id, | ||
}; | ||
} | ||
|
||
if (testParams.deliveryConfirmationName) { | ||
newShipmentPOJO.deliveryConfirmation = { | ||
id: this.deliveryService.deliveryConfirmations.find( | ||
(dc) => dc.name === testParams.deliveryConfirmationName, | ||
)!.id, | ||
}; | ||
} | ||
|
||
return { | ||
title, | ||
methodArgs: newShipmentPOJO, | ||
config, | ||
}; | ||
} | ||
|
||
buildTestArgs(): Array<TestArgs | undefined> { | ||
if (Array.isArray(this.config)) { | ||
return this.config.map((config: CreateShipmentReturnConfigOptions) => { | ||
return this.buildTestArg(config); | ||
}); | ||
} | ||
const config = this.config as CreateShipmentReturnConfigOptions; | ||
return [this.buildTestArg(config)]; | ||
} | ||
|
||
tests() { | ||
const testArgs = this.buildTestArgs().filter((args) => args !== undefined); | ||
|
||
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); | ||
|
||
if (!carrierApp.createShipment) { | ||
throw new Error("createShipment is not implemented"); | ||
} | ||
|
||
const shipmentConfirmation = await carrierApp.createShipment(transaction, testArg!.methodArgs); | ||
|
||
// If DeliveryServiceDefinition.isTrackable is true, then the shipment must have a trackingNumber set | ||
if (this.deliveryService?.isTrackable) { | ||
const customMsg = "The shipmentConfirmation.isTrackable returned from createShipment must be present when the given deliveryService.isTrackable is set to 'true'"; | ||
expect(shipmentConfirmation.trackingNumber, customMsg).to.be.ok; | ||
} | ||
|
||
const customMsg = "The shipment confirmation packages array should have the same number of packages that were on the request"; | ||
expect(shipmentConfirmation.packages.length).to.equal(testArg!.methodArgs.packages.length, 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
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.