aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/utilsSpec.js
blob: d0beca1433963dc232b13845852df81b3c20a69c (plain)
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
import * as utils from '../utils'

global.fetch = jest.fn()

describe('formatSize', () => {
    it('should return 0 when 0 byte', () => {
        expect(utils.formatSize(0)).toEqual('0')
    })

    it('should return formatted size', () => {
        expect(utils.formatSize(27104011)).toEqual("25.8mb")
        expect(utils.formatSize(1023)).toEqual("1023b")
    })
})

describe('formatTimeDelta', () => {
    it('should return formatted time', () => {
        expect(utils.formatTimeDelta(3600100)).toEqual("1h")
    })
})

describe('formatTimeSTamp', () => {
    it('should return formatted time', () => {
        expect(utils.formatTimeStamp(1483228800)).toEqual("2017-01-01 00:00:00.000")
    })
})

describe('reverseString', () => {
    it('should return reversed string', () => {
        let str1 = "abc", str2="xyz"
        expect(utils.reverseString(str1) > utils.reverseString(str2)).toBeTruthy()
    })
})

describe('fetchApi', () => {
    it('should handle fetch operation', () => {
        utils.fetchApi('http://foo/bar', {method: "POST"})
        expect(fetch.mock.calls[0][0]).toEqual(
            "http://foo/bar?_xsrf=undefined"
        )
        fetch.mockClear()

        utils.fetchApi('http://foo?bar=1', {method: "POST"})
        expect(fetch.mock.calls[0][0]).toEqual(
            "http://foo?bar=1&_xsrf=undefined"
        )

    })

    it('should be possible to do put request', () => {
        fetch.mockClear()
        utils.fetchApi.put("http://foo", [1, 2, 3], {})
        expect(fetch.mock.calls[0]).toEqual(
            [
                "http://foo?_xsrf=undefined",
                {
                    body: "[1,2,3]",
                    credentials: "same-origin",
                    headers: { "Content-Type": "application/json" },
                    method: "PUT"
                },
            ]
        )
    })
})

describe('getDiff', () => {
    it('should return json object including only the changed keys value pairs', () => {
        let obj1 = {a: 1, b:{ foo: 1} , c: [3]},
            obj2 = {a: 1, b:{ foo: 2} , c: [4]}
        expect(utils.getDiff(obj1, obj2)).toEqual({ b: {foo: 2}, c:[4]})
    })
})

describe('pure', () => {
    let tFunc = function({ className }) {
        return (<p className={ className }>foo</p>)
    },
        puredFunc = utils.pure(tFunc),
        f = new puredFunc('bar')

    it('should display function name', () => {
        expect(utils.pure(tFunc).displayName).toEqual('tFunc')
    })

    it('should render properties', () => {
        expect(f.render()).toEqual(tFunc('bar'))
    })

})