-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathformApiService.test.ts
More file actions
119 lines (106 loc) · 3.85 KB
/
formApiService.test.ts
File metadata and controls
119 lines (106 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { post, apiDelete } from "../../api/apiService"
import {
submitForm,
uploadProofFile,
deleteUploadedProofFile,
locateVerifiedAddress,
} from "../../api/formApiService"
jest.mock("axios")
jest.mock("../../api/apiService", () => ({
post: jest.fn(),
apiDelete: jest.fn(),
}))
describe("formApiService", () => {
beforeEach(() => {
jest.clearAllMocks()
;(post as jest.Mock).mockResolvedValue({ data: { id: "test-id" } })
;(apiDelete as jest.Mock).mockResolvedValue({ data: { success: true } })
})
describe("uploadProofFile", () => {
it("posts form data to the proof endpoint", async () => {
const mockResponse = { success: true, name: "file.pdf", created_at: "2026-01-01" }
;(post as jest.Mock).mockResolvedValue({ data: mockResponse })
const file = new File(["content"], "file.pdf", { type: "application/pdf" })
const result = await uploadProofFile("session-1", "listing-1", "pref-1", "Gas bill", file)
expect(post).toHaveBeenCalledWith("/api/v1/short-form/proof", expect.any(FormData), {
headers: { "Content-Type": "multipart/form-data" },
})
const formData: FormData = (post as jest.Mock).mock.calls[0][1]
expect(formData.get("uploaded_file[session_uid]")).toBe("session-1")
expect(formData.get("uploaded_file[listing_id]")).toBe("listing-1")
expect(formData.get("uploaded_file[listing_preference_id]")).toBe("pref-1")
expect(formData.get("uploaded_file[document_type]")).toBe("Gas bill")
expect(formData.get("uploaded_file[file]")).toBeInstanceOf(File)
expect(result).toEqual(mockResponse)
})
})
describe("deleteUploadedProofFile", () => {
it("sends a delete request with the correct payload", async () => {
const result = await deleteUploadedProofFile("session-1", "listing-1", "pref-1", "Gas bill")
expect(apiDelete).toHaveBeenCalledWith("/api/v1/short-form/proof", {
data: {
uploaded_file: {
session_uid: "session-1",
listing_id: "listing-1",
listing_preference_id: "pref-1",
document_type: "Gas bill",
},
},
})
expect(result).toEqual({ success: true })
})
})
describe("submitForm", () => {
it("submits the application form", async () => {
const formData = {
primaryApplicantFirstName: "First name",
primaryApplicantMiddleName: "Middle name",
primaryApplicantLastName: "Last name",
primaryApplicantDob: "1990-01-01",
}
await submitForm(formData, "testListingId")
const today = new Date().toISOString().split("T")[0]
expect(post).toHaveBeenCalledWith("/api/v1/short-form/application", {
application: {
listingID: "testListingId",
applicationLanguage: undefined,
status: "Submitted",
primaryApplicant: {
firstName: "First name",
middleName: "Middle name",
lastName: "Last name",
dob: "1990-01-01",
},
householdMembers: [],
annualIncome: 0,
applicationSubmittedDate: today,
},
autosave: false,
initialSave: true,
locale: "en",
uploaded_file: { file: "todo.png" },
})
})
})
describe("locateVerifiedAddress", () => {
it("sends the address fields to validate with the correct payload", async () => {
const result = await locateVerifiedAddress({
street1: "123 Main St",
street2: "Apt 4B",
city: "San Francisco",
state: "CA",
zip: "94105",
})
expect(post).toHaveBeenCalledWith("/api/v1/addresses/validate.json", {
address: {
street1: "123 Main St",
street2: "Apt 4B",
city: "San Francisco",
state: "CA",
zip: "94105",
},
})
expect(result).toEqual({ id: "test-id" })
})
})
})