File size: 8,000 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
/**
 * @jest-environment jsdom
 */
import { render, screen } from '@testing-library/react';
import { useDispatch } from 'react-redux';
import { updateBackupRetention } from 'calypso/state/rewind/retention/actions';
import getBackupCurrentSiteSize from 'calypso/state/rewind/selectors/get-backup-current-site-size';
import getBackupRetentionDays from 'calypso/state/rewind/selectors/get-backup-retention-days';
import getRewindBytesAvailable from 'calypso/state/rewind/selectors/get-rewind-bytes-available';
import BackupRetentionManagement from '../index';

const GB_IN_BYTES = 2 ** 30;
const EXAMPLE_SITE_SLUG = 'mysite.example';
const EXAMPLE_SITE_ID = 123;
const EXAMPLE_ADDON_STORAGE_INFO = {
	upsellSlug: 'jetpack_backup_storage_addon_10GB',
	originalPrice: 5,
	isPriceFetching: false,
	currencyCode: 'USD',
};

// Mock dependencies
jest.mock( 'react-redux', () => ( {
	...jest.requireActual( 'react-redux' ),
	useSelector: jest.fn( ( func ) => func() ),
	useDispatch: () => jest.fn(),
} ) );

jest.mock( 'i18n-calypso', () => ( {
	...jest.requireActual( 'i18n-calypso' ),
	useTranslate: jest.fn( () => ( text ) => text ),
} ) );

jest.mock( 'calypso/components/data/query-site-products', () => () => 'query-site-products' );
jest.mock( 'calypso/state/ui/selectors', () => ( {
	getSelectedSiteId: jest.fn().mockImplementation( () => EXAMPLE_SITE_ID ),
} ) );

jest.mock( 'calypso/state/rewind/selectors/is-requesting-rewind-policies', () =>
	jest.fn().mockImplementation( () => false )
);

jest.mock( 'calypso/state/rewind/selectors/is-requesting-rewind-size', () =>
	jest.fn().mockImplementation( () => false )
);

jest.mock( 'calypso/state/rewind/selectors/get-activity-log-visible-days', () =>
	jest.fn().mockImplementation( () => 30 )
);

jest.mock( 'calypso/state/rewind/selectors/get-backup-retention-days', () =>
	jest.fn().mockImplementation( () => 30 )
);

jest.mock( 'calypso/state/rewind/selectors/get-rewind-bytes-available', () =>
	jest.fn().mockImplementation( () => 10 * GB_IN_BYTES )
);

jest.mock( 'calypso/state/rewind/selectors/get-backup-retention-update-status', () =>
	jest.fn().mockImplementation( () => 'unsubmitted' )
);

jest.mock( 'react-redux', () => ( {
	...jest.requireActual( 'react-redux' ),
	useSelector: jest.fn().mockImplementation( ( selector ) => selector() ),
	useDispatch: jest.fn().mockImplementation( () => () => {} ),
} ) );

jest.mock( 'calypso/state/rewind/selectors/get-backup-current-site-size', () =>
	jest.fn().mockImplementation( () => GB_IN_BYTES )
);

jest.mock( 'calypso/state/sites/selectors/get-site-slug', () =>
	jest.fn().mockImplementation( () => EXAMPLE_SITE_SLUG )
);

jest.mock( 'calypso/components/backup-storage-space/usage-warning/use-upsell-slug', () =>
	jest.fn().mockImplementation( () => EXAMPLE_ADDON_STORAGE_INFO )
);

describe( 'BackupRetentionManagement', () => {
	describe( 'purchase and update flow', () => {
		beforeEach( () => {
			jest.clearAllMocks();
		} );

		test( 'should not update automatically when storagePurchased prop is not passed', () => {
			const retentionSelected = 7;
			const retentionOptionName = /7 days/;

			// Mock dispatch
			const mockedDispatch = jest.fn();
			useDispatch.mockReturnValue( mockedDispatch );

			// Render component
			render( <BackupRetentionManagement defaultRetention={ retentionSelected } /> );

			// Ensure the retention passed is checked
			expect( screen.getByRole( 'radio', { name: retentionOptionName } ) ).toBeChecked();

			// Ensure the retention passed is not being updated automatically
			expect( mockedDispatch ).not.toHaveBeenCalledWith(
				updateBackupRetention( EXAMPLE_SITE_ID, retentionSelected )
			);
		} );

		test( 'should not update automatically when storagePurchased prop is false', () => {
			const retentionSelected = 7;
			const retentionOptionName = /7 days/;

			// Mock dispatch
			const mockedDispatch = jest.fn();
			useDispatch.mockReturnValue( mockedDispatch );

			// Render component
			render(
				<BackupRetentionManagement
					defaultRetention={ retentionSelected }
					storagePurchased={ false }
				/>
			);

			// Ensure the retention passed is checked
			expect( screen.getByRole( 'radio', { name: retentionOptionName } ) ).toBeChecked();

			// Ensure the retention passed is not being updated automatically
			expect( mockedDispatch ).not.toHaveBeenCalledWith(
				updateBackupRetention( EXAMPLE_SITE_ID, retentionSelected )
			);
		} );

		test( 'should update automatically when storagePurchased is true, the retention is not the current plan and not requires upgrade', () => {
			const retentionSelected = 30;
			const retentionOptionName = /30 days/;

			// Set current plan to 7 days, so it differ from the retention selected
			getBackupRetentionDays.mockReturnValue( 7 );

			// Set storage limit to 100 GB
			getRewindBytesAvailable.mockReturnValue( 100 * GB_IN_BYTES );

			// Set site size to 1 GB, so it does not require upgrade for 30 days with 100 GB
			getBackupCurrentSiteSize.mockReturnValue( GB_IN_BYTES );

			// Mock dispatch
			const mockedDispatch = jest.fn();
			useDispatch.mockReturnValue( mockedDispatch );

			// Render component
			render(
				<BackupRetentionManagement defaultRetention={ retentionSelected } storagePurchased />
			);

			// Ensure the retention passed is checked
			expect( screen.getByRole( 'radio', { name: retentionOptionName } ) ).toBeChecked();

			// Ensure the retention passed is being updated automatically
			expect( mockedDispatch ).toHaveBeenCalledWith(
				updateBackupRetention( EXAMPLE_SITE_ID, retentionSelected )
			);
		} );

		test( 'should not update automatically when storagePurchased is true and retention passed is current plan', () => {
			const retentionSelected = 30;
			const retentionOptionName = /30 days/;

			// Set current plan to 30 days, so it is the same from the retention selected
			getBackupRetentionDays.mockReturnValue( 30 );

			// Mock dispatch
			const mockedDispatch = jest.fn();
			useDispatch.mockReturnValue( mockedDispatch );

			// Render component
			render(
				<BackupRetentionManagement defaultRetention={ retentionSelected } storagePurchased />
			);

			// Ensure the retention passed is checked
			expect( screen.getByRole( 'radio', { name: retentionOptionName } ) ).toBeChecked();

			// Ensure the retention passed is not being updated automatically
			expect( mockedDispatch ).not.toHaveBeenCalledWith(
				updateBackupRetention( EXAMPLE_SITE_ID, retentionSelected )
			);
		} );

		/**
		 * There are cases where the storage purchased is not enough to cover the retention period selected.
		 * In those scenarios, we are not updating the retention automatically, and allowing the user to purchase additional storage.
		 */
		test( 'should not update automatically when storagePurchased is true and retention passed requires upgrade', () => {
			const retentionSelected = 30;
			const retentionOptionName = /30 days/;

			// Set current plan to 7 days, so it is different from the retention selected
			getBackupRetentionDays.mockReturnValue( 7 );

			// Set storage limit to 10 GB
			getRewindBytesAvailable.mockReturnValue( 10 * GB_IN_BYTES );

			// Set site size to 1 GB, so it will require upgrade for 30 days with 10 GB
			getBackupCurrentSiteSize.mockReturnValue( GB_IN_BYTES );

			// Mock dispatch
			const mockedDispatch = jest.fn();
			useDispatch.mockReturnValue( mockedDispatch );

			// Render component
			render(
				<BackupRetentionManagement defaultRetention={ retentionSelected } storagePurchased />
			);

			// Ensure the retention passed is checked
			expect( screen.getByRole( 'radio', { name: retentionOptionName } ) ).toBeChecked();

			// CTA button should say `Purchase and update`
			expect( screen.getByRole( 'button', { name: 'Purchase and update' } ) ).toBeInTheDocument();

			// Ensure the retention passed is not being updated automatically
			expect( mockedDispatch ).not.toHaveBeenCalledWith(
				updateBackupRetention( EXAMPLE_SITE_ID, retentionSelected )
			);
		} );
	} );
} );