|
| 1 | +// @ts-nocheck |
| 2 | +// @ts-ignore |
| 3 | +import * as JustMyLuck from 'just-my-luck' |
| 4 | +import faker from 'faker' |
| 5 | +import { template, keys, reduce, templateSettings } from 'lodash' |
| 6 | +import combineProperties from 'combine-properties' |
| 7 | + |
| 8 | +templateSettings.interpolate = /{{([\s\S]+?)}}/g |
| 9 | + |
| 10 | +let jml |
| 11 | +const setupSeeds = () => { |
| 12 | + const seed = 2 |
| 13 | + |
| 14 | + faker.seed(seed) |
| 15 | + jml = new JustMyLuck(JustMyLuck.MersenneTwister(seed)) |
| 16 | +} |
| 17 | + |
| 18 | +setupSeeds() |
| 19 | + |
| 20 | +beforeEach(() => setupSeeds) |
| 21 | + |
| 22 | +/** |
| 23 | + * Component Naming Fixtures |
| 24 | + */ |
| 25 | +export const modifiers = [ |
| 26 | + 'Async', |
| 27 | + 'Dynamic', |
| 28 | + 'Static', |
| 29 | + 'Virtual', |
| 30 | + 'Lazy', |
| 31 | +] |
| 32 | + |
| 33 | +export const domainModels = [ |
| 34 | + 'Person', |
| 35 | + 'Product', |
| 36 | + 'Spec', |
| 37 | + 'Settings', |
| 38 | + 'Account', |
| 39 | + 'Login', |
| 40 | + 'Logout', |
| 41 | + 'Launchpad', |
| 42 | + 'Wizard', |
| 43 | +] |
| 44 | + |
| 45 | +export const componentNames = [ |
| 46 | + 'List', |
| 47 | + 'Table', |
| 48 | + 'Header', |
| 49 | + 'Footer', |
| 50 | + 'Button', |
| 51 | + 'Cell', |
| 52 | + 'Row', |
| 53 | + 'Skeleton', |
| 54 | + 'Loader', |
| 55 | + 'Layout', |
| 56 | +] |
| 57 | + |
| 58 | +export const specPattern = ['.spec', '_spec'] |
| 59 | + |
| 60 | +export const fileExtension = ['.tsx', '.jsx', '.ts', '.js'] |
| 61 | + |
| 62 | +export const directories = { |
| 63 | + rootDedicated: template('tests'), |
| 64 | + rootSrc: template('src'), |
| 65 | + monorepo: template('packages/{{component}}/test'), |
| 66 | + jestRoot: template('__test__'), |
| 67 | + jestNestedLib: template('lib/{{component}}{{component2}}/__test__'), |
| 68 | + dedicatedNested: template('lib/{{component}}/test'), |
| 69 | + jestNested: template('src/{{component}}/__test__'), |
| 70 | + componentsNested: template('src/components/{{component}}'), |
| 71 | + componentsFlat: template('src/{{component}}'), |
| 72 | + viewsFlat: template('src/views'), |
| 73 | + frontendFlat: template('frontend'), |
| 74 | + frontendComponentsFlat: template('frontend/components'), |
| 75 | +} |
| 76 | + |
| 77 | +const nameTemplates = { |
| 78 | + // Business Logic Components |
| 79 | + longDomain: template(`{{prefix}}{{modifier}}{{domain}}{{component}}`), |
| 80 | + longDomain2: template(`{{prefix}}{{domain}}{{component}}{{component2}}`), |
| 81 | + |
| 82 | + // App Components |
| 83 | + page1: template(`{{domain}}Page`), |
| 84 | + layout: template(`{{domain}}Layout`), |
| 85 | + |
| 86 | + presentationalShort: template(`Base{{component}}`), |
| 87 | + presentationalLong: template(`Base{{component}}{{component2}}`), |
| 88 | + medium1: template(`{{prefix}}{{modifier}}{{component}}`), |
| 89 | + medium2: template(`{{prefix}}{{component}}{{component2}}`), |
| 90 | + short: template(`{{prefix}}{{component}}`), |
| 91 | +} |
| 92 | + |
| 93 | +const prefixes = ['I', 'V', 'Cy', null] |
| 94 | + |
| 95 | +export const componentNameGenerator = (options: { template: any, omit: any, overrides: any } = { template: nameTemplates.medium1, omit: [], overrides: {} }) => { |
| 96 | + const withoutValues = reduce(options.omit, (acc, v) => { |
| 97 | + acc[v] = null |
| 98 | + |
| 99 | + return acc |
| 100 | + }, {}) |
| 101 | + |
| 102 | + const components = jml.pickCombination(componentNames, 2) |
| 103 | + const defaultOptions = { |
| 104 | + modifier: jml.pick(modifiers), |
| 105 | + domain: jml.pick(domainModels), |
| 106 | + prefix: jml.pick(prefixes), |
| 107 | + component: components[0], |
| 108 | + component2: components[1], |
| 109 | + } |
| 110 | + |
| 111 | + return options.template({ |
| 112 | + ...defaultOptions, |
| 113 | + ...withoutValues, |
| 114 | + ...options.overrides, |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +const allRandomComponents = combineProperties({ |
| 119 | + domain: domainModels, |
| 120 | + modifier: modifiers, |
| 121 | + prefix: prefixes, |
| 122 | + component: componentNames, |
| 123 | + component2: componentNames, |
| 124 | + fileExtension, |
| 125 | + specPattern, |
| 126 | + directory: keys(directories), |
| 127 | +}) |
| 128 | + |
| 129 | +export const randomComponents = (n = 200) => { |
| 130 | + return faker.random.arrayElements(allRandomComponents, n).map((d) => { |
| 131 | + const name = componentNameGenerator({ |
| 132 | + overrides: d, |
| 133 | + template: faker.random.objectElement(nameTemplates), |
| 134 | + }) |
| 135 | + |
| 136 | + const gitFileState = jml.pick(['modified', 'unmodified', 'added', 'deleted']) |
| 137 | + |
| 138 | + return { |
| 139 | + componentName: name, |
| 140 | + relativePath: directories[d.directory](d), |
| 141 | + specExtension: d.specPattern, |
| 142 | + fileExtension: d.fileExtension, |
| 143 | + name: `${name}${d.specPattern}${d.fileExtension}`, |
| 144 | + id: faker.datatype.uuid(), |
| 145 | + gitInfo: { |
| 146 | + comitter: gitFileState ? faker.internet.userName() : undefined, |
| 147 | + timeAgo: gitFileState ? faker.datatype.datetime() : undefined, |
| 148 | + fileState: gitFileState, |
| 149 | + }, |
| 150 | + } |
| 151 | + }, n) |
| 152 | +} |
0 commit comments