diff --git a/src/international_street/Client.js b/src/international_street/Client.js index ef0a322..727aa64 100644 --- a/src/international_street/Client.js +++ b/src/international_street/Client.js @@ -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); diff --git a/src/international_street/Lookup.js b/src/international_street/Lookup.js index 4357ccb..00b2a5c 100644 --- a/src/international_street/Lookup.js +++ b/src/international_street/Lookup.js @@ -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." }; @@ -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; } diff --git a/tests/international_street/test_Client.js b/tests/international_street/test_Client.js index 20e64e7..6224e5f 100644 --- a/tests/international_street/test_Client.js +++ b/tests/international_street/test_Client.js @@ -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); @@ -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 => { diff --git a/tests/international_street/test_Lookup.js b/tests/international_street/test_Lookup.js index ed4e40f..c2c9e91 100644 --- a/tests/international_street/test_Lookup.js +++ b/tests/international_street/test_Lookup.js @@ -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." }; @@ -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!"; @@ -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 () {