Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeg90 committed Apr 7, 2020
1 parent e5f6529 commit 5d31d41
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"browser": true,
"commonjs": true,
"es6": true,
"node": true
"node": true,
"jest": true
},
"plugins": ["react"],
"extends": ["eslint:recommended", "plugin:react/recommended"],
Expand Down
8 changes: 5 additions & 3 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ export default class App extends React.Component {
birthday={this.state.birthday}
gender={this.state.gender}
seeking={this.state.seeking}
interests={this.state.interests}
symptoms={this.state.symptoms}
about={this.state.about}
interests={
this.state.interests || []
}
symptoms={this.state.symptoms || []}
about={this.state.about || ""}
setProfile={userData =>
this.setProfile(userData)
}
Expand Down
2 changes: 1 addition & 1 deletion src/other-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class OtherProfile extends React.Component {
{
first: first,
last: last,
imgUrl: imgUrl,
imgUrl: imgUrl || "/default.png",
birthday: birthday,
gender: gender,
seeking: seeking,
Expand Down
10 changes: 6 additions & 4 deletions src/profile-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export default class ProfileEditor extends React.Component {
birthday: this.props.birthday,
gender: this.props.gender,
seeking: this.props.seeking,
interests: this.props.interests || [],
symptoms: this.props.symptoms || [],
about: this.props.about || ""
interests: this.props.interests,
symptoms: this.props.symptoms,
about: this.props.about
});
console.log("current state: ", this.state);
}
Expand Down Expand Up @@ -226,7 +226,9 @@ export default class ProfileEditor extends React.Component {
onChange={e => this.handleChange(e)}
/>
<br />
<button onClick={() => this.updateProfile()}>Save</button>
<button id="save" onClick={() => this.updateProfile()}>
Save
</button>
<button
onClick={() =>
this.setState({ beingEdited: false, error: false })
Expand Down
87 changes: 87 additions & 0 deletions src/tests/profile-editor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React from "react";
import ProfileEditor from "../profile-editor";
import { render, fireEvent, waitForElement } from "@testing-library/react";
import axios from "../axios";

test("When no profile props passed to it, an 'Add' button is rendered", () => {
const { container } = render(<ProfileEditor />);

expect(container.querySelector("button").innerHTML).toBe("Add profile");
});

test("When profile has been updated, an 'Edit' button is rendered", () => {
const { container } = render(
<ProfileEditor
profileUpdated={true}
birthday="1990-06-29"
gender="male"
seeking="females"
interests={[]}
symptoms={[]}
about=""
/>
);

expect(container.querySelector("button").innerHTML).toBe("Edit profile");
});

test("Clicking 'Add' button causes editProfile div and save button to be rendered", () => {
const myMockOnClick = jest.fn();
const { container } = render(
<ProfileEditor interests={[]} symptoms={[]} onClick={myMockOnClick} />
);

fireEvent.click(container.querySelector("button"));

expect(container.querySelector(".editProfile").innerHTML).toContain(
'<button id="save">Save</button>'
);
});

jest.mock("../axios.js");

test("Clicking the 'Save' button causes an ajax request", () => {
axios.post.mockResolvedValue({
data: {
success: true
}
});

const { container } = render(
<ProfileEditor interests={[]} symptoms={[]} />
);

fireEvent.click(container.querySelector("button"));

fireEvent.click(container.querySelector("#save"));

axios.post().then(({ data }) => {
expect(data.success).toBe(true);
});
});

test("When the mock request is successful, the function that was passed as a prop to the component gets called", async () => {
const myMockSetProfile = jest.fn();

axios.post.mockResolvedValue({
data: {
success: true
}
});

const { container } = render(
<ProfileEditor
interests={[]}
symptoms={[]}
setProfile={() => myMockSetProfile()}
/>
);

fireEvent.click(container.querySelector("button"));

fireEvent.click(container.querySelector("#save"));

await waitForElement(() => container.querySelector("div"));

expect(myMockSetProfile.mock.calls.length).toBe(1);
});

0 comments on commit 5d31d41

Please sign in to comment.