diff options
author | Wouter Admiraal <wouter.admiraal@sonarsource.com> | 2020-06-15 15:20:46 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-06-19 20:04:42 +0000 |
commit | 72a34678a0da474c17e1d375a7e9f8699f9433ce (patch) | |
tree | c3d59cf9f73a2a105e22a36501f15792a555e707 /server/sonar-web/src/main/js/components/controls | |
parent | 12818cc0a81f8c5843534966aceb71725b7506c5 (diff) | |
download | sonarqube-72a34678a0da474c17e1d375a7e9f8699f9433ce.tar.gz sonarqube-72a34678a0da474c17e1d375a7e9f8699f9433ce.zip |
SONAR-13114 Click on the Homepage button reverts back to the default homepage
Diffstat (limited to 'server/sonar-web/src/main/js/components/controls')
3 files changed, 73 insertions, 52 deletions
diff --git a/server/sonar-web/src/main/js/components/controls/HomePageSelect.tsx b/server/sonar-web/src/main/js/components/controls/HomePageSelect.tsx index 8eebe3fe573..ca7cc933a6e 100644 --- a/server/sonar-web/src/main/js/components/controls/HomePageSelect.tsx +++ b/server/sonar-web/src/main/js/components/controls/HomePageSelect.tsx @@ -41,11 +41,17 @@ interface Props extends StateProps, DispatchProps { currentPage: T.HomePage; } -class HomePageSelect extends React.PureComponent<Props> { +export const DEFAULT_HOMEPAGE: T.HomePage = { type: 'PROJECTS' }; + +export class HomePageSelect extends React.PureComponent<Props> { handleClick = () => { this.props.setHomePage(this.props.currentPage); }; + handleReset = () => { + this.props.setHomePage(DEFAULT_HOMEPAGE); + }; + render() { const { currentPage, currentUser } = this.props; @@ -54,23 +60,26 @@ class HomePageSelect extends React.PureComponent<Props> { } const { homepage } = currentUser; - const checked = homepage !== undefined && isSameHomePage(homepage, currentPage); - const tooltip = checked ? translate('homepage.current') : translate('homepage.check'); + const isChecked = homepage !== undefined && isSameHomePage(homepage, currentPage); + const isDefault = isChecked && isSameHomePage(currentPage, DEFAULT_HOMEPAGE); + const tooltip = isChecked + ? translate(isDefault ? 'homepage.current.is_default' : 'homepage.current') + : translate('homepage.check'); return ( <Tooltip overlay={tooltip}> - {checked ? ( + {isDefault ? ( <span aria-label={tooltip} className={classNames('display-inline-block', this.props.className)}> - <HomeIcon filled={checked} /> + <HomeIcon filled={isChecked} /> </span> ) : ( <ButtonLink aria-label={tooltip} className={classNames('link-no-underline', 'set-homepage-link', this.props.className)} - onClick={this.handleClick}> - <HomeIcon filled={checked} /> + onClick={isChecked ? this.handleReset : this.handleClick}> + <HomeIcon filled={isChecked} /> </ButtonLink> )} </Tooltip> diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/HomePageSelect-test.tsx b/server/sonar-web/src/main/js/components/controls/__tests__/HomePageSelect-test.tsx index 01d47eb4ece..98238f69f93 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/HomePageSelect-test.tsx +++ b/server/sonar-web/src/main/js/components/controls/__tests__/HomePageSelect-test.tsx @@ -19,53 +19,49 @@ */ import { shallow } from 'enzyme'; import * as React from 'react'; -import { click } from 'sonar-ui-common/helpers/testUtils'; -import { setHomePage } from '../../../api/users'; -import rootReducer, { getCurrentUser, Store } from '../../../store/rootReducer'; -import configureStore from '../../../store/utils/configureStore'; -import HomePageSelect from '../HomePageSelect'; +import { ButtonLink } from 'sonar-ui-common/components/controls/buttons'; +import { click, waitAndUpdate } from 'sonar-ui-common/helpers/testUtils'; +import { mockCurrentUser, mockLoggedInUser } from '../../../helpers/testMocks'; +import { DEFAULT_HOMEPAGE, HomePageSelect } from '../HomePageSelect'; -jest.mock('../../../api/users', () => ({ - setHomePage: jest.fn(() => Promise.resolve()) -})); - -const homepage: T.HomePage = { type: 'PROJECTS' }; - -it('should render unchecked', () => { - const store = configureStore(rootReducer, { - users: { currentUser: { isLoggedIn: true } } - } as Store); - expect(getWrapper(homepage, store)).toMatchSnapshot(); +it('should render correctly', () => { + expect(shallowRender()).toMatchSnapshot('unchecked'); + expect( + shallowRender({ currentUser: mockLoggedInUser({ homepage: { type: 'MY_PROJECTS' } }) }) + ).toMatchSnapshot('checked'); + expect( + shallowRender({ + currentUser: mockLoggedInUser({ homepage: DEFAULT_HOMEPAGE }), + currentPage: DEFAULT_HOMEPAGE + }) + ).toMatchSnapshot('checked, and on default'); + expect(shallowRender({ currentUser: mockCurrentUser() }).type()).toBeNull(); }); -it('should render checked', () => { - const store = configureStore(rootReducer, { - users: { currentUser: { isLoggedIn: true, homepage } as T.CurrentUser } - } as Store); - expect(getWrapper(homepage, store)).toMatchSnapshot(); -}); +it('should correctly call webservices', async () => { + const setHomePage = jest.fn(); + const currentPage: T.HomePage = { type: 'MY_ISSUES' }; + const wrapper = shallowRender({ setHomePage, currentPage }); -it('should set new home page', async () => { - const store = configureStore(rootReducer, { - users: { currentUser: { isLoggedIn: true } } - } as Store); - const wrapper = getWrapper(homepage, store); - click(wrapper.find('ButtonLink')); - await new Promise(setImmediate); - const currentUser = getCurrentUser(store.getState() as Store) as T.LoggedInUser; - expect(currentUser.homepage).toEqual(homepage); - expect(setHomePage).toBeCalledWith(homepage); -}); + // Set homepage. + click(wrapper.find(ButtonLink)); + await waitAndUpdate(wrapper); + expect(setHomePage).toHaveBeenLastCalledWith(currentPage); -it('should not render for anonymous', () => { - const store = configureStore(rootReducer, { - users: { currentUser: { isLoggedIn: false } } - } as Store); - expect(getWrapper(homepage, store).type()).toBeNull(); + // Reset. + wrapper.setProps({ currentUser: mockLoggedInUser({ homepage: currentPage }) }); + click(wrapper.find(ButtonLink)); + await waitAndUpdate(wrapper); + expect(setHomePage).toHaveBeenLastCalledWith(DEFAULT_HOMEPAGE); }); -function getWrapper(currentPage: T.HomePage, store: any) { - return shallow(<HomePageSelect currentPage={currentPage} />, { - context: { store } - }).dive(); +function shallowRender(props: Partial<HomePageSelect['props']> = {}) { + return shallow<HomePageSelect>( + <HomePageSelect + currentPage={{ type: 'MY_PROJECTS' }} + currentUser={mockLoggedInUser()} + setHomePage={jest.fn()} + {...props} + /> + ); } diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/HomePageSelect-test.tsx.snap b/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/HomePageSelect-test.tsx.snap index 75104dd2245..f05b2692ad1 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/HomePageSelect-test.tsx.snap +++ b/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/HomePageSelect-test.tsx.snap @@ -1,11 +1,27 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`should render checked 1`] = ` +exports[`should render correctly: checked 1`] = ` <Tooltip overlay="homepage.current" > - <span + <ButtonLink aria-label="homepage.current" + className="link-no-underline set-homepage-link" + onClick={[Function]} + > + <HomeIcon + filled={true} + /> + </ButtonLink> +</Tooltip> +`; + +exports[`should render correctly: checked, and on default 1`] = ` +<Tooltip + overlay="homepage.current.is_default" +> + <span + aria-label="homepage.current.is_default" className="display-inline-block" > <HomeIcon @@ -15,7 +31,7 @@ exports[`should render checked 1`] = ` </Tooltip> `; -exports[`should render unchecked 1`] = ` +exports[`should render correctly: unchecked 1`] = ` <Tooltip overlay="homepage.check" > |