From 3979ddc40af1325f5cc7889c6f0f277ccdb110c9 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Thu, 17 Feb 2022 16:35:07 +0800 Subject: [PATCH] fix(AddressEdit): selectedOption maybe undefined --- .../vant/src/address-edit/AddressEdit.tsx | 8 +- .../test/__snapshots__/demo.spec.ts.snap | 148 +++++++ .../test/__snapshots__/index.spec.js.snap | 373 ++++++++++++++++++ .../test/{demo.legacy.ts => demo.spec.ts} | 0 .../test/{index.legacy.js => index.spec.js} | 26 -- 5 files changed, 526 insertions(+), 29 deletions(-) create mode 100644 packages/vant/src/address-edit/test/__snapshots__/demo.spec.ts.snap create mode 100644 packages/vant/src/address-edit/test/__snapshots__/index.spec.js.snap rename packages/vant/src/address-edit/test/{demo.legacy.ts => demo.spec.ts} (100%) rename packages/vant/src/address-edit/test/{index.legacy.js => index.spec.js} (88%) diff --git a/packages/vant/src/address-edit/AddressEdit.tsx b/packages/vant/src/address-edit/AddressEdit.tsx index fa4ba1048..392a4b166 100644 --- a/packages/vant/src/address-edit/AddressEdit.tsx +++ b/packages/vant/src/address-edit/AddressEdit.tsx @@ -198,7 +198,7 @@ export default defineComponent({ Toast(t('areaEmpty')); } else { showAreaPopup.value = false; - assignAreaText(selectedOptions); + assignAreaText(selectedOptions as PickerOption[]); emit('changeArea', selectedOptions); } }; @@ -257,9 +257,11 @@ export default defineComponent({ const options = areaRef.value?.getSelectedOptions(); if ( options && - options.every((option) => option.value !== AREA_EMPTY_CODE) + options.every( + (option) => option && option.value !== AREA_EMPTY_CODE + ) ) { - assignAreaText(options); + assignAreaText(options as PickerOption[]); } }); }, diff --git a/packages/vant/src/address-edit/test/__snapshots__/demo.spec.ts.snap b/packages/vant/src/address-edit/test/__snapshots__/demo.spec.ts.snap new file mode 100644 index 000000000..cfbf956f8 --- /dev/null +++ b/packages/vant/src/address-edit/test/__snapshots__/demo.spec.ts.snap @@ -0,0 +1,148 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render demo and match snapshot 1`] = ` +
+
+
+
+
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+ +
+
+ + +
+
+
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+ +
+
+
+
+
+
+ + Set as the default address + +
+
+
+
+
+
+
+ + +
+
+
+`; diff --git a/packages/vant/src/address-edit/test/__snapshots__/index.spec.js.snap b/packages/vant/src/address-edit/test/__snapshots__/index.spec.js.snap new file mode 100644 index 000000000..1e95a6876 --- /dev/null +++ b/packages/vant/src/address-edit/test/__snapshots__/index.spec.js.snap @@ -0,0 +1,373 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should allow to custom validator with validator prop 1`] = ` +
+ foo name +
+`; + +exports[`should render AddressEdit correctly 1`] = ` +
+
+
+
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+ +
+
+ + +
+
+
+ +
+
+
+ +
+
+
+
+
+ +
+
+`; + +exports[`should render AddressEdit with props correctly 1`] = ` +
+
+
+
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+ +
+
+ + +
+
+
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+ +
+
+
+
+
+
+ + Set as the default address + +
+
+
+
+
+
+
+ +
+
+`; + +exports[`should valid address detail and render error message correctly 1`] = ` +
+
+ +
+
+
+ +
+
+ Address can not be empty +
+
+
+`; + +exports[`should valid area code and render error message correctly 1`] = ` +
+
+ +
+
+
+ +
+
+ + +
+`; + +exports[`should valid name and render error message correctly 1`] = ` +
+
+ +
+
+
+ +
+
+ Please fill in the name +
+
+
+`; + +exports[`should valid postal code and render error message correctly 1`] = ` +
+
+ +
+
+
+ +
+
+ Wrong postal code +
+
+
+`; + +exports[`should valid tel and render error message correctly 1`] = ` +
+
+ +
+
+
+ +
+
+ Malformed phone number +
+
+
+`; diff --git a/packages/vant/src/address-edit/test/demo.legacy.ts b/packages/vant/src/address-edit/test/demo.spec.ts similarity index 100% rename from packages/vant/src/address-edit/test/demo.legacy.ts rename to packages/vant/src/address-edit/test/demo.spec.ts diff --git a/packages/vant/src/address-edit/test/index.legacy.js b/packages/vant/src/address-edit/test/index.spec.js similarity index 88% rename from packages/vant/src/address-edit/test/index.legacy.js rename to packages/vant/src/address-edit/test/index.spec.js index f44e7adf8..323d04b4b 100644 --- a/packages/vant/src/address-edit/test/index.legacy.js +++ b/packages/vant/src/address-edit/test/index.spec.js @@ -121,32 +121,6 @@ test('should emit changeDetail event after changing address detail', () => { expect(wrapper.emitted('changeDetail')[0][0]).toEqual('123'); }); -test('should return current areas after calling getArea method', () => { - const wrapper = mount(AddressEdit, { - props: { areaList }, - }); - - expect(wrapper.vm.getArea()).toEqual([ - { code: '110000', name: '北京市' }, - { code: '110100', name: '北京市' }, - { code: '110101', name: '东城区' }, - ]); -}); - -test('should update current areas after calling setAreaCode method', async () => { - const wrapper = mount(AddressEdit, { - props: { areaList }, - }); - - wrapper.vm.setAreaCode('110102'); - await later(); - expect(wrapper.vm.getArea()).toEqual([ - { code: '110000', name: '北京市' }, - { code: '110100', name: '北京市' }, - { code: '110102', name: '西城区' }, - ]); -}); - test('should show search result after focusing to address detail', async () => { const wrapper = mount(AddressEdit, { props: {