Skip to content

Commit

Permalink
Hosting: Add testing coverage to SFTP/phpMyAdmin data layer (#38046)
Browse files Browse the repository at this point in the history
Adds testing coverage for all the SFTP/phpMyAdmin reducers and selectors available in the state.

This also removes some tests added in #37966 that were just mere boilerplate to double check implementations details rather than behavior. Such tests are discouraged by our unit testing guidelines.
  • Loading branch information
mmtr authored Dec 4, 2019
1 parent b3ea98a commit e6bfde5
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import { translate } from 'i18n-calypso';

/**
* Internal dependencies
*/
Expand All @@ -6,7 +11,6 @@ import { dispatchRequest } from 'state/data-layer/wpcom-http/utils';
import { registerHandlers } from 'state/data-layer/handler-registry';
import { HOSTING_RESTORE_DATABASE_PASSWORD } from 'state/action-types';
import { errorNotice, successNotice } from 'state/notices/actions';
import { translate } from 'i18n-calypso';

const requestRestoreDatabasePassword = action =>
http(
Expand All @@ -19,27 +23,25 @@ const requestRestoreDatabasePassword = action =>
action
);

const receiveRestoreDatabasePasswordSuccess = () => {
return successNotice( translate( 'Your database password has been restored.' ), {
const showSuccessNotification = () =>
successNotice( translate( 'Your database password has been restored.' ), {
duration: 5000,
} );
};

const receiveRestoreDatabasePasswordError = () => {
return errorNotice(
const showErrorNotification = () =>
errorNotice(
translate( 'Sorry, we had a problem restoring your database password. Please try again.' ),
{
duration: 5000,
}
);
};

registerHandlers( 'state/data-layer/wpcom/sites/hosting/index.js', {
registerHandlers( 'state/data-layer/wpcom/sites/hosting/restore-database-password.js', {
[ HOSTING_RESTORE_DATABASE_PASSWORD ]: [
dispatchRequest( {
fetch: requestRestoreDatabasePassword,
onSuccess: receiveRestoreDatabasePasswordSuccess,
onError: receiveRestoreDatabasePasswordError,
onSuccess: showSuccessNotification,
onError: showErrorNotification,
} ),
],
} );
32 changes: 0 additions & 32 deletions client/state/data-layer/wpcom/sites/rewind/backups/test/index.js

This file was deleted.

4 changes: 2 additions & 2 deletions client/state/hosting/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import { keyedReducer, combineReducers } from 'state/utils';
import { HOSTING_SFTP_USER_UPDATE, HOSTING_SFTP_USERS_SET } from 'state/action-types';

const sftpUsers = ( state = {}, { type, users } ) => {
export const sftpUsers = ( state = {}, { type, users } ) => {
if ( type === HOSTING_SFTP_USERS_SET ) {
return users;
}

if ( type === HOSTING_SFTP_USER_UPDATE ) {
if ( type === HOSTING_SFTP_USER_UPDATE && Array.isArray( state ) ) {
return state.map( user => {
const updatedUser = users.find( u => u.username === user.username );
return {
Expand Down
138 changes: 138 additions & 0 deletions client/state/hosting/test/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';

/**
* Internal dependencies
*/
import reducer, { sftpUsers } from '../reducer';
import { HOSTING_SFTP_USERS_SET, HOSTING_SFTP_USER_UPDATE, SERIALIZE } from 'state/action-types';
import { useSandbox } from 'test/helpers/use-sinon';

describe( 'reducer', () => {
useSandbox( sandbox => {
sandbox.stub( console, 'warn' );
} );

describe( '#sftpUsers()', () => {
test( 'should default to an empty object', () => {
const state = sftpUsers( undefined, {} );

expect( state ).toEqual( {} );
} );

test( 'should set given users', () => {
const state = sftpUsers( undefined, {
type: HOSTING_SFTP_USERS_SET,
users: [ 1, 2, 3 ],
} );

expect( state ).toEqual( [ 1, 2, 3 ] );
} );

test( 'should override previous users', () => {
const previousState = deepFreeze( [ 1, 2, 3 ] );
const state = sftpUsers( previousState, {
type: HOSTING_SFTP_USERS_SET,
users: [ 4, 5, 6 ],
} );

expect( state ).toEqual( [ 4, 5, 6 ] );
} );

test( 'should persist state', () => {
const previousState = deepFreeze( [ 1, 2, 3 ] );
const state = sftpUsers( previousState, { type: SERIALIZE } );

expect( state ).toEqual( [ 1, 2, 3 ] );
} );

test( 'should update existing users', () => {
const previousState = deepFreeze( [
{ username: 'u1', password: 'p1' },
{ username: 'u2', password: 'p2' },
{ username: 'u3', password: 'p3' },
] );
const state = sftpUsers( previousState, {
type: HOSTING_SFTP_USER_UPDATE,
users: [ { username: 'u2', password: 'p2-updated' } ],
} );

expect( state ).toEqual( [
{ username: 'u1', password: 'p1' },
{ username: 'u2', password: 'p2-updated' },
{ username: 'u3', password: 'p3' },
] );
} );

test( 'should not update non-existing users', () => {
const previousState = deepFreeze( [
{ username: 'u1', password: 'p1' },
{ username: 'u2', password: 'p2' },
{ username: 'u3', password: 'p3' },
] );
const state = sftpUsers( previousState, {
type: HOSTING_SFTP_USER_UPDATE,
users: [ { username: 'u4', password: 'p4' } ],
} );

expect( state ).toEqual( [
{ username: 'u1', password: 'p1' },
{ username: 'u2', password: 'p2' },
{ username: 'u3', password: 'p3' },
] );
} );

test( 'should not update if there are no users', () => {
const state = sftpUsers( undefined, {
type: HOSTING_SFTP_USER_UPDATE,
users: [ { username: 'u1', password: 'p1' } ],
} );

expect( state ).toEqual( {} );
} );
} );

test( 'should default to an empty object', () => {
const state = reducer( undefined, {} );

expect( state ).toEqual( {} );
} );

test( 'should map site ID', () => {
const state = reducer( undefined, {
type: HOSTING_SFTP_USERS_SET,
users: [ 1, 2, 3 ],
siteId: 12345678,
} );

expect( state ).toEqual( {
12345678: {
sftpUsers: [ 1, 2, 3 ],
},
} );
} );

test( 'should accumulate sites', () => {
const previousState = {
12345678: {
sftpUsers: [ 1, 2, 3 ],
},
};
const state = reducer( previousState, {
type: HOSTING_SFTP_USERS_SET,
users: [ 9, 8, 7 ],
siteId: 9876543,
} );

expect( state ).toEqual( {
12345678: {
sftpUsers: [ 1, 2, 3 ],
},
9876543: {
sftpUsers: [ 9, 8, 7 ],
},
} );
} );
} );
26 changes: 0 additions & 26 deletions client/state/rewind/backups/test/actions.js

This file was deleted.

62 changes: 62 additions & 0 deletions client/state/selectors/test/get-atomic-hosting-sftp-users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Internal dependencies
*/
import { getAtomicHostingSftpUsers } from 'state/selectors/get-atomic-hosting-sftp-users';

describe( 'getAtomicHostingSftpUsers()', () => {
test( 'should return null if there is no hosting data', () => {
const state = {};
const users = getAtomicHostingSftpUsers( state, 123 );

expect( users ).toBeNull();
} );

test( 'should return null if the site is not tracked', () => {
const state = {
atomicHosting: {
123: {
sftpUsers: [
{ username: 'u1', password: 'p1' },
{ username: 'u2', password: 'p3' },
{ username: 'u3', password: 'p3' },
],
},
},
};
const users = getAtomicHostingSftpUsers( state, 124 );

expect( users ).toBeNull();
} );

test( 'should return null if there are no users data', () => {
const state = {
atomicHosting: {
123: {},
},
};
const users = getAtomicHostingSftpUsers( state, 123 );

expect( users ).toBeNull();
} );

test( 'should return list of users', () => {
const state = {
atomicHosting: {
123: {
sftpUsers: [
{ username: 'u1', password: 'p1' },
{ username: 'u2', password: 'p3' },
{ username: 'u3', password: 'p3' },
],
},
},
};
const users = getAtomicHostingSftpUsers( state, 123 );

expect( users ).toEqual( [
{ username: 'u1', password: 'p1' },
{ username: 'u2', password: 'p3' },
{ username: 'u3', password: 'p3' },
] );
} );
} );

0 comments on commit e6bfde5

Please sign in to comment.