Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

combined signup and profile #147

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
29 changes: 27 additions & 2 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ class App extends Component {
});
}

async editProfile(user) {
return AuthService.editProfile(user).then((response) => {
if (response.message) {
// When the API returns `message`,
// that means the signup has failed
toast.error(response.message);
return -1;
} else {
this.setUsername(response.username);
this.setToken(response.token);
this.setProfile(response.profile);

// We only need to import toast in other components
// if we want to make a notification there.
toast.success("🚀 Successfully edited your profile!");

return 0;
}
});
}


async login(user) {
return AuthService.login(user).then((response) => {
if (response.message) {
Expand Down Expand Up @@ -145,6 +167,8 @@ class App extends Component {
});
}



isAdmin() {
return this.state.employeeOf.length !== 0;
}
Expand Down Expand Up @@ -207,10 +231,11 @@ class App extends Component {
<FoodSearchView />
</Route>
<Route path="/profile">
<ProfileView
{/* <ProfileView
profile={this.state.profile}
setProfile={this.setProfile.bind(this)}
/>
/> */}
<SignupView profile={this.state.profile} editProfile={this.editProfile.bind(this)} setProfile={this.setProfile.bind(this)}/>
</Route>
<Route path="/help">
<HelpView />
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/__tests__/Signup.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import React from "react";
import Adapter from "enzyme-adapter-react-16";
import { shallow } from "enzyme";
import { mount } from "enzyme";
import { Provider } from "react-redux";


import SignupView from "../components/Authentication/SignupView";
import "../setupTests";
import store from "../store";


describe("Signup tests", () => {
const wrapper = shallow(<SignupView />);
const wrapper = mount(
<Provider store={store}>
<SignupView />
</Provider>);

it("should have a submit button", () => {
// There should be one button in LoginView
Expand Down
53 changes: 53 additions & 0 deletions frontend/src/__tests__/Signup1.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from "react";
import Adapter from "enzyme-adapter-react-16";
import { mount } from "enzyme";
import { Provider } from "react-redux";

import SignupView from "../components/Authentication/SignupView";
import "../setupTests";
import store from "../store";

describe("Signup Profile mode tests", () => {
const mockProfile = {
username: "sean3",
firstName: "sean",
lastName: "cunningham",
email: "sjcunningham@wisc.edu",
phone: "sean's phone",
address: "street",
city: "city",
state: "wi",
zipcode: 53706,
type: "user",
};

const wrapper = mount(
<Provider store={store}>
<SignupView profile={mockProfile} />
</Provider>
);

it("should have two buttons", () => {
// There should be one button in LoginView
expect(wrapper.find("Button")).toHaveLength(2);
});

it("should have 10 inputs for profile fields", () => {
expect(wrapper.find("FormControl")).toHaveLength(10);
});

it("should have 10 states for fields in profile", () => {
const getProfileView = wrapper.find("SignupView");
expect(getProfileView.state("username")).toEqual("sean3");
expect(getProfileView.state("password")).toEqual("");
expect(getProfileView.state("phone")).toEqual("sean's phone");
expect(getProfileView.state("address")).toEqual("street");
expect(getProfileView.state("city")).toEqual("city");
expect(getProfileView.state("state")).toEqual("wi");
expect(getProfileView.state("zipcode")).toEqual(53706);
expect(getProfileView.state("email")).toEqual("sjcunningham@wisc.edu");
expect(getProfileView.state("first_name")).toEqual("sean");
expect(getProfileView.state("last_name")).toEqual("cunningham");
});
});

133 changes: 133 additions & 0 deletions frontend/src/__tests__/Signup2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import React from "react";
import Adapter from "enzyme-adapter-react-16";
import { mount } from "enzyme";
import { Provider } from "react-redux";


import SignupView from "../components/Authentication/SignupView";
import "../setupTests";
import store from "../store";


describe("Login tests", () => {
const wrapper = mount(<Provider store={store}><SignupView /></Provider>);

it("should have a login button", () => {
// There should be one button in LoginView
expect(wrapper.find("Button")).toHaveLength(1);

// Button should say "Login"
expect(wrapper.find("Button").text()).toEqual("Submit");
});

it("should have 10 inputs for a user's profile", () => {
expect(wrapper.find("FormControl")).toHaveLength(10);
});


it("should have 10 blank states for sign up profile", () => {
expect(wrapper.state("username")).toEqual("");
expect(wrapper.state("password")).toEqual("");
expect(wrapper.state("address")).toEqual("");
expect(wrapper.state("city")).toEqual("");
expect(wrapper.state("state")).toEqual("");
expect(wrapper.state("email")).toEqual("");
expect(wrapper.state("zipcode")).toEqual("");
expect(wrapper.state("firstName")).toEqual("");
expect(wrapper.state("lastName")).toEqual("");
expect(wrapper.state("phone")).toEqual("");

});

it("should not redirect to home if credentials are empty", async () => {
const getLoginButton = wrapper.find("Button");
await getLoginButton.simulate("click", {
preventDefault: jest.fn(),
});

expect(wrapper.state("toHomeView")).toEqual(false);
});

it("should update states when all fields are typed", async () => {
const usernameInput = wrapper.find("FormControl#username_input");
const passwordInput = wrapper.find("FormControl#password_input");
const firstNameInput = wrapper.find("FormControl#firstName_input");
const lastNameInput = wrapper.find("FormControl#lastName_input");
const emailInput = wrapper.find("FormControl#email_input");
const phoneInput = wrapper.find("FormControl#phone_input");
const addressInput = wrapper.find("FormControl#address_input");
const cityInput = wrapper.find("FormControl#city_input");
const stateInput = wrapper.find("FormControl#state_input");
const zipcodeInput = wrapper.find("FormControl#zipcode_input");


await usernameInput.simulate("change", { target: { value: "sean3" } });
await passwordInput.simulate("change", { target: { value: "abc" } });
await firstNameInput.simulate("change", { target: { value: "sean" } });
await lastNameInput.simulate("change", { target: { value: "cunningham" } });
await emailInput.simulate("change", { target: { value: "sjcunningham@wisc.edu" } });
await phoneInput.simulate("change", { target: { value: "sean's phone" } });
await addressInput.simulate("change", { target: { value: "street" } });
await cityInput.simulate("change", { target: { value: "city" } });
await stateInput.simulate("change", { target: { value: "wi" } });
await zipcodeInput.simulate("change", { target: { value: "53706" } });




expect(wrapper.state("username")).toEqual("sean3");
expect(wrapper.state("password")).toEqual("abc");
expect(wrapper.state("firstName")).toEqual("sean");
expect(wrapper.state("lastName")).toEqual("cunningham");
expect(wrapper.state("phone")).toEqual("sean's phone");
expect(wrapper.state("address")).toEqual("street");
expect(wrapper.state("city")).toEqual("city");
expect(wrapper.state("state")).toEqual("wi");
expect(wrapper.state("zipcode")).toEqual("53706");
expect(wrapper.state("email")).toEqual("sjcunningham@wisc.edu");


});

it("should redirect to home if toHomeView is set to true", async () => {
wrapper.setState({ toHomeView: true });

expect(wrapper.find("FormControl#username_input")).toHaveLength(0);
expect(wrapper.find("FormControl#password_input")).toHaveLength(0);
expect(wrapper.find("FormControl#firstName_input")).toHaveLength(0);
expect(wrapper.find("FormControl#lastName_input")).toHaveLength(0);
expect(wrapper.find("FormControl#email_input")).toHaveLength(0);
expect(wrapper.find("FormControl#phone_input")).toHaveLength(0);
expect(wrapper.find("FormControl#address_input")).toHaveLength(0);
expect(wrapper.find("FormControl#city_input")).toHaveLength(0);
expect(wrapper.find("FormControl#state_input")).toHaveLength(0);
expect(wrapper.find("FormControl#zipcode_input")).toHaveLength(0);

});

it("should redirect to home on login success", async () => {
wrapper.setState({ toHomeView: false });
wrapper.setProps({
signup: () => {
return 0; // mocks Login Success
},
});
const getLoginButton = wrapper.find("Button");
await getLoginButton.simulate("click", {
preventDefault: jest.fn(),
});

expect(wrapper.state("toHomeView")).toEqual(true);
expect(wrapper.find("FormControl#username_input")).toHaveLength(0);
expect(wrapper.find("FormControl#password_input")).toHaveLength(0);
expect(wrapper.find("FormControl#firstName_input")).toHaveLength(0);
expect(wrapper.find("FormControl#lastName_input")).toHaveLength(0);
expect(wrapper.find("FormControl#email_input")).toHaveLength(0);
expect(wrapper.find("FormControl#phone_input")).toHaveLength(0);
expect(wrapper.find("FormControl#address_input")).toHaveLength(0);
expect(wrapper.find("FormControl#city_input")).toHaveLength(0);
expect(wrapper.find("FormControl#state_input")).toHaveLength(0);
expect(wrapper.find("FormControl#zipcode_input")).toHaveLength(0);
});
});

Loading