Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Mar 11, 2025
1 parent ba2c7d6 commit 1977df2
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 380 deletions.
192 changes: 0 additions & 192 deletions packages/x-date-pickers/src/DateField/tests/editing.DateField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,198 +17,6 @@ describe('<DateField /> - Editing', () => {
'value props (value, defaultValue, onChange)',
DateField,
({ adapter, renderWithProps, clock }) => {
it('should not render any value when no value and no default value are defined', () => {
// Test with accessible DOM structure
let view = renderWithProps({
enableAccessibleFieldDOMStructure: true,
});

expectFieldValueV7(view.getSectionsContainer(), 'MM/DD/YYYY');

view.unmount();

// Test with non-accessible DOM structure
view = renderWithProps({
enableAccessibleFieldDOMStructure: false,
});

expectFieldValueV6(getTextbox(), '');
});

it('should use the default value when defined', () => {
// Test with accessible DOM structure
let view = renderWithProps({
enableAccessibleFieldDOMStructure: true,
defaultValue: adapter.date('2022-06-04'),
});

expectFieldValueV7(view.getSectionsContainer(), '06/04/2022');

view.unmount();

// Test with non-accessible DOM structure
view = renderWithProps({
enableAccessibleFieldDOMStructure: false,
defaultValue: adapter.date('2022-06-04'),
});

expectFieldValueV6(getTextbox(), '06/04/2022');
});

it('should use the controlled value instead of the default value when both are defined', () => {
// Test with accessible DOM structure
let view = renderWithProps({
enableAccessibleFieldDOMStructure: true,
value: adapter.date('2022-06-04'),
defaultValue: adapter.date('2023-06-04'),
});

expectFieldValueV7(view.getSectionsContainer(), '06/04/2022');

view.unmount();

// Test with non-accessible DOM structure
view = renderWithProps({
enableAccessibleFieldDOMStructure: false,
value: adapter.date('2022-06-04'),
defaultValue: adapter.date('2023-06-04'),
});

expectFieldValueV6(getTextbox(), '06/04/2022');
});

it('should use the controlled value instead of the default value when both are defined and the controlled value is null', () => {
// Test with accessible DOM structure
let view = renderWithProps({
enableAccessibleFieldDOMStructure: true,
value: null,
defaultValue: adapter.date('2023-06-04'),
});

expectFieldValueV7(view.getSectionsContainer(), 'MM/DD/YYYY');

view.unmount();

// Test with non-accessible DOM structure
view = renderWithProps({
enableAccessibleFieldDOMStructure: false,
value: null,
defaultValue: adapter.date('2023-06-04'),
});

expectFieldValueV6(getTextbox(), '');
});

it('should render nothing if neither `props.defaultValue` or `props.value` are passed', () => {
// Test with accessible DOM structure
let view = renderWithProps({
enableAccessibleFieldDOMStructure: true,
});

expectFieldValueV7(view.getSectionsContainer(), 'MM/DD/YYYY');

view.unmount();

// Test with non-accessible DOM structure
view = renderWithProps({
enableAccessibleFieldDOMStructure: false,
value: null,
defaultValue: adapter.date('2023-06-04'),
});

expectFieldValueV6(getTextbox(), '');
});

it('should react to controlled value update (from non null to another non null)', () => {
// Test with accessible DOM structure
let view = renderWithProps({
enableAccessibleFieldDOMStructure: true,
value: adapter.date('2022-06-04'),
});

expectFieldValueV7(view.getSectionsContainer(), '06/04/2022');

view.setProps({
value: adapter.date('2023-06-04'),
});
expectFieldValueV7(view.getSectionsContainer(), '06/04/2023');

view.unmount();

// Test with non-accessible DOM structure
view = renderWithProps({
enableAccessibleFieldDOMStructure: false,
value: adapter.date('2022-06-04'),
});

expectFieldValueV6(getTextbox(), '06/04/2022');

view.setProps({
value: adapter.date('2023-06-04'),
});
expectFieldValueV6(getTextbox(), '06/04/2023');
});

it('should react to a controlled value update (from non null to null)', () => {
// Test with accessible DOM structure
let view = renderWithProps({
enableAccessibleFieldDOMStructure: true,
value: adapter.date('2022-06-04'),
});

expectFieldValueV7(view.getSectionsContainer(), '06/04/2022');

view.setProps({
value: null,
});
expectFieldValueV7(view.getSectionsContainer(), 'MM/DD/YYYY');

view.unmount();

// Test with non-accessible DOM structure
view = renderWithProps({
enableAccessibleFieldDOMStructure: false,
value: adapter.date('2022-06-04'),
});

expectFieldValueV6(getTextbox(), '06/04/2022');

view.setProps({
value: null,
});
expectFieldValueV6(getTextbox(), '');
});

it('should react to a controlled value update (from null to non null', () => {
// Test with accessible DOM structure
let view = renderWithProps({
enableAccessibleFieldDOMStructure: true,
value: null,
});

expectFieldValueV7(view.getSectionsContainer(), 'MM/DD/YYYY');

view.setProps({
value: adapter.date('2022-06-04'),
});
expectFieldValueV7(view.getSectionsContainer(), '06/04/2022');

view.unmount();

// Test with non-accessible DOM structure
view = renderWithProps({
enableAccessibleFieldDOMStructure: false,
value: null,
});

expectFieldValueV6(getTextbox(), '');

view.setProps({
value: adapter.date('2022-06-04'),
});
expectFieldValueV6(getTextbox(), '06/04/2022');
});

it('should call the onChange callback when the value is updated but should not change the displayed value if the value is controlled', () => {
// Test with accessible DOM structure
const onChangeV7 = spy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ describe('<YearCalendar /> - Describe Value', () => {
assertRenderedValue: (expectedValue: any) => {
const activeYear = screen
.queryAllByRole('radio')
.find((cell) => cell.getAttribute('tabindex') === '0');
expect(activeYear).not.to.equal(null);
.find((cell) => cell.getAttribute('aria-checked') === 'true');

if (expectedValue == null) {
expect(activeYear).to.have.text(adapterToUse.getYear(adapterToUse.date()).toString());
expect(activeYear).to.equal(undefined);
} else {
expect(activeYear).not.to.equal(undefined);
expect(activeYear).to.have.text(adapterToUse.getYear(expectedValue).toString());
expect(activeYear).to.have.attribute('aria-checked', 'true');
}
},
setNewValue: (value) => {
Expand Down
Loading

0 comments on commit 1977df2

Please sign in to comment.