Skip to content

Replace jsdom with happy-dom testing environment to enable Modal tests (#461)#545

Merged
bedrich-schindler merged 1 commit into
bc/461from
bc/461-2
Jan 21, 2025
Merged

Replace jsdom with happy-dom testing environment to enable Modal tests (#461)#545
bedrich-schindler merged 1 commit into
bc/461from
bc/461-2

Conversation

@bedrich-schindler

@bedrich-schindler bedrich-schindler commented May 31, 2024

Copy link
Copy Markdown
Contributor

This is work-in-progress pull request!

I've replaced jsdom with happy-dom testing environment and with little changes (typically because of use of incorrect jest functions ) and Modal tests with <dialog> works almost as expected. But for some reason, there are 4 tests in Grid.test.jsx that does not work. TBH, I don't know why.

~~I debbuged test functions, respectively toHaveStyle which is part of node_modules/@testing-library/jest-dom/dist/matchers-342a062d.js. toHaveStyle internally uses isSubset function`. I edited function to be able to debug it step-by-step:

function isSubset(styles, computedStyle) {
  return (
    !!Object.keys(styles).length &&
    Object.entries(styles).every(([prop, value]) => {
      const isCustomProperty = prop.startsWith('--');
      const spellingVariants = [prop];
      if (!isCustomProperty) spellingVariants.push(prop.toLowerCase());

      return spellingVariants.some(
        name => {
          const a = computedStyle[name];
          const b = computedStyle.getPropertyValue(name);
          const v = [];
          for (let i = 0; i < computedStyle.length; i++) {
            v.push(computedStyle.item(i));
          }

          return a === value ||
            b === value;
        },
      )
    })
  )
}

And for some reason, not all CSS properties are part of computed styles (variable v) and that's the reason why the tests fails.

Can anybody look at it? Can we discuss if this is the right way? Because without happy-dom, we won't be able to test our Modal component which is problem guys from LMC's Spirit Design System encountered. (cc @adamkudrna @crishpeen @literat).

If we want to have Modal tested, we either skip those tests for rowGap and columnGap or we can test it different way, for example using .outerHTML.contains(<css>). It is not perfect nor good, but it works at least.

@bedrich-schindler

bedrich-schindler commented May 31, 2024

Copy link
Copy Markdown
Contributor Author

The problem is in var() values. For exact values it works as expected.

See: jsdom/jsdom#3299

@bedrich-schindler

bedrich-schindler commented Jun 1, 2024

Copy link
Copy Markdown
Contributor Author

Because of missing support for var() in testing environment, I have updated Grid.test.jsx to compare styles using outerHTML.includes(<CSS>) - the only way how to test it now.

It ready for review.

A mentioned guys from Spirit team for inspiration as their Modal is missing tests.

@adamkudrna adamkudrna left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As far as I can understand what's going on, a ✅ for me.

],
[
{ size: 'small' },
(rootElement) => expect(within(rootElement).getByRole('presentation')).toHaveClass('isRootSizeSmall'),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It could still be getByRole('dialog'), couldn't it?

Comment on lines +56 to +57
// While <dialog> component uses built-in browser dialog functionality for closing the dialog, we preserve this
// functionality due to lack of support for <dialog> in jsdom and happy-dom testing environments.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we have any jsdom/happy-dom issues to link to so we can remove this in future?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We don't. This is not only think that lacks support. I would write more about it later or I would speak about it on Monday.

Comment thread src/components/Modal/_hooks/useModalFocus.js
Comment thread package.json Outdated
Comment thread src/components/Grid/__tests__/Grid.test.jsx Outdated
Comment thread src/components/Grid/__tests__/Grid.test.jsx Outdated
assertFocus(el, true);
});

it('traps focus', async () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

HTML dialog handles focus trap manually, so there is no need to test it. What more, it does not work in testing environment as it is not implemented.

Comment thread src/components/Modal/_helpers/dialogOnKeyDownHandler.js
Comment thread src/components/Modal/_helpers/dialogOnKeyDownHandler.js
…` tests (#461)

`happy-dom` is used due to missing HtmlDialogElement support in `jest`.
Just to mention, `happy-dom` provides partial support for dialog element,
so not all test can be implemented.
@bedrich-schindler bedrich-schindler changed the title [WIP] Replace jsdom with happy-dom testing environment to enable Modal tests (#461) Replace jsdom with happy-dom testing environment to enable Modal tests (#461) Jan 21, 2025
@bedrich-schindler
bedrich-schindler merged commit 157b858 into bc/461 Jan 21, 2025
@bedrich-schindler
bedrich-schindler deleted the bc/461-2 branch January 21, 2025 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BC Breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants