All files / test data.js

36.78% Statements 32/87
0% Branches 0/23
55.56% Functions 20/36
42.67% Lines 32/75

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 3041x 1x 1x             1x     9x                             10x                                 1x 1x     14x                                                               1x 1x     13x                                           1x 1x     6x                             1x 1x     5x                                           1x 1x     5x                                           1x 1x     12x                                         1x 1x     2x                                               1x 1x     2x                                             21x                                                     15x                                      
const dirname = ['dist','src'][1]
const { query, serialize, $, removeNode, insertAfter, addClass, removeClass, getAbsoluteUrl, debounce, throttle, removeItemByIndex, removeItemByValue } = require('../' + dirname + '/mtfunit.js')
const CONF = {
  'url': {
    'root': 'http://127.0.0.1:8080/',
    'test': 'http://127.0.0.1:8080/test/',
    'dom': 'http://127.0.0.1:8080/test/dom/'
   }
}
module.exports = [
  { 
    name: 'query', // 测试分组名称
    f: data => expect(query(...data.slice(0, -1))).toBe(data[data.length - 1]), // 回调函数:如何使用测试用例
    datas: [ // 测试用例
      ['a', 'a=1', '1'], // [参数1, 参数2 ... 预期结果] => 生成描述:Tested:参数拼接字符串 + Excepted:预期结果
      ['a', 'b=1', undefined],
      ['a', 'b=1&a = 1 ', '1'],
      ['a', 'b= 1 & a = 1 ', '1'],
      ['a b', 'a b = 1 ', '1'],
      ['a b', 'a b = a b ', 'a b'],
      [undefined, 'b=1', undefined],
      [null, undefined, undefined],
      ['http://www.baidu.com','http%3A%2F%2Fwww.baidu.com=http%3A%2F%2Fwww.baidu.com', 'http://www.baidu.com']
    ]
  },
  { 
    name: 'serialize',
    f: data => expect(serialize(...data.slice(0, -1))).toBe(data[data.length - 1]),
    datas: [
      [undefined, ''],
      [null, ''],
      [{}, ''],
      [[], ''],
      [{ a: 1 }, 'a=1'],
      [{ a: 1, b: [1, 2] }, 'a=1&b=%5B1%2C2%5D'],
      [{ a: 1, b: { c : 2 } }, 'a=1&b=%7B%22c%22%3A2%7D'],
      [{ 1: 1, 2: 2}, '1=1&2=2'],
      [{ a: 'http://www.baidu.com' }, 'a=http%3A%2F%2Fwww.baidu.com'],
      [{ a: 'http://www.baidu.com', b: { c: 'http://www.baidu.com' } }, 'a=http%3A%2F%2Fwww.baidu.com&b=%7B%22c%22%3A%22http%3A%2F%2Fwww.baidu.com%22%7D']
    ]
  },
  {
    name: '$',
    async bf () { // 回调函数:调用所有测试用例前执行
      await page.goto(CONF['url']['dom'])
      await page.addScriptTag({ content: `${$}` })
    },
    async f (data) { // 回调函数:异步执行测试用例
      const ele = await page.evaluate(data => $(data[0]), data)
      switch(data[1]) {
        case 'null':
          await expect(ele).toBeNull()
          break
        case 'not null':
          await expect(ele).not.toBeNull()
          break
        case 'length':
          await expect(ele.length).toBe(data[2])
      }
    },
    datas: [
      ['', 'null'],
      [undefined, 'null'],
      [null, 'null'],
      [1, 'null'],
      ['123', 'null'],
      [[], 'null'],
      [{}, 'null'],
      ['#a', 'not null'],
      ['#a', 'not null'],
      ['div', 'not null'],
      ['.c', 'not null'],
      ['.c span', 'not null'],
      ['#a, #b', 'length', 2],
      ['ul li:first-child, ul li:last-child', 'length', 2]
    ]
  },
  {
    name: 'removeNode',
    async bf () {
      await page.goto(CONF['url']['dom'])
      await page.addScriptTag({ content: `${removeNode} ${$}` });
    },
    async f (data) {
      const ele = await page.evaluate(data => (removeNode($(data[0])), $(data[0])), data)
      await expect(ele ? (ele.length === void 0 ? 1 : ele.length) : 0).toBe(data[1])
    },
    datas: [
      ['', 0],
      [undefined, 0],
      [null, 0],
      [1, 0],
      ['123', 0],
      [[], 0],
      [{}, 0],
      ['#a div', 0],
      ['.c', 1],
      ['.c span', 0],
      ['ul li', 1],
      ['ul li', 0],
      ['#a, #b', 0],
    ]
  },
  {
    name: 'insertAfter',
    async bf () {
      await page.goto(CONF['url']['dom'])
      await page.addScriptTag({ content: `${insertAfter} ${$}` });
    },
    async f (data) {
      const attr = await page.evaluate(data => (insertAfter($(data[0]), $(data[1])), ($(data[1])?.nextSibling || ($(data[1]) || document.body).lastChild)[data[2]]), data)
      await expect(attr).toBe(data[3])
    },
    datas: [
      ['#a', '#b', 'id', 'a'],
      ['#a, #b', '.c', 'id', 'b'],
      ['ul li:first-child, ul li:last-child', '', 'tagName', 'LI'],
      ['ul li:first-child, ul li:last-child', undefined, 'tagName', 'LI'],
      ['li:first-of-type, li:last-of-type', null, 'tagName', 'LI'],
      [null, null, 'tagName', 'LI']
    ]
  },
  {
    name: 'addClass',
    async bf () {
      await page.goto(CONF['url']['dom'])
      await page.addScriptTag({ content: `${addClass} ${$}` });
    },
    async f (data) {
      const className = await page.evaluate(data => {
        addClass($(data[0]), data[1])
        const o = $(data[0])
        if (o) {
          if (o.length) return o.map(v => v.className).join(' ')
          return o.className
        } 
        return 'undefined'
      }, data)
      await expect(className).toMatch(data[2])
    },
    datas: [
      [null, 'a1', 'undefined'],
      [undefined, 'a1', 'undefined'],
      [undefined, null, ''],
      ['#a', 'a1', 'a1'],
      ['#a, #b', 'a2', /a2.*?a2/]
    ]
  },
  {
    name: 'removeClass',
    async bf () {
      await page.goto(CONF['url']['dom'])
      await page.addScriptTag({ content: `${removeClass} ${$}` });
    },
    async f (data) {
      const className = await page.evaluate(data => {
        removeClass($(data[0]), data[1])
        const o = $(data[0])
        if (o) {
          if (o.length) return o.map(v => v.className).join(' ')
          return o.className
        } 
        return ''
      }, data)
      await expect(className).not.toContain(data[1])
    },
    datas: [
      [null, 'a1', 'no error'],
      [undefined, 'a1', 'no error'],
      [undefined, null, 'no error'],
      ['#a', 'a-c', 'not a-c'],
      ['#a, #b', 'b-c', 'not b-c']
    ]
  },
  {
    name: 'getAbsoluteUrl',
    async bf () {
      await page.goto(CONF['url']['dom'])
      await page.addScriptTag({ content: `${getAbsoluteUrl}` });
    },
    async f (data) {
      const absoluteUrl = await page.evaluate(data => getAbsoluteUrl(data[0]), data)
      await expect(absoluteUrl).toBe(data[1])
    },
    datas: [
      ['', CONF['url']['dom']],
      [null, CONF['url']['dom']],
      [undefined, CONF['url']['dom']],
      ['/', CONF['url']['root']],
      ['/jerojiang', CONF['url']['root'] + 'jerojiang'],
      ['./jerojiang', CONF['url']['dom'] + 'jerojiang'],
      ['../jerojiang', CONF['url']['test'] + 'jerojiang'],
      ['../../jerojiang', CONF['url']['root'] + 'jerojiang'],
      ['../../../jerojiang', CONF['url']['root'] + 'jerojiang'],
      ['...././...../jerojiang', CONF['url']['root'] + 'jerojiang'],
      ['...././...../jerojiang/../', CONF['url']['root']],
      ['...././jerojiang/.././jerojiang/..././jerojiang', CONF['url']['test'] + 'jerojiang']
    ]
  },
  {
    name: 'debounce',
    async bf () {
      await page.goto(CONF['url']['dom'])
      await page.addScriptTag({ content: `${debounce}` });
    },
    async f (data) {
      const count = await page.evaluate(data => {
        let count = 0, scrollCount = 0, timer = null
        addEventListener('scroll', debounce(() => {
          count++
        }, data[0]))
        return new Promise((resolve => {
          timer = setInterval(() => {
            if (++scrollCount === 10) {
              clearInterval(timer)
              resolve(count + 1)
            } else scrollBy(0, 1)
          }, data[1])
        }))
      }, data)
      await expect(count).toBe(data[2])
    },
    datas: [
      [60, 1000 / 60, 1], // 防抖时间间隔 > 事件触发时间间隔,只触发1次
      [1000 / 60, 60, 10], // 防抖时间间隔 < 事件触发时间间隔,触发10次
    ]
  },
  {
    name: 'throttle',
    async bf () {
      await page.goto(CONF['url']['dom'])
      await page.addScriptTag({ content: `${throttle}` });
    },
    async f (data) {
      const count = await page.evaluate(data => {
        let count = 0, scrollCount = 0, timer = null
        addEventListener('scroll', throttle(() => {
          count++
        }, data[0]))
        return new Promise((resolve => {
          timer = setInterval(() => {
            if (++scrollCount === 10) {
              clearInterval(timer)
              resolve(count)
            } else scrollBy(0, 1)
          }, data[1])
        }))
      }, data)
      await expect(count).toBeGreaterThanOrEqual(data[2])
    },
    datas: [
      [50, 20, 3, '>=3'], // 节流时间间隔50ms > 事件触发间隔20ms,触发9次,180ms / 50ms | 0 >= 3
      [50, 60, 9, '>=9'], // 节流时间间隔50ms < 事件触发间隔60ms,触发9次,540ms / 50ms | 0 >= 9(9 - 10)
    ]
  },
  {
    name: 'removeItemByIndex',
    f: data => expect(removeItemByIndex(...data.slice(0, -1))).toStrictEqual(data[data.length - 1]),
    datas: [
      ['', null, null],
      ['1', [], []],
      ['-1', {}, {}],
      ['-1', void 0, void 0],
      [1n, [1, 2, 3], [1, 2, 3]],
      ['', [1, 2, 3], [2, 3]],
      [void 0, [1, 2, 3], [2, 3]],
      [null, [1, 2, 3], [2, 3]],
      [0, [1, 2, 3], [2, 3]],
      ['0', [1, 2, 3], [2, 3]],
      [false, [1, 2, 3], [2, 3]],
      ['1', [1, 2, 3], [1, 3]],
      [true, [1, 2, 3], [1, 3]],
      ['1.5', [1, 2, 3], [1, 3]],
      [[2], [1, 2, 3], [1, 2]],
      [['2'], [1, 2, 3], [1, 2]],
      [-1, [1, 2, 3], [1, 2]],
      ['-2', [1, 2, 3], [1, 3]],
      ['-4', [1, 2, 3], [2, 3]],
      [4, [1, 2, 3], [1, 2, 3]],
      ['5', [1, 2, 3], [1, 2, 3]]
    ]
  },
  {
    name: 'removeItemByValue',
    f: data => expect(removeItemByValue(...data.slice(0, -1))).toStrictEqual(data[data.length - 1]),
    datas: [
      ['', null, null],
      ['1', [], []],
      ['-1', {}, {}],
      ['-1', void 0, void 0],
      [1n, [0, 1, 2], [0, 1, 2]],
      ['', [0, 1, 2], [0, 1, 2]],
      [void 0, [0, 1, 2], [0, 1, 2]],
      [null, [0, 1, 2], [0, 1, 2]],
      ['0', [0, 1, 2], [0, 1, 2]],
      [+0, [0, 1, 2], [1, 2]],
      [-0, [0, 1, 2], [1, 2]],
      [1, [0, 1, 2], [0, 2]],
      [2, [0, 1, 2], [0, 1]],
      [-3, [0, 1, 2], [0, 1, 2]],
      [3, [0, 1, 2], [0, 1, 2]]
    ]
  }
]