From 1a63d586b13a02b75abc74312c8ac81e606e167b Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Fri, 27 Nov 2020 22:07:26 +0800 Subject: [PATCH] test(Notify): update test cases --- src/notify/index.js | 8 +-- .../test/__snapshots__/index.legacy.js.snap | 13 ---- .../test/__snapshots__/index.spec.js.snap | 14 ++++ src/notify/test/index.legacy.js | 71 ------------------- src/notify/test/index.spec.js | 70 ++++++++++++++++++ 5 files changed, 88 insertions(+), 88 deletions(-) delete mode 100644 src/notify/test/__snapshots__/index.legacy.js.snap create mode 100644 src/notify/test/__snapshots__/index.spec.js.snap delete mode 100644 src/notify/test/index.legacy.js create mode 100644 src/notify/test/index.spec.js diff --git a/src/notify/index.js b/src/notify/index.js index 5e72f66df..39757a346 100644 --- a/src/notify/index.js +++ b/src/notify/index.js @@ -52,14 +52,14 @@ function Notify(options) { function defaultOptions() { return { type: 'danger', - message: '', color: undefined, - background: undefined, - duration: 3000, - className: '', + message: '', onClose: null, onClick: null, onOpened: null, + duration: 3000, + className: '', + background: undefined, }; } diff --git a/src/notify/test/__snapshots__/index.legacy.js.snap b/src/notify/test/__snapshots__/index.legacy.js.snap deleted file mode 100644 index 68a85cc1c..000000000 --- a/src/notify/test/__snapshots__/index.legacy.js.snap +++ /dev/null @@ -1,13 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`create a notify 1`] = `
test
`; - -exports[`notify disappear 1`] = `
test
`; - -exports[`notify disappear 2`] = ``; - -exports[`notify disappear 3`] = `
text2
`; - -exports[`notify disappear 4`] = ``; - -exports[`type prop 1`] = `
test
`; diff --git a/src/notify/test/__snapshots__/index.spec.js.snap b/src/notify/test/__snapshots__/index.spec.js.snap new file mode 100644 index 000000000..e2c33f3be --- /dev/null +++ b/src/notify/test/__snapshots__/index.spec.js.snap @@ -0,0 +1,14 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render Notify correctly 1`] = ` +
+ + test + + +
+`; diff --git a/src/notify/test/index.legacy.js b/src/notify/test/index.legacy.js deleted file mode 100644 index cc8e761ac..000000000 --- a/src/notify/test/index.legacy.js +++ /dev/null @@ -1,71 +0,0 @@ -import Notify from '..'; -import { later } from '../../../test'; - -test('create a notify', async () => { - // should not cause error when call clear before show notify - Notify.clear(); - - const notify = Notify('test'); - - await later(); - expect(notify.$el.outerHTML).toMatchSnapshot(); -}); - -test('type prop', async () => { - const notify = Notify({ - message: 'test', - type: 'primary', - }); - - await later(); - expect(notify.$el.outerHTML).toMatchSnapshot(); -}); - -test('notify disappear', async () => { - const onClose = jest.fn(); - const notify = Notify({ - message: 'test', - color: 'red', - background: 'blue', - duration: 10, - onClose, - }); - - await later(); - expect(notify.$el.outerHTML).toMatchSnapshot(); - - await later(20); - expect(notify.$el.outerHTML).toMatchSnapshot(); - expect(onClose).toHaveBeenCalledTimes(1); - - Notify({ - message: 'text2', - duration: 0, - }); - - await later(); - expect(notify.$el.outerHTML).toMatchSnapshot(); - - Notify.clear(); - await later(); - expect(notify.$el.outerHTML).toMatchSnapshot(); -}); - -test('set default options', () => { - Notify.setDefaultOptions({ duration: 1000 }); - expect(Notify().duration).toEqual(1000); - Notify.resetDefaultOptions(); - expect(Notify().duration).toEqual(3000); - Notify.clear(); -}); - -test('onClick prop', async () => { - const onClick = jest.fn(); - const notify = Notify({ - message: 'test', - onClick, - }); - - notify.$el.click(); - expect(onClick).toHaveBeenCalled(); -}); diff --git a/src/notify/test/index.spec.js b/src/notify/test/index.spec.js new file mode 100644 index 000000000..6704bc2f0 --- /dev/null +++ b/src/notify/test/index.spec.js @@ -0,0 +1,70 @@ +import { createApp } from 'vue'; +import Notify from '..'; +import NotifyComponent from '../Notify'; +import { later } from '../../../test'; +import { trigger } from '../../utils'; + +test('should not throw error if calling clear method before render notify', () => { + Notify.clear(); +}); + +test('should render Notify correctly', async () => { + Notify('test'); + await later(); + expect(document.querySelector('.van-notify')).toMatchSnapshot(); +}); + +test('should add "van-notify--success" class when type is success', async () => { + Notify({ + message: 'test', + type: 'success', + }); + + await later(); + const notify = document.querySelector('.van-notify'); + expect(notify.classList.contains('van-notify--success')).toBeTruthy(); +}); + +test('should register component to app', () => { + const app = createApp(); + app.use(Notify); + expect(app.component(NotifyComponent.name)).toBeTruthy(); +}); + +test('should change default duration after calling setDefaultOptions method', () => { + Notify.setDefaultOptions({ duration: 1000 }); + expect(Notify.currentOptions.duration).toEqual(1000); + Notify.resetDefaultOptions(); + expect(Notify.currentOptions.duration).toEqual(3000); +}); + +test('should reset to default duration after calling resetDefaultOptions method', () => { + Notify.setDefaultOptions({ duration: 1000 }); + Notify.resetDefaultOptions(); + expect(Notify.currentOptions.duration).toEqual(3000); +}); + +test('should call onClose option when closing', async () => { + const onClose = jest.fn(); + Notify({ + message: 'test', + onClose, + duration: 1, + }); + + await later(20); + expect(onClose).toHaveBeenCalledTimes(1); +}); + +test('should call onClick option when clicked', async () => { + const onClick = jest.fn(); + Notify({ + message: 'test', + onClick, + }); + + await later(); + const notify = document.querySelector('.van-notify'); + trigger(notify, 'click'); + expect(onClick).toHaveBeenCalledTimes(1); +});