File size: 5,491 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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', () => {
	// eslint-disable-next-line jest/expect-expect
	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( [] );
	} );

	/**
	 * Before cleaning up a step, make sure to investigate if the step is 'phantom' submitted inside another step.
	 * Steps can be submitted without a step component or it being included in a flow.
	 * Eg: https://github.com/Automattic/wp-calypso/pull/81778
	 */
	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 );
		} );
	} );

	/***
	 * Not having the same dependencies has been a source of bugs in the signup framework, where the flow would fail in production.
	 * - p1691481660481399-slack-C04U5A26MJB
	 * - p1689619382168349-slack-C02FMH4G8
	 * - https://github.com/Automattic/wp-calypso/pull/17523
	 *
	 * This tests makes sure that any change that introduces a dependency
	 * is also introduced in all linked steps to a given module.
	 * Eg: if a dependency was added to the `domain-only` step, then then same dependency
	 * needs to be added to all steps that are linked by the module `domains` ( as mapped in `calypso/signup/config/step-components` ).
	 * This dependency can be added as an optionalDependency if the dependency is provided conditionally
	 *
	 * Read more: p4TIVU-aK6-p2
	 */
	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 ) => {
				/**
				 * Filter out a unique list of all dependencies
				 * provided by all steps that are linked to a module
				 */
				let allDependenciesProvidedForModule = new Set();
				moduleRelatedSteps.forEach( ( step ) => {
					( allStepDefinitions[ step ]?.providesDependencies ?? [] ).forEach( ( d ) =>
						allDependenciesProvidedForModule.add( d )
					);
				} );
				allDependenciesProvidedForModule = Array.from( allDependenciesProvidedForModule );

				/**
				 * Go over all steps that are linked to a module
				 * and make sure that they all provide the same dependencies
				 */
				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 ) );
		} );

		// Remove the `site` step manually since it is used in tests.
		definedSteps.delete( 'site' );

		// Do not consider the user step as deprecated since there is still a config flag
		// deciding whether user-social or user is used.
		definedSteps.delete( 'user' );

		expect( definedSteps ).toEqual( new Set() );
	} );
} );