-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[adapters] Fix localisation of the placeholder #6547
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dc7d66b
[luxon] Adapt expended format to the locale
alexfauquette a6547ed
fix eslint
alexfauquette 91bc69a
add tests
alexfauquette 513f490
fix moment locale
alexfauquette b978fdc
test pipeline modification
alexfauquette 19261af
fix lint
alexfauquette File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
packages/x-date-pickers/src/AdapterDateFns/localization.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as React from 'react'; | ||
import TextField from '@mui/material/TextField'; | ||
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker'; | ||
import { screen } from '@mui/monorepo/test/utils/createRenderer'; | ||
import { expect } from 'chai'; | ||
import { createPickerRenderer } from 'test/utils/pickers-utils'; | ||
import fr from 'date-fns/locale/fr'; | ||
import de from 'date-fns/locale/de'; | ||
|
||
const testDate = new Date(2018, 4, 15, 9, 35); | ||
const localizedTexts = { | ||
undefined: { | ||
placeholder: 'mm/dd/yyyy hh:mm (a|p)m', | ||
value: '05/15/2018 09:35 am', | ||
}, | ||
fr: { | ||
placeholder: 'dd/mm/y hh:mm', | ||
value: '15/05/2018 09:35', | ||
}, | ||
de: { | ||
placeholder: 'dd.mm.y hh:mm', | ||
value: '15.05.2018 09:35', | ||
}, | ||
}; | ||
describe('<AdapterDateFns />', () => { | ||
Object.keys(localizedTexts).forEach((localeKey) => { | ||
const localeName = localeKey === 'undefined' ? 'default' : `"${localeKey}"`; | ||
const localeObject = localeKey === 'undefined' ? undefined : { fr, de }[localeKey]; | ||
|
||
describe(`test with the ${localeName} locale`, () => { | ||
const { render, adapter } = createPickerRenderer({ | ||
clock: 'fake', | ||
adapterName: 'date-fns', | ||
locale: localeObject, | ||
}); | ||
|
||
it('should have correct placeholder', () => { | ||
render( | ||
<DateTimePicker | ||
renderInput={(params) => <TextField {...params} />} | ||
value={null} | ||
onChange={() => {}} | ||
disableMaskedInput | ||
/>, | ||
); | ||
|
||
expect(screen.getByRole('textbox')).to.have.attr( | ||
'placeholder', | ||
localizedTexts[localeKey].placeholder, | ||
); | ||
}); | ||
|
||
it('should have well formatted value', () => { | ||
render( | ||
<DateTimePicker | ||
renderInput={(params) => <TextField {...params} />} | ||
value={adapter.date(testDate)} | ||
onChange={() => {}} | ||
disableMaskedInput | ||
/>, | ||
); | ||
|
||
expect(screen.getByRole('textbox')).to.have.value(localizedTexts[localeKey].value); | ||
}); | ||
}); | ||
}); | ||
}); |
67 changes: 67 additions & 0 deletions
67
packages/x-date-pickers/src/AdapterDayjs/localization.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as React from 'react'; | ||
import TextField from '@mui/material/TextField'; | ||
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker'; | ||
import { screen } from '@mui/monorepo/test/utils/createRenderer'; | ||
import { expect } from 'chai'; | ||
import { createPickerRenderer } from 'test/utils/pickers-utils'; | ||
import 'dayjs/locale/fr'; | ||
import 'dayjs/locale/de'; | ||
|
||
const testDate = new Date(2018, 4, 15, 9, 35); | ||
const localizedTexts = { | ||
undefined: { | ||
placeholder: 'mm/dd/yyyy hh:mm (a|p)m', | ||
value: '05/15/2018 09:35 AM', | ||
}, | ||
fr: { | ||
placeholder: 'dd/mm/yyyy hh:mm', | ||
value: '15/05/2018 09:35', | ||
}, | ||
de: { | ||
placeholder: 'dd.mm.yyyy hh:mm', | ||
value: '15.05.2018 09:35', | ||
}, | ||
}; | ||
describe('<AdapterDayjs />', () => { | ||
Object.keys(localizedTexts).forEach((localeKey) => { | ||
const localeName = localeKey === 'undefined' ? 'default' : `"${localeKey}"`; | ||
const localeObject = localeKey === 'undefined' ? undefined : { code: localeKey }; | ||
|
||
describe(`test with the ${localeName} locale`, () => { | ||
const { render, adapter } = createPickerRenderer({ | ||
clock: 'fake', | ||
adapterName: 'dayjs', | ||
locale: localeObject, | ||
}); | ||
|
||
it('should have correct placeholder', () => { | ||
render( | ||
<DateTimePicker | ||
renderInput={(params) => <TextField {...params} />} | ||
value={null} | ||
onChange={() => {}} | ||
disableMaskedInput | ||
/>, | ||
); | ||
|
||
expect(screen.getByRole('textbox')).to.have.attr( | ||
'placeholder', | ||
localizedTexts[localeKey].placeholder, | ||
); | ||
}); | ||
|
||
it('should have well formatted value', () => { | ||
render( | ||
<DateTimePicker | ||
renderInput={(params) => <TextField {...params} />} | ||
value={adapter.date(testDate)} | ||
onChange={() => {}} | ||
disableMaskedInput | ||
/>, | ||
); | ||
|
||
expect(screen.getByRole('textbox')).to.have.value(localizedTexts[localeKey].value); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/x-date-pickers/src/AdapterLuxon/localization.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as React from 'react'; | ||
import TextField from '@mui/material/TextField'; | ||
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker'; | ||
import { screen } from '@mui/monorepo/test/utils/createRenderer'; | ||
import { expect } from 'chai'; | ||
import { createPickerRenderer } from 'test/utils/pickers-utils'; | ||
|
||
const testDate = new Date(2018, 4, 15, 9, 35); | ||
const localizedTexts = { | ||
undefined: { | ||
placeholder: 'm/d/yyyy hh:mm (a|p)m', | ||
value: '5/15/2018 09:35 AM', | ||
}, | ||
fr: { | ||
placeholder: 'd/m/yyyy h:m', | ||
value: '15/05/2018 09:35', | ||
}, | ||
de: { | ||
placeholder: 'd.m.yyyy h:m', | ||
value: '15.5.2018 09:35', | ||
}, | ||
}; | ||
describe('<AdapterLuxon />', () => { | ||
Object.keys(localizedTexts).forEach((localeKey) => { | ||
const localeName = localeKey === 'undefined' ? 'default' : `"${localeKey}"`; | ||
const localeObject = localeKey === 'undefined' ? undefined : { code: localeKey }; | ||
|
||
describe(`test with the ${localeName} locale`, () => { | ||
const { render, adapter } = createPickerRenderer({ | ||
clock: 'fake', | ||
adapterName: 'luxon', | ||
locale: localeObject, | ||
}); | ||
|
||
it('should have correct placeholder', () => { | ||
render( | ||
<DateTimePicker | ||
renderInput={(params) => <TextField {...params} />} | ||
value={null} | ||
onChange={() => {}} | ||
disableMaskedInput | ||
/>, | ||
); | ||
|
||
expect(screen.getByRole('textbox')).to.have.attr( | ||
'placeholder', | ||
localizedTexts[localeKey].placeholder, | ||
); | ||
}); | ||
|
||
it('should have well formatted value', () => { | ||
render( | ||
<DateTimePicker | ||
renderInput={(params) => <TextField {...params} />} | ||
value={adapter.date(testDate)} | ||
onChange={() => {}} | ||
disableMaskedInput | ||
/>, | ||
); | ||
|
||
expect(screen.getByRole('textbox')).to.have.value(localizedTexts[localeKey].value); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/x-date-pickers/src/AdapterMoment/localization.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import * as React from 'react'; | ||
import TextField from '@mui/material/TextField'; | ||
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker'; | ||
import { screen } from '@mui/monorepo/test/utils/createRenderer'; | ||
import { expect } from 'chai'; | ||
import { createPickerRenderer } from 'test/utils/pickers-utils'; | ||
import 'moment/locale/de'; | ||
import 'moment/locale/fr'; | ||
|
||
const testDate = new Date(2018, 4, 15, 9, 35); | ||
const localizedTexts = { | ||
en: { | ||
placeholder: 'mm/dd/yyyy hh:mm', | ||
value: '05/15/2018 09:35', | ||
}, | ||
fr: { | ||
placeholder: 'dd/mm/yyyy hh:mm', | ||
value: '15/05/2018 09:35', | ||
}, | ||
de: { | ||
placeholder: 'dd.mm.yyyy hh:mm', | ||
value: '15.05.2018 09:35', | ||
}, | ||
}; | ||
describe('<AdapterMoment />', () => { | ||
Object.keys(localizedTexts).forEach((localeKey) => { | ||
const localeObject = { code: localeKey }; | ||
|
||
describe(`test with the locale "${localeKey}"`, () => { | ||
const { render, adapter } = createPickerRenderer({ | ||
clock: 'fake', | ||
adapterName: 'moment', | ||
locale: localeObject, | ||
}); | ||
|
||
it('should have correct placeholder', () => { | ||
render( | ||
<DateTimePicker | ||
renderInput={(params) => <TextField {...params} />} | ||
value={null} | ||
onChange={() => {}} | ||
disableMaskedInput | ||
/>, | ||
); | ||
|
||
expect(screen.getByRole('textbox')).to.have.attr( | ||
'placeholder', | ||
localizedTexts[localeKey].placeholder, | ||
); | ||
}); | ||
|
||
it('should have well formatted value', () => { | ||
render( | ||
<DateTimePicker | ||
renderInput={(params) => <TextField {...params} />} | ||
value={adapter.date(testDate)} | ||
onChange={() => {}} | ||
disableMaskedInput | ||
/>, | ||
); | ||
|
||
expect(screen.getByRole('textbox')).to.have.value(localizedTexts[localeKey].value); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,12 @@ import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns'; | |
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'; | ||
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon'; | ||
import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment'; | ||
import { MuiPickersAdapter } from '@mui/x-date-pickers/internals'; | ||
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; | ||
import sinon from 'sinon'; | ||
import { useControlled } from '@mui/material/utils'; | ||
|
||
const availableAdapters = { | ||
const availableAdapters: { [key: string]: new (...args: any) => MuiPickersAdapter<any> } = { | ||
'date-fns': AdapterDateFns, | ||
dayjs: AdapterDayjs, | ||
luxon: AdapterLuxon, | ||
|
@@ -61,6 +62,7 @@ export const FakeTransitionComponent = React.forwardRef<HTMLDivElement, Transiti | |
interface CreatePickerRendererOptions extends CreateRendererOptions { | ||
// Set-up locale with date-fns object. Other are deduced from `locale.code` | ||
locale?: Locale; | ||
adapterName?: 'date-fns' | 'dayjs' | 'luxon' | 'moment'; | ||
} | ||
|
||
export function wrapPickerMount(mount: (node: React.ReactNode) => import('enzyme').ReactWrapper) { | ||
|
@@ -70,18 +72,25 @@ export function wrapPickerMount(mount: (node: React.ReactNode) => import('enzyme | |
|
||
export function createPickerRenderer({ | ||
locale, | ||
adapterName, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can run the entire test codebase with any of the adapters. But only some of them are relevant to be run on various adapters. That's why I add this parameter. By priority, tests run with the adapter provided by:
|
||
...createRendererOptions | ||
}: CreatePickerRendererOptions = {}) { | ||
const { clock, render: clientRender } = createRenderer(createRendererOptions); | ||
|
||
let adapterLocale = adapterToUse.lib === 'date-fns' ? locale : locale?.code; | ||
let adapterLocale = (adapterName ?? adapterToUse.lib) === 'date-fns' ? locale : locale?.code; | ||
|
||
if (typeof adapterLocale === 'string' && adapterLocale.length > 2) { | ||
adapterLocale = adapterLocale.slice(0, 2); | ||
} | ||
const adapter = adapterName | ||
? new availableAdapters[adapterName]({ locale: adapterLocale }) | ||
: new AdapterClassToUse({ locale: adapterLocale }); | ||
function Wrapper({ children }: { children?: React.ReactNode }) { | ||
return ( | ||
<LocalizationProvider adapterLocale={adapterLocale} dateAdapter={AdapterClassToUse}> | ||
<LocalizationProvider | ||
adapterLocale={adapterLocale} | ||
dateAdapter={adapterName ? availableAdapters[adapterName] : AdapterClassToUse} | ||
> | ||
{children} | ||
</LocalizationProvider> | ||
); | ||
|
@@ -92,6 +101,7 @@ export function createPickerRenderer({ | |
render(node: React.ReactElement, options?: Omit<RenderOptions, 'wrapper'>) { | ||
return clientRender(node, { ...options, wrapper: Wrapper }); | ||
}, | ||
adapter, | ||
}; | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed because otherwise the default locale of moment will be applied (the last imported one)
It's a part of code copypasted from date-io. after this PR I will add the modification to date-io