Skip to content

Commit

Permalink
added lambda proxy setting for create endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
troyblank committed May 17, 2020
1 parent 56a160f commit ddac227
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/components/createForm/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export const createHalfsie = (formData) => (

throw new Error('There was an issue saving your Halfise.');
})
.then((response) => {
const { balance } = JSON.parse(response.body);
.then((responseBody) => {
const { balance } = responseBody;
const { userName: user } = authStore;

dispatch(addLog({ user, ...log }));
Expand Down
33 changes: 23 additions & 10 deletions src/components/createForm/createForm.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import React from 'react';
import { assert } from 'chai';
import { shallow } from 'enzyme';
Expand All @@ -10,18 +9,25 @@ import CreateForm from './createForm';

describe('Create Form', () => {
const chance = new Chance();
const logStore = { log: [] };

it('should render', () => {
const wrapper = shallow(<CreateForm createStore={{}} />);
const wrapper = shallow(<CreateForm createStore={{}} logStore={logStore} />);

assert.equal(wrapper.find('h1').text(), 'Create a new Halfsie');
});

it('should not render without a log', () => {
const wrapper = shallow(<CreateForm createStore={{}} logStore={{}} />);

assert.equal(wrapper.html(), null);
});

it('should be able to sign in a user', () => {
const createStore = { pending: false };
const dispatch = sinon.spy();
const preventDefault = sinon.spy();
const wrapper = shallow(<CreateForm createStore={createStore} dispatch={dispatch} />);
const wrapper = shallow(<CreateForm createStore={createStore} logStore={logStore} dispatch={dispatch} />);
const form = wrapper.find('form');

form.simulate('submit', { preventDefault });
Expand All @@ -34,7 +40,7 @@ describe('Create Form', () => {
const createStore = { pending: true };
const dispatch = sinon.spy();
const preventDefault = sinon.spy();
const wrapper = shallow(<CreateForm createStore={createStore} dispatch={dispatch} />);
const wrapper = shallow(<CreateForm createStore={createStore} logStore={logStore} dispatch={dispatch} />);
const form = wrapper.find('form');

form.simulate('submit', { preventDefault });
Expand All @@ -46,16 +52,16 @@ describe('Create Form', () => {
it('should show an error message if the create form does not work', () => {
const errorMessage = chance.word();
const createStore = { pending: true, errorMessage };
const wrapper = shallow(<CreateForm createStore={createStore} />);
const wrapper = shallow(<CreateForm createStore={createStore} logStore={logStore} />);

assert.equal(wrapper.find('.alert__error strong').text(), errorMessage);
});

it('should keep amount in state', () => {
const value = chance.natural();
const dispatch = sinon.spy();
const wrapper = shallow(<CreateForm createStore={{}} dispatch={dispatch} />);
const { getByLabelText } = render(<CreateForm createStore={{}} dispatch={dispatch} />);
const wrapper = shallow(<CreateForm createStore={{}} logStore={logStore} dispatch={dispatch} />);
const { getByLabelText } = render(<CreateForm createStore={{}} logStore={logStore} dispatch={dispatch} />);
const amountInputDummy = wrapper.find('#amount');
const amountInput = getByLabelText('Amount');

Expand All @@ -69,8 +75,8 @@ describe('Create Form', () => {
it('should keep description in state', () => {
const value = chance.word();
const dispatch = sinon.spy();
const wrapper = shallow(<CreateForm createStore={{}} dispatch={dispatch} />);
const { getByLabelText } = render(<CreateForm createStore={{}} dispatch={dispatch} />);
const wrapper = shallow(<CreateForm createStore={{}} logStore={logStore} dispatch={dispatch} />);
const { getByLabelText } = render(<CreateForm createStore={{}} logStore={logStore} dispatch={dispatch} />);
const descriptionTextAreaDummy = wrapper.find('#description');
const descriptionTextArea = getByLabelText('Description');

Expand All @@ -88,12 +94,19 @@ describe('Create Form', () => {
const mockRouter = { push };
render(
<RouterContext.Provider value={mockRouter}>
<CreateForm createStore={createStore} dispatch={dispatch} />
<CreateForm createStore={createStore} logStore={logStore} dispatch={dispatch} />
</RouterContext.Provider>
);

assert.isTrue(push.calledOnce);
assert.isTrue(push.calledWith('/'));
assert.isTrue(dispatch.calledOnce);
});

it('should fech log if there is no log to be found', () => {
const dispatch = sinon.spy();
render(<CreateForm createStore={{}} logStore={{}} dispatch={dispatch} />);

assert.isTrue(dispatch.calledTwice);
});
});
10 changes: 9 additions & 1 deletion src/components/createForm/createForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import { useRouter } from 'next/router';
import Link from 'next/link';
import classnames from 'classnames';
import { createHalfsie, resetCreateForm } from './actions';
import { getLog } from '../log/actions';

export default function CreateFormComponent({ createStore, dispatch }) {
export default function CreateFormComponent({ createStore, logStore, dispatch }) {
const { pending, errorMessage } = createStore;
const { log } = logStore;
const router = useRouter();

const [amount, setAmount] = useState< string >('');
const [description, setDescription] = useState< string >('');

useEffect(() => {
if (!log) dispatch(getLog());
}, []);

useEffect(() => {
if (createStore.needsRedirect) router.push('/');

Expand All @@ -23,6 +29,8 @@ export default function CreateFormComponent({ createStore, dispatch }) {
e.preventDefault();
};

if (!log) return null;

return (
<section className={'page-wrap'}>
<h1>Create a new Halfsie</h1>
Expand Down
13 changes: 11 additions & 2 deletions src/lambdas/halfsie_createHalfsie/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.handler = async (newHalfsieData) => {
let newBalance;
let statusCode = 200;
let errorMessage;
const { accessToken, log } = newHalfsieData;
const { accessToken, log } = JSON.parse(newHalfsieData.body);
const { user, errorMessage: userErrorMessage } = await getUser(accessToken);

if (user) {
Expand All @@ -35,5 +35,14 @@ exports.handler = async (newHalfsieData) => {
}

const body = JSON.stringify({ newBalance, errorMessage });
return { statusCode, body };
const response = {
statusCode,
headers: {
'Access-Control-Allow-Origin': 'https://halfsies.troyblank.com',
'Content-Type': 'application/json'
},
body
};

return response;
};

0 comments on commit ddac227

Please sign in to comment.