From 86cd506c0e901220de1a912a733c648878023e44 Mon Sep 17 00:00:00 2001 From: supergaojian <402853745@qq.com> Date: Tue, 9 Apr 2019 17:53:58 +0800 Subject: [PATCH] test(ruler): add ruler jest test (#395) * fix(ActivityIndicator): fix wrong roller-success background-color * feat(ruler): add new component Ruler * feat(ruler): update for ruler * feat(ruler): finish ruler * code-style(activity-indicator): change color * feat(ruler): remove mouse event and add throttle * test(jest): add jest-canvas-mock --- components/ruler/index.vue | 7 +- .../test/__snapshots__/demo.spec.js.snap | 37 +++ components/ruler/test/cases/demo0.vue | 36 +++ components/ruler/test/cases/demo1.vue | 38 +++ components/ruler/test/cases/demo2.vue | 43 ++++ components/ruler/test/demo.spec.js | 16 ++ components/ruler/test/index.spec.js | 216 +++++++++++++++++- jest.config.js | 4 +- package.json | 1 + 9 files changed, 388 insertions(+), 10 deletions(-) create mode 100644 components/ruler/test/__snapshots__/demo.spec.js.snap create mode 100644 components/ruler/test/cases/demo0.vue create mode 100644 components/ruler/test/cases/demo1.vue create mode 100644 components/ruler/test/cases/demo2.vue create mode 100644 components/ruler/test/demo.spec.js diff --git a/components/ruler/index.vue b/components/ruler/index.vue index e813d36e9..3296af197 100644 --- a/components/ruler/index.vue +++ b/components/ruler/index.vue @@ -76,7 +76,10 @@ export default { realMin() { const {scope, min} = this - const [left] = scope + const [left, right] = scope + if (min > right) { + return left + } return min > left ? min : left }, @@ -84,7 +87,7 @@ export default { let {scope, max} = this const [left, right] = scope if (left > max) { - max = right + return right } return max > right ? right : max }, diff --git a/components/ruler/test/__snapshots__/demo.spec.js.snap b/components/ruler/test/__snapshots__/demo.spec.js.snap new file mode 100644 index 000000000..6bedbd9c5 --- /dev/null +++ b/components/ruler/test/__snapshots__/demo.spec.js.snap @@ -0,0 +1,37 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Radio Basic 1`] = ` +
+

当前值:1000

+
+
+
+
+
+
+
+`; + +exports[`Radio List 1`] = ` +
+

当前值:1300

+
+
+
+
+
+
+
+`; + +exports[`Radio With Field 1`] = ` +
+

当前值:1305

+
+
+
+
+
+
+
+`; diff --git a/components/ruler/test/cases/demo0.vue b/components/ruler/test/cases/demo0.vue new file mode 100644 index 000000000..a8f3427a5 --- /dev/null +++ b/components/ruler/test/cases/demo0.vue @@ -0,0 +1,36 @@ + + + + + diff --git a/components/ruler/test/cases/demo1.vue b/components/ruler/test/cases/demo1.vue new file mode 100644 index 000000000..9b5fdfb2f --- /dev/null +++ b/components/ruler/test/cases/demo1.vue @@ -0,0 +1,38 @@ + + + + + diff --git a/components/ruler/test/cases/demo2.vue b/components/ruler/test/cases/demo2.vue new file mode 100644 index 000000000..30b866179 --- /dev/null +++ b/components/ruler/test/cases/demo2.vue @@ -0,0 +1,43 @@ + + + + + diff --git a/components/ruler/test/demo.spec.js b/components/ruler/test/demo.spec.js new file mode 100644 index 000000000..89cd6fd3b --- /dev/null +++ b/components/ruler/test/demo.spec.js @@ -0,0 +1,16 @@ +import Demo0 from './cases/demo0' +import Demo1 from './cases/demo1' +import Demo2 from './cases/demo2' +import {renderToString} from '@vue/server-test-utils' + +describe('Radio', () => { + test(`Basic`, () => { + expect(renderToString(Demo0)).toMatchSnapshot() + }) + test(`With Field`, () => { + expect(renderToString(Demo1)).toMatchSnapshot() + }) + test(`List`, () => { + expect(renderToString(Demo2)).toMatchSnapshot() + }) +}) diff --git a/components/ruler/test/index.spec.js b/components/ruler/test/index.spec.js index d6b615f73..2cb3c2160 100644 --- a/components/ruler/test/index.spec.js +++ b/components/ruler/test/index.spec.js @@ -1,16 +1,220 @@ -import SingleComponent from '../index' -import {mount} from 'avoriaz' +import {Ruler} from 'mand-mobile' +import {mount} from '@vue/test-utils' +import triggerTouch from '../../popup/test/touch-trigger' -describe('SingleComponent', () => { +describe('Ruler', () => { let wrapper afterEach(() => { wrapper && wrapper.destroy() }) - it('create a simple ruler', () => { - wrapper = mount(SingleComponent) + it('create a ruler', () => { + wrapper = mount(Ruler) - expect(wrapper.hasClass('md-ruler')).to.be.true + expect(wrapper.classes()).toContain('md-ruler') + }) + + it('touch the ruler bigger', () => { + let value = 50 + + wrapper = mount(Ruler, { + propsData: { + value, + }, + listeners: { + input(val) { + value = val + }, + }, + }) + + // to bigger + triggerTouch(wrapper.element, 'touchstart', 0, 0) + triggerTouch(window, 'touchmove', 100, 0) + triggerTouch(window, 'touchmove', 50, 0) + triggerTouch(wrapper.element, 'touchend', 50, 0) + + expect(value).toBeGreaterThan(50) + }) + + it('touch the ruler lesser', () => { + let value = 50 + + wrapper = mount(Ruler, { + propsData: { + value, + }, + listeners: { + input(val) { + value = val + }, + }, + }) + + // to bigger + triggerTouch(wrapper.element, 'touchstart', 0, 0) + triggerTouch(window, 'touchmove', 100, 0) + triggerTouch(window, 'touchmove', 150, 0) + triggerTouch(wrapper.element, 'touchend', 50, 0) + + expect(value).toBeLessThan(50) + }) + + it('touch the ruler over max', () => { + const max = 90 + let value = 50 + + wrapper = mount(Ruler, { + propsData: { + value, + max, + }, + listeners: { + input(val) { + value = val + }, + }, + }) + + // over max + triggerTouch(wrapper.element, 'touchstart', 0, 0) + triggerTouch(window, 'touchmove', 5000, 0) + triggerTouch(window, 'touchmove', 0, 0) + triggerTouch(wrapper.element, 'touchend', 0, 0) + + expect(value).toEqual(max) + }) + + it('touch the ruler below min', () => { + const min = 10 + let value = 50 + + wrapper = mount(Ruler, { + propsData: { + value, + min, + }, + listeners: { + input(val) { + value = val + }, + }, + }) + + // below min + triggerTouch(wrapper.element, 'touchstart', 0, 0) + triggerTouch(window, 'touchmove', 100, 0) + triggerTouch(window, 'touchmove', 5000, 0) + triggerTouch(wrapper.element, 'touchend', 5000, 0) + + expect(value).toEqual(min) + }) + + it('change value', () => { + let value = 50 + + wrapper = mount(Ruler, { + propsData: { + value, + }, + listeners: { + input(val) { + value = val + }, + }, + }) + + value = 90 + wrapper.setProps({value: 90}) + + expect(value).toEqual(90) + }) + + it('change value when touching', () => { + let value = 50 + + wrapper = mount(Ruler, { + propsData: { + value, + }, + listeners: { + input(val) { + value = val + }, + }, + }) + + triggerTouch(wrapper.element, 'touchstart', 0, 0) + triggerTouch(window, 'touchmove', 100, 0) + + wrapper.setProps({value: 10}) + + triggerTouch(window, 'touchmove', 5000, 0) + triggerTouch(wrapper.element, 'touchend', 5000, 0) + + expect(value).toEqual(0) + }) + + it('value bigger than max', () => { + wrapper = mount(Ruler, { + propsData: { + value: 10000, + }, + }) + + expect(wrapper.vm.value).toEqual(10000) + }) + + it('props max over the max of scope', () => { + const scope = [0, 100] + + wrapper = mount(Ruler, { + propsData: { + scope, + max: 110, + }, + }) + + expect(wrapper.vm.realMax).toEqual(scope[1]) + }) + + it('props max below the min of scope', () => { + const scope = [0, 100] + + wrapper = mount(Ruler, { + propsData: { + scope, + max: -10, + }, + }) + + expect(wrapper.vm.realMax).toEqual(scope[1]) + }) + + it('props mix below the min of scope', () => { + const scope = [0, 100] + + wrapper = mount(Ruler, { + propsData: { + scope, + min: -10, + }, + }) + + expect(wrapper.vm.realMin).toEqual(scope[0]) + }) + + it('props mix over the max of scope', () => { + const scope = [0, 100] + + wrapper = mount(Ruler, { + propsData: { + scope, + min: 110, + }, + }) + + expect(wrapper.vm.realMin).toEqual(scope[0]) }) }) diff --git a/jest.config.js b/jest.config.js index 31f9bd134..c429a1d47 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,7 +2,7 @@ const TEST_TYPE = process.env.TEST_TYPE || 'components' // const TEST_BUNDLE = TEST_TYPE === 'bundle' ? 'lib/mand-mobile.umd' : '' module.exports = { - setupFiles: ['/test/jest.init.js'], + setupFiles: ['jest-canvas-mock', '/test/jest.init.js'], moduleFileExtensions: [ 'js', 'json', @@ -42,4 +42,4 @@ module.exports = { ], coverageReporters: ['html', 'lcov', 'text-summary'], coverageDirectory: './test/coverage' -} \ No newline at end of file +} diff --git a/package.json b/package.json index e2138258d..a1e58399b 100644 --- a/package.json +++ b/package.json @@ -146,6 +146,7 @@ "inject-loader": "^3.0.0", "inquirer": "^3.3.0", "jest": "^23.6.0", + "jest-canvas-mock": "^2.0.0-beta.1", "jest-serializer-vue": "^2.0.2", "jest-svg-sprite-loader": "^1.0.1", "jest-transform-stub": "^1.0.0",