chakra-ui icon indicating copy to clipboard operation
chakra-ui copied to clipboard

Storybook TypeError when using Checkbox with the defaultChecked prop

Open mustafa-turk opened this issue 5 years ago • 3 comments

🐛 Bug report

When using the Checkbox component in a story with the defaultChecked prop set, I get the following error from while doing snapshot testing with Storybook:

TypeError: The provided value is not of type 'Element'.

  at Object.exports.convert (node_modules/jsdom/lib/jsdom/living/generated/Element.js:26:9)
  at Window.getComputedStyle (node_modules/jsdom/lib/jsdom/browser/Window.js:644:19)
  at HTMLVisualElement.Object.<anonymous>.HTMLVisualElement.getComputedStyle (node_modules/framer-motion/dist/framer-motion.cjs.js:2121:23)
  at HTMLVisualElement.Object.<anonymous>.HTMLVisualElement.read (node_modules/framer-motion/dist/framer-motion.cjs.js:2007:34)
  at HTMLVisualElement.Object.<anonymous>.HTMLVisualElement.readNativeValue (node_modules/framer-motion/dist/framer-motion.cjs.js:2030:25)
  at checkTargetForNewValues (node_modules/framer-motion/dist/framer-motion.cjs.js:1824:95)
  at HTMLVisualElement.Object.<anonymous>.HTMLVisualElement.makeTargetAnimatable (node_modules/framer-motion/dist/framer-motion.cjs.js:2061:13)
  at animateTarget (node_modules/framer-motion/dist/framer-motion.cjs.js:3491:28)
  at getAnimation (node_modules/framer-motion/dist/framer-motion.cjs.js:3458:32)
  at animateVariant (node_modules/framer-motion/dist/framer-motion.cjs.js:3482:29)

💥 Steps to reproduce

  1. Setup Storybook
  2. Setup snapshot testing with Storybook
  3. Create a simple story using the Checkbox component with the defaultChecked property set to true
import React from "react";
import { Checkbox } from "@chakra-ui/react";

export default {
  title: "Checkbox"
};

export const Default = () => (
  <Checkbox defaultChecked={true} />
);

🧐 Expected behavior

Not getting a TypeError.

🌍 System information

Software Version(s)
Chakra UI 1.0.0
Operating System macOS

mustafa-turk avatar Jan 29 '21 14:01 mustafa-turk

Is there any way you could create a basic example repo with this set up? I'm not familiar with Storyshot.

with-heart avatar Jan 29 '21 16:01 with-heart

Is there any way you could create a basic example repo with this set up? I'm not familiar with Storyshot.

Here you go: https://github.com/mustafa-turk/chakra-story-example

To reproduce the error you can just run npx jest --watchAll which will then do snapshot testing with Storybook.

mustafa-turk avatar Feb 01 '21 10:02 mustafa-turk

@with-heart I managed to narrow it down such that storybook is not involved. Well, sort of. Storyshots invoke .unmount() on the rendered component after taking the snapshot, and it is that call that produces the error mentioned above. The following test case fails:

import React from "react";
import { Checkbox } from "@chakra-ui/react";
import reactTestRenderer from "react-test-renderer";

it("should render checkbox", () => {
  const tree = reactTestRenderer.create(<Checkbox defaultChecked={true} />);
  tree.unmount();
});

Without the defaultChecked, everything is fine.

The unmount triggers some useEffect within framer-motion, which invokes animateVariant. This is an async function that ultimately produces the error.

The component structure is as follows:

        in Unknown (created by ForwardRef(MotionComponent))
        in ForwardRef(MotionComponent) (created by CheckboxTransition)
        in PresenceChild (created by AnimatePresence)
        in AnimatePresence (created by CheckboxTransition)
        in CheckboxTransition (created by CheckboxIcon)
        in CheckboxIcon (created by Checkbox)
        in div (created by Styled(div))
        in Styled(div) (created by Checkbox)
        in label (created by Styled(label))
        in Styled(label) (created by Checkbox)
        in Checkbox (at checkbox.test.js:6)

Edit: it does not matter whether the test is async or not.

stimberm avatar Feb 03 '21 20:02 stimberm