Skip to content

Commit

Permalink
fix(ffe-datepicker-react): added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eksb1 committed Jan 18, 2018
1 parent 71883db commit bd41b39
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/ffe-datepicker-react/src/datepicker/Datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export default class Datepicker extends Component {
}

openCalendarOrLeaveOpen(type) {
return type === (dateErrorTypes.MIN_DATE || dateErrorTypes.MAX_DATE)
return type === dateErrorTypes.MIN_DATE ||
type === dateErrorTypes.MAX_DATE
? this.state.displayDatePicker
: false;
}
Expand Down
104 changes: 104 additions & 0 deletions packages/ffe-datepicker-react/src/datepicker/Datepicker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,108 @@ describe('<Datepicker />', () => {
});
});
});

describe('validate correct visibility of Calendar on DateInput blur', () => {
describe('should not be visible on invalid date value', () => {
beforeEach(() => {
wrapper = mount(
<Datepicker {...defaultProps} value={'iamturtles'} />,
);

wrapper
.find(DateInput)
.find('input')
.simulate('click');
wrapper
.find(DateInput)
.find('input')
.simulate('blur');
});

it('has an error message', () =>
expect(wrapper).to.have.descendants(errorClass));

it('hides error message if hideErrors prop is true', () => {
wrapper.setProps({ hideErrors: true });
expect(wrapper).to.not.have.descendants(errorClass);
});

it('hides calendar', () => {
expect(wrapper).to.not.have.descendants(Calendar);
});
});

describe('should be visible with valid date value', () => {
let errorMessage;

describe('when date is below minimum date', () => {
beforeEach(() => {
wrapper = mount(
<Datepicker
{...defaultProps}
value="31.12.2014"
minDate="01.01.2016"
/>,
);

wrapper
.find(DateInput)
.find('input')
.simulate('click');
wrapper
.find(DateInput)
.find('input')
.simulate('blur');
errorMessage = wrapper.find(errorClass);
});

it('has correct error-class', () =>
expect(wrapper).to.have.descendants(errorClass));

it('has correct error-message', () => {
expect(errorMessage).to.have.text(i18n.nb.MIN_DATE);
expect(wrapper).to.have.descendants(Calendar);
});

it('has calendar open', () =>
expect(wrapper)
.to.have.exactly(1)
.descendants(Calendar));
});

describe('when date is above maximum date', () => {
beforeEach(() => {
wrapper = mount(
<Datepicker
{...defaultProps}
value="31.12.2016"
maxDate="01.01.2016"
/>,
);

wrapper
.find(DateInput)
.find('input')
.simulate('click');

wrapper
.find(DateInput)
.find('input')
.simulate('blur');
errorMessage = wrapper.find(errorClass);
});

it('has correct error-class', () =>
expect(wrapper).to.have.descendants(errorClass));

it('has correct error-message', () =>
expect(errorMessage).to.have.text(i18n.nb.MAX_DATE));

it('has calendar open', () =>
expect(wrapper)
.to.have.exactly(1)
.descendants(Calendar));
});
});
});
});

0 comments on commit bd41b39

Please sign in to comment.