File size: 9,497 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
 * @jest-environment jsdom
 */

import flows from 'calypso/signup/config/flows';
import {
	canResumeFlow,
	getCompletedSteps,
	getValidPath,
	getStepName,
	getFlowName,
	getFilteredSteps,
	isPlanSelectionAvailableLaterInFlow,
} from '../utils';

jest.mock( 'calypso/signup/config/flows-pure', () => ( {
	generateFlows: () => require( './fixtures/flows' ).default,
} ) );

describe( 'utils', () => {
	const defaultFlowName = flows.defaultFlowName;

	describe( 'getStepName', () => {
		test( 'should find the step name in either the stepName or flowName fragment', () => {
			expect( getStepName( { stepName: 'user' } ) ).toBe( 'user' );
			expect( getStepName( { flowName: 'user' } ) ).toBe( 'user' );
		} );

		test( 'should return undefined if no step name is found', () => {
			expect( getStepName( { flowName: 'account' } ) ).toBeUndefined();
		} );
	} );

	describe( 'getFlowName', () => {
		test( 'should find the flow name in the flowName fragment if present', () => {
			expect( getFlowName( { flowName: 'other' } ) ).toBe( 'other' );
		} );

		test( 'should return the default flow if the flow is missing', () => {
			expect( getFlowName( {} ) ).toBe( defaultFlowName );
		} );
	} );

	describe( 'getFilteredSteps', () => {
		describe( 'when the given flow is found in the config', () => {
			const exampleFlowName = 'onboarding';

			describe( 'when there are a number of steps in the progress state', () => {
				describe( 'and some of them match that flow', () => {
					const userStep = { stepName: 'user' };
					const siteTypeStep = { stepName: 'site-type' };
					const someOtherStep = { stepName: 'some-other-step' };
					const exampleSteps = {
						user: userStep,
						'site-type': siteTypeStep,
						'some-other-step': someOtherStep,
					};

					const result = getFilteredSteps( exampleFlowName, exampleSteps );

					test( 'it returns an array', () => {
						expect( Array.isArray( result ) ).toBe( true );
					} );

					test( 'it should return only the step objects that match the flow', () => {
						expect( result ).toEqual( [ userStep, siteTypeStep ] );
					} );
				} );

				describe( 'but none of them match that flow', () => {
					const exampleSteps = {
						'some-step': { stepName: 'some-step' },
						'some-other-step': { stepName: 'some-other-step' },
					};
					const result = getFilteredSteps( exampleFlowName, exampleSteps );

					test( 'it should return an empty array', () => {
						expect( result ).toHaveLength( 0 );
						expect( Array.isArray( result ) ).toBe( true );
					} );
				} );
			} );

			describe( 'when there are no steps in the progress state', () => {
				const result = getFilteredSteps( exampleFlowName, {} );

				test( 'it should return an empty array', () => {
					expect( result ).toHaveLength( 0 );
					expect( Array.isArray( result ) ).toBe( true );
				} );
			} );
		} );

		describe( 'when the given flow is not found in the config', () => {
			const exampleFlowName = 'some-bad-flow';
			const exampleSteps = {
				user: { stepName: 'user' },
				'site-type': { stepName: 'site-type' },
			};
			const result = getFilteredSteps( exampleFlowName, exampleSteps );

			test( 'it should return an empty array', () => {
				expect( result ).toHaveLength( 0 );
				expect( Array.isArray( result ) ).toBe( true );
			} );
		} );
	} );

	describe( 'getValidPath', () => {
		test( 'should redirect to the default if no flow is present', () => {
			expect( getValidPath( {} ) ).toBe( '/start/user' );
		} );

		test( 'should redirect to the current flow default if no step is present', () => {
			expect( getValidPath( { flowName: 'account' } ) ).toBe( '/start/account/user' );
		} );

		test( 'should redirect to the default flow if the flow is the default', () => {
			expect( getValidPath( { flowName: defaultFlowName } ) ).toBe( '/start/user' );
		} );

		test( 'should redirect invalid steps to the default flow if no flow is present', () => {
			expect(
				getValidPath( {
					flowName: 'foo',
					lang: 'fr',
				} )
			).toBe( '/start/user/fr' );
		} );

		test( 'should preserve a step section name and redirect to the default flow', () => {
			expect(
				getValidPath( {
					flowName: 'foo',
					stepName: 'abc',
					lang: 'fr',
				} )
			).toBe( '/start/user/abc/fr' );
		} );

		test( 'should redirect missing steps to the current flow default', () => {
			expect(
				getValidPath( {
					flowName: 'account',
					lang: 'fr',
				} )
			).toBe( '/start/account/user/fr' );
		} );

		test( 'should handle arbitrary step section names', () => {
			const randomStepSectionName = 'random-step-section-' + Math.random();

			expect(
				getValidPath( {
					flowName: 'account',
					stepName: 'user',
					stepSectionName: randomStepSectionName,
					lang: 'fr',
				} )
			).toBe( `/start/account/user/${ randomStepSectionName }/fr` );
		} );

		test( 'should handle arbitrary step section names in the default flow', () => {
			const randomStepSectionName = 'random-step-section-' + Math.random();

			expect(
				getValidPath( {
					stepName: 'user',
					stepSectionName: randomStepSectionName,
					lang: 'fr',
				} )
			).toBe( `/start/user/${ randomStepSectionName }/fr` );
		} );
	} );

	describe( 'getCompletedSteps', () => {
		const mixedFlowsSignupProgress = [
			{ stepName: 'user', lastKnownFlow: 'onboarding', status: 'completed' },
			{ stepName: 'site-type', lastKnownFlow: 'onboarding', status: 'completed' },
			{ stepName: 'site-topic', lastKnownFlow: 'onboarding-blog', status: 'completed' },
			{ stepName: 'site-title', lastKnownFlow: 'onboarding-blog', status: 'completed' },
			{ stepName: 'domains', lastKnownFlow: 'onboarding-blog', status: 'pending' },
			{ stepName: 'plans', lastKnownFlow: 'onboarding-blog', status: 'pending' },
		];
		const singleFlowSignupProgress = [
			{ stepName: 'user', lastKnownFlow: 'onboarding', status: 'completed' },
			{ stepName: 'site-type', lastKnownFlow: 'onboarding', status: 'completed' },
			{ stepName: 'site-topic-with-preview', lastKnownFlow: 'onboarding', status: 'completed' },
			{ stepName: 'site-title-with-preview', lastKnownFlow: 'onboarding', status: 'completed' },
			{ stepName: 'domains-with-preview', lastKnownFlow: 'onboarding', status: 'pending' },
			{ stepName: 'plans', lastKnownFlow: 'onboarding', status: 'pending' },
		];

		test( 'step names should match steps of a particular flow given progress with mixed flows', () => {
			const completedSteps = getCompletedSteps( 'onboarding-blog', mixedFlowsSignupProgress );
			const stepNames = completedSteps.map( ( step ) => step.stepName );

			expect( stepNames ).toStrictEqual( flows.getFlow( 'onboarding-blog' ).steps );
		} );

		test( 'should not match steps of a flow given progress with mixed flows and `shouldMatchFlowName` flag', () => {
			const completedSteps = getCompletedSteps( 'onboarding-blog', mixedFlowsSignupProgress, {
				shouldMatchFlowName: true,
			} );
			const filteredOnboardingBlogSteps = mixedFlowsSignupProgress.filter(
				( step ) => step.lastKnownFlow === 'onboarding-blog'
			);
			const stepNames = completedSteps.map( ( step ) => step.stepName );

			expect( stepNames ).not.toStrictEqual( flows.getFlow( 'onboarding-blog' ).steps );
			expect( completedSteps ).toStrictEqual( filteredOnboardingBlogSteps );
		} );

		test( 'should match steps of a flow given progress with single flow and `shouldMatchFlowName` flag', () => {
			const completedSteps = getCompletedSteps( 'onboarding', singleFlowSignupProgress, {
				shouldMatchFlowName: true,
			} );
			const stepNames = completedSteps.map( ( step ) => step.stepName );

			expect( stepNames ).toStrictEqual( flows.getFlow( 'onboarding' ).steps );
			expect( completedSteps ).toStrictEqual( singleFlowSignupProgress );
		} );
	} );

	describe( 'canResumeFlow', () => {
		test( 'should return true when given flow matches progress state', () => {
			const signupProgress = [ { stepName: 'site-type', lastKnownFlow: 'onboarding' } ];
			const canResume = canResumeFlow( 'onboarding', signupProgress );

			expect( canResume ).toBe( true );
		} );

		test( 'should return false when given flow does not match progress state', () => {
			const signupProgress = [ { stepName: 'site-type', lastKnownFlow: 'onboarding' } ];
			const canResume = canResumeFlow( 'onboarding-blog', signupProgress );

			expect( canResume ).toBe( false );
		} );

		test( 'should return false when flow sets disallowResume', () => {
			const signupProgress = [ { stepName: 'site-type', lastKnownFlow: 'disallow-resume' } ];
			const canResume = canResumeFlow( 'disallow-resume', signupProgress );

			expect( canResume ).toBe( false );
		} );

		test( 'should return false when progress state is empty', () => {
			const signupProgress = [];
			const canResume = canResumeFlow( 'onboarding', signupProgress );

			expect( canResume ).toBe( false );
		} );
	} );

	describe( 'isPlanSelectionAvailableLaterInFlow', () => {
		const defaultFlowSteps = [ 'user', 'domains', 'plans' ];

		test( 'should return true when given flow contains "plans" step', () => {
			const isPlanSelectionAvailable = isPlanSelectionAvailableLaterInFlow( defaultFlowSteps );

			expect( isPlanSelectionAvailable ).toBe( true );
		} );

		test( 'should return false when given flow doesn`t contain "plans" step', () => {
			const flowSteps = [ 'user', 'domains' ];
			const isPlanSelectionAvailable = isPlanSelectionAvailableLaterInFlow( flowSteps );

			expect( isPlanSelectionAvailable ).toBe( false );
		} );
	} );
} );