Coder Social home page Coder Social logo

trurl-master / jsdom-testing-mocks Goto Github PK

View Code? Open in Web Editor NEW
105.0 3.0 5.0 1.57 MB

A set of tools for emulating browser behavior in jsdom environment

License: MIT License

TypeScript 99.71% JavaScript 0.29%
testing react-testing-library jsdom mocks intersectionobserver resizeobserver viewport matchmedia javascript reactjs

jsdom-testing-mocks's People

Contributors

adjsky avatar chromeq avatar davidr64 avatar trurl-master avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jsdom-testing-mocks's Issues

Error with type-fest on build project

Hi.
im using vite for build, and process stoped with this err.
Why could this be? I tried searching on google but didn't find an answer. tell me what could be causing this error and if it's my fault.

node_modules/.pnpm/[email protected][email protected]/node_modules/jsdom-testing-mocks/dist/index.d.ts:1:23 - error TS2305: Module '"type-fest"' has no exported member 'Writable'.

1 import { PartialDeep, Writable, RequireAtLeastOne } from 'type-fest';

Cheers.

Do not require window at import time

We run half of our tests in a node environment using the magic comments in Jest.

This causes the import of 'jsdom-testing-mocks' in setupTests.ts to fail.

Unfortunately, a workaround with a dynamic import is working locally but not in CI.

`@media not all and ` is not supported

Hi, thank you for your awesome library.)
I have been trying to test my own library and I encountered strange behavior.

Screenshot 2023-05-22 at 23 12 23

But, it's work in a browser.

I'm using jsdom-testing-mocks v1.9.0.

TypeError: Cannot redefine property: ResizeObserver (v1.4.0)

Hi

with version 1.4.0 this error starts to appear.

Node version 14.20.0
Jest version 27.5.1
jsdom => [email protected]

 FAIL  src/components/object-message/object-message.unit.js
  ● Test suite failed to run

    TypeError: Cannot redefine property: ResizeObserver
        at Function.defineProperty (<anonymous>)

       6 | import { click, getByText, mountToDom } from '@testutils'
       7 |
    >  8 | const resizeObserverMock = mockResizeObserver()
         |                            ^
       9 |
      10 | jest.mock('lodash/debounce', () => {
      11 |   return jest.fn((fn) => fn)

      at mockResizeObserver (node_modules/jsdom-testing-mocks/src/mocks/resize-observer.ts:101:10)
      at Object.<anonymous> (src/components/object-message/object-message.unit.js:8:28)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

LocalResizeObserver is not a constructor error in v1.10+

When updating this library I noticed that some of our chart tests (charts use visx and the resize component). They pass fine in 1.9.0 but fail in 1.10+.

TypeError: LocalResizeObserver is not a constructor
 ❯ node_modules/@visx/responsive/lib/components/ParentSize.js:61:20
 ❯ commitHookEffectListMount node_modules/react-dom/cjs/react-dom.development.js:23150:26
 ❯ commitPassiveMountOnFiber node_modules/react-dom/cjs/react-dom.development.js:24931:11
 ❯ commitPassiveMountEffects_complete node_modules/react-dom/cjs/react-dom.development.js:24891:9
 ❯ commitPassiveMountEffects_begin node_modules/react-dom/cjs/react-dom.development.js:24878:7
 ❯ commitPassiveMountEffects node_modules/react-dom/cjs/react-dom.development.js:24866:3
 ❯ flushPassiveEffectsImpl node_modules/react-dom/cjs/react-dom.development.js:27039:3
 ❯ flushPassiveEffects node_modules/react-dom/cjs/react-dom.development.js:26984:14
 ❯ node_modules/react-dom/cjs/react-dom.development.js:26769:9
 ❯ flushActQueue node_modules/react/cjs/react.development.js:2667:24

I have the library configs setup like this:

import { act, cleanup } from '@testing-library/react'
import { afterEach, beforeAll, vi } from 'vitest'
import { configMocks } from 'jsdom-testing-mocks'
import '@testing-library/jest-dom/vitest'

configMocks({ act })

Allow adding more properties to the IntersectionDescription

I am using mockIntersectionObserver.enterNode and the type allows passing a desc which is typed as IntersectionDescription
This type is too basic, some callbacks need to know more about the "entry" such as the intersectionRatio or boundingClientRect

I realise this is probably difficult to do as jsdom does not render actual layouts so a lot of these cannot be calculated easily (or at all)

However you do spread the desc in the enter/leave helper functions,

setNodeState(index, { ...desc, isIntersecting: true });

So I propose that the IntersectionDescription should look a lot more like the TS lib definition with all optionals so they are spread into your callback, then it is up to the developer to set the correct values as the test may require

Export `MockedResizeObserver` class

Overview

Can we please export MockedResizeObserver, similarly to MockedIntersectionObserver? It's helpful when the intention is just polyfilling the API in jsdom, as opposed to actually instantiating the utility methods.

[Request] Add compatibility with other libraries

Based on the name of this library I would have assumed that it was testing-library-agnostic, but it uses jest-specific mocking functions and therefore can't be used with other libraries like Vitest that also use the jsdom environment. I would love to see compatibility expanded, perhaps by providing a configuration option to provide the desired mocking functions or by detecting which testing library is in use.

Animations aren't captured

Hi I'm not sure if I'm doing anything wrong here or its a bug.
The .getAnimations always returns an empty array.

Accordion.tsx

'use client';

import { v4 as uuidv4 } from 'uuid';
import { Heading } from '@components/Heading/Heading';
import styles from './Accordion.module.scss';
import { useRef, useState } from 'react';

type AccordionProps = {
    label: React.ReactNode;
    children: React.ReactNode;
};

export const Accordion = (props: AccordionProps) => {
    const { label, children } = props;
    const detailsRef = useRef<HTMLDetailsElement>(null);
    const labelRef = useRef<HTMLElement>(null);
    const contentRef = useRef<HTMLDivElement>(null);
    const [animation, setAnimation] = useState<Animation | null>(null);
    const [isClosing, setIsClosing] = useState(false);
    const [isExpanding, setIsExpanding] = useState(false);
    const id = uuidv4();

    const onLabelClick = (e: React.MouseEvent<HTMLElement>): void => {
        e.preventDefault();

        if (!detailsRef.current || !contentRef.current) return;

        detailsRef.current.style.overflow = 'hidden';

        if (isClosing || !detailsRef.current.open) {
            open();
            return;
        }

        if (isExpanding || detailsRef.current.open) {
            shrink();
            return;
        }
    };

    const shrink = () => {
        if (!detailsRef.current || !labelRef.current || !contentRef.current) return;

        // Set the element as "being closing"
        setIsClosing(true);

        // Store the current height of the element
        const startHeight = `${detailsRef.current.offsetHeight}px`;

        // Calculate the height of the summary
        const endHeight = `${detailsRef.current.offsetHeight - contentRef.current.offsetHeight}px`;

        // If there is already an animation running
        if (animation) {
            // Cancel the current animation
            animation.cancel();
        }

        // Start a WAAPI animation
        const currentAnimation = detailsRef.current.animate(
            {
                // Set the keyframes from the startHeight to endHeight
                height: [startHeight, endHeight],
            },
            {
                duration: 400,
                easing: 'ease-in-out',
            },
        );

        // Set animation state
        setAnimation(currentAnimation);

        // When the animation is complete, call onAnimationFinish()
        currentAnimation.onfinish = () => onAnimationFinish(false);

        // If the animation is cancelled, isClosing variable is set to false
        currentAnimation.oncancel = () => setIsClosing(false);
    };

    const open = () => {
        if (!detailsRef.current) return;

        detailsRef.current.style.height = `${detailsRef.current.offsetHeight}px`;

        detailsRef.current.open = true;

        window.requestAnimationFrame(() => expand());
    };

    const expand = () => {
        if (!detailsRef.current || !labelRef.current || !contentRef.current) return;

        // Set the element as "being expanding"
        setIsExpanding(true);

        // Get the current fixed height of the element
        const startHeight = `${detailsRef.current.offsetHeight}px`;

        // Calculate the open height of the element (summary height + content height)
        const endHeight = `${detailsRef.current.offsetHeight + contentRef.current.offsetHeight}px`;

        // If there is already an animation running
        if (animation) {
            // Cancel the current animation
            animation.cancel();
        }

        // Start a WAAPI animation
        const currentAnimation = detailsRef.current.animate(
            {
                // Set the keyframes from the startHeight to endHeight
                height: [startHeight, endHeight],
            },
            {
                duration: 400,
                easing: 'ease-in-out',
            },
        );

        // Set animation state
        setAnimation(currentAnimation);

        // When the animation is complete, call onAnimationFinish()
        currentAnimation.onfinish = () => onAnimationFinish(true);
        // If the animation is cancelled, isExpanding variable is set to false
        currentAnimation.oncancel = () => setIsExpanding(false);
    };

    const onAnimationFinish = (open: boolean) => {
        if (!detailsRef.current || !labelRef.current || !contentRef.current) return;

        // Set the open attribute based on the parameter
        detailsRef.current.open = open;

        labelRef.current.setAttribute('aria-expanded', `${open}`);

        // Clear the stored animation
        setAnimation(null);

        // Reset isClosing & isExpanding
        setIsClosing(false);
        setIsExpanding(false);

        // Remove the overflow hidden and the fixed height
        detailsRef.current.style.height = detailsRef.current.style.overflow = '';
    };

    return (
        <div
            className={`${styles['accordion-wrapper']}`}
            onClick={onLabelClick}
            aria-controls={id}
            aria-expanded="false"
            role="button"
            tabIndex={0}
        >
            <details ref={detailsRef as React.LegacyRef<HTMLDetailsElement>} className={`${styles['accordion']}`}>
                <summary
                    tabIndex={-1}
                    role="decoration"
                    ref={labelRef as React.LegacyRef<HTMLElement>}
                    className={`${styles['label']}`}
                >
                    <Heading className={`${styles['heading']}`} semanticLevel={4} styledLevel={6}>
                        {label}
                    </Heading>
                </summary>

                <div id={id} ref={contentRef as React.LegacyRef<HTMLDivElement>} className={`${styles['content']}`}>
                    {children}
                </div>
            </details>
        </div>
    );
};

Accordion.test.ts

import { screen, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Accordion } from '../Accordion';
import { mockAnimationsApi } from 'jsdom-testing-mocks';

mockAnimationsApi();

describe('Accordion', () => {
    it('should render', () => {
        render(<Accordion label="Label">Content</Accordion>);
        expect(screen.getByText('Label')).toBeInTheDocument();
    });

    it('should open and close', async () => {
        render(<Accordion label="Label">Content</Accordion>);

        const wrapper = screen.getByRole('button');
        const details = screen.getByRole('group');

        expect(details.getAttribute('open')).toBeNull();

        await userEvent.click(wrapper);
        const animation = details.getAnimations(); // <!-- Always empty array -->

        expect(details.getAttribute('open')).not.toBeNull();
    });
});

ResizeObserver should fire the onResize handler when ro.observe(elem) is called

Hello,

Thanks for this lib! It's awesome!

The issue

I noticed one issue - per the whatwg spec:

Observation will fire when observation starts if Element is being rendered, and Element’s size is not 0,0.

Here's a quick example in the Chrome debugger:

  1. Create resizeobserver
  2. observe an element that's not in the DOM, no resize event fired
  3. observe an element in the DOM (has a size already), resize event fired immediately.

2022-06-27T16_10_480x257_screenshot

You can see this (I think... I don't speak C++) in Chromium here: https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/resize_observer/resize_observer.cc;l=97?q=ResizeObserver&ss=chromium

Possible solution:

Can we extract the code from resize (here) that looks up sizes and call it as well from observe?

Further notes

I'm not sure if these are bugs as well, but:

  1. I don't think the onResize handlers should be called if the elements size is (0,0)
  2. I don't think the onResize handler should be called on an element if the element is not present in the DOM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.