diff --git a/packages/ra-ui-materialui/src/button/SaveButton.js b/packages/ra-ui-materialui/src/button/SaveButton.js index 19539fa1955..e6f33ba943f 100644 --- a/packages/ra-ui-materialui/src/button/SaveButton.js +++ b/packages/ra-ui-materialui/src/button/SaveButton.js @@ -38,7 +38,7 @@ const sanitizeRestProps = ({ ...rest }) => rest; -export function SaveButton({ +const SaveButton = ({ className, classes: classesOverride = {}, invalid, @@ -52,7 +52,7 @@ export function SaveButton({ onClick, handleSubmitWithRedirect, ...rest -}) { +}) => { const classes = useStyles({ classes: classesOverride }); const notify = useNotify(); const translate = useTranslate(); @@ -91,6 +91,7 @@ export function SaveButton({ }; const type = submitOnEnter ? 'submit' : 'button'; + const displayedLabel = label && translate(label, { _: label }); return ( ); -} +}; SaveButton.propTypes = { className: PropTypes.string, diff --git a/packages/ra-ui-materialui/src/button/SaveButton.spec.js b/packages/ra-ui-materialui/src/button/SaveButton.spec.js index 8f2bd6273bb..796d49fa153 100644 --- a/packages/ra-ui-materialui/src/button/SaveButton.spec.js +++ b/packages/ra-ui-materialui/src/button/SaveButton.spec.js @@ -1,43 +1,43 @@ import { render, cleanup, fireEvent } from '@testing-library/react'; import React from 'react'; +import { TestContext } from 'ra-core'; -import { SaveButton } from './SaveButton'; - -const translate = label => label; +import SaveButton from './SaveButton'; describe('', () => { afterEach(cleanup); it('should render as submit type when submitOnEnter is true', () => { const { getByLabelText } = render( - - ); - expect(getByLabelText('ra.action.save').getAttribute('type')).toEqual( - 'submit' + + + ); + expect(getByLabelText('Save').getAttribute('type')).toEqual('submit'); }); it('should render as button type when submitOnEnter is false', () => { const { getByLabelText } = render( - + + + ); - expect(getByLabelText('ra.action.save').getAttribute('type')).toEqual( - 'button' - ); + expect(getByLabelText('Save').getAttribute('type')).toEqual('button'); }); it('should trigger submit action when clicked if no saving is in progress', () => { const onSubmit = jest.fn(); const { getByLabelText } = render( - + + + ); - fireEvent.mouseDown(getByLabelText('ra.action.save')); + fireEvent.mouseDown(getByLabelText('Save')); expect(onSubmit).toHaveBeenCalled(); }); @@ -45,31 +45,43 @@ describe('', () => { const onSubmit = jest.fn(); const { getByLabelText } = render( - + + + ); - fireEvent.mouseDown(getByLabelText('ra.action.save')); + fireEvent.mouseDown(getByLabelText('Save')); expect(onSubmit).not.toHaveBeenCalled(); }); + it('should show a notification if the form is not valid', () => { const onSubmit = jest.fn(); - const showNotification = jest.fn(); + let dispatchSpy; const { getByLabelText } = render( - + + {({ store }) => { + dispatchSpy = jest.spyOn(store, 'dispatch'); + return ( + + ); + }} + ); - fireEvent.mouseDown(getByLabelText('ra.action.save')); - expect(showNotification).toHaveBeenCalled(); + fireEvent.mouseDown(getByLabelText('Save')); + expect(dispatchSpy).toHaveBeenCalledWith({ + payload: { + message: 'ra.message.invalid_form', + messageArgs: {}, + type: 'warning', + undoable: false, + }, + type: 'RA/SHOW_NOTIFICATION', + }); expect(onSubmit).toHaveBeenCalled(); }); });