storybook/code/renderers/web-components/src/docs/web-components-properties.test.ts
2023-02-16 09:13:06 +08:00

68 lines
2.3 KiB
TypeScript

import 'jest-specific-snapshot';
import path from 'path';
import fs from 'fs';
import tmp from 'tmp';
import { sync as spawnSync } from 'cross-spawn';
// File hierarchy:
// __testfixtures__ / some-test-case / input.*
const inputRegExp = /^input\..*$/;
const runWebComponentsAnalyzer = (inputPath: string) => {
const { name: tmpDir, removeCallback } = tmp.dirSync();
const customElementsFile = `${tmpDir}/custom-elements.json`;
spawnSync(
path.join(__dirname, '../../../../node_modules/.bin/wca'),
['analyze', inputPath, '--outFile', customElementsFile],
{
stdio: 'ignore',
shell: true,
}
);
const output = fs.readFileSync(customElementsFile, 'utf8');
try {
removeCallback();
} catch (e) {
//
}
return output;
};
describe('web-components component properties', () => {
// we need to mock lit and dynamically require custom-elements
// because lit is distributed as ESM not CJS
// https://github.com/Polymer/lit-html/issues/516
jest.mock('lit', () => {});
jest.mock('lit/directive-helpers.js', () => {});
// eslint-disable-next-line global-require
const { extractArgTypesFromElements } = require('./custom-elements');
const fixturesDir = path.join(__dirname, '__testfixtures__');
fs.readdirSync(fixturesDir, { withFileTypes: true }).forEach((testEntry) => {
if (testEntry.isDirectory()) {
const testDir = path.join(fixturesDir, testEntry.name);
const testFile = fs.readdirSync(testDir).find((fileName) => inputRegExp.test(fileName));
if (testFile) {
it(`${testEntry.name}`, () => {
const inputPath = path.join(testDir, testFile);
// snapshot the output of wca
const customElementsJson = runWebComponentsAnalyzer(inputPath);
const customElements = JSON.parse(customElementsJson);
customElements.tags.forEach((tag: any) => {
// eslint-disable-next-line no-param-reassign
tag.path = 'dummy-path-to-component';
});
expect(customElements).toMatchSpecificSnapshot(
path.join(testDir, 'custom-elements.snapshot')
);
// snapshot the properties
const properties = extractArgTypesFromElements('input', customElements);
expect(properties).toMatchSpecificSnapshot(path.join(testDir, 'properties.snapshot'));
});
}
}
});
});