File size: 4,017 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
/**
 * @jest-environment jsdom
 */
// @ts-nocheck - TODO: Fix TypeScript issues
import { clearSignupDestinationCookie } from 'calypso/signup/storageUtils';
import siteSetupFlow from '../flows/site-setup-flow/site-setup-flow';
import { STEPS } from '../internals/steps';
import { renderFlow } from './helpers';
// we need to save the original object for later to not affect tests from other files
const originalLocation = window.location;

// Mock the signup utils
jest.mock( 'calypso/signup/storageUtils', () => ( {
	clearSignupDestinationCookie: jest.fn(),
} ) );

describe( 'Site Setup Flow', () => {
	beforeAll( () => {
		Object.defineProperty( window, 'location', {
			value: { assign: jest.fn(), pathname: '' },
		} );
	} );

	afterAll( () => {
		Object.defineProperty( window, 'location', originalLocation );
	} );

	beforeEach( () => {
		jest.resetAllMocks();
	} );

	describe( 'when the current step is importListing', () => {
		it( 'redirects the user to the site-migration-import-or-content step when the origin param is set as site-migration-identify', async () => {
			const { runUseStepNavigationSubmit } = renderFlow( siteSetupFlow );

			runUseStepNavigationSubmit( {
				currentURL:
					'/some-path?origin=site-migration-identify&siteSlug=example.wordpress.com&siteId=123',
				currentStep: STEPS.IMPORT_LIST.slug,
				dependencies: {
					platform: 'wordpress',
				},
			} );

			expect( window.location.assign ).toHaveBeenCalledWith(
				'/setup/site-migration/site-migration-import-or-migrate?siteSlug=example.wordpress.com&siteId=123'
			);
		} );

		it( 'continues the regular flow when the origin param is not available', async () => {
			const { runUseStepNavigationSubmit } = renderFlow( siteSetupFlow );

			runUseStepNavigationSubmit( {
				currentStep: STEPS.IMPORT_LIST.slug,
				dependencies: {
					platform: 'wordpress',
				},
			} );

			expect( window.location.assign ).not.toHaveBeenCalledWith(
				expect.stringContaining( '/setup/site-migration/' )
			);
		} );
	} );

	//It is important because importReady and importListing are sharing the same logic
	describe( 'when the current step is not importReady', () => {
		it( 'ignores origin param', async () => {
			const { runUseStepNavigationSubmit } = renderFlow( siteSetupFlow );

			runUseStepNavigationSubmit( {
				currentURL:
					'/some-path?origin=site-migration-identify&siteSlug=example.wordpress.com&siteId=123',
				currentStep: STEPS.IMPORT_READY.slug,
				dependencies: {
					platform: 'wordpress',
				},
			} );

			expect( window.location.assign ).not.toHaveBeenCalledWith(
				expect.stringContaining( '/setup/site-migration/' )
			);
		} );
	} );

	describe( 'goBack', () => {
		it( 'redirects the user to site-migration flow when clicking back on importList step without backToFlow', () => {
			const { runUseStepNavigationGoBack } = renderFlow( siteSetupFlow );

			runUseStepNavigationGoBack( {
				currentStep: STEPS.IMPORT_LIST.slug,
			} );

			expect( window.location.assign ).toHaveBeenCalledWith(
				expect.stringContaining( '/setup/site-migration' )
			);
		} );

		it( 'redirects the users to previous FLOW when backToFlow is defined', () => {
			const { runUseStepNavigationGoBack } = renderFlow( siteSetupFlow );

			runUseStepNavigationGoBack( {
				currentURL: '/some-path?backToFlow=some-flow/some-step',
				currentStep: STEPS.IMPORT_LIST.slug,
			} );

			expect( window.location.assign ).toHaveBeenCalledWith(
				expect.stringContaining( '/setup/some-flow/some-step' )
			);
		} );
	} );

	describe( 'when finishing the Site Setup Flow', () => {
		beforeEach( () => {
			jest.clearAllMocks();
		} );

		it( 'exitFlow should clear signup destination cookie', () => {
			const { runUseStepNavigationSubmit } = renderFlow( siteSetupFlow );

			runUseStepNavigationSubmit( {
				currentStep: 'processing',
				dependencies: {
					processingResult: 'success',
				},
			} );

			// Verify the cookie was cleared
			expect( clearSignupDestinationCookie ).toHaveBeenCalled();
		} );
	} );
} );