|
1 | 1 | /* global describe,test,expect,beforeEach,CustomEvent */
|
2 |
| -import sortable from '../../src/html5sortable' |
| 2 | +import enableHoverClass from '../../src/hoverClass' |
| 3 | +import store from '../../src/store' |
3 | 4 | import {mockInnerHTML} from '../helpers'
|
4 | 5 |
|
5 | 6 | describe('Testing mouseenter and mouseleave events for hoverClass', () => {
|
6 |
| - let ul, li |
| 7 | + let ul, li, liSecond |
7 | 8 | // setup html
|
8 | 9 | beforeEach(() => {
|
9 | 10 | document.body.innerHTML = mockInnerHTML
|
10 | 11 | ul = document.querySelector('.sortable')
|
11 | 12 | li = ul.querySelector('li')
|
| 13 | + liSecond = ul.querySelector('.li-second') |
| 14 | + // setup data |
| 15 | + store(ul).config = { |
| 16 | + hoverClass: 'hover-class', |
| 17 | + items: 'li', |
| 18 | + throttleTime: 0 |
| 19 | + } |
| 20 | + // enable module |
| 21 | + enableHoverClass(ul, true) |
12 | 22 | })
|
13 | 23 |
|
14 |
| - test('should not add class on hover event', () => { |
15 |
| - // setup sortable with no hoverClass |
16 |
| - sortable(ul, { |
17 |
| - items: 'li', |
18 |
| - hoverClass: false |
19 |
| - }) |
| 24 | + test('should not add class on hover event when disabled', () => { |
| 25 | + // disable hover class |
| 26 | + enableHoverClass(ul, false) |
| 27 | + store(ul).setConfig('hoverClass', null) |
20 | 28 | // get inital class list length
|
21 | 29 | let classListLength = li.classList.length
|
22 | 30 | // trigger mouseenter event
|
23 |
| - li.dispatchEvent(new CustomEvent('mouseenter')) |
| 31 | + li.dispatchEvent(new MouseEvent('mousemove', { |
| 32 | + bubbles: true, |
| 33 | + cancelable: true, |
| 34 | + buttons: 0, |
| 35 | + target: li |
| 36 | + })) |
24 | 37 | // assert that class list lenght did not change
|
25 | 38 | expect(li.classList.length).toBe(classListLength)
|
26 | 39 | })
|
27 |
| - |
28 |
| - test('should correctly add class on hover event', () => { |
29 |
| - // setup sortable with hoverClass |
30 |
| - sortable(ul, { |
31 |
| - 'items': 'li', |
32 |
| - hoverClass: 'sortable-item-over' |
33 |
| - }) |
| 40 | + |
| 41 | + test('should not add class on hover event hoverClass is null', () => { |
| 42 | + // disable hover class |
| 43 | + enableHoverClass(ul, false) |
| 44 | + store(ul).setConfig('hoverClass', null) |
| 45 | + enableHoverClass(ul, true) |
| 46 | + // get inital class list length |
| 47 | + let classListLength = li.classList.length |
34 | 48 | // trigger mouseenter event
|
35 |
| - li.dispatchEvent(new CustomEvent('mouseenter')) |
| 49 | + li.dispatchEvent(new MouseEvent('mousemove', { |
| 50 | + bubbles: true, |
| 51 | + cancelable: true, |
| 52 | + buttons: 0, |
| 53 | + target: li |
| 54 | + })) |
| 55 | + // assert that class list lenght did not change |
| 56 | + expect(li.classList.length).toBe(classListLength) |
| 57 | + }) |
| 58 | + |
| 59 | + test('should correctly add class on hover event, and remove on hover other element', () => { |
| 60 | + expect(li.classList.contains('hover-class')).toBe(false) |
| 61 | + // trigger mouse event |
| 62 | + li.dispatchEvent(new MouseEvent('mousemove', { |
| 63 | + bubbles: true, |
| 64 | + cancelable: true, |
| 65 | + buttons: 0, |
| 66 | + target: li |
| 67 | + })) |
36 | 68 | // assert that class was added
|
37 |
| - expect(li.classList.contains('sortable-item-over')).toBe(true) |
38 |
| - // trigger mouseleave event |
39 |
| - li.dispatchEvent(new CustomEvent('mouseleave')) |
| 69 | + expect(li.classList.contains('hover-class')).toBe(true) |
| 70 | + |
| 71 | + // trigger mouseleave events |
| 72 | + liSecond.dispatchEvent(new MouseEvent('mousemove', { |
| 73 | + bubbles: true, |
| 74 | + cancelable: true, |
| 75 | + buttons: 0, |
| 76 | + target: liSecond |
| 77 | + })) |
40 | 78 | // assert that class was removed
|
41 |
| - expect(li.classList.contains('sortable-item-over')).toBe(false) |
| 79 | + expect(liSecond.classList.contains('hover-class')).toBe(true) |
| 80 | + expect(li.classList.contains('hover-class')).toBe(false) |
42 | 81 | })
|
43 | 82 |
|
44 |
| - test('should correctly add and remove both classes on hover event', () => { |
45 |
| - // setup sortable with multiple hoverClasses |
46 |
| - sortable(ul, { |
47 |
| - 'items': 'li', |
48 |
| - hoverClass: 'sortable-item-over sortable-item-over-second' |
49 |
| - }) |
50 |
| - // trigger mouseenter event |
51 |
| - li.dispatchEvent(new CustomEvent('mouseenter')) |
52 |
| - // assert that class were added |
53 |
| - expect(li.classList.contains('sortable-item-over')).toBe(true) |
54 |
| - expect(li.classList.contains('sortable-item-over-second')).toBe(true) |
55 |
| - // trigger mouseleave event |
56 |
| - li.dispatchEvent(new CustomEvent('mouseleave')) |
| 83 | + test('should remove class when leaving sortable', () => { |
| 84 | + // trigger mouse event |
| 85 | + li.dispatchEvent(new MouseEvent('mousemove', { |
| 86 | + bubbles: true, |
| 87 | + cancelable: true, |
| 88 | + buttons: 0, |
| 89 | + target: li |
| 90 | + })) |
| 91 | + // assert that class was added |
| 92 | + expect(li.classList.contains('hover-class')).toBe(true) |
| 93 | + // trigger mouseleave events |
| 94 | + ul.dispatchEvent(new MouseEvent('mouseleave', { |
| 95 | + bubbles: true, |
| 96 | + cancelable: true, |
| 97 | + buttons: 0, |
| 98 | + target: ul |
| 99 | + })) |
57 | 100 | // assert that class was removed
|
58 |
| - expect(li.classList.contains('sortable-item-over')).toBe(false) |
59 |
| - expect(li.classList.contains('sortable-item-over-second')).toBe(false) |
| 101 | + expect(li.classList.contains('hover-class')).toBe(false) |
| 102 | + }) |
| 103 | + |
| 104 | + test('should not fire when button is pressed', () => { |
| 105 | + // trigger mouse event |
| 106 | + li.dispatchEvent(new MouseEvent('mousemove', { |
| 107 | + bubbles: true, |
| 108 | + cancelable: true, |
| 109 | + buttons: 1 |
| 110 | + target: li |
| 111 | + })) |
| 112 | + // assert that class was added |
| 113 | + expect(li.classList.contains('hover-class')).toBe(false) |
60 | 114 | })
|
| 115 | + |
61 | 116 | })
|
0 commit comments