|
| 1 | +import chai from "chai"; |
| 2 | +import sinon from "sinon"; |
| 3 | + |
| 4 | +import TransactionService from "../../../src/services/transaction/service"; |
| 5 | +import { RequestClient } from "../../../src/http"; |
| 6 | +import { Transaction, TransactionResource } from "../../../src/services/transaction"; |
| 7 | +const expect = chai.expect; |
| 8 | + |
| 9 | +const requestClient = new RequestClient({ baseUrl: "URL-NOT-USED", oauthToken: "TOKEN-NOT-USED" }); |
| 10 | + |
| 11 | +describe("transaction", () => { |
| 12 | + beforeEach(() => { |
| 13 | + sinon.reset(); |
| 14 | + sinon.restore(); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(done => { |
| 18 | + sinon.reset(); |
| 19 | + sinon.restore(); |
| 20 | + done(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("returns an error response on failure", async () => { |
| 24 | + const mockPostResponse = { |
| 25 | + status: 401, |
| 26 | + error: "An error occurred" |
| 27 | + }; |
| 28 | + |
| 29 | + const mockRequest = sinon.stub(requestClient, "httpPost").resolves(mockPostResponse); |
| 30 | + const transaction : TransactionService = new TransactionService(requestClient); |
| 31 | + const data = await transaction.postTransaction({} as Transaction); |
| 32 | + |
| 33 | + expect(data.httpStatusCode).to.equal(401); |
| 34 | + expect(data.resource).to.be.undefined; |
| 35 | + }); |
| 36 | + |
| 37 | + it("maps the company field data items correctly", async () => { |
| 38 | + const mockResponseBody : TransactionResource = ({ |
| 39 | + id: "12345678", |
| 40 | + company_name: "HELLO LTD", |
| 41 | + company_number: "88", |
| 42 | + links: { |
| 43 | + self: "/self" |
| 44 | + }, |
| 45 | + reference: "ref", |
| 46 | + description: "desc" |
| 47 | + }); |
| 48 | + |
| 49 | + const mockPostResponse = { |
| 50 | + status: 200, |
| 51 | + body: mockResponseBody |
| 52 | + }; |
| 53 | + |
| 54 | + const mockRequest = sinon.stub(requestClient, "httpPost").resolves(mockPostResponse); |
| 55 | + const transaction : TransactionService = new TransactionService(requestClient); |
| 56 | + const data = await transaction.postTransaction({} as Transaction); |
| 57 | + |
| 58 | + expect(data.httpStatusCode).to.equal(200); |
| 59 | + expect(data.resource.companyName).to.equal(mockResponseBody.company_name); |
| 60 | + expect(data.resource.companyNumber).to.equal(mockResponseBody.company_number); |
| 61 | + expect(data.resource.links.self).to.equal(mockResponseBody.links.self); |
| 62 | + expect(data.resource.reference).to.equal(mockResponseBody.reference); |
| 63 | + expect(data.resource.description).to.equal(mockResponseBody.description); |
| 64 | + }); |
| 65 | +}); |
0 commit comments