diff --git a/packages/client/src/team/TeamConnector.test.tsx b/packages/client/src/team/TeamConnector.test.tsx index aacf13f..01920b5 100644 --- a/packages/client/src/team/TeamConnector.test.tsx +++ b/packages/client/src/team/TeamConnector.test.tsx @@ -1,16 +1,36 @@ import * as Feature from './' // export ReactDOM from 'react-dom' -// import T from 'react-dom/test-utils' -import {HTTPClientMock, TestUtils/*, getError*/} from '../test-utils' +import T from 'react-dom/test-utils' +import {HTTPClientMock, TestUtils, getError} from '../test-utils' import {IAPIDef, ITeam, IUserInTeam} from '@rondo/common' import React from 'react' +import {MemoryRouter} from 'react-router-dom' const test = new TestUtils() describe('TeamConnector', () => { - const http = new HTTPClientMock() - const teamActions = new Feature.TeamActions(http) + let teamActions!: Feature.TeamActions + let http: HTTPClientMock + beforeEach(() => { + http = new HTTPClientMock() + + http.mockAdd({ + method: 'get', + url: '/my/teams', + }, teams) + http.mockAdd({ + method: 'get', + url: '/teams/:teamId/users', + params: { + teamId: 123, + }, + }, users) + + teamActions = new Feature.TeamActions(http) + }) + + const historyEntries = ['/teams'] const createTestProvider = () => test.withProvider({ reducers: {Team: Feature.Team}, @@ -20,7 +40,11 @@ describe('TeamConnector', () => { new Feature .TeamConnector(teamActions) .connect(select)) - .withJSX((Component, props) => ) + .withJSX((Component, props) => + + + , + ) const teams: ITeam[] = [{id: 100, name: 'my-team', userId: 1}] @@ -33,20 +57,52 @@ describe('TeamConnector', () => { }] it('it fetches user teams on render', async () => { - http.mockAdd({ - method: 'get', - url: '/my/teams', - }, teams) - http.mockAdd({ - method: 'get', - url: '/teams/:teamId/users', - params: { - teamId: 123, - }, - }, users) - const {node} = createTestProvider().render({editTeamId123: 123}) + const {node} = createTestProvider().render({}) await http.wait() expect(node.innerHTML).toContain('my-team') }) + describe('add team', () => { + it('sends a POST request to POST /teams', async () => { + const newTeam: Partial = {id: 101, name: 'new-team'} + http.mockAdd({ + method: 'post', + url: '/teams', + data: {name: 'new-team'}, + }, newTeam) + const {render, store} = createTestProvider() + const {node} = render({}) + const addTeamForm = node.querySelector('.team-add') as HTMLFormElement + const nameInput = addTeamForm + .querySelector('input') as HTMLInputElement + T.Simulate.change(nameInput, {target: {value: newTeam.name}} as any) + T.Simulate.submit(addTeamForm) + await http.wait() + expect(nameInput.value).toEqual('') + const {Team} = store.getState() + expect(Team.teamIds).toEqual([100, 101]) + expect(Team.teamsById[101]).toEqual(newTeam) + }) + + it('displays an error', async () => { + const error = {error: 'An error'} + http.mockAdd({ + method: 'post', + url: '/teams', + data: {name: 'test'}, + }, error, 400) + const {render} = createTestProvider() + const {node} = render({}) + const addTeamForm = node.querySelector('.team-add') as HTMLFormElement + const nameInput = addTeamForm + .querySelector('input') as HTMLInputElement + T.Simulate.change(nameInput, {target: {value: 'test'}} as any) + T.Simulate.submit(addTeamForm) + const error2 = await getError(http.wait()) + expect(error2.message).toMatch(/HTTP Status: 400/) + expect(nameInput.value).toEqual('test') + expect(addTeamForm.innerHTML).toMatch(/HTTP Status: 400/) + }) + }) + }) diff --git a/packages/client/src/team/TeamEditor.tsx b/packages/client/src/team/TeamEditor.tsx index 931b490..3da37d9 100644 --- a/packages/client/src/team/TeamEditor.tsx +++ b/packages/client/src/team/TeamEditor.tsx @@ -1,5 +1,5 @@ import React from 'react' -import {Button, Control, Heading, Input} from 'bloomer' +import {Button, Control, Heading, Help, Input} from 'bloomer' import {ITeam} from '@rondo/common' import {TeamActions} from './TeamActions' import {FaPlusSquare, FaCheck, FaEdit} from 'react-icons/fa' @@ -14,6 +14,8 @@ export type ITeamEditorProps = { } export interface ITeamEditorState { + // TODO use redux state for errors! + error: string name: string } @@ -22,6 +24,7 @@ extends React.PureComponent { constructor(props: ITeamEditorProps) { super(props) this.state = { + error: '', name: props.type === 'update' ? this.getName(props.team) : '', } } @@ -45,15 +48,22 @@ extends React.PureComponent { handleSubmit = async (event: React.FormEvent) => { event.preventDefault() const {name} = this.state - if (this.props.type === 'update') { - const {team} = this.props - await this.props.onUpdateTeam({id: team.id, name}) - } else { - await this.props.onAddTeam({name}) + try { + if (this.props.type === 'update') { + const {team} = this.props + await this.props.onUpdateTeam({id: team.id, name}).payload + } else { + await this.props.onAddTeam({name}).payload + } + } catch (err) { + this.setState({error: err.message}) + return } - this.setState({name: ''}) + this.setState({error: '', name: ''}) } render() { + const {error} = this.state + return (
{ {this.props.type === 'update' ? : } + {error && ( + {error} + )}