Skip to content
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

fix(datepicker): fix exports #232

Merged
merged 5 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/datepicker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"collection": "dist/collection/collection-manifest.json",
"collection:main": "dist/collection/index.js",
"scripts": {
"build:ci": "npm run build:stencil",
"build:prod": "npm run build:stencil --if-present && npm run build:react --if-present && npm run build:vue --if-present",
"build": "echo \"use build:prod\"",
"build:stencil": "stencil build --docs --prod --config stencil.config.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class OdsDatepickerController {
if(!this.component.disabled) {
if (!this.validateValue(newValue)) {
this.component.value = null;
this.component.datepickerInstance?.setDate({ clear: true });
this.component.datepickerInstanceAccessor?.setDate({ clear: true });
this.component.emitDatepickerValueChange(null, oldValue ? oldValue : null);
} else {
this.component.value = newValue;
this.component.datepickerInstance?.setDate(newValue);
this.component.datepickerInstanceAccessor?.setDate(newValue);
this.component.emitDatepickerValueChange(newValue, oldValue ? oldValue : null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { EventEmitter } from '@stencil/core';
interface OdsDatepickerValueChangeEventDetail {
value?: Date | null;
oldValue?: Date | null;
formattedValue?: string;
}

type OdsDatepickerValueChangeEvent = CustomEvent<OdsDatepickerValueChangeEventDetail>;
Expand Down
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 @@ -24,9 +24,14 @@ export class OsdsDatepicker implements OdsDatepickerAttribute, OdsDatepickerEven
@Element() el!: HTMLElement;

@State() hasFocus = false;
@State() datepickerInstance: Datepicker | undefined = undefined;
@State() hiddenInput: HTMLInputElement | undefined = undefined;
@State() datepickerElement: HTMLElement | undefined = undefined;

private datepickerInstance: Datepicker | undefined = undefined;
get datepickerInstanceAccessor(): Datepicker | undefined {
return this.datepickerInstance;
}

private hiddenInput: HTMLInputElement | undefined = undefined;
private datepickerElement: HTMLElement | undefined = undefined;

/** @see OdsDatepickerAttribute.clearable */
@Prop({ reflect: true }) clearable?: boolean = DEFAULT_ATTRIBUTE.clearable;
Expand Down Expand Up @@ -119,7 +124,7 @@ export class OsdsDatepicker implements OdsDatepickerAttribute, OdsDatepickerEven
}

emitDatepickerValueChange(newValue: Date | undefined | null, oldValue?: Date | undefined | null): void {
this.odsDatepickerValueChange.emit({ value: newValue, oldValue: oldValue });
this.odsDatepickerValueChange.emit({ value: newValue, oldValue: oldValue, formattedValue: newValue && this.format ? Datepicker.formatDate(newValue, this.format) : undefined });
dpellier marked this conversation as resolved.
Show resolved Hide resolved
}

onBlur(): void {
Expand All @@ -135,9 +140,12 @@ export class OsdsDatepicker implements OdsDatepickerAttribute, OdsDatepickerEven
}

componentDidLoad() {
if(!this.el.shadowRoot) {
return;
}
this.initializeDatepicker();
}

initializeDatepicker() {
if(!this.el.shadowRoot) return;
if(this.datepickerInstance) return;

if (this.hiddenInput && !this.hiddenInput.getAttribute('initialized')) {
this.datepickerInstance = new Datepicker(this.hiddenInput, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export type { OdsDatepickerAttribute } from './interfaces/attributes';
export type { OdsDatepickerEvent, OdsDatepickerValueChangeEvent, OdsDatepickerValueChangeEventDetail } from './interfaces/events';
export { ODS_DATEPICKER_DAY, ODS_DATEPICKER_DAYS } from './constants/datepicker-day';
export { OsdsDatepicker } from './osds-datepicker';
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,
};
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"jsx": "react",
"jsxFactory": "h",
"jsxFragmentFactory": "Fragment",
"lib": ["dom", "es2018", "esnext.array"],
"lib": ["dom", "es2018", "esnext.array", "es2019"],
"module": "esnext",
"moduleResolution": "node",
"noImplicitAny": true,
Expand Down