@fluentui/react-conformance
TypeScript icon, indicating that this package has built-in type declarations

0.19.0 • Public • Published

@fluentui/react-conformance

A tool used to run standardized tests which follow Fluent UI React's component guidelines. It also can be extended and allows for adding your own conformance tests.

Configuration

isConformant ( base configuration )

Add isConformant within your package and configure any globally applied test options.

my-proj/
├─ common/
│  ├─ isConformant.ts 👈
├─ src/
├─ node_modules/
├─ package.json
import { isConformant as baseIsConformant } from '@fluentui/react-conformance';
import type { IsConformantOptions } from '@fluentui/react-conformance';

export function isConformant<TProps = {}>(
  testInfo: Omit<IsConformantOptions<TProps>, 'componentPath'> & { componentPath?: string },
) {
  const defaultOptions: Partial<IsConformantOptions<TProps>> = {
    componentPath: require.main?.filename.replace('.test', ''),
    // 👆 Put any required test options here ( ex: componentPath, asPropHandlesRef, ... )
  };

  baseIsConformant(defaultOptions, testInfo);
}

isConformant ( running tests )

Within your component's test file:

my-proj/
├─ common/
├─ src/
│  ├─ components
│  │  ├─ Foo
│  │  │  ├─ ...
│  │  │  ├─ Foo.test 👈
├─ node_modules/
├─ package.json

Import the isConformant file that you just created:

import { isConformant } from '../../common/isConformant';

describe('Foo', () => {
  isConformant({
    Component: Foo,
    displayName: 'Foo',
    disabledTests: [],
    // 👆 For tests that don't fit the guidelines of your component you can disable them.
  });
});

isConformant with React Portals

By default isConformant inspects a component's immediate parent container. Because React Portals are typically rendered outside this container components using Portals will fail conformance. For example the component-has-static-classnames-object tests inspect the rendered DOM for certain class names but, with default settings, will fail for anything rendered into a Portal.

Portals can be inspected by providing a getTargetElement function to isConformant.

// Assume that `Foo` is a component that renders a Portal.
// It takes a prop called `idForPortal` that renders the
// provided id in the Portal, allowing it to be looked up
// via `getPortalElement()`.

const getPortalElement = (result, attr) => {
  return result.baseElement.querySelector("#portal-id");
};

describe('Foo', () => {
  isConformant({
    Component: Foo,
    displayName: 'Foo'
    requiredProps: { idForPortal: "portal-id" },
    getTargetElement: getPortalElement
  });
});

Readme

Keywords

none

Package Sidebar

Install

npm i @fluentui/react-conformance

Weekly Downloads

345

Version

0.19.0

License

MIT

Unpacked Size

249 kB

Total Files

31

Last publish

Collaborators

  • sopranopillow
  • microsoft1es
  • justslone
  • chrisdholt
  • miroslavstastny
  • levithomason
  • uifabricteam
  • uifrnbot
  • dzearing
  • layershifter
  • ling1726
  • travisspomer