|
| 1 | +// 插槽测试 |
| 2 | +import { mount } from '@vue/test-utils' |
| 3 | +import JsonTree from '@/index.js' |
| 4 | + |
| 5 | +// 使用这个方法生成的 json 结构数据 |
| 6 | +// 根据 JsonTree 的默认项 |
| 7 | +// 将会全部展开(默认展开两级), 并显示 3 条连接线 |
| 8 | +const genJsonData = () => { |
| 9 | + return { |
| 10 | + name: 'Tom', |
| 11 | + age: 2, |
| 12 | + nums: [1, 1, 2, 3, 5, 8], |
| 13 | + friends: [ |
| 14 | + { name: 'Jerry', age: 1 }, |
| 15 | + { name: 'Tuffy', age: 2 } |
| 16 | + ] |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +const factory = (conf) => { |
| 21 | + return mount(JsonTree, { |
| 22 | + propsData: { |
| 23 | + jsonData: genJsonData(), |
| 24 | + expandDeep: 2 |
| 25 | + }, |
| 26 | + ...conf |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +describe('JsonTree slots 测试', () => { |
| 31 | + // TODO: 测试 scoped slots |
| 32 | + it('toggle slot', () => { |
| 33 | + const wrapper = factory({ |
| 34 | + scopedSlots: { |
| 35 | + toggle ({ collapse }) { |
| 36 | + return this.$createElement('i', { |
| 37 | + 'class': collapse ? 'el-icon-caret-right' : 'el-icon-caret-bottom' |
| 38 | + }) |
| 39 | + } |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + // 根据 genJsonData() 生成的数据 |
| 44 | + const toggles = wrapper.findAll('.json-tree__toggle:not(.json-tree__toggle--hidden)') |
| 45 | + // console.log(toggles.length) |
| 46 | + let expandedCnt = 0 |
| 47 | + let collapsedCnt = 0 |
| 48 | + toggles.wrappers.forEach(v => { |
| 49 | + // console.log(v.html()) |
| 50 | + if (v.find('.el-icon-caret-bottom').exists()) { |
| 51 | + expandedCnt++ |
| 52 | + } else if (v.find('.el-icon-caret-right').exists()) { |
| 53 | + collapsedCnt++ |
| 54 | + } |
| 55 | + }) |
| 56 | + // console.log(expandedCnt, collapsedCnt) |
| 57 | + // 应该有 3 个可见的收起图标(.el-icon-caret-bottom), 2 个可见的展开图标(.el-icon-caret-right) |
| 58 | + // const expandedIcons = toggles.filter(v => v.classes('el-icon-caret-bottom')) |
| 59 | + // const collapsedIcons = toggles.filter(v => v.classes('el-icon-caret-right')) |
| 60 | + // console.log(expandedIcons.length, collapsedIcons.length) |
| 61 | + |
| 62 | + // 应该有 3 个可见的收起图标(.el-icon-caret-bottom) |
| 63 | + // 2 个可见的展开图标(.el-icon-caret-right) |
| 64 | + // 注意:以上结果依赖于 expandDeep 和 genJsonData() |
| 65 | + expect(expandedCnt).toBe(3) |
| 66 | + expect(collapsedCnt).toBe(2) |
| 67 | + }) |
| 68 | + |
| 69 | + it('count slot', () => { |
| 70 | + const wrapper = factory({ |
| 71 | + scopedSlots: { |
| 72 | + count ({ type, count }) { |
| 73 | + return this.$createElement('span', { |
| 74 | + 'class': 'slot-count' |
| 75 | + }, `// 值类型:${type},共${count}项`) |
| 76 | + } |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + expect(wrapper.find('.slot-count').exists()).toBe(true) |
| 81 | + |
| 82 | + const texts = wrapper.findAll('.slot-count').wrappers.map(v => v.text()) |
| 83 | + expect(texts).toContain('// 值类型:object,共4项') |
| 84 | + }) |
| 85 | + |
| 86 | + it('key slot', () => { |
| 87 | + const wrapper = factory({ |
| 88 | + scopedSlots: { |
| 89 | + key ({ itemKey }) { |
| 90 | + return this.$createElement('span', { class: 'slot-key' }, itemKey) |
| 91 | + } |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + expect(wrapper.find('.slot-key').exists()).toBe(true) |
| 96 | + |
| 97 | + const texts = wrapper.findAll('.slot-key').wrappers.map(v => v.text()) |
| 98 | + expect(texts).toContain('name') |
| 99 | + expect(texts).toContain('age') |
| 100 | + expect(texts).toContain('nums') |
| 101 | + expect(texts).toContain('friends') |
| 102 | + }) |
| 103 | + |
| 104 | + it('val slot', () => { |
| 105 | + const wrapper = factory({ |
| 106 | + scopedSlots: { |
| 107 | + val ({ itemVal, type }) { |
| 108 | + return this.$createElement('span', { |
| 109 | + class: 'slot-val' |
| 110 | + }, `${itemVal}(${type})`) |
| 111 | + } |
| 112 | + } |
| 113 | + }) |
| 114 | + |
| 115 | + expect(wrapper.find('.slot-val').exists()).toBe(true) |
| 116 | + const texts = wrapper.findAll('.slot-val').wrappers.map(v => v.text()) |
| 117 | + expect(texts).toContain('Tom(string)') |
| 118 | + expect(texts).toContain('2(number)') |
| 119 | + }) |
| 120 | +}) |
0 commit comments