aboutsummaryrefslogtreecommitdiff
path: root/test/unique.unit.js
blob: 47b38d9dab10f04a7edbd2b8b521db55d491691e (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
if (typeof module !== 'undefined') {
    var assert = require('assert');
    var sinon = require('sinon');
    var faker = require('../index');
}

describe("unique.js", function () {
    describe("unique()", function () {

        it("is able to call a function with no arguments and return a result", function () {
          var result = faker.unique(faker.internet.email);
          assert.equal(typeof result, 'string');
        });

        it("is able to call a function with arguments and return a result", function () {
            var result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email
            assert.ok(result.match(/\@c/));
        });

        it("is able to call same function with arguments and return a result", function () {
            var result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email
            assert.ok(result.match(/\@c/));
        });

        it("is able to exclude results as array", function () {
            var result = faker.unique(faker.internet.protocol, [], { exclude: ['https'] });
            assert.equal(result, 'http');
        });

        it("is able to limit unique call by maxTime in ms", function () {
            var result;
            try {
              result = faker.unique(faker.internet.protocol, [], { maxTime: 1, maxRetries: 9999, exclude: ['https', 'http'] });
            } catch (err) {
              assert.equal(err.message.substr(0, 16), 'Exceeded maxTime');
            }
        });

        it("is able to limit unique call by maxRetries", function () {
            var result;
            try {
              result = faker.unique(faker.internet.protocol, [], { maxTime: 5000, maxRetries: 5, exclude: ['https', 'http'] });
            } catch (err) {
              assert.equal(err.message.substr(0, 19), 'Exceeded maxRetries');
            }
        });

        it("is able to call last function with arguments and return a result", function () {
            var result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email
            assert.ok(result.match(/\@c/));
        });

    });
});