|
| 1 | +import { MultiLineAddress } from "../models"; |
| 2 | +import { IdentityValidationApi } from "../api/identity-validation-api"; |
| 3 | +import { fail } from "./testUtilities"; |
| 4 | +import { |
| 5 | + CONFIG_FOR_UNIT, |
| 6 | + CONFIG_WITH_BASE_OPTIONS_FOR_UNIT, |
| 7 | +} from "./testFixtures"; |
| 8 | + |
| 9 | +// Axios Mock |
| 10 | +import axios from "axios"; |
| 11 | +const axiosRequest: jest.Mock = axios.request as jest.Mock; |
| 12 | +jest.mock("axios", () => ({ |
| 13 | + request: jest.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +describe("IdentityValidationApi", () => { |
| 17 | + it("can be instantiated", () => { |
| 18 | + const intlApi = new IdentityValidationApi(CONFIG_FOR_UNIT); |
| 19 | + expect(intlApi).toBeDefined(); |
| 20 | + expect(typeof intlApi).toEqual("object"); |
| 21 | + expect(intlApi).toBeInstanceOf(IdentityValidationApi); |
| 22 | + }); |
| 23 | + |
| 24 | + it("can be instantiated with base options", () => { |
| 25 | + const intlApi = new IdentityValidationApi( |
| 26 | + CONFIG_WITH_BASE_OPTIONS_FOR_UNIT |
| 27 | + ); |
| 28 | + expect(intlApi).toBeDefined(); |
| 29 | + expect(typeof intlApi).toEqual("object"); |
| 30 | + expect(intlApi).toBeInstanceOf(IdentityValidationApi); |
| 31 | + }); |
| 32 | + |
| 33 | + describe("validate", () => { |
| 34 | + const multilineAddress = new MultiLineAddress({ |
| 35 | + recipient: "Lob.com", |
| 36 | + primary_line: "210 King St", |
| 37 | + city: "San Francisco", |
| 38 | + state: "CA", |
| 39 | + zip_code: "94107", |
| 40 | + }); |
| 41 | + |
| 42 | + it("exists", () => { |
| 43 | + const intlApi = new IdentityValidationApi(CONFIG_FOR_UNIT); |
| 44 | + expect(intlApi.validate).toBeDefined(); |
| 45 | + expect(typeof intlApi.validate).toEqual("function"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("handles errors returned by the api", async () => { |
| 49 | + axiosRequest.mockImplementationOnce(async () => { |
| 50 | + throw { |
| 51 | + message: "error", |
| 52 | + response: { data: { error: { message: "error reported by API" } } }, |
| 53 | + }; |
| 54 | + }); |
| 55 | + |
| 56 | + try { |
| 57 | + await new IdentityValidationApi(CONFIG_FOR_UNIT).validate( |
| 58 | + multilineAddress |
| 59 | + ); |
| 60 | + } catch (err: any) { |
| 61 | + expect(err.message).toEqual("error reported by API"); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + it("handles errors returned by the api with missing response.data", async () => { |
| 66 | + axiosRequest.mockImplementationOnce(async () => { |
| 67 | + throw { |
| 68 | + message: "error", |
| 69 | + response: {}, |
| 70 | + }; |
| 71 | + }); |
| 72 | + |
| 73 | + try { |
| 74 | + await new IdentityValidationApi(CONFIG_FOR_UNIT).validate( |
| 75 | + multilineAddress |
| 76 | + ); |
| 77 | + fail("Should throw"); |
| 78 | + } catch (err: any) { |
| 79 | + expect(err.message).toEqual("error"); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + it("handles errors returned by the api with missing response.data.error", async () => { |
| 84 | + axiosRequest.mockImplementationOnce(async () => { |
| 85 | + throw { |
| 86 | + message: "error", |
| 87 | + response: { data: {} }, |
| 88 | + }; |
| 89 | + }); |
| 90 | + |
| 91 | + try { |
| 92 | + await new IdentityValidationApi(CONFIG_FOR_UNIT).validate( |
| 93 | + multilineAddress |
| 94 | + ); |
| 95 | + fail("Should throw"); |
| 96 | + } catch (err: any) { |
| 97 | + expect(err.message).toEqual("error"); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + it("handles errors in making the request", async () => { |
| 102 | + axiosRequest.mockImplementationOnce(async () => { |
| 103 | + throw new Error("Unknown Error"); |
| 104 | + }); |
| 105 | + |
| 106 | + try { |
| 107 | + await new IdentityValidationApi(CONFIG_FOR_UNIT).validate( |
| 108 | + multilineAddress |
| 109 | + ); |
| 110 | + fail("Should throw"); |
| 111 | + } catch (err: any) { |
| 112 | + expect(err.message).toEqual("Unknown Error"); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + it("validates whether a given name is associated with an address", async () => { |
| 117 | + axiosRequest.mockImplementationOnce(async () => ({ |
| 118 | + data: { id: "id_validation_fakeId" }, |
| 119 | + })); |
| 120 | + |
| 121 | + const response = await new IdentityValidationApi( |
| 122 | + CONFIG_FOR_UNIT |
| 123 | + ).validate(multilineAddress); |
| 124 | + expect(response.id).toBeDefined(); |
| 125 | + expect(response.id).toEqual("id_validation_fakeId"); |
| 126 | + }); |
| 127 | + |
| 128 | + it("includes custom headers while it validates", async () => { |
| 129 | + axiosRequest.mockImplementationOnce(async () => ({ |
| 130 | + data: { id: "id_validation_fakeId" }, |
| 131 | + })); |
| 132 | + |
| 133 | + const response = await new IdentityValidationApi( |
| 134 | + CONFIG_WITH_BASE_OPTIONS_FOR_UNIT |
| 135 | + ).validate(multilineAddress); |
| 136 | + expect(response.id).toBeDefined(); |
| 137 | + expect(response.id).toEqual("id_validation_fakeId"); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments