|
|
import { intersection, isEmpty, keys } from 'lodash'; |
|
|
import flows from '../flows'; |
|
|
import { generateFlows } from '../flows-pure'; |
|
|
import { getStepModuleMap } from '../step-components'; |
|
|
import steps from '../steps'; |
|
|
import { generateSteps } from '../steps-pure'; |
|
|
|
|
|
jest.mock( 'calypso/lib/signup/step-actions', () => ( {} ) ); |
|
|
jest.mock( 'calypso/components/file-picker/component-file-picker', () => <div></div> ); |
|
|
jest.mock( 'calypso/lib/explat', () => { |
|
|
() => { |
|
|
return [ false, null ]; |
|
|
}; |
|
|
} ); |
|
|
jest.mock( '@automattic/calypso-config', () => ( { |
|
|
isEnabled: () => true, |
|
|
} ) ); |
|
|
|
|
|
describe( 'index', () => { |
|
|
|
|
|
test( 'should not have overlapping step/flow names', () => { |
|
|
const overlappingNames = intersection( keys( steps ), keys( flows.getFlows() ) ); |
|
|
|
|
|
if ( ! isEmpty( overlappingNames ) ) { |
|
|
throw new Error( |
|
|
'Step and flow names must be unique. The following names are used as both step and flow names: [' + |
|
|
overlappingNames + |
|
|
'].' |
|
|
); |
|
|
} |
|
|
} ); |
|
|
|
|
|
test( 'All step components should have a step definition', () => { |
|
|
const stepModuleMap = getStepModuleMap(); |
|
|
const stepNames = new Set( Object.keys( stepModuleMap ) ); |
|
|
const allStepDefinitions = generateSteps(); |
|
|
|
|
|
stepNames.forEach( ( stepName ) => { |
|
|
expect( allStepDefinitions ).toHaveProperty( stepName ); |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
test( 'All step components should have a step implementation', async () => { |
|
|
const stepModuleMap = getStepModuleMap(); |
|
|
const allModules = new Set( Object.values( stepModuleMap ) ); |
|
|
const nonExistentModules = []; |
|
|
await Promise.all( |
|
|
Array.from( allModules ).map( async ( module ) => { |
|
|
const path = `calypso/signup/steps/${ module }`; |
|
|
try { |
|
|
await import( path ); |
|
|
} catch ( e ) { |
|
|
if ( e.message.includes( 'Cannot find module' ) ) { |
|
|
console.error( e ); |
|
|
nonExistentModules.push( path ); |
|
|
} |
|
|
} |
|
|
} ) |
|
|
); |
|
|
|
|
|
expect( nonExistentModules ).toEqual( [] ); |
|
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test( 'All step definitions should have a step component mapping', () => { |
|
|
const stepModuleMap = getStepModuleMap(); |
|
|
const allStepDefinitions = generateSteps(); |
|
|
Object.keys( allStepDefinitions ).forEach( ( stepName ) => { |
|
|
expect( stepModuleMap ).toHaveProperty( stepName ); |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test( 'All steps should have the same dependencies provided', () => { |
|
|
const stepModuleMap = getStepModuleMap(); |
|
|
const moduleStepMap = {}; |
|
|
Object.entries( stepModuleMap ).forEach( ( [ step, module ] ) => { |
|
|
if ( ! moduleStepMap[ module ] ) { |
|
|
moduleStepMap[ module ] = []; |
|
|
} |
|
|
moduleStepMap[ module ].push( step ); |
|
|
} ); |
|
|
const allStepDefinitions = generateSteps(); |
|
|
|
|
|
Object.values( moduleStepMap ) |
|
|
.filter( ( moduleRelatedSteps ) => moduleRelatedSteps.length > 1 ) |
|
|
.forEach( ( moduleRelatedSteps ) => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let allDependenciesProvidedForModule = new Set(); |
|
|
moduleRelatedSteps.forEach( ( step ) => { |
|
|
( allStepDefinitions[ step ]?.providesDependencies ?? [] ).forEach( ( d ) => |
|
|
allDependenciesProvidedForModule.add( d ) |
|
|
); |
|
|
} ); |
|
|
allDependenciesProvidedForModule = Array.from( allDependenciesProvidedForModule ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
moduleRelatedSteps |
|
|
.filter( ( step ) => allStepDefinitions[ step ]?.providesDependencies ) |
|
|
.forEach( ( step ) => { |
|
|
const stepProvidedDependencies = allStepDefinitions[ step ].providesDependencies; |
|
|
|
|
|
expect( { step, deps: stepProvidedDependencies } ).toEqual( { |
|
|
step, |
|
|
deps: expect.arrayContaining( allDependenciesProvidedForModule ), |
|
|
} ); |
|
|
} ); |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
test( 'there should be no unused steps', () => { |
|
|
const flowDefinitions = generateFlows(); |
|
|
const allStepDefinitions = generateSteps(); |
|
|
const definedSteps = new Set( Object.keys( allStepDefinitions ) ); |
|
|
|
|
|
Object.values( flowDefinitions ).forEach( ( flow ) => { |
|
|
flow.steps.forEach( ( step ) => definedSteps.delete( step ) ); |
|
|
} ); |
|
|
|
|
|
|
|
|
definedSteps.delete( 'site' ); |
|
|
|
|
|
|
|
|
|
|
|
definedSteps.delete( 'user' ); |
|
|
|
|
|
expect( definedSteps ).toEqual( new Set() ); |
|
|
} ); |
|
|
} ); |
|
|
|