Skip to content

Commit

Permalink
Improve unit tests for name.findName() and name.prefix()
Browse files Browse the repository at this point in the history
  • Loading branch information
titonobre committed Oct 15, 2018
1 parent f4ea60e commit 31b3f48
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions test/name.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,44 @@ describe("name.js", function () {
faker.random.number.restore();
});

it("occasionally returns a male full name with a prefix", function () {
sinon.stub(faker.random, 'number')
.withArgs(8).returns(0) // with prefix
.withArgs(1).returns(0); // gender male

sinon.stub(faker.name, 'prefix').withArgs(0).returns('X');
sinon.stub(faker.name, 'firstName').withArgs(0).returns('Y');
sinon.stub(faker.name, 'lastName').withArgs(0).returns('Z');

var name = faker.name.findName();

assert.equal(name, 'X Y Z');

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

it("occasionally returns a female full name with a prefix", function () {
sinon.stub(faker.random, 'number')
.withArgs(8).returns(0) // with prefix
.withArgs(1).returns(1); // gender female

sinon.stub(faker.name, 'prefix').withArgs(1).returns('J');
sinon.stub(faker.name, 'firstName').withArgs(1).returns('K');
sinon.stub(faker.name, 'lastName').withArgs(1).returns('L');

var name = faker.name.findName();

assert.equal(name, 'J K L');

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

it("occasionally returns a first name and last name with a suffix", function () {
sinon.stub(faker.random, 'number').returns(1);
sinon.stub(faker.name, 'suffix').returns('Jr.');
Expand Down Expand Up @@ -103,4 +141,61 @@ describe("name.js", function () {
faker.name.jobType.restore();
});
});

describe("prefix()", function () {
describe('when using a locale with gender specific name prefixes', function () {
beforeEach(function(){
this.oldLocale = faker.locale;
faker.locale = 'TEST';

faker.locales['TEST'] = {
name: {
male_prefix: ['Mp'],
female_prefix: ['Fp']
}
};
});

afterEach(function () {
faker.locale = this.oldLocale;
delete faker.locale['TEST'];
})

it("returns male prefix", function () {
var prefix = faker.name.prefix(0);

assert.equal(prefix, 'Mp')
});

it("returns female prefix", function () {
var prefix = faker.name.prefix(1);

assert.equal(prefix, 'Fp');
});
});

describe('when using a locale without gender specific name prefixes', function () {
beforeEach(function(){
this.oldLocale = faker.locale;
faker.locale = 'TEST';

faker.locales['TEST'] = {
name: {
prefix: ['P']
}
};
});

afterEach(function () {
faker.locale = this.oldLocale;
delete faker.locale['TEST'];
})

it("returns a prefix", function () {
var prefix = faker.name.prefix();

assert.equal(prefix, 'P');
});
});
});
});

0 comments on commit 31b3f48

Please sign in to comment.