Skip to content

Commit

Permalink
test: eventEmitter. Component's hooks, watch, some methods
Browse files Browse the repository at this point in the history
+ 6%
  • Loading branch information
iliyaZelenko committed Oct 24, 2019
1 parent 9f0e223 commit f27afc9
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 9 deletions.
114 changes: 112 additions & 2 deletions tests/unit/component.spec.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -13,6 +15,7 @@ it('Plugin installation', () => {
})

describe('MainComponent.vue', () => {
const Vue = require('vue')
const itemsDefault = [
'JS',
'PHP',
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/eventEmitter.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
11 changes: 11 additions & 0 deletions tests/unit/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
47 changes: 46 additions & 1 deletion tests/unit/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject, getOffsetSum, scrollIfNeeded } from '~/helpers'
import { isObject, getOffsetSum, scrollIfNeeded, mergeDeep } from '~/helpers'

describe('helpers.js', () => {
it('checks isObject', () => {
Expand Down Expand Up @@ -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
}
})
})
})
34 changes: 28 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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=

[email protected], glob@^7.0.0, glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4:
[email protected], 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==
Expand All @@ -5958,6 +5963,18 @@ [email protected], 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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit f27afc9

Please sign in to comment.