Skip to content

fix #1218 where carousel is not calculating children correctly #1219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/components/carousel/CarouselPresenter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, {ReactNode} from 'react';
import _ from 'lodash';
import {CarouselProps, CarouselState} from './types';

export function getChildrenLength(props: CarouselProps): number {
const length = _.get(props, 'children.length') || 0;
return length;
type PropsWithChildren = CarouselProps & { children?: ReactNode }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'children' are not already part of CarouselProps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - It is fine in the component because the class uses a generic which looks like readonly props: Readonly<P> & Readonly<{ children?: ReactNode }>;


export function getChildrenLength(props: PropsWithChildren): number {
return React.Children.count(props.children);
}

export function calcOffset(props: CarouselProps, state: Omit<CarouselState, 'initialOffset' | 'prevProps'>) {
Expand Down
52 changes: 27 additions & 25 deletions src/components/carousel/__tests__/CarouselPresenter.spec.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
import React from 'react';
import * as uut from '../CarouselPresenter';

describe('Carousel presenter', () => {
it('should getChildrenLength', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could argue this test is pointless now and can be deleted. It's just testing that React.Children.count works - which doesn't need to be tested.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you changing the tests from objects to tags?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because {} is not a valid component in react. If you try and call React.children.count() with {} you'll get Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead.

expect(uut.getChildrenLength({children: [{}, {}, {}]})).toBe(3);
expect(uut.getChildrenLength({children: [{}]})).toBe(1);
expect(uut.getChildrenLength()).toBe(0);
expect(uut.getChildrenLength({children: [<></>, <></>, <></>]})).toBe(3);
expect(uut.getChildrenLength({children: [<></>]})).toBe(1);
expect(uut.getChildrenLength({children: [[], <></>]})).toBe(1);
expect(uut.getChildrenLength(<></>)).toBe(0);
});

describe('calcOffset', () => {
it('should calcOffset (default mode)', () => {
expect(uut.calcOffset({children: [{}, {}, {}], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 0})).toStrictEqual({x: 0, y: 0});
expect(uut.calcOffset({children: [{}, {}, {}], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 1})).toStrictEqual({x: 120, y: 0});
expect(uut.calcOffset({children: [{}, {}, {}], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 2})).toStrictEqual({x: 240, y: 0});
expect(uut.calcOffset({children: [{}, {}, {}], horizontal: false}, {pageWidth: 80, pageHeight: 150, currentPage: 0})).toStrictEqual({x: 0, y: 0});
expect(uut.calcOffset({children: [{}, {}, {}], horizontal: false}, {pageWidth: 80, pageHeight: 150, currentPage: 1})).toStrictEqual({x: 0, y: 150});
expect(uut.calcOffset({children: [{}, {}, {}], horizontal: false}, {pageWidth: 80, pageHeight: 150, currentPage: 2})).toStrictEqual({x: 0, y: 300});
expect(uut.calcOffset({children: [<></>, <></>, <></>], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 0})).toStrictEqual({x: 0, y: 0});
expect(uut.calcOffset({children: [<></>, <></>, <></>], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 1})).toStrictEqual({x: 120, y: 0});
expect(uut.calcOffset({children: [<></>, <></>, <></>], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 2})).toStrictEqual({x: 240, y: 0});
expect(uut.calcOffset({children: [<></>, <></>, <></>], horizontal: false}, {pageWidth: 80, pageHeight: 150, currentPage: 0})).toStrictEqual({x: 0, y: 0});
expect(uut.calcOffset({children: [<></>, <></>, <></>], horizontal: false}, {pageWidth: 80, pageHeight: 150, currentPage: 1})).toStrictEqual({x: 0, y: 150});
expect(uut.calcOffset({children: [<></>, <></>, <></>], horizontal: false}, {pageWidth: 80, pageHeight: 150, currentPage: 2})).toStrictEqual({x: 0, y: 300});
});

it('should calcOffset (loop mode)', () => {
expect(uut.calcOffset({loop: true, children: [{}, {}, {}], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 0})).toStrictEqual({x: 120, y: 0});
expect(uut.calcOffset({loop: true, children: [{}, {}, {}], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 1})).toStrictEqual({x: 240, y: 0});
expect(uut.calcOffset({loop: true, children: [{}, {}, {}], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 2})).toStrictEqual({x: 360, y: 0});
expect(uut.calcOffset({loop: true, children: [<></>, <></>, <></>], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 0})).toStrictEqual({x: 120, y: 0});
expect(uut.calcOffset({loop: true, children: [<></>, <></>, <></>], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 1})).toStrictEqual({x: 240, y: 0});
expect(uut.calcOffset({loop: true, children: [<></>, <></>, <></>], horizontal: true}, {pageWidth: 120, pageHeight: 100, currentPage: 2})).toStrictEqual({x: 360, y: 0});
});
});

describe('calcPageIndex', () => {
it('should calcPageIndex', () => {
expect(uut.calcPageIndex(120, {children: [{}, {}, {}]}, 120)).toBe(1);
expect(uut.calcPageIndex(245, {children: [{}, {}, {}]}, 120)).toBe(2);
expect(uut.calcPageIndex(481, {children: [{}, {}, {}]}, 120)).toBe(2);
expect(uut.calcPageIndex(5, {children: [{}, {}, {}]}, 120)).toBe(0);
expect(uut.calcPageIndex(120, {children: [<></>, <></>, <></>]}, 120)).toBe(1);
expect(uut.calcPageIndex(245, {children: [<></>, <></>, <></>]}, 120)).toBe(2);
expect(uut.calcPageIndex(481, {children: [<></>, <></>, <></>]}, 120)).toBe(2);
expect(uut.calcPageIndex(5, {children: [<></>, <></>, <></>]}, 120)).toBe(0);
});

it('should calcPageIndex (loop mode)', () => {
expect(uut.calcPageIndex(120, {loop: true, children: [{}, {}, {}]}, 120)).toBe(0);
expect(uut.calcPageIndex(245, {loop: true, children: [{}, {}, {}]}, 120)).toBe(1);
expect(uut.calcPageIndex(481, {loop: true, children: [{}, {}, {}]}, 120)).toBe(0);
expect(uut.calcPageIndex(5, {loop: true, children: [{}, {}, {}]}, 120)).toBe(2);
expect(uut.calcPageIndex(120, {loop: true, children: [<></>, <></>, <></>]}, 120)).toBe(0);
expect(uut.calcPageIndex(245, {loop: true, children: [<></>, <></>, <></>]}, 120)).toBe(1);
expect(uut.calcPageIndex(481, {loop: true, children: [<></>, <></>, <></>]}, 120)).toBe(0);
expect(uut.calcPageIndex(5, {loop: true, children: [<></>, <></>, <></>]}, 120)).toBe(2);
});
});

it('should return isOutsideLimits', () => {
expect(uut.isOutOfBounds(120, {children: [{}, {}, {}]}, 120)).toBe(false);
expect(uut.isOutOfBounds(1125, {children: [{}, {}, {}, {}]}, 375)).toBe(false);
expect(uut.isOutOfBounds(0, {children: [{}, {}, {}]}, 120)).toBe(true);
expect(uut.isOutOfBounds(481, {children: [{}, {}, {}]}, 120)).toBe(true);
expect(uut.isOutOfBounds(1875, {children: [{}, {}, {}, {}]}, 375)).toBe(true);
expect(uut.isOutOfBounds(120, {children: [<></>, <></>, <></>]}, 120)).toBe(false);
expect(uut.isOutOfBounds(1125, {children: [<></>, <></>, <></>, <></>]}, 375)).toBe(false);
expect(uut.isOutOfBounds(0, {children: [<></>, <></>, <></>]}, 120)).toBe(true);
expect(uut.isOutOfBounds(481, {children: [<></>, <></>, <></>]}, 120)).toBe(true);
expect(uut.isOutOfBounds(1875, {children: [<></>, <></>, <></>, <></>]}, 375)).toBe(true);
});
});