|
| 1 | +import chai from "chai"; |
| 2 | +import sinon from "sinon"; |
| 3 | + |
| 4 | +import { EnhancedSearchService } from "../../../../src/services/search/enhanced-search"; |
| 5 | +import { RequestClient } from "../../../../src/http"; |
| 6 | +import { CompaniesResource } from "../../../../src/services/search/enhanced-search/types"; |
| 7 | +import Resource from "../../../../src/services/resource"; |
| 8 | + |
| 9 | +const expect = chai.expect; |
| 10 | + |
| 11 | +const requestClient = new RequestClient({ baseUrl: "URL-NOT-USED", oauthToken: "TOKEN-NOT-USED" }); |
| 12 | + |
| 13 | +const mockResponseBody : CompaniesResource = ({ |
| 14 | + etag: "etag", |
| 15 | + top_hit: { |
| 16 | + company_name: "test company", |
| 17 | + company_number: "0000789", |
| 18 | + company_status: "active", |
| 19 | + company_type: "company type", |
| 20 | + kind: "kind", |
| 21 | + links: { |
| 22 | + company_profile: "/company/FC022000" |
| 23 | + }, |
| 24 | + date_of_cessation: (new Date("19910212")), |
| 25 | + date_of_creation: (new Date("19910212")), |
| 26 | + registered_office_address: { |
| 27 | + address_line_1: "test house", |
| 28 | + address_line_2: "test street", |
| 29 | + locality: "cardiff", |
| 30 | + postal_code: "cf5 6rb", |
| 31 | + premises: "premises", |
| 32 | + country: "country" |
| 33 | + } |
| 34 | + }, |
| 35 | + items: [ |
| 36 | + { |
| 37 | + company_name: "test company", |
| 38 | + company_number: "0000789", |
| 39 | + company_status: "active", |
| 40 | + company_type: "company type", |
| 41 | + kind: "kind", |
| 42 | + links: { |
| 43 | + company_profile: "/company/FC022000" |
| 44 | + }, |
| 45 | + date_of_cessation: (new Date("19910212")), |
| 46 | + date_of_creation: (new Date("19910212")), |
| 47 | + registered_office_address: { |
| 48 | + address_line_1: "test house", |
| 49 | + address_line_2: "test street", |
| 50 | + locality: "cardiff", |
| 51 | + postal_code: "cf5 6rb", |
| 52 | + premises: "premises", |
| 53 | + country: "country" |
| 54 | + } |
| 55 | + } |
| 56 | + ], |
| 57 | + kind: "kind", |
| 58 | + hits: 1 |
| 59 | +}) |
| 60 | + |
| 61 | +const mockRequestId = "fdskfhsdoifhsffsif"; |
| 62 | +const testCompanyName = "TEST COMPANY NAME"; |
| 63 | +const testLocation = "TEST LOCATION"; |
| 64 | +const testIncorporatedFrom = " TEST INCORPORATED FROM"; |
| 65 | +const testIncorporatedTo = " TEST INCORPORATED TO"; |
| 66 | +const searchType = "enhanced"; |
| 67 | + |
| 68 | +describe("create an enhanced search GET", () => { |
| 69 | + beforeEach(() => { |
| 70 | + sinon.reset(); |
| 71 | + sinon.restore(); |
| 72 | + }); |
| 73 | + |
| 74 | + afterEach(done => { |
| 75 | + sinon.reset(); |
| 76 | + sinon.restore(); |
| 77 | + done(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("returns an error response on failure", async () => { |
| 81 | + const mockGetRequest = { |
| 82 | + status: 401, |
| 83 | + error: "An error occurred" |
| 84 | + }; |
| 85 | + |
| 86 | + const mockRequest = sinon.stub(requestClient, "httpGet").resolves(mockGetRequest); |
| 87 | + const search: EnhancedSearchService = new EnhancedSearchService(requestClient); |
| 88 | + const data: Resource<CompaniesResource> = await search.getCompanies(testCompanyName, testLocation, testIncorporatedFrom, testIncorporatedTo, mockRequestId); |
| 89 | + |
| 90 | + expect(data.httpStatusCode).to.equal(401); |
| 91 | + expect(data.resource).to.be.undefined; |
| 92 | + }); |
| 93 | + |
| 94 | + it("returns enhanced search results correctly", async () => { |
| 95 | + const mockGetRequest = { |
| 96 | + status: 200, |
| 97 | + body: mockResponseBody |
| 98 | + }; |
| 99 | + |
| 100 | + const mockRequest = sinon.stub(requestClient, "httpGet").resolves(mockGetRequest); |
| 101 | + const search: EnhancedSearchService = new EnhancedSearchService(requestClient); |
| 102 | + const data: Resource<CompaniesResource> = await search.getCompanies(testCompanyName, testLocation, testIncorporatedFrom, testIncorporatedTo, mockRequestId); |
| 103 | + const item = data.resource.items[0]; |
| 104 | + const mockItem = mockResponseBody.items[0]; |
| 105 | + |
| 106 | + expect(data.httpStatusCode).to.equal(200); |
| 107 | + expect(data.resource.etag).to.equal(mockResponseBody.etag); |
| 108 | + expect(data.resource.top_hit.company_name).to.equal(mockResponseBody.top_hit.company_name); |
| 109 | + expect(data.resource.top_hit.company_number).to.equal(mockResponseBody.top_hit.company_number); |
| 110 | + expect(data.resource.top_hit.company_status).to.equal(mockResponseBody.top_hit.company_status); |
| 111 | + expect(data.resource.top_hit.company_type).to.equal(mockResponseBody.top_hit.company_type); |
| 112 | + expect(data.resource.top_hit.kind).to.equal(mockResponseBody.top_hit.kind); |
| 113 | + expect(data.resource.top_hit.links.company_profile).to.equal(mockResponseBody.top_hit.links.company_profile); |
| 114 | + expect(data.resource.top_hit.date_of_cessation).to.equal(mockResponseBody.top_hit.date_of_cessation); |
| 115 | + expect(data.resource.top_hit.date_of_creation).to.equal(mockResponseBody.top_hit.date_of_creation); |
| 116 | + expect(data.resource.top_hit.registered_office_address.address_line_1).to.equal(mockResponseBody.top_hit.registered_office_address.address_line_1); |
| 117 | + expect(data.resource.top_hit.registered_office_address.address_line_2).to.equal(mockResponseBody.top_hit.registered_office_address.address_line_2); |
| 118 | + expect(data.resource.top_hit.registered_office_address.locality).to.equal(mockResponseBody.top_hit.registered_office_address.locality); |
| 119 | + expect(data.resource.top_hit.registered_office_address.postal_code).to.equal(mockResponseBody.top_hit.registered_office_address.postal_code); |
| 120 | + expect(data.resource.top_hit.registered_office_address.premises).to.equal(mockResponseBody.top_hit.registered_office_address.premises); |
| 121 | + expect(data.resource.top_hit.registered_office_address.country).to.equal(mockResponseBody.top_hit.registered_office_address.country); |
| 122 | + expect(item.company_name).to.equal(mockItem.company_name); |
| 123 | + expect(item.company_number).to.equal(mockItem.company_number); |
| 124 | + expect(item.company_status).to.equal(mockItem.company_status); |
| 125 | + expect(item.kind).to.equal(mockItem.kind); |
| 126 | + expect(item.links.company_profile).to.equal(mockItem.links.company_profile); |
| 127 | + expect(item.date_of_cessation).to.equal(mockItem.date_of_cessation); |
| 128 | + expect(item.date_of_creation).to.equal(mockItem.date_of_creation); |
| 129 | + expect(item.registered_office_address.address_line_1).to.equal(mockItem.registered_office_address.address_line_1); |
| 130 | + expect(item.registered_office_address.address_line_2).to.equal(mockItem.registered_office_address.address_line_2); |
| 131 | + expect(item.registered_office_address.locality).to.equal(mockItem.registered_office_address.locality); |
| 132 | + expect(item.registered_office_address.postal_code).to.equal(mockItem.registered_office_address.postal_code); |
| 133 | + expect(item.registered_office_address.premises).to.equal(mockItem.registered_office_address.premises); |
| 134 | + expect(data.resource.kind).to.equal(mockResponseBody.kind); |
| 135 | + expect(data.resource.hits).to.equal(mockResponseBody.hits); |
| 136 | + }); |
| 137 | +}); |
0 commit comments