|
|
|
|
|
|
|
|
|
|
|
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', |
|
|
}; |
|
|
|
|
|
|
|
|
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/; |
|
|
|
|
|
|
|
|
const mockedDispatch = jest.fn(); |
|
|
useDispatch.mockReturnValue( mockedDispatch ); |
|
|
|
|
|
|
|
|
render( <BackupRetentionManagement defaultRetention={ retentionSelected } /> ); |
|
|
|
|
|
|
|
|
expect( screen.getByRole( 'radio', { name: retentionOptionName } ) ).toBeChecked(); |
|
|
|
|
|
|
|
|
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/; |
|
|
|
|
|
|
|
|
const mockedDispatch = jest.fn(); |
|
|
useDispatch.mockReturnValue( mockedDispatch ); |
|
|
|
|
|
|
|
|
render( |
|
|
<BackupRetentionManagement |
|
|
defaultRetention={ retentionSelected } |
|
|
storagePurchased={ false } |
|
|
/> |
|
|
); |
|
|
|
|
|
|
|
|
expect( screen.getByRole( 'radio', { name: retentionOptionName } ) ).toBeChecked(); |
|
|
|
|
|
|
|
|
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/; |
|
|
|
|
|
|
|
|
getBackupRetentionDays.mockReturnValue( 7 ); |
|
|
|
|
|
|
|
|
getRewindBytesAvailable.mockReturnValue( 100 * GB_IN_BYTES ); |
|
|
|
|
|
|
|
|
getBackupCurrentSiteSize.mockReturnValue( GB_IN_BYTES ); |
|
|
|
|
|
|
|
|
const mockedDispatch = jest.fn(); |
|
|
useDispatch.mockReturnValue( mockedDispatch ); |
|
|
|
|
|
|
|
|
render( |
|
|
<BackupRetentionManagement defaultRetention={ retentionSelected } storagePurchased /> |
|
|
); |
|
|
|
|
|
|
|
|
expect( screen.getByRole( 'radio', { name: retentionOptionName } ) ).toBeChecked(); |
|
|
|
|
|
|
|
|
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/; |
|
|
|
|
|
|
|
|
getBackupRetentionDays.mockReturnValue( 30 ); |
|
|
|
|
|
|
|
|
const mockedDispatch = jest.fn(); |
|
|
useDispatch.mockReturnValue( mockedDispatch ); |
|
|
|
|
|
|
|
|
render( |
|
|
<BackupRetentionManagement defaultRetention={ retentionSelected } storagePurchased /> |
|
|
); |
|
|
|
|
|
|
|
|
expect( screen.getByRole( 'radio', { name: retentionOptionName } ) ).toBeChecked(); |
|
|
|
|
|
|
|
|
expect( mockedDispatch ).not.toHaveBeenCalledWith( |
|
|
updateBackupRetention( EXAMPLE_SITE_ID, retentionSelected ) |
|
|
); |
|
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test( 'should not update automatically when storagePurchased is true and retention passed requires upgrade', () => { |
|
|
const retentionSelected = 30; |
|
|
const retentionOptionName = /30 days/; |
|
|
|
|
|
|
|
|
getBackupRetentionDays.mockReturnValue( 7 ); |
|
|
|
|
|
|
|
|
getRewindBytesAvailable.mockReturnValue( 10 * GB_IN_BYTES ); |
|
|
|
|
|
|
|
|
getBackupCurrentSiteSize.mockReturnValue( GB_IN_BYTES ); |
|
|
|
|
|
|
|
|
const mockedDispatch = jest.fn(); |
|
|
useDispatch.mockReturnValue( mockedDispatch ); |
|
|
|
|
|
|
|
|
render( |
|
|
<BackupRetentionManagement defaultRetention={ retentionSelected } storagePurchased /> |
|
|
); |
|
|
|
|
|
|
|
|
expect( screen.getByRole( 'radio', { name: retentionOptionName } ) ).toBeChecked(); |
|
|
|
|
|
|
|
|
expect( screen.getByRole( 'button', { name: 'Purchase and update' } ) ).toBeInTheDocument(); |
|
|
|
|
|
|
|
|
expect( mockedDispatch ).not.toHaveBeenCalledWith( |
|
|
updateBackupRetention( EXAMPLE_SITE_ID, retentionSelected ) |
|
|
); |
|
|
} ); |
|
|
} ); |
|
|
} ); |
|
|
|