File size: 5,213 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 |
/**
* @jest-environment jsdom
*/
import { FEATURE_SFTP, FEATURE_SSH } from '@automattic/calypso-products';
import { render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import configureStore from 'redux-mock-store';
import { SftpCard } from '../card';
jest.mock( '@automattic/components/src/spinner', () => ( {
__esModule: true,
Spinner: () => <div data-testid="spinner" />,
} ) );
jest.mock( 'calypso/launchpad/hooks/use-complete-launchpad-tasks-with-notice', () => ( {
useCompleteLaunchpadTasksWithNotice: () => jest.fn(),
} ) );
const INITIAL_STATE = {
ui: { selectedSiteId: 1 },
currentUser: { id: 1 },
sites: {},
};
const mockStore = configureStore();
const defaultStore = mockStore( INITIAL_STATE );
const storeWithUsername = mockStore( {
...INITIAL_STATE,
atomicHosting: {
1: { sftpUsers: [ { username: 'testuser' } ] },
},
} );
describe( 'SftpCard', () => {
beforeAll( () => {
// Mock the missing `window.matchMedia` function that's not even in JSDOM
Object.defineProperty( window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation( ( query ) => ( {
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
} ) ),
} );
} );
function renderWithProvider( props, store = defaultStore ) {
render(
<Provider store={ store }>
<SftpCard { ...props } />
</Provider>
);
}
describe( 'Sftp Questions', () => {
it( 'should display sftp questions if no sftp username', async () => {
const store = mockStore( {
...INITIAL_STATE,
atomicHosting: {
1: { sftpUsers: [ { username: null } ] },
},
} );
renderWithProvider( {}, store );
expect( screen.getByText( 'What is SFTP?' ) ).toBeVisible();
expect( screen.getByText( 'What is SSH?' ) ).toBeVisible();
} );
it( 'should not display sftp questions if sftp username is set', () => {
renderWithProvider( {}, storeWithUsername );
expect( screen.queryByText( 'What is SFTP?' ) ).not.toBeInTheDocument();
expect( screen.queryByText( 'What is SSH?' ) ).not.toBeInTheDocument();
} );
it( 'should not display sftp questions if loading', () => {
const store = mockStore( {
...INITIAL_STATE,
sites: {
features: {
1: {
data: {
active: [ FEATURE_SFTP, FEATURE_SSH ],
},
},
},
},
atomicHosting: {
1: { isLoadingSftpUsers: true },
},
} );
renderWithProvider( {}, store );
expect( screen.queryByText( 'What is SFTP?' ) ).not.toBeInTheDocument();
expect( screen.queryByText( 'What is SSH?' ) ).not.toBeInTheDocument();
} );
} );
describe( 'Loading', () => {
it( 'should display spinner if username not set and not disabled', () => {
renderWithProvider( {} );
expect( screen.queryByTestId( 'spinner' ) ).toBeVisible();
} );
it( 'should not display spinner if disabled', () => {
renderWithProvider( { disabled: true } );
expect( screen.queryByTestId( 'spinner' ) ).not.toBeInTheDocument();
} );
} );
describe( 'Create SFTP credentials', () => {
const btnName = 'Create credentials';
it( 'should display create SFTP credentials if username not set', () => {
renderWithProvider( { disabled: true } );
expect( screen.getByRole( 'button', { name: btnName } ) ).toBeVisible();
} );
it( 'should not display create SFTP credentials if username set', () => {
renderWithProvider( {}, storeWithUsername );
expect( screen.queryByRole( 'button', { name: btnName } ) ).not.toBeInTheDocument();
} );
} );
describe( 'User info fields', () => {
it( 'should display user info fields if username set', () => {
renderWithProvider( {}, storeWithUsername );
expect( screen.getByLabelText( 'URL' ) ).toHaveValue( 'sftp.wp.com' );
expect( screen.getByLabelText( 'Port' ) ).toHaveValue( '22' );
expect( screen.getByLabelText( 'Username' ) ).toHaveValue( 'testuser' );
} );
it( 'should not display user info fields if username not set', () => {
renderWithProvider();
expect( screen.queryByLabelText( 'URL' ) ).not.toBeInTheDocument();
expect( screen.queryByLabelText( 'Port' ) ).not.toBeInTheDocument();
expect( screen.queryByLabelText( 'Username' ) ).not.toBeInTheDocument();
} );
} );
describe( 'Password', () => {
it( 'should display password field if password set', () => {
const store = mockStore( {
...INITIAL_STATE,
atomicHosting: {
1: { sftpUsers: [ { username: 'testuser', password: 'secret' } ] },
},
} );
renderWithProvider( {}, store );
expect( screen.getByLabelText( 'Password' ) ).toBeVisible();
} );
it( 'should not display password field if password not set', () => {
renderWithProvider( {}, storeWithUsername );
expect( screen.queryByLabelText( 'Password' ) ).not.toBeInTheDocument();
} );
it( 'should display password reset button if password not set', () => {
renderWithProvider( {}, storeWithUsername );
expect( screen.getByRole( 'button', { name: 'Reset password' } ) ).toBeVisible();
} );
} );
} );
|