diff options
author | Guillaume Peoc'h <guillaume.peoch@sonarsource.com> | 2022-02-02 15:36:37 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-02-09 20:02:55 +0000 |
commit | 2b13f016dee62cc813d0374e9f835dee5f4cda28 (patch) | |
tree | a14a39e129bb993ab041c8b29af4f9963bdf0002 /server/sonar-web/src/main/js/apps/change-admin-password | |
parent | 2d48cb3c1eb4f6cd68bd091c4eb9d62fa71deff8 (diff) | |
download | sonarqube-2b13f016dee62cc813d0374e9f835dee5f4cda28.tar.gz sonarqube-2b13f016dee62cc813d0374e9f835dee5f4cda28.zip |
SONAR-15909 Extract AppState Redux
Diffstat (limited to 'server/sonar-web/src/main/js/apps/change-admin-password')
3 files changed, 15 insertions, 44 deletions
diff --git a/server/sonar-web/src/main/js/apps/change-admin-password/ChangeAdminPasswordApp.tsx b/server/sonar-web/src/main/js/apps/change-admin-password/ChangeAdminPasswordApp.tsx index 80c821e4c79..5bb5d8b601b 100644 --- a/server/sonar-web/src/main/js/apps/change-admin-password/ChangeAdminPasswordApp.tsx +++ b/server/sonar-web/src/main/js/apps/change-admin-password/ChangeAdminPasswordApp.tsx @@ -18,16 +18,15 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as React from 'react'; -import { connect } from 'react-redux'; import { changePassword } from '../../api/users'; +import withAppStateContext from '../../app/components/app-state/withAppStateContext'; import { Location, withRouter } from '../../components/hoc/withRouter'; -import { getAppState, Store } from '../../store/rootReducer'; +import { AppState } from '../../types/types'; import ChangeAdminPasswordAppRenderer from './ChangeAdminPasswordAppRenderer'; import { DEFAULT_ADMIN_LOGIN, DEFAULT_ADMIN_PASSWORD } from './constants'; interface Props { - canAdmin?: boolean; - instanceUsesDefaultAdminCredentials?: boolean; + appState: AppState; location: Location; } @@ -49,7 +48,7 @@ export class ChangeAdminPasswordApp extends React.PureComponent<Props, State> { passwordValue: '', confirmPasswordValue: '', submitting: false, - success: !props.instanceUsesDefaultAdminCredentials + success: !props.appState.instanceUsesDefaultAdminCredentials }; } @@ -93,7 +92,10 @@ export class ChangeAdminPasswordApp extends React.PureComponent<Props, State> { }; render() { - const { canAdmin, location } = this.props; + const { + appState: { canAdmin }, + location + } = this.props; const { canSubmit, confirmPasswordValue, passwordValue, submitting, success } = this.state; return ( <ChangeAdminPasswordAppRenderer @@ -112,9 +114,4 @@ export class ChangeAdminPasswordApp extends React.PureComponent<Props, State> { } } -export const mapStateToProps = (state: Store) => { - const { canAdmin, instanceUsesDefaultAdminCredentials } = getAppState(state); - return { canAdmin, instanceUsesDefaultAdminCredentials }; -}; - -export default connect(mapStateToProps)(withRouter(ChangeAdminPasswordApp)); +export default withRouter(withAppStateContext(ChangeAdminPasswordApp)); diff --git a/server/sonar-web/src/main/js/apps/change-admin-password/__tests__/ChangeAdminPasswordApp-test.tsx b/server/sonar-web/src/main/js/apps/change-admin-password/__tests__/ChangeAdminPasswordApp-test.tsx index cf173422826..fe8e3e54412 100644 --- a/server/sonar-web/src/main/js/apps/change-admin-password/__tests__/ChangeAdminPasswordApp-test.tsx +++ b/server/sonar-web/src/main/js/apps/change-admin-password/__tests__/ChangeAdminPasswordApp-test.tsx @@ -20,21 +20,15 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import { changePassword } from '../../../api/users'; -import { mockLocation } from '../../../helpers/testMocks'; +import { mockAppState, mockLocation } from '../../../helpers/testMocks'; import { waitAndUpdate } from '../../../helpers/testUtils'; -import { getAppState, Store } from '../../../store/rootReducer'; -import { ChangeAdminPasswordApp, mapStateToProps } from '../ChangeAdminPasswordApp'; +import { ChangeAdminPasswordApp } from '../ChangeAdminPasswordApp'; import { DEFAULT_ADMIN_LOGIN, DEFAULT_ADMIN_PASSWORD } from '../constants'; jest.mock('react-redux', () => ({ connect: jest.fn(() => (a: any) => a) })); -jest.mock('../../../store/rootReducer', () => ({ - ...jest.requireActual('../../../store/rootReducer'), - getAppState: jest.fn() -})); - jest.mock('../../../api/users', () => ({ changePassword: jest.fn().mockResolvedValue(null) })); @@ -43,9 +37,9 @@ beforeEach(jest.clearAllMocks); it('should render correctly', () => { expect(shallowRender()).toMatchSnapshot('default'); - expect(shallowRender({ instanceUsesDefaultAdminCredentials: undefined })).toMatchSnapshot( - 'admin is not using the default password' - ); + expect( + shallowRender({ appState: mockAppState({ instanceUsesDefaultAdminCredentials: undefined }) }) + ).toMatchSnapshot('admin is not using the default password'); }); it('should correctly handle input changes', () => { @@ -99,29 +93,10 @@ it('should correctly update the password', async () => { expect(wrapper.state().success).toBe(false); }); -describe('redux', () => { - it('should correctly map state props', () => { - (getAppState as jest.Mock) - .mockReturnValueOnce({}) - .mockReturnValueOnce({ canAdmin: false, instanceUsesDefaultAdminCredentials: true }); - - expect(mapStateToProps({} as Store)).toEqual({ - canAdmin: undefined, - instanceUsesDefaultAdminCredentials: undefined - }); - - expect(mapStateToProps({} as Store)).toEqual({ - canAdmin: false, - instanceUsesDefaultAdminCredentials: true - }); - }); -}); - function shallowRender(props: Partial<ChangeAdminPasswordApp['props']> = {}) { return shallow<ChangeAdminPasswordApp>( <ChangeAdminPasswordApp - canAdmin={true} - instanceUsesDefaultAdminCredentials={true} + appState={mockAppState({ canAdmin: true, instanceUsesDefaultAdminCredentials: true })} location={mockLocation()} {...props} /> diff --git a/server/sonar-web/src/main/js/apps/change-admin-password/__tests__/__snapshots__/ChangeAdminPasswordApp-test.tsx.snap b/server/sonar-web/src/main/js/apps/change-admin-password/__tests__/__snapshots__/ChangeAdminPasswordApp-test.tsx.snap index 5371a8f4386..e7e4585e6ae 100644 --- a/server/sonar-web/src/main/js/apps/change-admin-password/__tests__/__snapshots__/ChangeAdminPasswordApp-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/change-admin-password/__tests__/__snapshots__/ChangeAdminPasswordApp-test.tsx.snap @@ -2,7 +2,6 @@ exports[`should render correctly: admin is not using the default password 1`] = ` <ChangeAdminPasswordAppRenderer - canAdmin={true} confirmPasswordValue="" location={ Object { |