-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix screen measurement when scrollWidth is a float
- Loading branch information
1 parent
b8edcae
commit 16cd0dd
Showing
7 changed files
with
151 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 @@ | ||
--- | ||
'nuka-carousel': patch | ||
--- | ||
|
||
Fix screen measurement when scrollWidth is a float |
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 |
---|---|---|
|
@@ -37,6 +37,7 @@ dist | |
types | ||
yarn-error.log* | ||
package-lock.json | ||
coverage | ||
|
||
cypress/screenshots | ||
cypress/videos | ||
|
This file was deleted.
Oops, something went wrong.
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
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,140 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { renderHook } from '@testing-library/react'; | ||
|
||
import { useMeasurement } from './use-measurement'; | ||
import * as hooks from './use-resize-observer'; | ||
|
||
const domElement = {} as any; | ||
jest.spyOn(hooks, 'useResizeObserver').mockImplementation(() => domElement); | ||
|
||
describe('useMeasurement', () => { | ||
it('return the default values', () => { | ||
const { result } = renderHook(() => | ||
useMeasurement({ | ||
element: { current: null }, | ||
scrollDistance: 'screen', | ||
}), | ||
); | ||
|
||
const { totalPages, scrollOffset } = result.current; | ||
|
||
expect(totalPages).toBe(0); | ||
expect(scrollOffset).toEqual([]); | ||
}); | ||
|
||
it('should return measurements for screen', () => { | ||
const element = { | ||
current: { | ||
// this test covers a specific rounding error that can | ||
// occur when the scrollWidth/offsetWidth returns a float | ||
scrollWidth: 900, | ||
offsetWidth: 500, | ||
querySelector: () => ({ | ||
children: [ | ||
{ offsetWidth: 200 }, | ||
{ offsetWidth: 300 }, | ||
{ offsetWidth: 400 }, | ||
], | ||
}), | ||
}, | ||
} as any; | ||
|
||
const { result } = renderHook(() => | ||
useMeasurement({ | ||
element, | ||
scrollDistance: 'screen', | ||
}), | ||
); | ||
|
||
const { totalPages, scrollOffset } = result.current; | ||
|
||
expect(totalPages).toBe(2); | ||
expect(scrollOffset).toEqual([0, 500]); | ||
}); | ||
|
||
it('should return measurements for screen with fractional pixels', () => { | ||
const element = { | ||
current: { | ||
// this test covers a specific rounding error that can | ||
// occur when the scrollWidth/offsetWidth returns a float | ||
scrollWidth: 1720, | ||
offsetWidth: 573, | ||
querySelector: () => ({ | ||
children: [ | ||
{ offsetWidth: 573 }, | ||
{ offsetWidth: 573 }, | ||
{ offsetWidth: 573 }, | ||
], | ||
}), | ||
}, | ||
} as any; | ||
|
||
const { result } = renderHook(() => | ||
useMeasurement({ | ||
element, | ||
scrollDistance: 'screen', | ||
}), | ||
); | ||
|
||
const { totalPages, scrollOffset } = result.current; | ||
|
||
expect(totalPages).toBe(3); | ||
expect(scrollOffset).toEqual([0, 573, 1146]); | ||
}); | ||
|
||
it('should return measurements for slide distance', () => { | ||
const element = { | ||
current: { | ||
scrollWidth: 900, | ||
offsetWidth: 500, | ||
querySelector: () => ({ | ||
children: [ | ||
{ offsetWidth: 200 }, | ||
{ offsetWidth: 300 }, | ||
{ offsetWidth: 400 }, | ||
], | ||
}), | ||
}, | ||
} as any; | ||
|
||
const { result } = renderHook(() => | ||
useMeasurement({ | ||
element, | ||
scrollDistance: 'slide', | ||
}), | ||
); | ||
|
||
const { totalPages, scrollOffset } = result.current; | ||
|
||
expect(totalPages).toBe(3); | ||
expect(scrollOffset).toEqual([0, 200, 500]); | ||
}); | ||
|
||
it('should return measurements for numbered distance', () => { | ||
const element = { | ||
current: { | ||
scrollWidth: 900, | ||
offsetWidth: 500, | ||
querySelector: () => ({ | ||
children: [ | ||
{ offsetWidth: 200 }, | ||
{ offsetWidth: 300 }, | ||
{ offsetWidth: 400 }, | ||
], | ||
}), | ||
}, | ||
} as any; | ||
|
||
const { result } = renderHook(() => | ||
useMeasurement({ | ||
element, | ||
scrollDistance: 200, | ||
}), | ||
); | ||
|
||
const { totalPages, scrollOffset } = result.current; | ||
|
||
expect(totalPages).toBe(3); | ||
expect(scrollOffset).toEqual([0, 200, 400]); | ||
}); | ||
}); |
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