Skip to content
This repository has been archived by the owner on Oct 8, 2022. It is now read-only.

Commit

Permalink
fix: Call onCardDragEnd and onColumnDragEnd only when the movement is…
Browse files Browse the repository at this point in the history
… to another position (#261)

Co-authored-by: MatheusPoliCamilo <matheuspolicamilo@gmail.com>
Co-authored-by: sousajunior <cdesousajunior@gmail.com>
  • Loading branch information
sousajunior and MatheusPoliCamilo authored Feb 13, 2020
1 parent 8122c94 commit a119d3e
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/components/Board/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import withDroppable from '../withDroppable'
import { when, partialRight } from '@services/utils'
import DefaultColumnHeader from './components/DefaultColumnHeader'
import DefaultCard from './components/DefaultCard'
import { getCard, getCoordinates, isAColumnMove } from './services'
import {
getCard,
getCoordinates,
isAColumnMove,
isMovingAColumnToAnotherPosition,
isMovingACardToAnotherPosition
} from './services'
import { moveCard, moveColumn, addColumn, removeColumn, changeColumn, addCard, removeCard } from '@services/helpers'

const StyledBoard = styled.div`
Expand Down Expand Up @@ -216,8 +222,10 @@ function BoardContainer({
if (!coordinates.source) return

isAColumnMove(event.type)
? onColumnDragEnd({ ...coordinates, subject: board.columns[coordinates.source.fromPosition] })
: onCardDragEnd({ ...coordinates, subject: getCard(board, coordinates.source) })
? isMovingAColumnToAnotherPosition(coordinates) &&
onColumnDragEnd({ ...coordinates, subject: board.columns[coordinates.source.fromPosition] })
: isMovingACardToAnotherPosition(coordinates) &&
onCardDragEnd({ ...coordinates, subject: getCard(board, coordinates.source) })
}

return (
Expand Down
54 changes: 54 additions & 0 deletions src/components/Board/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ describe('<Board />', () => {
})
})

describe('whe the user moves a card to the same position', () => {
beforeEach(() => {
act(() => {
callbacks.onDragEnd({
source: { droppableId: '1', index: 0 },
destination: { droppableId: '1', index: 0 }
})
})
})

it('does not call onCardDragEnd callback', () => {
expect(onCardDragEnd).not.toHaveBeenCalled()
})
})

describe('when the user moves a card to another position', () => {
beforeEach(() => {
act(() => {
Expand Down Expand Up @@ -138,6 +153,18 @@ describe('<Board />', () => {
})
})

describe('when the user moves a column to same position', () => {
beforeEach(() => {
act(() => {
callbacks.onDragEnd({ source: { index: 0 }, destination: { index: 0 }, type: 'BOARD' })
})
})

it('does not call onColumnDragEnd callback', () => {
expect(onColumnDragEnd).not.toHaveBeenCalled()
})
})

describe('when the user moves a column to another position', () => {
beforeEach(() => {
act(() => {
Expand Down Expand Up @@ -624,6 +651,21 @@ describe('<Board />', () => {
})
})

describe('whe the user moves a card to the same position', () => {
beforeEach(() => {
act(() => {
callbacks.onDragEnd({
source: { droppableId: '1', index: 0 },
destination: { droppableId: '1', index: 0 }
})
})
})

it('does not call onCardDragEnd callback', () => {
expect(onCardDragEnd).not.toHaveBeenCalled()
})
})

describe('when the user moves a card to another position', () => {
beforeEach(() => {
act(() => {
Expand Down Expand Up @@ -695,6 +737,18 @@ describe('<Board />', () => {
})
})

describe('when the user moves a column to same position', () => {
beforeEach(() => {
act(() => {
callbacks.onDragEnd({ source: { index: 0 }, destination: { index: 0 }, type: 'BOARD' })
})
})

it('does not call onColumnDragEnd callback', () => {
expect(onColumnDragEnd).not.toHaveBeenCalled()
})
})

describe('when the user moves a column to another position', () => {
beforeEach(() => {
act(() => {
Expand Down
13 changes: 12 additions & 1 deletion src/components/Board/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ function getColumn(board, droppableId) {
return board.columns.find(({ id }) => String(id) === droppableId)
}

export { getCard, getCoordinates, isAColumnMove }
function isMovingAColumnToAnotherPosition(coordinates) {
return coordinates.source.fromPosition !== coordinates.destination.toPosition
}

function isMovingACardToAnotherPosition(coordinates) {
return !(
coordinates.source.fromPosition === coordinates.destination.toPosition &&
coordinates.source.fromColumnId === coordinates.destination.toColumnId
)
}

export { getCard, getCoordinates, isAColumnMove, isMovingAColumnToAnotherPosition, isMovingACardToAnotherPosition }
69 changes: 68 additions & 1 deletion src/components/Board/services/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getCard, isAColumnMove, getCoordinates } from './'
import {
getCard,
isAColumnMove,
getCoordinates,
isMovingAColumnToAnotherPosition,
isMovingACardToAnotherPosition
} from './'

describe('#getCoordinates', () => {
describe('when the event does not have destination', () => {
Expand Down Expand Up @@ -65,3 +71,64 @@ describe('#getCard', () => {
expect(getCard(board, { fromColumnId: 1, fromPosition: 1 })).toEqual({ id: 2 })
})
})

describe('#isMovingAColumnToAnotherPosition', () => {
describe('when coordinates does not have same source and destination', () => {
const validColumnCoordinates = {
source: { fromPosition: 0 },
destination: { toPosition: 1 }
}

it('returns true', () => {
expect(isMovingAColumnToAnotherPosition(validColumnCoordinates)).toEqual(true)
})
})

describe('when coordinates has same source and destination', () => {
const invalidColumnCoordinates = {
source: { fromPosition: 0 },
destination: { toPosition: 0 }
}

it('returns false', () => {
expect(isMovingAColumnToAnotherPosition(invalidColumnCoordinates)).toEqual(false)
})
})
})

describe('#isMovingACardToAnotherPosition', () => {
describe('when coordinates does not have same source and destination', () => {
describe('when the source column is different from the destination column', () => {
const validCardCoordinates = {
source: { fromPosition: 0, fromColumnId: 0 },
destination: { toPosition: 0, toColumnId: 1 }
}

it('returns true', () => {
expect(isMovingACardToAnotherPosition(validCardCoordinates)).toEqual(true)
})
})

describe('when the source position is different from the destination position', () => {
const validCardCoordinates = {
source: { fromPosition: 0, fromColumnId: 0 },
destination: { toPosition: 1, toColumnId: 0 }
}

it('returns true', () => {
expect(isMovingACardToAnotherPosition(validCardCoordinates)).toEqual(true)
})
})
})

describe('when coordinates has same source and destination', () => {
const validCardCoordinates = {
source: { fromPosition: 0, fromColumnId: 0 },
destination: { toPosition: 0, toColumnId: 0 }
}

it('returns false', () => {
expect(isMovingACardToAnotherPosition(validCardCoordinates)).toEqual(false)
})
})
})

0 comments on commit a119d3e

Please sign in to comment.