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

Commit

Permalink
Added back fin-matching-available tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Robertson committed Nov 12, 2020
1 parent 634e97b commit b5252b1
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 16 deletions.
24 changes: 10 additions & 14 deletions src/core/test-app/utils/find-matching-available-countries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Country, DeliveryService } from "@shipengine/connect-sdk";

const matchReducer = (matchingCountries: Country[], newCountries: Country[]): Country[] => {
return newCountries.filter(country => matchingCountries.includes(country));
};

/**
* Find the shared availableCountries from an array of delivery services
*/
Expand All @@ -9,19 +13,11 @@ export function findMatchingAvailableCountries(deliveryServices: DeliveryService
throw new Error("Multiple Delivery Services must be specified");
}

const sharedavailableCountries = deliveryServices.reduce((acc, currentDS) => {

const filteredArray = [];

for (const country of acc) {
if (currentDS.availableCountries.includes(country)) {
filteredArray.push(country);
}
}

return filteredArray;

}, deliveryServices[0].availableCountries);
const serviceCountries = deliveryServices.map(service => service.availableCountries as Country[]);
const sharedCountries = serviceCountries.reduce(matchReducer, serviceCountries[0]);
if(sharedCountries.length === 0) {
throw new Error('Specified delivery services do not share origin and destination countries');
}

return sharedavailableCountries as Country[];
return sharedCountries;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ describe("The create shipment domestic test suite", () => {
const app = new CarrierApp(appDefinition);
const args = { app, connectArgs, staticConfigTests, options };
const testSuite = new CreateShipmentDomestic(args);

const tests = testSuite.tests();
console.log(tests);
expect(tests.length).to.equal(0);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"use strict";

const { expect } = require("chai");
const pojo = require("../../../../utils/pojo");
const { findMatchingAvailableCountries } = require("../../../../../lib/core/test-app/utils/find-matching-available-countries");
const { CarrierApp } = require("@shipengine/connect-sdk/lib/internal/carriers/carrier-app");

describe("findMatchingAvailableCountries", () => {
it("returns a set of available countries that all given delivery services share", () => {

const appDefinition = pojo.carrierApp();
const deliveryService = pojo.deliveryService();
appDefinition.deliveryServices = [deliveryService];

appDefinition.deliveryServices[0].availableCountries = ["US", "MX", "CA"];

appDefinition.deliveryServices.push({
id: "9df1bfda-7ee4-4f03-96f6-6eab52243eee",
name: "Another Delivery Service",
code: "ad",
manifestType: "digital",
availableCountries: ["US", "MX", "CA"],
labelFormats: ["pdf"],
labelSizes: ["A4"],
packaging: [pojo.packaging()]
});

appDefinition.deliveryServices.push({
id: "9cf1bfda-7ee4-4f03-96f6-6eab52243eee",
name: "Better Delivery Service",
code: "bd",
manifestType: "digital",
availableCountries: ["US", "MX"],
packaging: [pojo.packaging()]
});

const app = new CarrierApp(appDefinition);
const results = findMatchingAvailableCountries(app.deliveryServices);
expect(results.length).to.equal(2);
});

it("throw an error if only 0 or 1 delivery services are sent into the function", () => {
const appDefinition = pojo.carrierApp();
const deliveryService = pojo.deliveryService();
appDefinition.deliveryServices = [deliveryService];

const app = new CarrierApp(appDefinition);

expect(() => findMatchingAvailableCountries(app.deliveryServices)).to.throw(
Error,
/Multiple Delivery Services must be specified/,
);
});

it("throws an error when a matching set of countries are unable to be found for the given delivery services", () => {

const appDefinition = pojo.carrierApp();
const deliveryService = pojo.deliveryService();
appDefinition.deliveryServices = [deliveryService];

appDefinition.deliveryServices[0].availableCountries = ["US", "MX", "CA"];

appDefinition.deliveryServices.push({
id: "9df1bfda-7ee4-4f03-96f6-6eab52243eee",
name: "Another Delivery Service",
code: "ad",
manifestType: "digital",
availableCountries: ["CA"],
packaging: [pojo.packaging()]
});

appDefinition.deliveryServices.push({
id: "9cf1bfda-7ee4-4f03-96f6-6eab52243eee",
name: "Better Delivery Service",
code: "bd",
manifestType: "digital",
availableCountries: ["US", "MX"],
packaging: [pojo.packaging()]
});

const app = new CarrierApp(appDefinition);

expect(() => findMatchingAvailableCountries(app.deliveryServices)).to.throw(
Error,
/Specified delivery services do not share origin and destination countries/,
);
});
});

0 comments on commit b5252b1

Please sign in to comment.