Skip to content
This repository has been archived by the owner on Mar 13, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into DX-217-cancel-a-shipment
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrauskopf committed Aug 20, 2020
2 parents a2d0a74 + 0e7ff51 commit 69305ae
Show file tree
Hide file tree
Showing 8 changed files with 613 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/core/test-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
CreateShipmentDomestic,
CreateShipmentWithInsurance,
CreateShipmentMultiPackage,
RateShipment
RateShipment,
CreateShipmentReturn
} from "./test-app/tests";
import { SdkApp } from "./types";
import { TestResults, useTestResults } from "./test-app/runner/test-results";
Expand Down Expand Up @@ -144,7 +145,8 @@ function registerTestSuiteModules(app: SdkApp): RegisteredTestSuiteModules {
CreateShipmentInternational,
CreateShipmentDomestic,
CreateShipmentMultiPackage,
CreateShipmentWithInsurance
CreateShipmentWithInsurance,
CreateShipmentReturn
],
rateShipment: [
RateShipment
Expand Down
3 changes: 3 additions & 0 deletions src/core/test-app/runner/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CreateShipmentMultiPackageConfigOptions } from './config/create-shipmen
import { CreateShipmentWithInsuranceConfigOptions } from './config/create-shipment-insurance';
import { RateShipmentConfigOptions } from './config/rate-shipment';
import { CancelShipmentConfigOptions } from './config/cancel-shipment';
import { CreateShipmentReturnConfigOptions } from './config/create-shipment-return';

export interface TestsConfig {
// cancelPickups?: (TestOptions & TestOptions) | [TestOptions];
Expand All @@ -17,8 +18,10 @@ export interface TestsConfig {
| [CreateShipmentInternationalConfigOptions];
createShipment_multi_package?: CreateShipmentMultiPackageConfigOptions | [CreateShipmentMultiPackageConfigOptions];
createShipment_with_insurance?: CreateShipmentWithInsuranceConfigOptions | [CreateShipmentWithInsuranceConfigOptions];
createShipment_return?: CreateShipmentReturnConfigOptions | [CreateShipmentReturnConfigOptions];
rateShipment?: RateShipmentConfigOptions | [RateShipmentConfigOptions];


// createShipment_multi_package?: TestOptions | [TestOptions];
// rateShipmentWithOneService?: RateShipmentConfigOptions | [RateShipmentConfigOptions];
// schedulePickup?: SchedulePickupOptions | [SchedulePickupOptions];
Expand Down
26 changes: 26 additions & 0 deletions src/core/test-app/runner/config/create-shipment-return.ts
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 {}
1 change: 1 addition & 0 deletions src/core/test-app/runner/load-and-validate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function validate(config: Config): void {
"createShipment_multi_package",
"createShipment_regional",
"createShipment_with_insurance",
"createShipment_return",
"rateShipment",
"cancelShipment",
"schedulePickup",
Expand Down
214 changes: 214 additions & 0 deletions src/core/test-app/tests/create-shipment-return.ts
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);
}
);
});
}
}
1 change: 1 addition & 0 deletions src/core/test-app/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from "./create-shipment-domestic";
export * from "./create-shipment-multipackage";
export * from "./create-shipment-with-insurance";
export * from "./create-shipment-international";
export * from "./create-shipment-return";
// export * from "./get-sales-order-test-suite";
// export * from "./get-sales-orders-by-date-test-suite";
// export * from "./get-seller-test-suite";
Expand Down
5 changes: 4 additions & 1 deletion src/core/test-app/utils/object-to-test-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ function formatTitleParameter(key: string, value: any) {
return value.country;
case "shipFrom":
return value.country;
case "returnTo":
return value.country;
case "packages":
return `${value.length}`;

case "deliveryServiceNames":
if (Array.isArray(value)) {
return `${value.join(", ")}`;
}

return `${value}`;

case "packageInsuredValue":
return `${value.value} ${value.currency}`;

default:
return value;
}
Expand Down
Loading

0 comments on commit 69305ae

Please sign in to comment.