From 09e25a28892e13283475607b2fc5f242e21a404a Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Thu, 17 May 2018 10:40:50 +0800 Subject: [PATCH] test: add affix unit test --- components/affix/__tests__/index.test.js | 71 ++++++++++++++++++++++++ tests/setup.js | 4 +- tests/shared/demoTest.js | 3 + 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 components/affix/__tests__/index.test.js diff --git a/components/affix/__tests__/index.test.js b/components/affix/__tests__/index.test.js new file mode 100644 index 000000000..5bb0ffb90 --- /dev/null +++ b/components/affix/__tests__/index.test.js @@ -0,0 +1,71 @@ +import Affix from '..' +import Button from '../../button' +import { mount } from '@vue/test-utils' +const events = {} + +const AffixMounter = { + mounted () { + this.$refs.container.scrollTop = 100 + this.$refs.container.addEventListener = jest.fn().mockImplementation((event, cb) => { + events[event] = cb + }) + }, + methods: { + getTarget () { + return this.$refs.container + }, + }, + + render () { + return ( +
+
+ this.$refs.container} + ref='affix' + > + + +
+
+ ) + }, +} +describe('Affix Render', () => { + beforeAll(() => { + jest.useFakeTimers() + }) + + afterAll(() => { + jest.useRealTimers() + }) + it('Affix render perfectly', () => { + const wrapper = mount(AffixMounter, { attachToDocument: true }) + jest.runAllTimers() + + wrapper.vm.$refs.affix.$refs.fixedNode.parentNode.getBoundingClientRect = jest.fn(() => { + return { + bottom: 100, height: 28, left: 0, right: 0, top: -50, width: 195, + } + }) + events.scroll({ + type: 'scroll', + }) + jest.runAllTimers() + expect(wrapper.vm.$refs.affix.affixStyle).not.toBe(null) + }) +}) diff --git a/tests/setup.js b/tests/setup.js index cbd61b820..42fbf73b0 100644 --- a/tests/setup.js +++ b/tests/setup.js @@ -1,6 +1,4 @@ -import Vue from 'vue' -import antd from 'vue-antd-ui' -Vue.use(antd) + /* eslint-disable global-require */ if (typeof window !== 'undefined') { global.window.resizeTo = (width, height) => { diff --git a/tests/shared/demoTest.js b/tests/shared/demoTest.js index 2dc8d73da..772fcae90 100644 --- a/tests/shared/demoTest.js +++ b/tests/shared/demoTest.js @@ -2,6 +2,9 @@ import glob from 'glob' import { renderToString } from '@vue/server-test-utils' import MockDate from 'mockdate' import moment from 'moment' +import Vue from 'vue' +import antd from 'vue-antd-ui' +Vue.use(antd) export default function demoTest (component, options = {}) { const files = glob.sync(`./components/${component}/demo/*.md`)