All files / test/utils test-examples.ts

100% Statements 11/11
100% Branches 7/7
100% Functions 2/2
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                                    65x 65x 1x   64x 166x   7x       159x     159x   159x 159x   159x      
import React, { FunctionComponent, ComponentClass } from "react";
import * as fs from "fs";
import * as path from "path";
import { render } from "@testing-library/react";
 
export interface TestExampleOverrides {
  [exampleFileName: string]: (
    Component: FunctionComponent<unknown> | ComponentClass<unknown>
  ) => void | Promise<void>;
}
 
/**
 * 测试组件的所有 Example
 */
export function testExamples(
  dirname: string,
  overrides: TestExampleOverrides = {}
) {
  const exampleDir = path.resolve(dirname, "../_example");
  if (!fs.existsSync(exampleDir)) {
    return;
  }
  for (const exampleFilename of fs.readdirSync(exampleDir)) {
    if (!/Example\.jsx$/.test(exampleFilename)) {
      // eslint-disable-next-line no-continue
      continue;
    }
 
    // eslint-disable-next-line
    const Example = require(path.join(exampleDir, exampleFilename)).default;
 
    const runner =
      overrides[exampleFilename] ||
      (() => {
        const { asFragment } = render(React.createElement(Example));
        expect(asFragment()).toMatchSnapshot();
      });
    test(exampleFilename, runner);
  }
}