Skip to content

Commit

Permalink
fix(datepicker): addressing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Leotheluck authored and dpellier committed Oct 11, 2023
1 parent e0d5a13 commit 13c7aae
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ODS_THEME_COLOR_INTENT } from '@ovhcloud/ods-common-theming';
import { OsdsDatepicker } from './osds-datepicker';
import { OdsDatepickerController } from './core/controller';
import { ODS_DATEPICKER_DAY } from './constants/datepicker-day';
import { Datepicker } from '../../jestStub';

describe('spec:osds-datepicker', () => {
let page: SpecPage;
Expand Down Expand Up @@ -278,12 +279,51 @@ describe('spec:osds-datepicker', () => {
expect(spy).toHaveBeenCalled();
});

it('should emit odsDatepickerValueChange event with newValue and oldValue', () => {
const spy = jest.spyOn(instance.odsDatepickerValueChange, 'emit');
const newValue = new Date('2023-10-03');
const oldValue = new Date('2023-10-02');
instance.emitDatepickerValueChange(newValue, oldValue);
expect(spy).toHaveBeenCalledWith({ value: newValue, oldValue: oldValue });
describe('emitDatepickerValueChange', () => {
let mockFormatDate: jest.SpyInstance;

beforeAll(() => {
mockFormatDate = jest.spyOn(Datepicker, 'formatDate');
});

afterEach(() => {
mockFormatDate.mockClear();
});

afterAll(() => {
mockFormatDate.mockRestore();
});

beforeEach(async () => {
await setup({});
});

it('should emit odsDatepickerValueChange event with newValue and oldValue', () => {
const spy = jest.spyOn(instance.odsDatepickerValueChange, 'emit');
const newValue = new Date('2023-10-03');
const oldValue = new Date('2023-10-02');
instance.emitDatepickerValueChange(newValue, oldValue);
expect(spy).toHaveBeenCalledWith({ value: newValue, oldValue: oldValue, formattedValue: `${newValue} dd/mm/yyyy` });
});

it('should call Datepicker.formatDate when format is defined', () => {
const testDate = new Date('2023-10-03');
instance.format = 'dd/mm/yyyy';

instance.emitDatepickerValueChange(testDate);

expect(mockFormatDate).toHaveBeenCalledWith(testDate, 'dd/mm/yyyy');
});

it('should emit event with undefined formattedValue when format is not defined', () => {
const testDate = new Date('2023-10-03');
instance.format = undefined;

const spy = jest.spyOn(instance.odsDatepickerValueChange, 'emit');
instance.emitDatepickerValueChange(testDate);

expect(spy).toHaveBeenCalledWith({ value: testDate, oldValue: undefined, formattedValue: undefined });
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export class OsdsDatepicker implements OdsDatepickerAttribute, OdsDatepickerEven
placeholder={placeholder}
type={ODS_INPUT_TYPE.text}
value={this.formatDate(value)}
style={{ '--placeholder-color': 'red' }}
></osds-input>
<input
tabindex={-1}
Expand Down
5 changes: 3 additions & 2 deletions packages/components/datepicker/src/jestStub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ class Stub {

}

static formatDate() {

static formatDate(date, format) {
return `${date} ${format}`;
}

static parseDate() {

}
}

// eslint-disable-next-line no-undef
module.exports = {
Datepicker: Stub,
};

0 comments on commit 13c7aae

Please sign in to comment.