Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/international_street/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Client {
send(lookup) {
if (typeof lookup === "undefined") throw new Errors.UndefinedLookupError();

lookup.ensureEnoughInfo();

let request = new Request();
request.parameters = buildInputData(lookup, keyTranslationFormat);

Expand Down
9 changes: 1 addition & 8 deletions src/international_street/Lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const UnprocessableEntityError = require("../Errors").UnprocessableEntityError;
const messages = {
countryRequired: "Country field is required.",
freeformOrAddress1Required: "Either freeform or address1 is required.",
insufficientInformation: "Insufficient information: One or more required fields were not set on the lookup.",
badGeocode: "Invalid input: geocode can only be set to 'true' (default is 'false'.",
invalidLanguage: "Invalid input: language can only be set to 'latin' or 'native'. When not set, the the output language will match the language of the input values."
};
Expand Down Expand Up @@ -45,13 +44,7 @@ class Lookup {
ensureEnoughInfo() {
if (fieldIsMissing(this.country)) throw new UnprocessableEntityError(messages.countryRequired);

if (fieldIsSet(this.freeform)) return true;

if (fieldIsMissing(this.address1)) throw new UnprocessableEntityError(messages.freeformOrAddress1Required);

if (fieldIsSet(this.postalCode)) return true;

if (fieldIsMissing(this.locality) || fieldIsMissing(this.administrativeArea)) throw new UnprocessableEntityError(messages.insufficientInformation);
if (fieldIsMissing(this.freeform) && fieldIsMissing(this.address1)) throw new UnprocessableEntityError(messages.freeformOrAddress1Required);

return true;
}
Expand Down
16 changes: 15 additions & 1 deletion tests/international_street/test_Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ describe("An International Street client", function () {
expect(client.send).to.throw(errors.UndefinedLookupError);
});

it("throws an error if sending an empty lookup.", function () {
let mockSender = new MockSender();
let client = new Client(mockSender);

expect(() => client.send(new Lookup())).to.throw(errors.UnprocessableEntityError, "Country field is required.");
});

it("throws an error if sending a lookup with country but missing freeform and address1.", function () {
let mockSender = new MockSender();
let client = new Client(mockSender);

expect(() => client.send(new Lookup("CA"))).to.throw(errors.UnprocessableEntityError, "Either freeform or address1 is required.");
});

it("correctly assigns request parameters based on lookup input.", function () {
let mockSender = new MockSender();
let client = new Client(mockSender);
Expand Down Expand Up @@ -60,7 +74,7 @@ describe("An International Street client", function () {
const expectedMockPayload = [{address1: "A", }];
let mockSender = new MockSenderWithResponse(expectedMockPayload);
const client = new Client(mockSender);
let lookup = new Lookup();
let lookup = new Lookup("CA", "123 Main St");
let expectedResult = new Candidate({address1: "A"});

return client.send(lookup).then(response => {
Expand Down
31 changes: 0 additions & 31 deletions tests/international_street/test_Lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ describe("An International Street lookup", function () {
const messages = {
countryRequired: "Country field is required.",
freeformOrAddress1Required: "Either freeform or address1 is required.",
insufficientInformation: "Insufficient information: One or more required fields were not set on the lookup.",
badGeocode: "Invalid input: geocode can only be set to 'true' (default is 'false'.",
invalidLanguage: "Invalid input: language can only be set to 'latin' or 'native'. When not set, the the output language will match the language of the input values."
};
Expand All @@ -27,29 +26,6 @@ describe("An International Street lookup", function () {
ensureValidationThrows(new Lookup("a").ensureEnoughInfo, messages.freeformOrAddress1Required);
});

it("rejects lookups with only a country and address 1.", function () {
let lookup = new Lookup("a");
lookup.address1 = "b";

ensureValidationThrows(lookup.ensureEnoughInfo, messages.insufficientInformation);
});

it("rejects lookups with only a country, address 1, and locality.", function () {
let lookup = new Lookup("a");
lookup.address1 = "b";
lookup.locality = "c";

ensureValidationThrows(lookup.ensureEnoughInfo, messages.insufficientInformation);
});

it("rejects lookups with only a country, address 1, and adminstrative area.", function () {
let lookup = new Lookup("a");
lookup.address1 = "b";
lookup.administrativeArea = "c";

ensureValidationThrows(lookup.ensureEnoughInfo, messages.insufficientInformation);
});

it("rejects lookups with an invalid geocode value.", function () {
let lookup = new Lookup();
lookup.geocode = "Blarg!";
Expand All @@ -69,16 +45,9 @@ describe("An International Street lookup", function () {

let lookup2 = new Lookup("a");
lookup2.address1 = "b";
lookup2.postalCode = "c";

let lookup3 = new Lookup("a");
lookup3.address1 = "b";
lookup3.locality = "c";
lookup3.administrativeArea = "d";

expect(lookup1.ensureEnoughInfo()).to.equal(true);
expect(lookup2.ensureEnoughInfo()).to.equal(true);
expect(lookup3.ensureEnoughInfo()).to.equal(true);
});

it("accepts lookups with a valid language.", function () {
Expand Down