Skip to content

Commit

Permalink
fix(phone-number): trigger event
Browse files Browse the repository at this point in the history
  • Loading branch information
aesteves60 authored and dpellier committed Nov 16, 2023
1 parent 1e6a59f commit 9104220
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,49 @@ describe('e2e:osds-phone-number', () => {
});
});

it('should not receive event odsValueChange on type in input', async() => {
it('should receive event odsValueChange with invalid validity', async() => {
await setup({ attributes: { isoCode: ODS_COUNTRY_ISO_CODE.FR }, cbkInterceptorRequest: myCbk });

const spyOdsValueChange = await page.spyOnEvent('odsValueChange');

await input.type('0');
await page.waitForChanges();
expect(spyOdsValueChange).toHaveReceivedEventTimes(0);
expect(spyOdsValueChange).toHaveReceivedEventTimes(1);
expect(spyOdsValueChange).toHaveReceivedEventDetail({
isoCode: 'fr',
value: '0',
validity: {
customError: false,
forbiddenValue: false,
invalid: true,
stepMismatch: false,
valid: false,
valueMissing: false,
},
});
});

it('should receive event odsValueChange with invalid validity', async() => {
await setup({ attributes: { isoCode: ODS_COUNTRY_ISO_CODE.FR }, cbkInterceptorRequest: myCbk });

const spyOdsValueChange = await page.spyOnEvent('odsValueChange');

await input.type('0642');
await page.waitForChanges();
expect(spyOdsValueChange).toHaveReceivedEventTimes(4);
expect(spyOdsValueChange).toHaveReceivedEventDetail({
isoCode: 'fr',
value: '0642',
oldValue: '064',
validity: {
customError: false,
forbiddenValue: false,
invalid: true,
stepMismatch: false,
valid: false,
valueMissing: false,
},
});
});

it('should receive event odsValueChange with options selected', async() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ describe('spec:osds-phone-number', () => {
expect(instance.value).toBe(detail.value);
expect(instance.error).toBe(false);
expect(spyEmitOdsValueChange).toHaveBeenCalledTimes(1);
expect(spyEmitOdsValueChange).toHaveBeenCalledWith({
value: '+33653535353',
isoCode: 'fr',
validity: {
invalid: false,
valid: true,
},
});
});

it('should handler the input change value with invalid value', async() => {
Expand All @@ -257,7 +265,15 @@ describe('spec:osds-phone-number', () => {
instance.handlerOdsValueChange(new CustomEvent('odsValueChange', { detail }));
expect(instance.value).toBe(detail.value);
expect(instance.error).toBe(true);
expect(spyEmitOdsValueChange).toHaveBeenCalledTimes(0);
expect(spyEmitOdsValueChange).toHaveBeenCalledTimes(1);
expect(spyEmitOdsValueChange).toHaveBeenCalledWith({
value: '065353qsd5353',
isoCode: 'fr',
validity: {
invalid: true,
valid: false,
},
});
});

it('should handler the input change value with empty value', async() => {
Expand All @@ -268,6 +284,14 @@ describe('spec:osds-phone-number', () => {
expect(instance.value).toBe(detail.value);
expect(instance.error).toBe(false);
expect(spyEmitOdsValueChange).toHaveBeenCalledTimes(1);
expect(spyEmitOdsValueChange).toHaveBeenCalledWith({
value: '',
isoCode: 'fr',
validity: {
invalid: false,
valid: true,
},
});
});

it('should emit an event with old value', async() => {
Expand All @@ -280,6 +304,10 @@ describe('spec:osds-phone-number', () => {
value: '+33653535353',
oldValue: '+330612345',
isoCode: 'fr',
validity: {
invalid: false,
valid: true,
},
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,29 @@ export class OsdsPhoneNumber implements OdsPhoneNumberAttribute, OdsPhoneNumberE

private handlerInputEvent(event: OdsInputValueChangeEventDetail) {
this.value = event.value || '';
if(!this.isValidValue(this.value)) {
this.error = true;
return;
}
this.error = !this.isValidValue(this.value);
const number = this.controller.parseNumber(event.value);
const oldNumber =this.controller.parseNumber(event.oldValue);
const oldNumber = this.controller.parseNumber(event.oldValue);
this.odsValueChange.emit({
...event,
value: number && this.phoneUtils.format(number, PhoneNumberFormat.E164),
oldValue: oldNumber && this.phoneUtils.format(oldNumber, PhoneNumberFormat.E164) || undefined,
value: this.formatValue(number, this.value),
oldValue: this.formatValue(oldNumber, event.oldValue) ,
isoCode: this.isoCode,
validity: {
...event.validity,
invalid: this.error,
valid: !this.error,
},
});
}

private formatValue(number: PhoneNumber | null, defaultValue?: string | null): string | undefined {
if (this.error || !number) {
return defaultValue ?? undefined;
}
return this.phoneUtils.format(number, PhoneNumberFormat.E164)
}

@Watch('value')
private validateValue(): void {
this.error = !this.isValidValue(this.value);
Expand Down
3 changes: 1 addition & 2 deletions packages/components/phone-number/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@
<br/>
<br/>
<osds-phone-number disabled value="0642665892"></osds-phone-number>

<script>
const phoneNumber = document.getElementById('phoneNumber');
phoneNumber.addEventListener('odsValueChange', (event) => {
console.log('event', event);
console.log('event', event.detail);
})
</script>
</body>
Expand Down

0 comments on commit 9104220

Please sign in to comment.