UNPKG

wix-style-react

Version:
102 lines (83 loc) 3.23 kB
const path = require('path'); const fs = require('fs'); const listAllComponents = require('./list-all-components'); const TESTKIT_DEFINITIONS = require('./testkit-definitions'); const shouldCreateExport = name => TESTKIT_DEFINITIONS[name] ? ['noTestkit', 'manualExport'].every( property => !TESTKIT_DEFINITIONS[name][property], ) : true; const wrapItemWithFunction = (fn, item) => `${fn}(${item})`; // load() is function included during build time. It comes from test/generate-testkit-exports/templates/load.js // It is a helper that `require`s given path and extracts export (default or only one found) const wrapWithLoad = path => wrapItemWithFunction('load', `'${path}'`); const pathResolve = (...a) => path.resolve(__dirname, ...a); const getExportableTestkits = ({ factoryCreator, uniFactoryCreator }) => [ ...listAllComponents({ cwd: path.resolve(__dirname, '..', '..', 'src') }), // TODO: AllComponents does not yet include all components, because there are nested folders that are treated as top level components ...Object.keys(TESTKIT_DEFINITIONS), ] .filter(shouldCreateExport) .reduce((testkits, name) => { const definition = TESTKIT_DEFINITIONS[name] || {}; const entryName = name[0].toLowerCase() + name.slice(1) + 'TestkitFactory'; const testkitEntry = wrapItemWithFunction( definition.unidriver ? uniFactoryCreator : factoryCreator, wrapWithLoad( definition.testkitPath || ['..', 'src', name, name + '.driver'].join('/'), ), ); testkits[entryName] = testkitEntry; return testkits; }, {}); const warningBanner = templatePath => [ '/* eslint-disable */', '/*', ` * DO NOT EDIT THIS FILE.`, ' * YOUR CHANGES WILL BE OVERWRITTEN.', ` * FILE IS BASED ON scripts/generate-testkit-exports/templates/${path.basename( templatePath, )}`, ` * AND GENERATED BY scripts/generate-testkit-exports`, ' */', ].join('\n'); const generateTestkits = ({ templatePath, outputPath, factoryCreator, uniFactoryCreator, }) => { const loadUtilPath = pathResolve('templates', 'load.js'); const originalSource = fs.readFileSync(templatePath, 'utf8'); const loadUtilSource = fs.readFileSync(loadUtilPath, 'utf8'); const testkitImportsSource = Object.entries( getExportableTestkits({ factoryCreator, uniFactoryCreator }), ) .map(([name, entry]) => `export const ${name} = ${entry};`) .join('\n'); const source = [ warningBanner(templatePath), originalSource, loadUtilSource, testkitImportsSource, ].join('\n'); fs.writeFileSync(outputPath, source); }; // enzyme testkit exports generateTestkits({ templatePath: pathResolve('templates', 'enzyme.js'), outputPath: pathResolve('..', '..', 'testkit', 'enzyme.js'), factoryCreator: 'enzymeTestkitFactoryCreator', uniFactoryCreator: 'enzymeUniTestkitFactoryCreator', }); // vanilla (ReactTestUtils) testkit exports generateTestkits({ templatePath: pathResolve('templates', 'vanilla.js'), outputPath: pathResolve('..', '..', 'testkit', 'index.js'), factoryCreator: 'testkitFactoryCreator', uniFactoryCreator: 'uniTestkitFactoryCreator', });