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.
- Loading branch information
Showing
6 changed files
with
430 additions
and
42 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
83 changes: 83 additions & 0 deletions
83
src/components/Board/components/DefaultLaneHeader/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,83 @@ | ||
import React, { Fragment, useState } from 'react' | ||
import styled from 'styled-components' | ||
|
||
const LaneHeaderSkeleton = styled.div` | ||
padding-bottom: 10px; | ||
font-weight: bold; | ||
display: flex; | ||
justify-content: space-between; | ||
span:nth-child(2) { | ||
cursor: pointer; | ||
} | ||
` | ||
|
||
const CursorPointer = styled.span` | ||
cursor: pointer; | ||
` | ||
|
||
const DefaultButton = styled.button` | ||
color: #333333; | ||
background-color: #FFFFFF; | ||
border-color: #CCCCCC; | ||
:hover, :focus, :active { | ||
background-color: #E6E6E6; | ||
} | ||
` | ||
|
||
const Input = styled.input` | ||
:focus { | ||
outline: none; | ||
} | ||
` | ||
|
||
function useRenameMode (state) { | ||
const [renameMode, setRenameMode] = useState(state) | ||
|
||
function toggleRenameMode () { | ||
setRenameMode(!renameMode) | ||
} | ||
|
||
return [renameMode, toggleRenameMode] | ||
} | ||
|
||
export default function ({ children: lane, allowRemoveLane, onLaneRemove, allowRenameLane, onLaneRename }) { | ||
const [renameMode, toggleRenameMode] = useRenameMode(false) | ||
const [title, setTitle] = useState(lane.title) | ||
const [titleInput, setTitleInput] = useState('') | ||
|
||
function handleRenameLane (title) { | ||
onLaneRename(lane.id, title) | ||
setTitle(title) | ||
toggleRenameMode() | ||
} | ||
|
||
function handleRenameMode () { | ||
setTitleInput(title) | ||
toggleRenameMode() | ||
} | ||
|
||
return ( | ||
<LaneHeaderSkeleton> | ||
{allowRenameLane && renameMode ? ( | ||
<Fragment> | ||
<span> | ||
<Input type='text' value={titleInput} onChange={({ target: { value } }) => setTitleInput(value)} autoFocus /> | ||
</span> | ||
<span> | ||
<DefaultButton type='button' onClick={() => handleRenameLane(titleInput)}>Rename</DefaultButton> | ||
<DefaultButton type='button' onClick={handleRenameMode}>Cancel</DefaultButton> | ||
</span> | ||
</Fragment> | ||
) : ( | ||
<Fragment> | ||
<CursorPointer onClick={handleRenameMode}> | ||
{title} | ||
</CursorPointer> | ||
{allowRemoveLane && <span onClick={() => onLaneRemove(lane)}>×</span>} | ||
</Fragment> | ||
)} | ||
</LaneHeaderSkeleton> | ||
) | ||
} |
148 changes: 148 additions & 0 deletions
148
src/components/Board/components/DefaultLaneHeader/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,148 @@ | ||
import React from 'react' | ||
import { render, fireEvent } from '@testing-library/react' | ||
import DefaultLaneHeader from './' | ||
|
||
describe('<DefaultLaneHeader />', () => { | ||
let subject | ||
|
||
const onLaneRemove = jest.fn() | ||
const onLaneRename = jest.fn() | ||
|
||
const lane = { id: 1, title: 'Lane title' } | ||
|
||
function reset () { | ||
subject = undefined | ||
onLaneRemove.mockClear() | ||
onLaneRename.mockClear() | ||
} | ||
|
||
function mount (props) { | ||
subject = render( | ||
<DefaultLaneHeader onLaneRemove={onLaneRemove} onLaneRename={onLaneRename} {...props}> | ||
{lane} | ||
</DefaultLaneHeader> | ||
) | ||
return subject | ||
} | ||
|
||
beforeEach(reset) | ||
|
||
it('renders a lane header with the title', () => { | ||
expect(mount().queryByText('Lane title')).toBeInTheDocument() | ||
}) | ||
|
||
describe('about the remove lane button', () => { | ||
describe('when the component does not receive the allowRemoveLane prop', () => { | ||
beforeEach(() => mount({ onLaneRemove })) | ||
|
||
it('does not show the remove button', () => { | ||
expect(subject.queryByText('×')).not.toBeInTheDocument() | ||
}) | ||
|
||
it('does not call the "onLaneRemove" callback', () => { | ||
expect(onLaneRemove).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
describe('when the component receives the "allowRemoveLane" prop', () => { | ||
beforeEach(() => mount({ allowRemoveLane: true })) | ||
|
||
it('shows the remove button', () => { | ||
expect(subject.queryByText('×')).toBeInTheDocument() | ||
}) | ||
|
||
it('does not call the "onLaneRemove" callback', () => { | ||
expect(onLaneRemove).not.toHaveBeenCalled() | ||
}) | ||
|
||
describe('when the user clicks on the remove button', () => { | ||
beforeEach(() => fireEvent.click(subject.queryByText('×'))) | ||
|
||
it('calls the "onLaneRemove" callback passing the lane', () => { | ||
expect(onLaneRemove).toHaveBeenCalledTimes(1) | ||
expect(onLaneRemove).toHaveBeenCalledWith(lane) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('about the lane title renaming', () => { | ||
describe('when the component does not receive the "allowRenameLane" prop', () => { | ||
describe('when the user clicks on the lane title', () => { | ||
beforeEach(() => { | ||
mount({ onLaneRename }) | ||
fireEvent.click(subject.queryByText('Lane title')) | ||
}) | ||
|
||
it('does not allow the user to rename the lane', () => { | ||
expect(subject.queryByText('Lane title')).toBeInTheDocument() | ||
expect(subject.container.querySelector('input')).not.toBeInTheDocument() | ||
}) | ||
|
||
it('does not call the "onLaneRename" callback', () => { | ||
expect(onLaneRename).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when the component receives the "allowRenameLane" prop', () => { | ||
describe('when the user clicks on the lane title', () => { | ||
beforeEach(() => { | ||
mount({ allowRenameLane: true, onLaneRename }) | ||
fireEvent.click(subject.queryByText('Lane title')) | ||
}) | ||
|
||
it('does not call the "onLaneRename" callback', () => { | ||
expect(onLaneRename).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('toggles the title for an input for typing a new title', () => { | ||
expect(subject.queryByText('Lane title')).not.toBeInTheDocument() | ||
expect(subject.container.querySelector('input')).toBeInTheDocument() | ||
expect(subject.queryByText('Rename', { selector: 'button' })).toBeInTheDocument() | ||
expect(subject.queryByText('Cancel', { selector: 'button' })).toBeInTheDocument() | ||
}) | ||
|
||
it('focuses on the input', () => { | ||
expect(subject.container.querySelector('input')).toHaveFocus() | ||
}) | ||
|
||
it('fills the input with the lane title', () => { | ||
expect(subject.container.querySelector('input')).toHaveValue('Lane title') | ||
}) | ||
|
||
describe('when the user types a new name and confirms it', () => { | ||
beforeEach(() => { | ||
fireEvent.change(subject.container.querySelector('input'), { target: { value: 'New title' } }) | ||
fireEvent.click(subject.queryByText('Rename', { selector: 'button' })) | ||
}) | ||
|
||
it('toggles the input for the new lane title', () => { | ||
expect(subject.queryByText('New title')).toBeInTheDocument() | ||
expect(subject.container.querySelector('input')).not.toBeInTheDocument() | ||
}) | ||
|
||
it('calls the "onLaneRename" callback passing the lane id with the new title', () => { | ||
expect(onLaneRename).toHaveBeenCalledTimes(1) | ||
expect(onLaneRename).toHaveBeenCalledWith(1, 'New title') | ||
}) | ||
}) | ||
|
||
describe('when the user cancels the renaming', () => { | ||
beforeEach(() => { | ||
fireEvent.click(subject.queryByText('Cancel', { selector: 'button' })) | ||
}) | ||
|
||
it('cancels the renaming', () => { | ||
expect(subject.queryByText('Lane title')).toBeInTheDocument() | ||
expect(subject.container.querySelector('input')).not.toBeInTheDocument() | ||
}) | ||
|
||
it('does call the "onLaneRename" callback', () => { | ||
expect(onLaneRename).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.