diff --git a/packages/varlet-ui/src/snackbar/__tests__/component.spec.js b/packages/varlet-ui/src/snackbar/__tests__/component.spec.js index 60e064da3ec..58bb047a8eb 100644 --- a/packages/varlet-ui/src/snackbar/__tests__/component.spec.js +++ b/packages/varlet-ui/src/snackbar/__tests__/component.spec.js @@ -3,7 +3,7 @@ import VarSnackbar from '../Snackbar' import example from '../example' import { mount } from '@vue/test-utils' import { createApp } from 'vue' -import { delay } from '../../utils/jest' +import { delay, mockStubs } from '../../utils/jest' test('test snackbar example', () => { const wrapper = mount(example) @@ -50,3 +50,47 @@ test('test snackbar style', async () => { await delay(500) expect(document.querySelector('.var-snackbar').style.display).toBe('none') }) + +test('test snackbar event', async () => { + const open = jest.fn() + const opened = jest.fn() + const close = jest.fn() + const closed = jest.fn() + const { mockRestore } = mockStubs() + + const template = ` + + Snackbar + ` + + const wrapper = mount({ + components: { + [VarSnackbar.name]: VarSnackbar + }, + data() { + return { + show: false + } + }, + methods: { + open, + opened, + close, + closed + }, + template, + }) + + await delay(0) + await wrapper.setData({ show: true }) + await delay(200) + await wrapper.setData({ show: false }) + await delay(200) + + expect(open).toHaveBeenCalledTimes(1) + expect(opened).toHaveBeenCalledTimes(1) + expect(close).toHaveBeenCalledTimes(1) + expect(closed).toHaveBeenCalledTimes(1) + + mockRestore() +})