This repository has been archived by the owner on Oct 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add capability to add a new card (#209)
- Loading branch information
Showing
12 changed files
with
743 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/components/Board/components/Column/components/CardAdder/components/CardForm/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React, { useRef } from 'react' | ||
import styled from 'styled-components' | ||
import { when } from '@services/utils' | ||
import CardSkeleton from '@components/Board/components/CardSkeleton' | ||
|
||
const DefaultCard = styled(CardSkeleton)` | ||
border-radius: 3px; | ||
background-color: #fff; | ||
padding: 10px; | ||
margin-bottom: 7px; | ||
input { | ||
border: 0px; | ||
font-family: inherit; | ||
font-size: inherit; | ||
} | ||
` | ||
|
||
const CardTitle = styled.input` | ||
font-weight: bold; | ||
border-bottom: 1px solid #eee; | ||
padding-bottom: 5px; | ||
font-weight: bold; | ||
display: flex; | ||
justify-content: space-between; | ||
width: 100%; | ||
padding: 0px; | ||
` | ||
|
||
const CardDescription = styled.input` | ||
input { | ||
width: 100%; | ||
} | ||
margin-top: 10px; | ||
` | ||
const StyledFormButtons = styled.div` | ||
display: flex; | ||
justify-content: space-between; | ||
margin-top: 5px; | ||
` | ||
|
||
const StyledButton = styled.button` | ||
background-color: #eee; | ||
border: none; | ||
padding: 5px; | ||
width: 45%; | ||
margin-top: 5px; | ||
border-radius: 3px; | ||
&:hover { | ||
transition: 0.3s; | ||
cursor: pointer; | ||
background-color: #ccc; | ||
} | ||
` | ||
|
||
function CardForm({ onConfirm, onCancel }) { | ||
const inputCardTitle = useRef() | ||
const inputCardDescription = useRef() | ||
|
||
function addCard(event) { | ||
event.preventDefault() | ||
when(inputCardTitle.current.value)(value => { | ||
onConfirm({ title: value, description: inputCardDescription.current.value }) | ||
}) | ||
} | ||
|
||
return ( | ||
<DefaultCard> | ||
<form onSubmit={addCard}> | ||
<CardTitle name='title' autoFocus defaultValue='Title' ref={inputCardTitle} /> | ||
<CardDescription name='description' defaultValue='Description' ref={inputCardDescription} /> | ||
<StyledFormButtons> | ||
<StyledButton type='submit'>Add</StyledButton> | ||
<StyledButton type='button' onClick={onCancel}> | ||
Cancel | ||
</StyledButton> | ||
</StyledFormButtons> | ||
</form> | ||
</DefaultCard> | ||
) | ||
} | ||
|
||
export default CardForm |
78 changes: 78 additions & 0 deletions
78
...components/Board/components/Column/components/CardAdder/components/CardForm/index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React from 'react' | ||
import { render, fireEvent } from '@testing-library/react' | ||
import CardForm from './' | ||
|
||
describe('<CardForm />', () => { | ||
let subject, onConfirm, onCancel | ||
|
||
function mount() { | ||
onConfirm = jest.fn() | ||
onCancel = jest.fn() | ||
|
||
subject = render(<CardForm onConfirm={onConfirm} onCancel={onCancel} />) | ||
} | ||
|
||
beforeEach(mount) | ||
afterEach(() => { | ||
subject = onConfirm = onCancel = undefined | ||
}) | ||
|
||
it('renders the card inputs', () => { | ||
expect(subject.container.querySelector('input[name="title"]')).toBeInTheDocument() | ||
expect(subject.container.querySelector('input[name="description"]')).toBeInTheDocument() | ||
}) | ||
|
||
it('focuses on the title input', () => { | ||
expect(subject.container.querySelector('input[name="title"]')).toHaveFocus() | ||
}) | ||
|
||
describe('when the user clicks confirm the input', () => { | ||
describe('when the user has informed a valid card', () => { | ||
beforeEach(() => { | ||
fireEvent.change(subject.container.querySelector('input[name="title"]'), { target: { value: 'Card title' } }) | ||
fireEvent.change(subject.container.querySelector('input[name="description"]'), { | ||
target: { value: 'Description' } | ||
}) | ||
fireEvent.click(subject.queryByText('Add')) | ||
}) | ||
|
||
it('calls the onConfirm prop passing the values', () => { | ||
expect(onConfirm).toHaveBeenCalledTimes(1) | ||
expect(onConfirm).toHaveBeenCalledWith({ title: 'Card title', description: 'Description' }) | ||
}) | ||
|
||
it('does not call the onCancel prop', () => { | ||
expect(onCancel).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
describe('when the user has not typed a card title', () => { | ||
beforeEach(() => { | ||
fireEvent.change(subject.container.querySelector('input[name="title"]'), { target: { value: '' } }) | ||
fireEvent.click(subject.queryByText('Add')) | ||
}) | ||
|
||
it('does not call the onConfirm prop', () => { | ||
expect(onConfirm).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('does not call the onCancel prop', () => { | ||
expect(onCancel).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when the user cancels the input', () => { | ||
beforeEach(() => { | ||
fireEvent.click(subject.queryByText('Cancel')) | ||
}) | ||
|
||
it('calls the onCancel prop', () => { | ||
expect(onCancel).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('does not call the onConfirm prop', () => { | ||
expect(onConfirm).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
38 changes: 38 additions & 0 deletions
38
src/components/Board/components/Column/components/CardAdder/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React, { useState } from 'react' | ||
import styled from 'styled-components' | ||
import CardForm from './components/CardForm' | ||
|
||
const AddCardButton = styled.button` | ||
width: 100%; | ||
margin-top: 5px; | ||
background-color: transparent; | ||
cursor: pointer; | ||
border: 1px solid #ccc; | ||
transition: 0.3s; | ||
:hover { | ||
background-color: #ccc; | ||
} | ||
border-radius: 3px; | ||
font-size: 20px; | ||
margin-bottom: 10px; | ||
font-weight: bold; | ||
` | ||
|
||
export default function CardAdder({ column, onConfirm }) { | ||
function confirmCard(card) { | ||
onConfirm(column, card) | ||
setAddingCard(false) | ||
} | ||
|
||
const [addingCard, setAddingCard] = useState(false) | ||
|
||
return ( | ||
<> | ||
{addingCard ? ( | ||
<CardForm onConfirm={confirmCard} onCancel={() => setAddingCard(false)} /> | ||
) : ( | ||
<AddCardButton onClick={() => setAddingCard(!addingCard)}>+</AddCardButton> | ||
)} | ||
</> | ||
) | ||
} |
78 changes: 78 additions & 0 deletions
78
src/components/Board/components/Column/components/CardAdder/index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React from 'react' | ||
import { render, fireEvent } from '@testing-library/react' | ||
import CardAdder from './' | ||
|
||
describe('<CardAdder />', () => { | ||
let subject, onConfirm | ||
const column = { id: 1 } | ||
function mount() { | ||
onConfirm = jest.fn() | ||
|
||
subject = render(<CardAdder column={column} onConfirm={onConfirm} />) | ||
} | ||
|
||
beforeEach(mount) | ||
afterEach(() => { | ||
subject = onConfirm = undefined | ||
}) | ||
|
||
it('renders the button to add a new card', () => { | ||
expect(subject.queryByText('+')).toBeInTheDocument() | ||
}) | ||
|
||
describe('when the user clicks to add a new card', () => { | ||
beforeEach(() => fireEvent.click(subject.queryByText('+'))) | ||
|
||
it('hides the card placeholder', () => { | ||
expect(subject.queryByText('+')).not.toBeInTheDocument() | ||
}) | ||
|
||
it('renders the card inputs', () => { | ||
expect(subject.container.querySelector('input[name="title"]')).toBeInTheDocument() | ||
expect(subject.container.querySelector('input[name="description"]')).toBeInTheDocument() | ||
}) | ||
|
||
describe('when the user confirms the new card', () => { | ||
beforeEach(() => { | ||
fireEvent.change(subject.container.querySelector('input[name="title"]'), { | ||
target: { value: 'Card Added by user' } | ||
}) | ||
fireEvent.change(subject.container.querySelector('input[name="description"]'), { | ||
target: { value: 'Description' } | ||
}) | ||
fireEvent.click(subject.queryByText('Add')) | ||
}) | ||
|
||
it('calls the "onConfirm" prop passing the new card and the column', () => { | ||
expect(onConfirm).toHaveBeenCalledTimes(1) | ||
expect(onConfirm).toHaveBeenCalledWith(column, { title: 'Card Added by user', description: 'Description' }) | ||
}) | ||
|
||
it('hides the input', () => { | ||
expect(subject.container.querySelector('input')).not.toBeInTheDocument() | ||
}) | ||
|
||
it('renders the placeholder to add a new card', () => { | ||
expect(subject.queryByText('+')).toBeInTheDocument() | ||
}) | ||
}) | ||
|
||
describe('when the user cancels the new card', () => { | ||
beforeEach(() => { | ||
fireEvent.click(subject.queryByText('Cancel')) | ||
}) | ||
|
||
it('does not call the "onConfirm" prop', () => { | ||
expect(onConfirm).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('hides the input', () => { | ||
expect(subject.container.querySelector('input')).not.toBeInTheDocument() | ||
}) | ||
|
||
it('renders the placeholder to add a new card', () => { | ||
expect(subject.queryByText('+')).toBeInTheDocument() | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.