Skip to content
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

Unable to test component using Scrollbooster #69

Open
AlexanderVikenfalk opened this issue Apr 26, 2021 · 0 comments
Open

Unable to test component using Scrollbooster #69

AlexanderVikenfalk opened this issue Apr 26, 2021 · 0 comments

Comments

@AlexanderVikenfalk
Copy link

AlexanderVikenfalk commented Apr 26, 2021

Hi!

I have a component which relies on refs and Scrollbooster (see below). When trying to mount the component with Enzyme I get an error saying:

at processImmediate (timers.js:658:5) ReferenceError: Element is not defined
at new t (/home/vikenfalk/third-party-client/tp-client/node_modules/scrollbooster/dist/webpack:/ScrollBooster/src/index.js:76:70)

If I log the reference that I'm passing to the Scrollbooster it looks fine and identical to what I can see when I run the actual application. The error seems to be related to JSDOM, but I'm wondering if anybody knows a way around this issue so that I can mount the component and test the calculations within the UseEffect function?

const ProgressBarScroller = (props) => {
    const [progressbarWrapperRef, progressBarTaskRef, progressBarRef] = [
        useRef(null),
        useRef(null),
        useRef(null)
    ];
    const getElementWidth = (refName) => refName.current.getBoundingClientRect().width;

    useEffect(() => {
        if (!progressBarTaskRef.current || !progressBarRef.current || !progressbarWrapperRef.current) return;

        const taskWidth = getElementWidth(progressBarTaskRef);
        const progressBarWidth = getElementWidth(progressBarRef);
        const progressbarWrapperWidth = getElementWidth(progressbarWrapperRef);

        const middleOfActiveTask =
            (props.currentTaskIndex + 1) * taskWidth - taskWidth / 2;
        const requestedX = middleOfActiveTask - progressbarWrapperWidth / 2;
        const maxPossibleX = progressBarWidth - progressbarWrapperWidth;


        const scroller = new scrollBooster({
            viewport: progressbarWrapperRef.current,
            direction: 'horizontal',
            scrollMode: 'transform',
            friction: 0.2
        });

        /**
         * returns 0 if requested X position is lower than minimum.
         * returns the highest possible X position if requested X is higher than maximum.
         * Otherwise returns requested X position.
         * @returns {number}
         */
        const getValidXPosition = () => {
            return requestedX <= 0 ? 0
                : requestedX >= maxPossibleX ? maxPossibleX
                    : requestedX;
        };

        (function moveToDefaultPosition() {
            scroller.setPosition({
                x: getValidXPosition(),
                y: 0
            });
        })();

        //Cleans up ScrollBooster on unmount
        return () => {
            scroller.destroy();
        };
    }, []);

    return (
        <div className={'cs-progressbar'} ref={progressbarWrapperRef}>
            <ul className={`cs-progressbar__list`} ref={progressBarRef}>
                {props.tasks.map((task, index) => {
                    return (
                        <ProgressBarTask
                            {...task}
                            key={task.id}
                            innerRef={
                                props.currentTaskIndex === index
                                    ? progressBarTaskRef
                                    : null
                            }
                        />
                    );
                })}
            </ul>
        </div>
    );
};
library version
enzyme 3.11.0
react 16.13.1
react-dom 16.13.1
react-test-renderer 17.0.1
scrollbooster 2.3.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant