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

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrauskopf committed Oct 13, 2020
1 parent bcc5604 commit 8eeb189
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/core/test-app/runner/config/cancel-shipments-multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { BaseTestConfigOptions } from "./base-test-config-options";

export interface ShipmentConfig {
deliveryServiceName: string;
shipFrom?: AddressWithContactInfoPOJO;
shipTo?: AddressWithContactInfoPOJO;
shipFrom: AddressWithContactInfoPOJO;
shipTo: AddressWithContactInfoPOJO;
weight: {
value: number;
unit: WeightUnit;
Expand Down
26 changes: 15 additions & 11 deletions src/core/test-app/runner/validate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,21 @@ const cancelShipmentsSingleTestParamsSchema = Joi.object({
}
});

const cancelShipmentsMultipleTestParamsSchema = Joi.object({
...baseTestParamValidations,
...[{
deliveryServiceName: Joi.string().optional(),
shipDateTime: DateTimeZone[_internal].schema.optional(),
shipFrom: AddressWithContactInfo[_internal].schema.optional(),
shipTo: AddressWithContactInfo[_internal].schema.optional(),
weight: Weight[_internal].schema.optional(),
dimensions: Dimensions[_internal].schema.optional()
}]
});
const cancelShipmentsMultipleTestParamsSchema = Joi.object(
Object.assign(
baseTestParamValidations,
{
shipments: Joi.array().items({
deliveryServiceName: Joi.string().optional(),
shipDateTime: DateTimeZone[_internal].schema.optional(),
shipFrom: AddressWithContactInfo[_internal].schema.optional(),
shipTo: AddressWithContactInfo[_internal].schema.optional(),
weight: Weight[_internal].schema.optional(),
dimensions: Dimensions[_internal].schema.optional()
})
}
)
);

const createShipmentDomesticTestParamsSchema = Joi.object({
...baseTestParamValidations,
Expand Down
44 changes: 14 additions & 30 deletions src/core/test-app/tests/cancel-shipments-multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,11 @@ export class CancelShipmentsMultiple extends Suite {
throw new Error("connect.config.js shipments must contain two or more shipments");
}

const userOverrides: {
deliveryServiceName: string;
shipFrom?: AddressWithContactInfoPOJO,
shipTo?: AddressWithContactInfoPOJO,
shipDateTime: string;
}[] = [];

const shipmentNumber = config.shipments.length || 2;
const defaults: CancelShipmentsMultipleTestParams = { shipments: [] };
const { tomorrow } = initializeTimeStamps();

// Genereate the potential user overrides
for (let i = 0; i < shipmentNumber; i++) {
const deliveryServiceName = config.shipments[i] && config.shipments[i].deliveryServiceName || "";
const deliveryService = this.setDeliveryService(deliveryServiceName);
Expand All @@ -69,27 +65,11 @@ export class CancelShipmentsMultiple extends Suite {
break;
}

const { tomorrow } = initializeTimeStamps();

userOverrides.push({
defaults.shipments.push({
deliveryServiceName: deliveryService.name,
shipFrom,
shipTo,
shipDateTime: tomorrow
});
}

if (userOverrides.length === 0) {
return undefined;
}

const defaults: CancelShipmentsMultipleTestParams = { shipments: [] };
defaults.shipments = userOverrides.map((test) => {
return {
deliveryServiceName: test.deliveryServiceName,
shipDateTime: test.shipDateTime,
shipFrom: test.shipFrom,
shipTo: test.shipTo,
shipDateTime: tomorrow,
shipFrom: shipFrom,
shipTo: shipTo,
weight: {
unit: WeightUnit.Pounds,
value: 50.0,
Expand All @@ -100,15 +80,19 @@ export class CancelShipmentsMultiple extends Suite {
height: 12,
unit: LengthUnit.Inches
}
}
});
});
}

if (defaults.shipments.length === 0) {
return undefined;
}

const testParams = { shipments: [] } as CancelShipmentsMultipleTestParams;

for (let i = 0; i < shipmentNumber; i++) {
const reduced = reduceDefaultsWithConfig<
ShipmentConfig
>(defaults.shipments[i], config.shipments[i] || {});
>(defaults.shipments[i], config.shipments[i]);

testParams.shipments.push(reduced);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/test-app/utils/reduce-defaults-with-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
export default function reduceDefaultsWithConfig<T>(
defaultObject: T,
configObject: object,
configObject: object = {},
): T {
const whiteListKeys = Object.keys(defaultObject);

Expand Down
40 changes: 40 additions & 0 deletions test/specs/core/test-app/tests/cancel-shipments-multiple.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,46 @@ describe("The cancel multiple shipments test suite", () => {
});
});

describe("When a user configs a more than two shipments", () => {
it("should update the methodargs to reflect the new properties", () => {
const { appDefinition, connectArgs, staticConfigTests, options } = generateBasicAppAndConfigs();
appDefinition.deliveryServices.push({
id: "9cf1bfda-7ee4-4f03-96f6-6eab52243eee",
name: "Better Delivery Service",
code: "better_ds",
manifestType: "physical",
originCountries: ["MX"],
destinationCountries: ["MX"],
labelFormats: ["pdf"],
labelSizes: ["A4"],
packaging: [pojo.packaging()]
});

staticConfigTests.cancelShipments_multiple = {
shipments: [
{
deliveryServiceName: "Better Delivery Service"
},
{
deliveryServiceName: "Dummy Delivery Service"
},
{
deliveryServiceName: "Better Delivery Service"
}
]
};

const app = new CarrierApp(appDefinition);
const args = { app, connectArgs, staticConfigTests, options };
const testSuite = new CancelShipmentsMultiple(args);
const tests = testSuite.tests();

expect(tests[0].title).to.include("3 shipments");
expect(tests[0].methodArgs[0].deliveryService.id).to.equal("9cf1bfda-7ee4-4f03-96f6-6eab52243eee");
expect(tests[0].methodArgs[1].deliveryService.id).to.equal("22222222-2222-2222-2222-222222222222");
});
});

describe("When a delivery service has addresses that we don't have samples but user uses valid configs", () => {
it("should generate tests", () => {
const { appDefinition, connectArgs, staticConfigTests, options } = generateBasicAppAndConfigs();
Expand Down

0 comments on commit 8eeb189

Please sign in to comment.