feat(Sku): add sku message formatter test

This commit is contained in:
liuhaihong 2020-07-17 18:27:27 +08:00 committed by neverland
parent 5cce17ab4b
commit b1460ce42e
2 changed files with 45 additions and 1 deletions

View File

@ -115,11 +115,12 @@ export default createComponent({
* The phone number copied from IOS mobile phone address book
* will add spaces and invisible Unicode characters
* which cannot pass the /^\d+$/ verification
* so keep numbers and dots
*/
getFormatter(message) {
return function formatter(value) {
if (message.type === 'mobile' || message.type === 'tel') {
return value.replace(/\s/g, '').replace(/[\u202c|\u202d]/g, '');
return value.replace(/[^\d.]/g, '');
}
return value;

View File

@ -32,6 +32,49 @@ test('resetSelectedSku method', () => {
expect(wrapper.emitted('buy-clicked').length).toEqual(1);
});
test('message formatter', () => {
const skuData = getSkuData();
skuData.sku.messages = skuData.sku.messages.filter(
message => message.type === 'tel'
);
const wrapper = mount(Sku, {
propsData: {
value: true,
initialSku,
sku: skuData.sku,
quota: skuData.quota,
goods: skuData.goods_info,
goodsId: skuData.goods_id,
quotaUsed: skuData.quota_used,
hideStock: skuData.sku.hide_stock,
startSaleNum: skuData.start_sale_num,
},
});
const input = wrapper.find('input');
const correctValue = '15000000000';
// \u202c
input.element.value = '15000000000';
input.trigger('input');
expect(input.element.value).toEqual(correctValue);
// \u0020
input.element.value = '150 0000 0000';
input.trigger('input');
expect(input.element.value).toEqual(correctValue);
// /[a-zA-z]/
input.element.value = 'a-zA-z';
input.trigger('input');
expect(input.element.value).toEqual('');
});
test('stringToDate', () => {
expect(dateToString(stringToDate(''))).toEqual('');
expect(dateToString(stringToDate('2020-07-01'))).toEqual('2020-07-01');