Skip to content

Commit

Permalink
fix: add oldValue to all form element
Browse files Browse the repository at this point in the history
  • Loading branch information
aesteves60 authored and dpellier committed Jul 29, 2024
1 parent 0321d35 commit ef348bc
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/ods/src/components/datepicker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lint:scss": "stylelint 'src/components/**/*.scss'",
"lint:ts": "eslint '{src,tests}/**/*.{js,ts,tsx}'",
"start": "stencil build --dev --watch --serve",
"test:e2e": "stencil test --e2e --config stencil.config.ts",
"test:e2e": "stencil test --e2e --runInBand --config stencil.config.ts",
"test:e2e:ci": "tsc --noEmit && stencil test --e2e --ci --runInBand --config stencil.config.ts",
"test:spec": "stencil test --spec --config stencil.config.ts --coverage",
"test:spec:ci": "tsc --noEmit && stencil test --config stencil.config.ts --spec --ci --coverage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,17 @@ export class OdsDatepicker {
// Manage the component value change
// Triggered either by user selection or `setDate` instance method call
this.inputElement.addEventListener('changeDate', (event: Event) => {
const newDate = (event as CustomEvent).detail.date;
const newDate: Date = (event as CustomEvent).detail.date;
const formattedNewDate = formatDate(newDate, this.format);

const oldValue = this.value;
this.value = newDate;
setFormValue(this.internals, formattedNewDate);

this.odsChange.emit({
formattedValue: formattedNewDate,
name: this.name,
oldValue,
validity: this.inputElement?.validity,
value: this.value,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface OdsDatepickerEventChangeDetail {
formattedValue?: string;
name: string;
oldValue?: Date | null;
validity?: ValidityState;
value?: Date | null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,31 @@ describe('ods-datepicker behaviour', () => {
});
});
});

describe('event', () => {
describe('odsChange', () => {
it('should emit an odsChange event', async() => {
const value = new Date('10 May 2024');
await setup('<ods-datepicker name="ods-datepicker"></ods-datepicker>');
await page.evaluate((value) => {
document.querySelector<OdsDatepicker & HTMLElement>('ods-datepicker')!.value = value;
}, value);
const odsOdsChangeSpy = await page.spyOnEvent('odsChange');

await page.keyboard.press('Tab');
await page.keyboard.type('11/05/2024');
await page.keyboard.press('Enter');
await page.waitForChanges();

expect(odsOdsChangeSpy).toHaveReceivedEventTimes(1);
expect(odsOdsChangeSpy).toHaveReceivedEventDetail({
formattedValue: '11/05/2024',
name: 'ods-datepicker',
oldValue: value.toISOString(),
validity: {},
value: '2024-05-10T22:00:00.000Z',
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,20 @@ describe('ods-input behaviour', () => {
describe('event:odsChange ', () => {
it('should receive odsChange event', async() => {
const typeValue = 'some text';
await setup('<ods-input></ods-input>');
await setup('<ods-input name="ods-input"></ods-input>');
const odsChangeSpy = await page.spyOnEvent('odsChange');

await part.type(typeValue);
await page.waitForChanges();

expect(await el.getProperty('value')).toBe(typeValue);
expect(odsChangeSpy).toHaveReceivedEventTimes(typeValue.length);
expect(odsChangeSpy).toHaveReceivedEventDetail({
name: 'ods-input',
oldValue: 'some tex',
validity: {},
value: 'some text',
});
});

it('should do nothing because of disabled', async() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('ods-password behaviour', () => {
describe('event:odsChange ', () => {
it('should receive odsChange event', async() => {
const typeValue = 'some text';
await setup('<ods-password></ods-password>');
await setup('<ods-password name="ods-password"></ods-password>');
const odsChangeSpy = await page.spyOnEvent('odsChange');

await page.keyboard.press('Tab');
Expand All @@ -66,6 +66,12 @@ describe('ods-password behaviour', () => {

expect(await el.getProperty('value')).toBe(typeValue);
expect(odsChangeSpy).toHaveReceivedEventTimes(typeValue.length);
expect(odsChangeSpy).toHaveReceivedEventDetail({
name: 'ods-password',
oldValue: 'some tex',
validity: {},
value: 'some text',
});
});

it('should do nothing because of disabled', async() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class OdsPhoneNumber {
this.odsChange.emit({
isoCode: this.isoCode,
name: this.name,
oldValue: event.detail.oldValue?.toString(),
validity: getValidityState(this.hasError, event.detail.validity),
value: formattedValue,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,52 @@ describe('ods-phone-number behaviour', () => {
});
});
});

describe('event', () => {
describe('odsChange', () => {
it('should emit an odsChange event', async() => {
await setup('<ods-phone-number name="ods-phone-number" iso-code="fr"></ods-phone-number>');
const odsChangeSpy = await page.spyOnEvent('odsChange');

const newValue = '0987654321';
await page.keyboard.press('Tab');
await page.keyboard.type(newValue);
await page.waitForChanges();

expect(await el.getProperty('value')).toBe(newValue);
expect(odsChangeSpy).toHaveReceivedEventTimes(newValue.length);
expect(odsChangeSpy).toHaveReceivedEventDetail({
isoCode: 'fr',
name: 'ods-phone-number',
oldValue: newValue.slice(0, -1),
validity: {
badInput: false,
customError: false,
patternMismatch: false,
rangeOverflow: false,
rangeUnderflow: false,
stepMismatch: false,
tooLong: false,
tooShort: false,
typeMismatch: false,
valid: true,
valueMissing: false,
},
value: `+33${newValue.substring(1)}`,
});
});

it('should not emit an odsChange event if disabled', async() => {
await setup('<ods-phone-number is-disabled name="ods-phone-number" iso-code="fr"></ods-phone-number>');
const odsChangeSpy = await page.spyOnEvent('odsChange');

const newValue = '0987654321';
await page.keyboard.press('Tab');
await page.keyboard.type(newValue);
await page.waitForChanges();

expect(odsChangeSpy).toHaveReceivedEventTimes(0);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,33 @@ describe('ods-quantity behaviour', () => {

describe('button minus', () => {
it('should decrement value', async() => {
await setup('<ods-quantity value="0"></ods-quantity>');
await setup('<ods-quantity name="ods-quantity" value="0"></ods-quantity>');
const odsChangeSpy = await page.spyOnEvent('odsChange');
await buttonMinus.click();
await page.waitForChanges();
expect(await el.getProperty('value')).toBe(-1);
expect(odsChangeSpy).toHaveReceivedEventTimes(1);
expect(odsChangeSpy).toHaveReceivedEventDetail({
name: 'ods-quantity',
oldValue: 0,
validity: {},
value: -1,
});
});

it('should decrement value with step', async() => {
await setup('<ods-quantity value="0" step="10"></ods-quantity>');
await setup('<ods-quantity name="ods-quantity" value="0" step="10"></ods-quantity>');
const odsChangeSpy = await page.spyOnEvent('odsChange');
await buttonMinus.click();
await page.waitForChanges();
expect(await el.getProperty('value')).toBe(-10);
expect(odsChangeSpy).toHaveReceivedEventTimes(1);
expect(odsChangeSpy).toHaveReceivedEventDetail({
name: 'ods-quantity',
oldValue: 0,
validity: {},
value: -10,
});
});

it('should not decrement value with is-disabled', async() => {
Expand Down Expand Up @@ -103,21 +115,33 @@ describe('ods-quantity behaviour', () => {

describe('button add', () => {
it('should increment value', async() => {
await setup('<ods-quantity value="0"></ods-quantity>');
await setup('<ods-quantity name="ods-quantity" value="0"></ods-quantity>');
const odsChangeSpy = await page.spyOnEvent('odsChange');
await buttonAdd.click();
await page.waitForChanges();
expect(await el.getProperty('value')).toBe(1);
expect(odsChangeSpy).toHaveReceivedEventTimes(1);
expect(odsChangeSpy).toHaveReceivedEventDetail({
name: 'ods-quantity',
oldValue: 0,
validity: {},
value: 1,
});
});

it('should increment value with step', async() => {
await setup('<ods-quantity value="0" step="10"></ods-quantity>');
await setup('<ods-quantity name="ods-quantity" value="0" step="10"></ods-quantity>');
const odsChangeSpy = await page.spyOnEvent('odsChange');
await buttonAdd.click();
await page.waitForChanges();
expect(await el.getProperty('value')).toBe(10);
expect(odsChangeSpy).toHaveReceivedEventTimes(1);
expect(odsChangeSpy).toHaveReceivedEventDetail({
name: 'ods-quantity',
oldValue: 0,
validity: {},
value: 10,
});
});

it('should not increment value with is-disabled', async() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class OdsRadio {
return document.querySelectorAll(`ods-radio[name="${this.name}"]`);
}

private onInput(event: Event): void {
private onInput(event: InputEvent): void {
this.odsChange.emit({
checked: (event.target as HTMLInputElement)?.checked,
name: this.name,
Expand All @@ -84,7 +84,7 @@ export class OdsRadio {
checked={ this.isChecked }
disabled={ this.isDisabled }
onBlur={ (): CustomEvent<void> => this.odsBlur.emit() }
onInput={ (event: Event): void => this.onInput(event) }
onInput={ (event: InputEvent): void => this.onInput(event) }
onFocus={ (): CustomEvent<void> => this.odsFocus.emit() }
id={ this.inputId }
name={ this.name }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,20 @@ describe('ods-textarea behaviour', () => {
describe('on value change', () => {
it('should emit an odsChange event', async() => {
const dummyValue = 'dummy value';
await setup('<ods-textarea></ods-textarea>');
await setup('<ods-textarea name="ods-textarea"></ods-textarea>');
const odsValueChangeSpy = await page.spyOnEvent('odsChange');

await textarea.type(dummyValue);
await page.waitForChanges();

expect(await el.getProperty('value')).toBe(dummyValue);
expect(odsValueChangeSpy).toHaveReceivedEventTimes(dummyValue.length);
expect(odsValueChangeSpy).toHaveReceivedEventDetail({
name: 'ods-textarea',
oldValue: dummyValue.slice(0, -1),
validity: {},
value: dummyValue,
});
});

it('should do nothing if disabled', async() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class OdsTimepicker {
private defaultCurrentTimezone?: OdsTimezone;
private odsInput?: OdsInput & HTMLElement;
private odsSelect?: OdsSelect & HTMLElement;
private oldValue?: string | null;
private timezonesList: OdsTimezone[] = [];

@Element() el!: HTMLElement;
Expand Down Expand Up @@ -74,6 +75,7 @@ export class OdsTimepicker {
const value = formatValue(this.odsInput?.value as string, this.withSeconds);

if (value) {
this.oldValue = this.value ?? null;
this.value = value;
}
setFormValue(this.internals, this.value);
Expand All @@ -83,6 +85,7 @@ export class OdsTimepicker {
if (!this.value) {
this.value = this.defaultValue ?? null;
}
console.log('this.value init', this.value)
this.initTimezones();
this.formatValue();
this.defaultCurrentTimezone = this.currentTimezone;
Expand All @@ -109,12 +112,13 @@ export class OdsTimepicker {
if (isFromSelect) {
this.currentTimezone = event.detail.value as OdsTimezone;
} else {
this.oldValue = event.detail.oldValue as string;
this.value = event.detail.value as string;
}
this.odsChange.emit({
currentTimezone: this.currentTimezone,
name: this.name,
oldValue: '',
oldValue: this.oldValue ?? undefined,
validity: await this.odsInput?.getValidity(),
value: this.value ?? '',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,22 @@ describe('ods-timepicker behavior', () => {

describe('events', () => {
it('should call odsChange event', async() => {
const value = '12:12';
await setup('<ods-timepicker value="11:11"></ods-timepicker>');
const newValue = '12:12';
const value = '11:11';
await setup(`<ods-timepicker name="ods-timepicker" value="${value}"></ods-timepicker>`);
const odsChangeSpy = await page.spyOnEvent('odsChange');

await el.setProperty('value', value);
await el.setProperty('value', newValue);
await page.waitForChanges();

expect(await el.getProperty('value')).toBe(value);
expect(await el.getProperty('value')).toBe(newValue);
expect(odsChangeSpy).toHaveReceivedEventTimes(1);
expect(odsChangeSpy).toHaveReceivedEventDetail({
name: 'ods-timepicker',
oldValue: value,
validity: {},
value: newValue,
});
});
});
});

0 comments on commit ef348bc

Please sign in to comment.