Skip to content

Commit 43629c3

Browse files
author
Lukas Oppermann
authored
[WIP] Remove _listConnected on _test property & fix tests to use module (#406)
* remove _listConnected on test * add tests from isConnected * remove old test * move test to main folder
1 parent a7456d1 commit 43629c3

File tree

5 files changed

+130
-128
lines changed

5 files changed

+130
-128
lines changed

__tests__/helpers.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-env jest */
22

33
export const mockInnerHTML: string = `
4-
<ul class="sortable">
4+
<ul class="sortable all-sortables">
55
<li class="li-first">
66
<span class="handle"></span>
77
item
@@ -14,4 +14,5 @@ export const mockInnerHTML: string = `
1414
<span class="handle"></span>
1515
item 3
1616
</li>
17-
</ul>`
17+
</ul>
18+
<ul class="second-sortable all-sortables"></ul>`

__tests__/isConnected.test.ts

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/* global describe,test,expect */
2+
import { mockInnerHTML } from './helpers'
3+
import isConnected from '../src/isConnected'
4+
import sortable from '../src/html5sortable'
5+
/* eslint-env jest */
6+
7+
describe('Testing isConnected modules', () => {
8+
let sortableElement, secondSortable
9+
// setup
10+
beforeEach(() => {
11+
document.body.innerHTML = mockInnerHTML
12+
sortableElement = document.body.querySelector('.sortable')
13+
secondSortable = document.body.querySelector('.second-sortable')
14+
})
15+
16+
test('each sortable ul should connect with itself by default', () => {
17+
sortable(sortableElement, {})
18+
// test if sortable is connected to itself (should be true)
19+
expect(isConnected(sortableElement, sortableElement)).toEqual(true)
20+
})
21+
22+
test('acceptFrom fails when invalid selector is given', () => {
23+
sortable(sortableElement, {
24+
acceptFrom: secondSortable
25+
})
26+
sortable(secondSortable, {
27+
acceptFrom: true
28+
})
29+
// assert
30+
expect(() => { isConnected(sortableElement, sortableElement) }).toThrowError('HTML5Sortable: Wrong argument, "acceptFrom" must be "null", "false", or a valid selector string.')
31+
expect(() => { isConnected(secondSortable, sortableElement) }).toThrowError('HTML5Sortable: Wrong argument, "acceptFrom" must be "null", "false", or a valid selector string.')
32+
})
33+
34+
test('acceptFrom does not accept when empty selector is given', () => {
35+
sortable(sortableElement, {
36+
acceptFrom: ''
37+
})
38+
expect(isConnected(sortableElement, secondSortable)).toEqual(false)
39+
expect(isConnected(sortableElement, sortableElement)).toEqual(false)
40+
})
41+
42+
test('acceptFrom is disconnected to itself if set to false', () => {
43+
sortable(sortableElement, {
44+
acceptFrom: false
45+
})
46+
// assert
47+
expect(isConnected(sortableElement, secondSortable)).toEqual(false)
48+
expect(isConnected(sortableElement, sortableElement)).toEqual(false)
49+
})
50+
51+
test('acceptFrom is connected to itself if set to null & disconnecte from other sortables', () => {
52+
sortable(sortableElement, {
53+
acceptFrom: null
54+
})
55+
// assert
56+
expect(isConnected(sortableElement, secondSortable)).toEqual(false)
57+
expect(isConnected(sortableElement, sortableElement)).toEqual(true)
58+
})
59+
60+
test('acceptFrom is connected to other sortables if set to selector & disconnected from itself', () => {
61+
sortable(sortableElement, {
62+
acceptFrom: '.second-sortable'
63+
})
64+
// assert
65+
expect(isConnected(sortableElement, secondSortable)).toEqual(true)
66+
expect(isConnected(sortableElement, sortableElement)).toEqual(false)
67+
// one direction connected only
68+
expect(isConnected(secondSortable, sortableElement)).toEqual(false)
69+
})
70+
71+
test('acceptFrom is connected to other sortables including itself if selector matches', () => {
72+
sortable(sortableElement, {
73+
acceptFrom: '.all-sortables'
74+
})
75+
// assert
76+
expect(isConnected(sortableElement, secondSortable)).toEqual(true)
77+
expect(isConnected(sortableElement, sortableElement)).toEqual(true)
78+
// one direction connected only
79+
expect(isConnected(secondSortable, sortableElement)).toEqual(false)
80+
})
81+
82+
test('⚠️ [deprecated] connectWith: sortables are connected in all way', () => {
83+
// disable warnings in console
84+
global.console.warn = (input) => {}
85+
// setup
86+
sortable(sortableElement, {
87+
connectWith: 'someValue'
88+
})
89+
sortable(secondSortable, {
90+
connectWith: 'someValue'
91+
})
92+
// because ul was never instantiated with connectWith: '.sortable' they are not connected, so all should be false
93+
expect(isConnected(sortableElement, secondSortable)).toEqual(true)
94+
expect(isConnected(sortableElement, sortableElement)).toEqual(true)
95+
expect(isConnected(secondSortable, secondSortable)).toEqual(true)
96+
expect(isConnected(secondSortable, sortableElement)).toEqual(true)
97+
})
98+
99+
test('⚠️ [deprecated] connectWith: if one sortable misses value, they are not connected', () => {
100+
// disable warnings in console
101+
global.console.warn = (input) => {}
102+
sortable(sortableElement, {
103+
connectWith: 'someValue'
104+
})
105+
sortable(secondSortable, {})
106+
// because ul was never instantiated with connectWith: '.sortable' they are not connected, so all should be false
107+
expect(isConnected(sortableElement, secondSortable)).toEqual(false)
108+
expect(isConnected(sortableElement, sortableElement)).toEqual(true)
109+
expect(isConnected(secondSortable, secondSortable)).toEqual(true)
110+
expect(isConnected(secondSortable, sortableElement)).toEqual(false)
111+
})
112+
})

__tests__/sortableMethodsTests/_listsConnected.test.ts

-114
This file was deleted.

src/html5sortable.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,6 @@ sortable.__testing = {
585585
_data: _data,
586586
_removeItemEvents: _removeItemEvents,
587587
_removeItemData: _removeItemData,
588-
_removeSortableData: _removeSortableData,
589-
_listsConnected: _listsConnected
588+
_removeSortableData: _removeSortableData
590589
}
591590
/* END.TESTS_ONLY */

src/isConnected.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
import store from './store'
22
/**
3-
* Check if two lists are connected
4-
* @param {HTMLElement} curList
5-
* @param {HTMLElement} destList
3+
* Check if curList accepts items from destList
4+
* @param {sortable} destination the container an item is move to
5+
* @param {sortable} origin the container an item comes from
66
*/
7-
export default (curList: sortable, destList: sortable) => {
7+
export default (destination: sortable, origin: sortable) => {
88
// check if valid sortable
9-
if (curList.isSortable === true) {
10-
const acceptFrom = store(curList).getConfig('acceptFrom')
9+
if (destination.isSortable === true) {
10+
const acceptFrom = store(destination).getConfig('acceptFrom')
11+
// check if acceptFrom is valid
12+
if (acceptFrom !== null && acceptFrom !== false && typeof acceptFrom !== 'string') {
13+
throw new Error('HTML5Sortable: Wrong argument, "acceptFrom" must be "null", "false", or a valid selector string.')
14+
}
1115

1216
if (acceptFrom !== null) {
1317
return acceptFrom !== false && acceptFrom.split(',').filter(function (sel) {
14-
return sel.length > 0 && destList.matches(sel)
18+
return sel.length > 0 && origin.matches(sel)
1519
}).length > 0
1620
}
1721
// drop in same list
18-
if (curList === destList) {
22+
if (destination === origin) {
1923
return true
2024
}
2125
// check if lists are connected with connectWith
22-
if (store(curList).getConfig('connectWith') !== undefined && store(curList).getConfig('connectWith') !== null) {
23-
return store(curList).getConfig('connectWith') === store(destList).getConfig('connectWith')
26+
if (store(destination).getConfig('connectWith') !== undefined && store(destination).getConfig('connectWith') !== null) {
27+
return store(destination).getConfig('connectWith') === store(origin).getConfig('connectWith')
2428
}
2529
}
2630
return false

0 commit comments

Comments
 (0)