-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(trackersOrder): add orderTracker reducer
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
testTracker1, | ||
testTracker1Id, | ||
testTracker2, | ||
testTracker2Id, | ||
testTracker3, | ||
testTracker3Id | ||
} from '../../FAKE_DATA'; | ||
import trackersReducer, { initialState, orderTracker } from '../../trackersSlice'; | ||
|
||
describe('trackers reducer', () => { | ||
describe('Move a tracker', () => { | ||
it('should not do anything if the new index is the same as the previous one', () => { | ||
const finalState = trackersReducer( | ||
{ | ||
...initialState, | ||
trackers: [testTracker1, testTracker2, testTracker3] | ||
}, | ||
orderTracker({ trackerIdSource: testTracker1Id, trackerIdDestination: testTracker1Id }) | ||
); | ||
const { trackers: trackers } = finalState; | ||
expect(trackers[0].id).toBe(testTracker1Id); | ||
expect(trackers[1].id).toBe(testTracker2Id); | ||
expect(trackers[2].id).toBe(testTracker3Id); | ||
}); | ||
it('should move the first tracker from the start to the end.', () => { | ||
const finalState = trackersReducer( | ||
{ | ||
...initialState, | ||
trackers: [testTracker1, testTracker2, testTracker3] | ||
}, | ||
orderTracker({ trackerIdSource: testTracker1Id, trackerIdDestination: testTracker3Id }) | ||
); | ||
const { trackers: trackers } = finalState; | ||
expect(trackers[0].id).toBe(testTracker2Id); | ||
expect(trackers[1].id).toBe(testTracker3Id); | ||
expect(trackers[2].id).toBe(testTracker1Id); | ||
}); | ||
it('should move the last tracker from the end to the start.', () => { | ||
const finalState = trackersReducer( | ||
{ | ||
...initialState, | ||
trackers: [testTracker1, testTracker2, testTracker3] | ||
}, | ||
orderTracker({ trackerIdSource: testTracker3Id, trackerIdDestination: testTracker1Id }) | ||
); | ||
const { trackers: trackers } = finalState; | ||
expect(trackers[0].id).toBe(testTracker3Id); | ||
expect(trackers[1].id).toBe(testTracker1Id); | ||
expect(trackers[2].id).toBe(testTracker2Id); | ||
}); | ||
}); | ||
}); |
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,20 @@ | ||
import { PayloadAction } from '@reduxjs/toolkit'; | ||
|
||
import TrackersState from '../TrackersState'; | ||
|
||
export type Indexes = { | ||
trackerIdSource: string; | ||
trackerIdDestination: string; | ||
}; | ||
|
||
const orderTrackerReducer = (state: TrackersState, action: PayloadAction<Indexes>) => { | ||
const { trackerIdSource, trackerIdDestination } = action.payload; | ||
const sourceIndex = state.trackers.findIndex((t) => t.id === trackerIdSource); | ||
const destinationIndex = state.trackers.findIndex((t) => t.id === trackerIdDestination); | ||
if (sourceIndex !== -1 && destinationIndex !== -1) { | ||
const tracker = state.trackers.splice(sourceIndex, 1)[0]; | ||
state.trackers.splice(destinationIndex, 0, tracker); | ||
} | ||
}; | ||
|
||
export { orderTrackerReducer }; |
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