|
1 | 1 | import NestedSort from './main'
|
2 | 2 |
|
3 |
| -describe('sample', () => { |
4 |
| - it('should work', () => { |
5 |
| - expect(true).toBeTruthy(); |
6 |
| - }); |
7 |
| -}); |
| 3 | +describe('NestedSort', () => { |
| 4 | + const dynamicListWrapperId = 'dynamic-list-wrapper-id' |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + document.body.innerHTML = `<div id="${dynamicListWrapperId}"></div>` |
| 8 | + }) |
| 9 | + |
| 10 | + describe('How it deals with List Class Names', () => { |
| 11 | + it('should convert the listClassNames prop on the initialisation and assign it to this.listClassNames', () => { |
| 12 | + [ |
| 13 | + 'class1 class2', |
| 14 | + ['class1', 'class2'], |
| 15 | + ].forEach(listClassNames => { |
| 16 | + const ns = new NestedSort({ |
| 17 | + data: [ |
| 18 | + { id: 1, text: 'Item 1' } |
| 19 | + ], |
| 20 | + el: `#${dynamicListWrapperId}`, |
| 21 | + listClassNames, |
| 22 | + }) |
| 23 | + |
| 24 | + expect(ns.listClassNames).toEqual([ |
| 25 | + 'class1', |
| 26 | + 'class2', |
| 27 | + ]) |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should assign the class names to the main list and all the nested ones', () => { |
| 32 | + const listClassNames = ['class1', 'class2'] |
| 33 | + const ns = new NestedSort({ |
| 34 | + data: [ |
| 35 | + { id: 1, text: 'Item 1' }, |
| 36 | + { id: 11, text: 'Item 1-1', parent: 1 }, |
| 37 | + { id: 2, text: 'Item 2' }, |
| 38 | + { id: 21, text: 'Item 2-1', parent: 2 }, |
| 39 | + { id: 3, text: 'Item 3' }, |
| 40 | + ], |
| 41 | + el: `#${dynamicListWrapperId}`, |
| 42 | + listClassNames, |
| 43 | + }) |
| 44 | + |
| 45 | + const list = ns.getSortableList() |
| 46 | + const nestedLists = list.querySelectorAll('ul') |
| 47 | + const lists = [ |
| 48 | + list, |
| 49 | + ...nestedLists |
| 50 | + ] |
| 51 | + |
| 52 | + expect(nestedLists.length).toBe(2) |
| 53 | + lists.forEach(ul => { |
| 54 | + expect(Object.values(ul.classList)).toEqual(listClassNames) |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should assign the class names to the placeholder list when initialising it', () => { |
| 59 | + const listClassNames = ['class1', 'class2'] |
| 60 | + const ns = new NestedSort({ |
| 61 | + data: [ |
| 62 | + { id: 1, text: 'Item 1' } |
| 63 | + ], |
| 64 | + el: `#${dynamicListWrapperId}`, |
| 65 | + listClassNames, |
| 66 | + }) |
| 67 | + |
| 68 | + ns.initPlaceholderList() |
| 69 | + |
| 70 | + expect(ns.placeholderUl.nodeName).toBe('UL') |
| 71 | + expect(Object.values(ns.placeholderUl.classList)).toEqual(expect.arrayContaining(listClassNames)) |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + describe('How it deals with List Item Class Names', () => { |
| 76 | + it('should convert the listItemClassNames prop on the initialisation and assign it to this.listItemClassNames', () => { |
| 77 | + [ |
| 78 | + 'class1 class2', |
| 79 | + ['class1', 'class2'], |
| 80 | + ].forEach(listItemClassNames => { |
| 81 | + const ns = new NestedSort({ |
| 82 | + data: [ |
| 83 | + { id: 1, text: 'Item 1' } |
| 84 | + ], |
| 85 | + el: `#${dynamicListWrapperId}`, |
| 86 | + listItemClassNames, |
| 87 | + }) |
| 88 | + expect(ns.listItemClassNames).toEqual([ |
| 89 | + 'class1', |
| 90 | + 'class2', |
| 91 | + ]) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + it('should assign the listItemClassNames to all the list items', () => { |
| 96 | + const listItemClassNames = ['class1', 'class2'] |
| 97 | + const ns = new NestedSort({ |
| 98 | + data: [ |
| 99 | + { id: 1, text: 'Item 1' }, |
| 100 | + { id: 2, text: 'Item 2' }, |
| 101 | + { id: 3, text: 'Item 3' }, |
| 102 | + ], |
| 103 | + el: `#${dynamicListWrapperId}`, |
| 104 | + listItemClassNames, |
| 105 | + }) |
| 106 | + |
| 107 | + const list = ns.getSortableList() |
| 108 | + list.querySelectorAll('li').forEach(li => { |
| 109 | + expect(Object.values(li.classList)).toEqual(listItemClassNames) |
| 110 | + }) |
| 111 | + }) |
| 112 | + }) |
| 113 | +}) |
0 commit comments