diff --git a/src/card/README.md b/src/card/README.md index 0a628e774..afca185f9 100644 --- a/src/card/README.md +++ b/src/card/README.md @@ -80,8 +80,8 @@ Use slot to custom content. | Event | Description | Arguments | |------|------|------| -| click | Triggered when clicked | - | -| click-thumb | Triggered when thumb clicked | - | +| click | Triggered when clicked | event: Event | +| click-thumb | Triggered when thumb clicked | event: Event | ### Slots diff --git a/src/card/README.zh-CN.md b/src/card/README.zh-CN.md index edbc8a311..2c131f6a6 100644 --- a/src/card/README.zh-CN.md +++ b/src/card/README.zh-CN.md @@ -82,8 +82,8 @@ Vue.use(Card); | 事件名 | 说明 | 回调参数 | |------|------|------| -| click | 点击时触发 | - | -| click-thumb | 点击自定义图片时触发 | - | +| click | 点击时触发 | event: Event | +| click-thumb | 点击自定义图片时触发 | event: Event | ### Slots diff --git a/src/card/index.tsx b/src/card/index.tsx index 4acff4b3f..5f98e3801 100644 --- a/src/card/index.tsx +++ b/src/card/index.tsx @@ -53,9 +53,9 @@ function Card( const showOriginPrice = slots['origin-price'] || isDef(props.originPrice); const showBottom = showNum || showPrice || showOriginPrice; - const onThumbClick = () => { - emit(ctx, 'click-thumb'); - }; + function onThumbClick(event: MouseEvent) { + emit(ctx, 'click-thumb', event); + } function ThumbTag() { if (slots.tag || props.tag) { diff --git a/src/card/test/index.spec.js b/src/card/test/index.spec.js index f4af8a3c0..c0c7cc3b8 100644 --- a/src/card/test/index.spec.js +++ b/src/card/test/index.spec.js @@ -1,6 +1,47 @@ import Card from '..'; import { mount } from '../../../test/utils'; +test('click event', () => { + const onClick = jest.fn(); + const wrapper = mount(Card, { + context: { + on: { + click: onClick + } + } + }); + + wrapper.trigger('click'); + + expect(onClick).toHaveBeenCalledWith( + expect.objectContaining({ + isTrusted: expect.any(Boolean) + }) + ); +}); + +test('click-thumb event', () => { + const onClickThumb = jest.fn(); + const wrapper = mount(Card, { + propsData: { + thumb: 'xx' + }, + context: { + on: { + 'click-thumb': onClickThumb + } + } + }); + + wrapper.find('.van-card__thumb').trigger('click'); + + expect(onClickThumb).toHaveBeenCalledWith( + expect.objectContaining({ + isTrusted: expect.any(Boolean) + }) + ); +}); + test('render price & num slot', () => { const wrapper = mount(Card, { scopedSlots: {