commit | author | age
|
bae328
|
1 |
import React from "react"; |
JR |
2 |
import { shallow, ShallowWrapper } from "enzyme"; |
|
3 |
import AdoptionForm from "./AdoptionForm"; |
|
4 |
import AdoptionFakeService from "../Services/AdoptionFakeService"; |
|
5 |
import { AdoptionService } from "../Services/AdoptionService"; |
|
6 |
import { Residency } from "../Models/Residency"; |
|
7 |
|
|
8 |
|
|
9 |
describe("AdoptionForm", () => { |
|
10 |
|
|
11 |
let adoptionService: AdoptionService; |
|
12 |
let component: ShallowWrapper; |
|
13 |
|
|
14 |
beforeEach(() => { |
|
15 |
adoptionService = new AdoptionFakeService(); |
|
16 |
component = shallow(<AdoptionForm |
|
17 |
adoptionService={adoptionService} |
|
18 |
animalId="fake-animal-id" |
|
19 |
/>); |
|
20 |
}); |
|
21 |
|
|
22 |
test("Changes state when username is changed", async () => { |
|
23 |
component.find("#adoption-form-username").simulate("change", "John"); |
|
24 |
expect(component.state("username")).toBe("John"); |
|
25 |
}); |
|
26 |
|
|
27 |
test("Changes state when email is changed", async () => { |
|
28 |
component.find("#adoption-form-email").simulate("change", "test@example.com"); |
|
29 |
expect(component.state("email")).toBe("test@example.com"); |
|
30 |
}); |
|
31 |
|
|
32 |
test("Changes state when occupation is changed", async () => { |
|
33 |
component.find("#adoption-form-occupation").simulate("change", "Pilot"); |
|
34 |
expect(component.state("occupation")).toBe("Pilot"); |
|
35 |
}); |
|
36 |
|
|
37 |
test("Changes state when footage is changed", async () => { |
|
38 |
component.find("#adoption-form-footage").simulate("change", "100"); |
|
39 |
expect(component.state("squareFootageOfHome")).toBe(100); |
|
40 |
}); |
|
41 |
|
|
42 |
test("Calls adoptionService.create when form is submitted", () => { |
|
43 |
adoptionService.applyForAdoption = jest.fn(); |
|
44 |
|
|
45 |
component.setState({ |
|
46 |
username: "John", |
|
47 |
email: "test@example.com", |
|
48 |
occupation: "Pilot", |
|
49 |
residency: Residency.APARTMENT, |
|
50 |
squareFootageOfHome: 0, |
|
51 |
kidsUnder16: true, |
|
52 |
ownOtherAnimals: false, |
|
53 |
}); |
|
54 |
|
|
55 |
simulateSubmit(component.find("Form")); |
|
56 |
|
|
57 |
expect(adoptionService.applyForAdoption).toHaveBeenCalledWith({ |
|
58 |
animalId: "fake-animal-id", |
|
59 |
username: "John", |
|
60 |
email: "test@example.com", |
|
61 |
occupation: "Pilot", |
|
62 |
residency: "APARTMENT", |
|
63 |
squareFootageOfHome: 0, |
|
64 |
kidsUnder16: true, |
|
65 |
ownOtherAnimals: false, |
|
66 |
}); |
|
67 |
}); |
|
68 |
|
|
69 |
test("Changes state to show success alert when form is submitted", async () => { |
|
70 |
component.setState({ |
|
71 |
username: "John", |
|
72 |
email: "test@example.com", |
|
73 |
occupation: "Pilot", |
|
74 |
residency: Residency.APARTMENT, |
|
75 |
squareFootageOfHome: 0, |
|
76 |
kidsUnder16: true, |
|
77 |
ownOtherAnimals: false, |
|
78 |
}); |
|
79 |
|
|
80 |
simulateSubmit(component.find("Form")); |
|
81 |
|
|
82 |
// Wait until all promises all resolved |
|
83 |
await Promise.resolve(); |
|
84 |
|
d4efcf
|
85 |
expect(component.state("showAdoptSuccessAlert")).toBe(true); |
bae328
|
86 |
}); |
JR |
87 |
|
|
88 |
test("Does not call AdoptionService.create when empty form is submitted", () => { |
|
89 |
adoptionService.applyForAdoption = jest.fn(); |
|
90 |
|
|
91 |
simulateSubmit(component.find("Form")); |
|
92 |
|
|
93 |
expect(adoptionService.applyForAdoption).not.toHaveBeenCalled(); |
|
94 |
}); |
|
95 |
|
|
96 |
test("Changes state to show invalid form alert when empty form is submitted", () => { |
|
97 |
simulateSubmit(component.find("Form")); |
|
98 |
|
|
99 |
expect(component.state("showInvalidFormAlert")).toBe(true); |
|
100 |
}); |
|
101 |
|
|
102 |
}); |
|
103 |
|
|
104 |
|
|
105 |
function simulateSubmit(formComponent: ShallowWrapper) { |
|
106 |
// we need to create a fake browser event to simulate a submit |
|
107 |
const event = { preventDefault: () => { } }; |
|
108 |
formComponent.simulate("submit", event); |
|
109 |
} |