aboutsummaryrefslogtreecommitdiff
path: root/test/internet.unit.js
blob: 2fc0db1096b73cace9802b102d93eaf64c222b2e (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
91
92
93
94
95
96
97
if (typeof module !== 'undefined') {
    var assert = require('assert');
    var sinon = require('sinon');
    var faker = require('../index');
}

describe("internet.js", function () {
    describe("email()", function () {
        it("returns an email", function () {
            sinon.stub(faker.internet, 'userName').returns('Aiden.Harann55');
            var email = faker.internet.email("Aiden.Harann55");
            var res = email.split("@");
            res = res[0];
            assert.equal(res, 'Aiden.Harann55');
            faker.internet.userName.restore();
        });
    });

    describe("userName()", function () {
        it("occasionally returns a single firstName", function () {
            sinon.stub(faker.random, 'number').returns(0);
            sinon.spy(faker.name, 'firstName');
            var username = faker.internet.userName();

            assert.ok(username);
            assert.ok(faker.name.firstName.called);

            faker.random.number.restore();
            faker.name.firstName.restore();
        });

        it("occasionally returns a firstName with a period or hyphen and a lastName", function () {
            sinon.stub(faker.random, 'number').returns(1);
            sinon.spy(faker.name, 'firstName');
            sinon.spy(faker.name, 'lastName');
            sinon.spy(faker.random, 'array_element');
            var username = faker.internet.userName();

            assert.ok(username);
            assert.ok(faker.name.firstName.called);
            assert.ok(faker.name.lastName.called);
            assert.ok(faker.random.array_element.calledWith(['.', '_']));

            faker.random.number.restore();
            faker.name.firstName.restore();
            faker.name.lastName.restore();
        });
    });

    describe("domainName()", function () {
        it("returns a domainWord plus a random suffix", function () {
            sinon.stub(faker.internet, 'domainWord').returns('bar');
            sinon.stub(faker.internet, 'domainSuffix').returns('net');

            var domain_name = faker.internet.domainName();

            assert.equal(domain_name, 'bar.net');

            faker.internet.domainWord.restore();
            faker.internet.domainSuffix.restore();
        });
    });

    describe("domainWord()", function () {
        it("returns a lower-case firstName", function () {
            sinon.stub(faker.name, 'firstName').returns('FOO');
            var domain_word = faker.internet.domainWord();

            assert.ok(domain_word);
            assert.strictEqual(domain_word, 'foo');

            faker.name.firstName.restore();
        });
    });

    describe("ip()", function () {
        it("returns a random IP address with four parts", function () {
            var ip = faker.internet.ip();
            var parts = ip.split('.');
            assert.equal(parts.length, 4);
        });
    });

    describe("userAgent()", function () {
        it("returns a valid user-agent", function () {
            var ua = faker.internet.userAgent();
            assert.ok(ua);
        });
    });

    describe("color()", function () {
        it("returns a valid hex value (like #ffffff)", function () {
            var color = faker.internet.color(100, 100, 100);
            assert.ok(color.match(/^#[a-f0-9]{6}$/));
        });
    });
});