๐Ÿ“ฆ YMFE / yapi

๐Ÿ“„ commons.test.js ยท 86 lines
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
86import test from 'ava';
import {
  ltrim,
  rtrim,
  trim,
  handleParams,
  verifyPath, 
  sandbox,
  handleVarPath
} from '../../server/utils/commons.js';

test('trim', t => {
    t.is(trim(" a   b  ksjdfk    "), 'a   b  ksjdfk');
    t.is(trim(1), '1')
});

test('ltrim', t => {
  t.is(ltrim(" a   b  ksjdfk    "), 'a   b  ksjdfk    ');
  t.is(ltrim(1), '1')
});

test('rtrim', t => {
  t.is(rtrim(" a   b  ksjdfk    "), ' a   b  ksjdfk');
  t.is(rtrim(1), '1')
});

test('handleParams', t=>{
    t.deepEqual(handleParams({
        a: '  s k ',
        b: " a123456 "
    }, {
        a: 'string',
        b: 'number'
    }), {
        a: 's k',
        b: 0
    })
})

test('verifyPath', t=>{
    t.false(verifyPath('a/b'));
    t.true(verifyPath('/a:b/t/.api/k_-/tt'))
    t.true(verifyPath('/a:b/t/.api/k_-/tt/'))
})

test('sandbox', t=>{
    t.deepEqual(sandbox({
        a: 1
    }, 'a=2'), {a : 2});
})

test('handleVarPath', t=>{
    let result = [];
    let pathname = '/a/:id'
    handleVarPath(pathname, result);

    t.deepEqual(result, [{
        name: 'id',
        desc: ''
    }])
})

test('handleVarPath2', t=>{
    let result = [];
    let pathname = '/a/{id}'
    handleVarPath(pathname, result);

    t.deepEqual(result, [{
        name: 'id',
        desc: ''
    }])
})

test('handleVarPath4', t=>{
    let result = [];
    let pathname = '/a/id={id}/tt/:sub/kk'
    handleVarPath(pathname, result);

    t.deepEqual(result, [{
        name: 'sub',
        desc: ''
    }, {
        name: 'id',
        desc: ''
    }])
})