File: test.file.js

package info (click to toggle)
node-chance 2.2.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,612 kB
  • sloc: javascript: 16,572; makefile: 4
file content (117 lines) | stat: -rw-r--r-- 4,080 bytes parent folder | download | duplicates (2)
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
const Chance = require('../chance.js')
const _ = require('lodash')

const chance = new Chance()

const fileExtensions = {
    "raster": [ "bmp", "gif", "gpl", "ico", "jpeg", "psd", "png", "psp", "raw",
                "tiff" ],
    "vector": [ "3dv", "amf", "awg", "ai", "cgm", "cdr", "cmx", "dxf", "e2d",
                "egt", "eps", "fs", "odg", "svg", "xar" ],
    "3d": [ "3dmf", "3dm", "3mf", "3ds", "an8", "aoi", "blend", "cal3d", "cob",
            "ctm", "iob", "jas", "max", "mb", "mdx", "obj", "x", "x3d" ],
    "document": [ "doc", "docx", "dot", "html", "xml", "odt", "odm", "ott", "csv",
                  "rtf", "tex", "xhtml", "xps" ]
}

// chance.file()
test('file() returns random file length with random extension', () => {
    _.times(1000, () => {
        let file = chance.file()
        expect(_.isString(file)).toBe(true)
        expect(file.split('.').length).toBe(2)
    })
})

test('file() returns error if wrong fileType provided', () => {
    _.times(1000, () => {
        const fn = () => chance.file({ fileType: 'not_specified' })
        expect(fn).toThrowError(
            'Chance: Expect file type value to be \'raster\', \'vector\', \'3d\' or \'document\''
        )
    })
})

test('file() does not return error if legit fileType provided', () => {
    _.times(1000, () => {
        const fn = () => chance.file({ fileType: 'raster' })
        expect(fn).not.toThrow()
    })
})

test('file() returns filename with specific extension type', () => {
    _.times(1000, () => {
        let file = chance.file({ fileType: 'raster' })
        expect(_.isString(file)).toBe(true)
        let extension = file.split('.')[1]
        expect(fileExtensions['raster'].indexOf(extension) !== -1).toBe(true)
    })
})

test('file() returns filename with specific extension', () => {
    _.times(1000, () => {
        let file = chance.file({ extension: 'doc' })
        let extension = file.split('.')[1]
        expect(extension).toBe('doc')
    })
})

test('file() can take a length and obey it', () => {
    _.times(1000, () => {
        let length = chance.d10()
        let file = chance.file({ length: length })
        let filename = file.split('.')[0]
        expect(filename.length).toBe(length)
    })
})

test('file() can take a pool of extensions and obey them', () => {
    _.times(1000, () => {
        let extensions = [ 'bmp', '3dv', '3dmf', 'doc' ]
        let file = chance.file({ extensions: extensions })
        let extension = file.split('.')[1]
        expect(extensions.indexOf(extension) !== -1).toBe(true)
    })
})

test('file() can take pool of extensions by object collection and obey them', () => {
    const objectExtensionCollection = {
        "one": [ "extension_one_1", "extension_one_2", "extension_one_3" ],
        "two": [ "extension_two_1", "extension_two_2", "extension_two_3" ],
        "three": [ "extension_three_1", "extension_three_2", "extension_three_3" ]
    }

    _.times(1000, () => {
        let file = chance.file({ extensions: objectExtensionCollection })
        let extension = file.split('.')[1]
        let extensionCount = 0
        for (let key in objectExtensionCollection) {
            let collection = objectExtensionCollection[key]
            collection.map((ext) => {
                if (ext === extension) {
                    extensionCount++
                }
            })
        }
        expect(extensionCount).toBe(1)
    })
})

test('file() throws if bad extensions option provided', () => {
    const fn = () => chance.file({ extensions: 10 })
    expect(fn).toThrowError('Chance: Extensions must be an Array or Object')
})

test('file() does not throw if good extensions option provided as array', () => {
    _.times(1000, () => {
        const fn = () => chance.file({ extensions: fileExtensions.document })
        expect(fn).not.toThrow()
    })
})

test('file() does not throw if good extensions option provided as object', () => {
    _.times(1000, () => {
        const fn = () => chance.file({ extensions: fileExtensions })
        expect(fn).not.toThrow()
    })
})