Skip to content

Commit

Permalink
Prevent divide by zero errors in measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonrobot committed Apr 8, 2024
1 parent 67e5fee commit be52ed2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/neat-chefs-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nuka-carousel': patch
---

Prevent divide by zero errors in measurements
72 changes: 72 additions & 0 deletions packages/nuka/src/hooks/use-measurement.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,78 @@ describe('useMeasurement', () => {
expect(scrollOffset).toEqual([]);
});

it('should return default values if offsetWidth is 0', () => {
const element = {
current: {
scrollWidth: 0,
offsetWidth: 0,
querySelector: () => ({
children: [],
}),
},
} as any;

const { result } = renderHook(() =>
useMeasurement({
element,
scrollDistance: 'screen',
}),
);

const { totalPages, scrollOffset } = result.current;

expect(totalPages).toBe(0);
expect(scrollOffset).toEqual([]);
});

it('should return default values if scrollDistance is 0', () => {
const element = {
current: {
scrollWidth: 1000,
offsetWidth: 500,
querySelector: () => ({
children: [],
}),
},
} as any;

const { result } = renderHook(() =>
useMeasurement({
element,
scrollDistance: 0,
}),
);

const { totalPages, scrollOffset } = result.current;

expect(totalPages).toBe(0);
expect(scrollOffset).toEqual([]);
});

it('should return default values if scrollDistance is < 0', () => {
const element = {
current: {
scrollWidth: 1000,
offsetWidth: 500,
querySelector: () => ({
children: [],
}),
},
} as any;

const { result } = renderHook(() =>
useMeasurement({
element,
scrollDistance: -1,
}),
);

const { totalPages, scrollOffset } = result.current;

expect(totalPages).toBe(0);
expect(scrollOffset).toEqual([]);
});

it('should return measurements for screen', () => {
const element = {
current: {
Expand Down
4 changes: 3 additions & 1 deletion packages/nuka/src/hooks/use-measurement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export function useMeasurement({ element, scrollDistance }: MeasurementProps) {
const visibleWidth = container.offsetWidth;
const remainder = scrollWidth - visibleWidth;

if (visibleWidth === 0) return;

switch (scrollDistance) {
case 'screen': {
const pageCount = Math.round(scrollWidth / visibleWidth);
Expand Down Expand Up @@ -55,7 +57,7 @@ export function useMeasurement({ element, scrollDistance }: MeasurementProps) {
break;
}
default: {
if (typeof scrollDistance === 'number') {
if (typeof scrollDistance === 'number' && scrollDistance > 0) {
// find the number of pages required to scroll all the slides
// to the end of the container
const pageCount = Math.ceil(remainder / scrollDistance) + 1;
Expand Down

0 comments on commit be52ed2

Please sign in to comment.