Skip to content

Commit

Permalink
Adds button to hamburger menu to format code (#2382)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianfalleiro authored Oct 27, 2020
1 parent 0781da1 commit 0db2527
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"export-gist": "Export Gist",
"export-repo": "Export Repo",
"update-repo": "Update Repo",
"format-code": "Format Code",
"share-to-classroom": "Share To Classroom",
"add-instructions": "Add Instructions",
"edit-instructions": "Edit Instructions",
Expand Down
6 changes: 6 additions & 0 deletions src/components/TopBar/HamburgerMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ const HamburgerMenu = createMenu({
hasInstructions,
isEditingInstructions,
isUserAuthenticated,
onAutoFormat,
onStartEditingInstructions,
onStartGithubLogIn,
}) {
return (
<Fragment>
<MenuItem onClick={onAutoFormat}>
{i18next.t('top-bar.format-code')}
</MenuItem>

<MenuItem
isDisabled={isEditingInstructions}
onClick={onStartEditingInstructions}
Expand Down Expand Up @@ -87,6 +92,7 @@ HamburgerMenu.propTypes = {
hasInstructions: PropTypes.bool.isRequired,
isEditingInstructions: PropTypes.bool.isRequired,
isUserAuthenticated: PropTypes.bool.isRequired,
onAutoFormat: PropTypes.func.isRequired,
onStartEditingInstructions: PropTypes.func.isRequired,
onStartGithubLogIn: PropTypes.func.isRequired,
};
Expand Down
3 changes: 3 additions & 0 deletions src/components/TopBar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default function TopBar({
projectKeys,
shouldShowSavedIndicator,
validationState,
onAutoFormat,
onClickMenu,
onCloseMenu,
onCreateNewProject,
Expand Down Expand Up @@ -130,6 +131,7 @@ export default function TopBar({
isEditingInstructions={isEditingInstructions}
isOpen={openMenu === 'hamburger'}
isUserAuthenticated={isUserAuthenticated}
onAutoFormat={onAutoFormat}
onClick={partial(onClickMenu, 'hamburger')}
onStartEditingInstructions={partial(
onStartEditingInstructions,
Expand Down Expand Up @@ -162,6 +164,7 @@ TopBar.propTypes = {
projectKeys: PropTypes.arrayOf(PropTypes.string).isRequired,
shouldShowSavedIndicator: PropTypes.bool.isRequired,
validationState: PropTypes.string.isRequired,
onAutoFormat: PropTypes.func.isRequired,
onClickMenu: PropTypes.func.isRequired,
onCloseMenu: PropTypes.func.isRequired,
onCreateNewProject: PropTypes.func.isRequired,
Expand Down
81 changes: 81 additions & 0 deletions src/components/__tests__/HamburgerMenu.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {fromJS} from 'immutable';
import React from 'react';
import {Provider} from 'react-redux';
import {act, create} from 'react-test-renderer';
import configureStore from 'redux-mock-store';

import {MenuItem} from '../TopBar/createMenu';
import HamburgerMenu from '../TopBar/HamburgerMenu';

jest.mock('i18next', () => {
return {
t: jest.fn().mockImplementation(translationKey => translationKey),
init: jest.fn(),
};
});

const mockStore = configureStore([]);
const store = mockStore(
fromJS({
ui: {
openTopBarMenu: 'hamburger',
},
}),
);

const DEFAULT_PROPS = {
hasInstructions: false,
isEditingInstructions: false,
isUserAuthenticated: false,
onAutoFormat: jest.fn(),
onStartEditingInstructions: jest.fn(),
onStartGithubLogIn: jest.fn(),
};

function buildComponent() {
return (
<Provider store={store}>
<HamburgerMenu {...DEFAULT_PROPS} />
</Provider>
);
}

function renderComponent(props = {}) {
let component;
act(() => {
component = create(buildComponent(props));
});
return component;
}

describe('hamburger menu', () => {
let menu;

beforeEach(() => {
menu = renderComponent();
DEFAULT_PROPS.onAutoFormat.mockClear();
DEFAULT_PROPS.onStartEditingInstructions.mockClear();
DEFAULT_PROPS.onStartGithubLogIn.mockClear();
});

it('renders the format code button', () => {
const menuItems = menu.root.findAllByType(MenuItem);
const formatCodeBtn = menuItems.find(
btn => btn.props.children === 'top-bar.format-code',
);
expect(formatCodeBtn).toBeTruthy();
});

it('calls the passed `onAutoFormat()` method when format code button is pressed', () => {
const menuItems = menu.root.findAllByType(MenuItem);
const formatCodeBtn = menuItems.find(
btn => btn.props.children === 'top-bar.format-code',
);

act(() => {
formatCodeBtn.props.onClick();
});

expect(DEFAULT_PROPS.onAutoFormat).toHaveBeenCalledTimes(1);
});
});
5 changes: 5 additions & 0 deletions src/containers/TopBar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {connect} from 'react-redux';

import {
beautifyProjectSource,
closeTopBarMenu,
createProject,
createSnapshot,
Expand Down Expand Up @@ -72,6 +73,10 @@ function exportRepo(dispatch) {

function mapDispatchToProps(dispatch) {
return {
onAutoFormat() {
dispatch(beautifyProjectSource());
},

onClickMenu(menuKey) {
dispatch(toggleTopBarMenu(menuKey));
},
Expand Down

0 comments on commit 0db2527

Please sign in to comment.