Skip to content

Commit f3bdb18

Browse files
authored
fix #1218 where carousel is not calculating children correctly (#1219)
* fix #1218 where carousel is not calculating children correctly * remove duplicate assertion
1 parent 35aa22d commit f3bdb18

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

src/components/carousel/CarouselPresenter.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import React, {ReactNode} from 'react';
12
import _ from 'lodash';
23
import {CarouselProps, CarouselState} from './types';
34

4-
export function getChildrenLength(props: CarouselProps): number {
5-
const length = _.get(props, 'children.length') || 0;
6-
return length;
5+
type PropsWithChildren = CarouselProps & { children?: ReactNode }
6+
7+
export function getChildrenLength(props: PropsWithChildren): number {
8+
return React.Children.count(props.children);
79
}
810

911
export function calcOffset(props: CarouselProps, state: Omit<CarouselState, 'initialOffset' | 'prevProps'>) {
Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,52 @@
1+
import React from 'react';
12
import * as uut from '../CarouselPresenter';
23

34
describe('Carousel presenter', () => {
45
it('should getChildrenLength', () => {
5-
expect(uut.getChildrenLength({children: [{}, {}, {}]})).toBe(3);
6-
expect(uut.getChildrenLength({children: [{}]})).toBe(1);
7-
expect(uut.getChildrenLength()).toBe(0);
6+
expect(uut.getChildrenLength({children: [<></>, <></>, <></>]})).toBe(3);
7+
expect(uut.getChildrenLength({children: [<></>]})).toBe(1);
8+
expect(uut.getChildrenLength({children: [[], <></>]})).toBe(1);
9+
expect(uut.getChildrenLength(<></>)).toBe(0);
810
});
911

1012
describe('calcOffset', () => {
1113
it('should calcOffset (default mode)', () => {
12-
expect(uut.calcOffset({children: [{}, {}, {}], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 0})).toStrictEqual({x: 0, y: 0});
13-
expect(uut.calcOffset({children: [{}, {}, {}], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 1})).toStrictEqual({x: 120, y: 0});
14-
expect(uut.calcOffset({children: [{}, {}, {}], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 2})).toStrictEqual({x: 240, y: 0});
15-
expect(uut.calcOffset({children: [{}, {}, {}], horizontal: false}, {pageWidth: 80, pageHeight: 150, currentPage: 0})).toStrictEqual({x: 0, y: 0});
16-
expect(uut.calcOffset({children: [{}, {}, {}], horizontal: false}, {pageWidth: 80, pageHeight: 150, currentPage: 1})).toStrictEqual({x: 0, y: 150});
17-
expect(uut.calcOffset({children: [{}, {}, {}], horizontal: false}, {pageWidth: 80, pageHeight: 150, currentPage: 2})).toStrictEqual({x: 0, y: 300});
14+
expect(uut.calcOffset({children: [<></>, <></>, <></>], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 0})).toStrictEqual({x: 0, y: 0});
15+
expect(uut.calcOffset({children: [<></>, <></>, <></>], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 1})).toStrictEqual({x: 120, y: 0});
16+
expect(uut.calcOffset({children: [<></>, <></>, <></>], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 2})).toStrictEqual({x: 240, y: 0});
17+
expect(uut.calcOffset({children: [<></>, <></>, <></>], horizontal: false}, {pageWidth: 80, pageHeight: 150, currentPage: 0})).toStrictEqual({x: 0, y: 0});
18+
expect(uut.calcOffset({children: [<></>, <></>, <></>], horizontal: false}, {pageWidth: 80, pageHeight: 150, currentPage: 1})).toStrictEqual({x: 0, y: 150});
19+
expect(uut.calcOffset({children: [<></>, <></>, <></>], horizontal: false}, {pageWidth: 80, pageHeight: 150, currentPage: 2})).toStrictEqual({x: 0, y: 300});
1820
});
1921

2022
it('should calcOffset (loop mode)', () => {
21-
expect(uut.calcOffset({loop: true, children: [{}, {}, {}], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 0})).toStrictEqual({x: 120, y: 0});
22-
expect(uut.calcOffset({loop: true, children: [{}, {}, {}], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 1})).toStrictEqual({x: 240, y: 0});
23-
expect(uut.calcOffset({loop: true, children: [{}, {}, {}], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 2})).toStrictEqual({x: 360, y: 0});
23+
expect(uut.calcOffset({loop: true, children: [<></>, <></>, <></>], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 0})).toStrictEqual({x: 120, y: 0});
24+
expect(uut.calcOffset({loop: true, children: [<></>, <></>, <></>], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 1})).toStrictEqual({x: 240, y: 0});
25+
expect(uut.calcOffset({loop: true, children: [<></>, <></>, <></>], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 2})).toStrictEqual({x: 360, y: 0});
2426
});
2527
});
2628

2729
describe('calcPageIndex', () => {
2830
it('should calcPageIndex', () => {
29-
expect(uut.calcPageIndex(120, {children: [{}, {}, {}]}, 120)).toBe(1);
30-
expect(uut.calcPageIndex(245, {children: [{}, {}, {}]}, 120)).toBe(2);
31-
expect(uut.calcPageIndex(481, {children: [{}, {}, {}]}, 120)).toBe(2);
32-
expect(uut.calcPageIndex(5, {children: [{}, {}, {}]}, 120)).toBe(0);
31+
expect(uut.calcPageIndex(120, {children: [<></>, <></>, <></>]}, 120)).toBe(1);
32+
expect(uut.calcPageIndex(245, {children: [<></>, <></>, <></>]}, 120)).toBe(2);
33+
expect(uut.calcPageIndex(481, {children: [<></>, <></>, <></>]}, 120)).toBe(2);
34+
expect(uut.calcPageIndex(5, {children: [<></>, <></>, <></>]}, 120)).toBe(0);
3335
});
3436

3537
it('should calcPageIndex (loop mode)', () => {
36-
expect(uut.calcPageIndex(120, {loop: true, children: [{}, {}, {}]}, 120)).toBe(0);
37-
expect(uut.calcPageIndex(245, {loop: true, children: [{}, {}, {}]}, 120)).toBe(1);
38-
expect(uut.calcPageIndex(481, {loop: true, children: [{}, {}, {}]}, 120)).toBe(0);
39-
expect(uut.calcPageIndex(5, {loop: true, children: [{}, {}, {}]}, 120)).toBe(2);
38+
expect(uut.calcPageIndex(120, {loop: true, children: [<></>, <></>, <></>]}, 120)).toBe(0);
39+
expect(uut.calcPageIndex(245, {loop: true, children: [<></>, <></>, <></>]}, 120)).toBe(1);
40+
expect(uut.calcPageIndex(481, {loop: true, children: [<></>, <></>, <></>]}, 120)).toBe(0);
41+
expect(uut.calcPageIndex(5, {loop: true, children: [<></>, <></>, <></>]}, 120)).toBe(2);
4042
});
4143
});
4244

4345
it('should return isOutsideLimits', () => {
44-
expect(uut.isOutOfBounds(120, {children: [{}, {}, {}]}, 120)).toBe(false);
45-
expect(uut.isOutOfBounds(1125, {children: [{}, {}, {}, {}]}, 375)).toBe(false);
46-
expect(uut.isOutOfBounds(0, {children: [{}, {}, {}]}, 120)).toBe(true);
47-
expect(uut.isOutOfBounds(481, {children: [{}, {}, {}]}, 120)).toBe(true);
48-
expect(uut.isOutOfBounds(1875, {children: [{}, {}, {}, {}]}, 375)).toBe(true);
46+
expect(uut.isOutOfBounds(120, {children: [<></>, <></>, <></>]}, 120)).toBe(false);
47+
expect(uut.isOutOfBounds(1125, {children: [<></>, <></>, <></>, <></>]}, 375)).toBe(false);
48+
expect(uut.isOutOfBounds(0, {children: [<></>, <></>, <></>]}, 120)).toBe(true);
49+
expect(uut.isOutOfBounds(481, {children: [<></>, <></>, <></>]}, 120)).toBe(true);
50+
expect(uut.isOutOfBounds(1875, {children: [<></>, <></>, <></>, <></>]}, 375)).toBe(true);
4951
});
5052
});

0 commit comments

Comments
 (0)