Skip to content

Commit

Permalink
Added and passed test for application reducer
Browse files Browse the repository at this point in the history
Removed unused code
  • Loading branch information
Michael-Xie committed Jan 28, 2020
1 parent 68b89b6 commit 0d6b9d2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@
"watchPathIgnorePatterns": [
"<rootDir>/node_modules/",
"<rootDir>/.git/"
],
"collectCoverageFrom": [
"src/**/*.js",
"!src/index.js"
]

},
"proxy": "http://localhost:8001"
}
40 changes: 18 additions & 22 deletions src/hooks/useApplicationData.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useReducer } from "react";
import axios from "axios";

import {reducer, SET_INTERVIEW, SET_DAY, SET_APPLICATION_DATA} from "reducers/application";
import { reducer, SET_INTERVIEW, SET_DAY, SET_APPLICATION_DATA } from "reducers/application";

export default function useApplicationData() {

Expand All @@ -24,21 +24,22 @@ export default function useApplicationData() {
console.log("appointments", appointments);
console.log("interviewers", interviewers);
dispatch({
type: SET_APPLICATION_DATA,
type: SET_APPLICATION_DATA,
value: {
days: days.data,
appointments: appointments.data,
days: days.data,
appointments: appointments.data,
interviewers: interviewers.data
}});
}
});
})
}, []); // run once with the [], else none would run each render

// useEffect(() => {
// const sock = new WebSocket(process.env.REACT_APP_WEBSOCKET_URL);
// console.log(sock);
// }, [])
const setDay = function(day) {
dispatch({type: SET_DAY, value: day});
const setDay = function (day) {
dispatch({ type: SET_DAY, value: day });
}

function bookInterview(id, interview) {
Expand All @@ -51,13 +52,11 @@ export default function useApplicationData() {
[id]: appointment
};
console.log("bookInterview", interview, id);
if (interview && id) {
return axios.put(`/api/appointments/${id}`, { interview })
.then((res) => {
console.log("put request for interview", res);
dispatch({type: SET_INTERVIEW, value: {appointmentId: id, appointments: appointments}})
});
}
return axios.put(`/api/appointments/${id}`, { interview })
.then((res) => {
console.log("put request for interview", res);
dispatch({ type: SET_INTERVIEW, value: { appointmentId: id, appointments: appointments } })
});
}

function cancelInterview(id) {
Expand All @@ -71,14 +70,11 @@ export default function useApplicationData() {
[id]: appointment
};
console.log("cancelInterview", appointments, id);
if (id) {
return axios.delete(`/api/appointments/${id}`)
.then((res) => {
console.log("cancelInterview", res);
dispatch({type: SET_INTERVIEW, value: {appointmentId: id, appointments: appointments}})
})
}

return axios.delete(`/api/appointments/${id}`)
.then((res) => {
console.log("cancelInterview", res);
dispatch({ type: SET_INTERVIEW, value: { appointmentId: id, appointments: appointments } })
})
}

return { state, setDay, bookInterview, cancelInterview };
Expand Down
4 changes: 3 additions & 1 deletion src/reducers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ const reducer = (prevState, action) => {
if (handler) {
return handler(prevState, action);
}
return prevState;
throw new Error(
`Tried to reduce with unsupported action type: ${action.type}`
);
};

export {SET_APPLICATION_DATA, SET_DAY, SET_INTERVIEW, reducer};
8 changes: 8 additions & 0 deletions src/reducers/application.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {reducer} from "./application";
describe("Application Reducer", () => {
it("throws an error with an unsupported type", () => {
expect(() => reducer({}, { type: null })).toThrowError(
/tried to reduce with unsupported action type/i
);
});
});

0 comments on commit 0d6b9d2

Please sign in to comment.