-
Notifications
You must be signed in to change notification settings - Fork 690
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix deck state transitions. Add test cases for deck state reducer. (#…
…1290) * Fix deck state transitions. Add test cases for deck state reducer. * Annotate the tests explaining each action. * Remove debugging console log. * Remove temporary stepper from example. * Changeset
- Loading branch information
1 parent
6153f1a
commit caa013f
Showing
5 changed files
with
224 additions
and
5 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,5 @@ | ||
--- | ||
'spectacle': patch | ||
--- | ||
|
||
Fixed deck transitions for presenter mode, added test coverage around deck reducer. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { renderHook, act } from '@testing-library/react'; | ||
import useDeckState, { DeckView } from './use-deck-state'; | ||
|
||
describe('useDeckState', () => { | ||
const initialState: DeckView = { | ||
slideIndex: 1, | ||
stepIndex: 1 | ||
}; | ||
|
||
/** | ||
* The INITIALIZE_TO should set the active and pending views | ||
* to the values provided in the payload. | ||
*/ | ||
it('should handle INITIALIZE_TO action', () => { | ||
const { result } = renderHook(() => useDeckState(initialState)); | ||
|
||
act(() => { | ||
result.current.initializeTo({ slideIndex: 2, stepIndex: 2 }); | ||
}); | ||
|
||
expect(result.current.activeView).toEqual({ slideIndex: 2, stepIndex: 2 }); | ||
expect(result.current.pendingView).toEqual({ slideIndex: 2, stepIndex: 2 }); | ||
expect(result.current.initialized).toBe(true); | ||
}); | ||
|
||
/** | ||
* The SKIP_TO action should set the pending view slide index to | ||
* the slide index provided by the payload and set the navigation | ||
* direction based on a delta of the previous and pending slides. | ||
*/ | ||
it('should handle SKIP_TO action', () => { | ||
const { result } = renderHook(() => useDeckState(initialState)); | ||
|
||
act(() => { | ||
result.current.skipTo({ slideIndex: 3 }); | ||
}); | ||
|
||
expect(result.current.navigationDirection).toBe(1); | ||
expect(result.current.pendingView.slideIndex).toBe(3); | ||
}); | ||
|
||
it('should handle SKIP_TO action in reverse', () => { | ||
const { result } = renderHook(() => | ||
useDeckState({ slideIndex: 5, stepIndex: 0, slideId: 0 }) | ||
); | ||
|
||
act(() => { | ||
result.current.skipTo({ slideIndex: 3 }); | ||
}); | ||
|
||
expect(result.current.navigationDirection).toBe(-1); | ||
expect(result.current.pendingView.slideIndex).toBe(3); | ||
}); | ||
|
||
/** | ||
* The STEP_FORWARD action should increment the pending slide index by 1 | ||
* and have a positive navigation direction. | ||
*/ | ||
it('should handle STEP_FORWARD action', () => { | ||
const { result } = renderHook(() => useDeckState(initialState)); | ||
|
||
act(() => { | ||
result.current.stepForward(); | ||
}); | ||
|
||
expect(result.current.pendingView.stepIndex).toBe( | ||
initialState.stepIndex + 1 | ||
); | ||
expect(result.current.navigationDirection).toBe(1); | ||
}); | ||
|
||
/** | ||
* The STEP_FORWARD action should decrement the pending slide index by 1 | ||
* and have a negative navigation direction. | ||
*/ | ||
it('should handle STEP_BACKWARD action', () => { | ||
const { result } = renderHook(() => useDeckState(initialState)); | ||
|
||
act(() => { | ||
result.current.stepBackward(); | ||
}); | ||
|
||
expect(result.current.pendingView.stepIndex).toBe( | ||
initialState.stepIndex - 1 | ||
); | ||
expect(result.current.navigationDirection).toBe(-1); | ||
}); | ||
|
||
/** | ||
* The ADVANCE_SLIDE action should increment the pending slide index by 1, | ||
* reset the step index, and have a positive navigation direction. | ||
*/ | ||
it('should handle ADVANCE_SLIDE action', () => { | ||
const { result } = renderHook(() => useDeckState(initialState)); | ||
|
||
act(() => { | ||
result.current.advanceSlide(); | ||
}); | ||
|
||
expect(result.current.pendingView.slideIndex).toBe( | ||
initialState.slideIndex + 1 | ||
); | ||
expect(result.current.pendingView.stepIndex).toBe(0); | ||
expect(result.current.navigationDirection).toBe(1); | ||
}); | ||
|
||
/** | ||
* The REGRESS_SLIDE action should decrement the pending slide index by 1, | ||
* reset the step index, and have a negative navigation direction. | ||
*/ | ||
it('should handle REGRESS_SLIDE action', () => { | ||
const { result } = renderHook(() => useDeckState(initialState)); | ||
|
||
act(() => { | ||
result.current.regressSlide({ stepIndex: 0 }); | ||
}); | ||
|
||
expect(result.current.pendingView.slideIndex).toBe( | ||
initialState.slideIndex - 1 | ||
); | ||
expect(result.current.pendingView.stepIndex).toBe(0); | ||
expect(result.current.navigationDirection).toBe(-1); | ||
}); | ||
|
||
/** | ||
* The COMMIT_TRANSITION action should set the active slide view | ||
* and pending slide view to the payload values. | ||
*/ | ||
it('should handle COMMIT_TRANSITION action', () => { | ||
const { result } = renderHook(() => useDeckState(initialState)); | ||
|
||
act(() => { | ||
result.current.commitTransition({ slideIndex: 2, stepIndex: 2 }); | ||
}); | ||
|
||
expect(result.current.activeView).toEqual({ slideIndex: 2, stepIndex: 2 }); | ||
expect(result.current.pendingView).toEqual({ slideIndex: 2, stepIndex: 2 }); | ||
}); | ||
|
||
/** | ||
* The CANCEL_TRANSITION action should cancel the slide transition | ||
* by reverting the pending view values to what the current active slide values are. | ||
*/ | ||
it('should handle CANCEL_TRANSITION action', () => { | ||
const { result } = renderHook(() => useDeckState(initialState)); | ||
|
||
act(() => { | ||
result.current.cancelTransition(); | ||
}); | ||
|
||
expect(result.current.pendingView).toEqual(result.current.activeView); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import clamp, { toFiniteNumber } from './clamp'; | ||
|
||
describe('toFiniteNumber', () => { | ||
it('should return 0 for NaN', () => { | ||
expect(toFiniteNumber(NaN)).toBe(0); | ||
expect(toFiniteNumber(Number.NaN)).toBe(0); | ||
}); | ||
|
||
it('should convert finite values to finite numbers', () => { | ||
expect(toFiniteNumber(123)).toBe(123); | ||
expect(toFiniteNumber(-456.789)).toBe(-456.789); | ||
}); | ||
|
||
it('should return Number.MAX_SAFE_INTEGER for Infinity', () => { | ||
expect(toFiniteNumber(Infinity)).toBe(Number.MAX_SAFE_INTEGER); | ||
expect(toFiniteNumber(-Infinity)).toBe(-Number.MAX_SAFE_INTEGER); | ||
}); | ||
}); | ||
|
||
describe('clamp', () => { | ||
it('should return NaN for NaN input', () => { | ||
expect(clamp(NaN)).toBeNaN(); | ||
}); | ||
|
||
it('should clamp value to specified range', () => { | ||
expect(clamp(10, 0, 5)).toBe(5); | ||
expect(clamp(-3, -10, 0)).toBe(-3); | ||
expect(clamp(7, 5, 10)).toBe(7); | ||
expect(clamp(-15, -10, 10)).toBe(-10); | ||
}); | ||
|
||
it('should not clamp when range is not specified', () => { | ||
expect(clamp(10)).toBe(10); | ||
expect(clamp(-3)).toBe(-3); | ||
expect(clamp(7)).toBe(7); | ||
expect(clamp(-15)).toBe(-15); | ||
}); | ||
}); |
caa013f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
spectacle-docs-website – ./
spectacle-docs-website-git-main-formidable-labs.vercel.app
spectacle-docs-website-formidable-labs.vercel.app
spectacle-website-mu.vercel.app