From f27afc97eecd209b762d9756dc116f53d0e602d6 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 24 Oct 2019 19:37:13 +0300 Subject: [PATCH] test: eventEmitter. Component's hooks, watch, some methods + 6% --- tests/unit/component.spec.js | 114 +++++++++++++++++++++++++++++++- tests/unit/eventEmitter.spec.ts | 27 ++++++++ tests/unit/helpers.js | 11 +++ tests/unit/helpers.spec.js | 47 ++++++++++++- yarn.lock | 34 ++++++++-- 5 files changed, 224 insertions(+), 9 deletions(-) create mode 100644 tests/unit/eventEmitter.spec.ts diff --git a/tests/unit/component.spec.js b/tests/unit/component.spec.js index 35560c9c..1d7c67bf 100644 --- a/tests/unit/component.spec.js +++ b/tests/unit/component.spec.js @@ -1,8 +1,10 @@ -import { mount } from '@vue/test-utils' +import { mount, shallowMount } from '@vue/test-utils' import clone from 'clone' import MainComponent from '~/component.vue' import { MENU_POSITIONS } from '~/constants' -import createLocalVueWithPlugin from './helpers' +import createLocalVueWithPlugin, { isTextSelected } from './helpers' + +jest.useFakeTimers() it('Plugin installation', () => { const localVue = createLocalVueWithPlugin() @@ -13,6 +15,7 @@ it('Plugin installation', () => { }) describe('MainComponent.vue', () => { + const Vue = require('vue') const itemsDefault = [ 'JS', 'PHP', @@ -61,6 +64,113 @@ describe('MainComponent.vue', () => { expect(wrapper.is('div')).toBe(true) }) + it('is text selected after setInputSelected', () => { + // isTextSelected + const wrapper = mount(MainComponent, { + propsData: { items: itemsDefault } + }) + + wrapper.vm.setInputSelected() + + const isSelected = isTextSelected( + wrapper.find({ ref: 'IZ-select__input-for-text' }).element + ) + + expect(isSelected).toBe(true) + expect(setTimeout).toHaveBeenCalledTimes(1) + expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 100) + }) + + it('is searchText (watch property) works correctly', () => { + const localVue = createLocalVueWithPlugin() + const wrapper = mount(MainComponent, { + localVue, + propsData: { items: itemsDefault } + }) + const searchData = 'TEST' + const mock = jest.spyOn(wrapper.vm, 'setSearchData') + + wrapper.setMethods({ + setSearchData: mock + }) + + wrapper.vm.$options.watch.searchText.call(wrapper.vm, searchData) + + expect(mock).toBeCalledWith(searchData) + expect(wrapper.vm.searchData).toBe(searchData) + }) + + it('is items (watch property) works correctly', () => { + const localVue = createLocalVueWithPlugin() + const wrapper = mount(MainComponent, { + localVue, + propsData: { items: itemsDefault } + }) + const mock = jest.spyOn(wrapper.vm, 'setSelectedItemByValue') + + wrapper.setMethods({ setSelectedItemByValue: mock }) + wrapper.vm.$options.watch.items.call(wrapper.vm) + + expect(mock).toBeCalled() + }) + + it('tests beforeDestroy', () => { + const mockFn = jest.fn() + + shallowMount(MainComponent, { + propsData: { items: itemsDefault }, + destroyed: mockFn + }).destroy() + + expect(mockFn).toHaveBeenCalled() + }) + + // it('tests mousedown', async () => { + // const mockMousedown = jest.fn() + // const wrapper = mount(MainComponent, { + // propsData: { items: itemsDefault }, + // data () { + // return { + // listeners: { + // mousedown: mockMousedown + // } + // } + // } + // }) + // // const inputForText = wrapper.find({ + // // ref: 'IZ-select__input-for-text' + // // }) + // + // const mockSetBlured = jest.fn() // jest.spyOn(wrapper.vm, 'setBlured') + // + // wrapper.setMethods({ setBlured: mockSetBlured }) + // + // wrapper.vm.setFocused() + // + // await Vue.nextTick() + // + // wrapper.vm.listeners.mousedown.call({ + // $refs: { + // 'IZ-select': wrapper.find({ ref: 'IZ-select' }).element + // }, + // focused: true + // }, { + // target: document.createElement('div') // wrapper.find({ ref: 'IZ-select__input-for-text' }).element + // }) + // + // await Vue.nextTick() + // + // // + // // inputForText.trigger('mousedown') + // // + // // await Vue.nextTick() + // + // expect(mockSetBlured).toBeCalled() + // expect(mockMousedown).toHaveBeenCalled() + // }) + + // inputForText.trigger('focus') + it('tests "item-value", "itemText" props, checks values for every item, checks if an item is selected', () => { const localVue = createLocalVueWithPlugin() const itemIndex = 0 diff --git a/tests/unit/eventEmitter.spec.ts b/tests/unit/eventEmitter.spec.ts new file mode 100644 index 00000000..1597d89c --- /dev/null +++ b/tests/unit/eventEmitter.spec.ts @@ -0,0 +1,27 @@ +import { EventEmitter } from '~/main' + +describe('eventsListeners', () => { + it('checks EventEmitter', () => { + const instance = new EventEmitter() + + expect(typeof EventEmitter).toEqual('function') + expect(typeof instance).toEqual('object') + + expect(typeof instance.on).toEqual('function') + expect(typeof instance.onOnce).toEqual('function') + expect(typeof instance.emit).toEqual('function') + + expect(instance.on( + 'myEvent', + () => {} + )).toBeUndefined() + expect(instance.onOnce( + 'myEvent', + () => {} + )).toBeUndefined() + expect(instance.emit( + 'myEvent', + 'testData' + )).toBeUndefined() + }) +}) diff --git a/tests/unit/helpers.js b/tests/unit/helpers.js index 46bf8317..9e856090 100644 --- a/tests/unit/helpers.js +++ b/tests/unit/helpers.js @@ -8,3 +8,14 @@ export default function createLocalVueWithPlugin () { return localVue } + +// Выделен ли текст в инпуте. https://stackoverflow.com/a/5001669/5286034 +export function isTextSelected (input) { + if (typeof input.selectionStart === 'number') { + return input.selectionStart === 0 && input.selectionEnd === input.value.length + } else if (typeof document.selection !== 'undefined') { + input.focus() + + return document.selection.createRange().text === input.value + } +} diff --git a/tests/unit/helpers.spec.js b/tests/unit/helpers.spec.js index 87b486d5..9e7dad01 100644 --- a/tests/unit/helpers.spec.js +++ b/tests/unit/helpers.spec.js @@ -1,4 +1,4 @@ -import { isObject, getOffsetSum, scrollIfNeeded } from '~/helpers' +import { isObject, getOffsetSum, scrollIfNeeded, mergeDeep } from '~/helpers' describe('helpers.js', () => { it('checks isObject', () => { @@ -97,4 +97,49 @@ describe('helpers.js', () => { TEST3.container.scrollTop ).toBe(TEST3.container.scrollTop) }) + + it('checks mergeDeep', () => { + const obj = { + a: 10, + b: { + b1: 20 + } + } + + expect( + mergeDeep(obj, { + c: 40 + }) + ).toEqual({ ...obj, c: 40 }) + + expect( + mergeDeep(obj, { + b: { + b2: 20 + } + }) + ).toEqual({ + a: 10, + b: { + b1: 20, + b2: 20 + } + }) + + expect( + mergeDeep(obj, { + c: { + test: 123 + } + }) + ).toEqual({ + a: 10, + b: { + b1: 20 + }, + c: { + test: 123 + } + }) + }) }) diff --git a/yarn.lock b/yarn.lock index a67a7949..c6e2fda8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1282,11 +1282,16 @@ resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d" integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA== -"@types/node@*", "@types/node@^12.0.2", "@types/node@^12.7.5": +"@types/node@*", "@types/node@^12.7.5": version "12.7.5" resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.5.tgz#e19436e7f8e9b4601005d73673b6dc4784ffcc2f" integrity sha512-9fq4jZVhPNW8r+UYKnxF1e2HkDWOWKM5bC2/7c9wPV835I0aOrVbS/Hw/pWPk2uKrNXQqg9Z959Kz+IYDd5p3w== +"@types/node@^12.0.2": + version "12.11.6" + resolved "https://registry.yarnpkg.com/@types/node/-/node-12.11.6.tgz#2f8d551aef252de78f42acdccd53f5a8ce0cac4d" + integrity sha512-4uPUyY1Aofo1YzoypalYHNd2SnKYxH2b6LzXwpryZCJKA2XlagZSynXx5C8sfPH0r1cSltUpaVHV2q5sYXschQ== + "@types/normalize-package-data@^2.4.0": version "2.4.0" resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e" @@ -3612,9 +3617,9 @@ conventional-changelog-writer@^4.0.0: through2 "^3.0.0" conventional-commit-types@^2.0.0: - version "2.1.1" - resolved "https://registry.yarnpkg.com/conventional-commit-types/-/conventional-commit-types-2.1.1.tgz#352eb53f56fbc7c1a6c1ba059c2b6670c90b2a8a" - integrity sha512-0Ts+fEdmjqYDOQ1yZ+LNgdSPO335XZw9qC10M7CxtLP3nIMGmeMhmkM8Taffa4+MXN13bRPlp0CtH+QfOzKTzw== + version "2.3.0" + resolved "https://registry.yarnpkg.com/conventional-commit-types/-/conventional-commit-types-2.3.0.tgz#bc3c8ebba0a9e4b3ecc548f1d0674e251ab8be22" + integrity sha512-6iB39PrcGYdz0n3z31kj6/Km6mK9hm9oMRhwcLnKxE7WNoeRKZbTAobliKrbYZ5jqyCvtcVEfjCiaEzhL3AVmQ== conventional-commits-filter@^2.0.0: version "2.0.1" @@ -5946,7 +5951,7 @@ glob-to-regexp@^0.3.0: resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.3.0.tgz#8c5a1494d2066c570cc3bfe4496175acc4d502ab" integrity sha1-jFoUlNIGbFcMw7/kSWF1rMTVAqs= -glob@7.1.4, glob@^7.0.0, glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4: +glob@7.1.4, glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4: version "7.1.4" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.4.tgz#aa608a2f6c577ad357e1ae5a5c26d9a8d1969255" integrity sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A== @@ -5958,6 +5963,18 @@ glob@7.1.4, glob@^7.0.0, glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glo once "^1.3.0" path-is-absolute "^1.0.0" +glob@^7.0.0: + version "7.1.5" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.5.tgz#6714c69bee20f3c3e64c4dd905553e532b40cdc0" + integrity sha512-J9dlskqUXK1OeTOYBEn5s8aMukWMwWfs+rPTn/jn50Ux4MNXVhubL1wu/j2t+H4NVI+cXEcCaYellqaPVGXNqQ== + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^3.0.4" + once "^1.3.0" + path-is-absolute "^1.0.0" + global-dirs@^0.1.0, global-dirs@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/global-dirs/-/global-dirs-0.1.1.tgz#b319c0dd4607f353f3be9cca4c72fc148c49f445" @@ -6063,11 +6080,16 @@ got@^6.7.1: unzip-response "^2.0.1" url-parse-lax "^1.0.0" -graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.2: +graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.2.2: version "4.2.2" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.2.tgz#6f0952605d0140c1cfdb138ed005775b92d67b02" integrity sha512-IItsdsea19BoLC7ELy13q1iJFNmd7ofZH5+X/pJr90/nRoPEX0DJo1dHDbgtYWOhJhcCgMDTOw84RZ72q6lB+Q== +graceful-fs@^4.1.6, graceful-fs@^4.2.0: + version "4.2.3" + resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423" + integrity sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ== + growly@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"