diff options
| author | rob.scott <[email protected]> | 2015-03-10 13:57:02 +0000 |
|---|---|---|
| committer | rob.scott <[email protected]> | 2015-03-10 13:57:02 +0000 |
| commit | 3418839d8b818485331ef7931dea1de26c751da2 (patch) | |
| tree | 7dedc0b4a35e0d1d22b3b3c22210d10b31a5b7a8 /test | |
| parent | a39082f6f49fb326111bc2a0d9014c9dc65fe1fa (diff) | |
| download | faker-3418839d8b818485331ef7931dea1de26c751da2.tar.xz faker-3418839d8b818485331ef7931dea1de26c751da2.zip | |
Add Commerce functions from https://github.com/stympy/faker into javascript
Diffstat (limited to 'test')
| -rw-r--r-- | test/all.functional.js | 4 | ||||
| -rw-r--r-- | test/commerce.unit.js | 112 |
2 files changed, 115 insertions, 1 deletions
diff --git a/test/all.functional.js b/test/all.functional.js index 1a2e836a..966987e3 100644 --- a/test/all.functional.js +++ b/test/all.functional.js @@ -22,7 +22,9 @@ var modules = { phone: ['phoneNumber'], - finance: ['account', 'accountName', 'mask', 'amount', 'transactionType', 'currencyCode', 'currencyName', 'currencySymbol'] + finance: ['account', 'accountName', 'mask', 'amount', 'transactionType', 'currencyCode', 'currencyName', 'currencySymbol'], + + commerce: ['color', 'department', 'productName', 'price'] }; describe("functional tests", function () { diff --git a/test/commerce.unit.js b/test/commerce.unit.js new file mode 100644 index 00000000..856c3be6 --- /dev/null +++ b/test/commerce.unit.js @@ -0,0 +1,112 @@ +if(typeof module !== 'undefined') { + var assert = require('assert'), + sinon = require('sinon'), + faker = require('../index'); +} + +describe("commerce.js", function() { + + describe("color()", function() { + it("returns random value from commerce.color array", function() { + var color = faker.commerce.color(); + assert.ok(faker.definitions.commerce.color.indexOf(color) !== -1); + }); + }); + + describe("department(max, fixedValue)", function() { + it("should use the default amounts when not passing arguments", function() { + var department = faker.commerce.department(); + + assert.ok(department.split(" ").length <= 4); + }); + + it("should return only one value if we specify a maximum of one", function() { + sinon.spy(faker.random, 'array_element'); + + var department = faker.commerce.department(1); + + assert.strictEqual(department.split(" ").length, 1); + assert.ok(faker.random.array_element.calledOnce); + + faker.random.array_element.restore(); + }); + + it("should return the maxiumum value if we specify the fixed value", function() { + sinon.spy(faker.random, 'array_element'); + + var department = faker.commerce.department(5, true); + + console.log(department); + + // account for the separator + assert.strictEqual(department.split(" ").length, 6); + // Sometimes it will generate duplicates that aren't used in the final string, + // so we check if array_element has been called exactly or more than 5 times + assert.ok(faker.random.array_element.callCount >= 5); + + faker.random.array_element.restore(); + }) + }); + + describe("productName()", function() { + it("returns name comprising of an adjective, material and product", function() { + sinon.spy(faker.random, 'array_element'); + sinon.spy(faker.commerce, 'productAdjective'); + sinon.spy(faker.commerce, 'productMaterial'); + sinon.spy(faker.commerce, 'product'); + var name = faker.commerce.productName(); + + assert.ok(name.split(' ').length >= 3); + assert.ok(faker.random.array_element.calledThrice); + assert.ok(faker.commerce.productAdjective.calledOnce); + assert.ok(faker.commerce.productMaterial.calledOnce); + assert.ok(faker.commerce.product.calledOnce); + + faker.random.array_element.restore(); + faker.commerce.productAdjective.restore(); + faker.commerce.productMaterial.restore(); + faker.commerce.product.restore(); + }); + }); + + describe("price(min, max, dec, symbol", function() { + it("should use the default amounts when not passing arguments", function() { + var price = faker.commerce.price(); + + assert.ok(price); + assert.equal((price > 0), true, "the amount should be greater than 0"); + assert.equal((price < 1001), true, "the amount should be less than 1000"); + }); + + it("should use the default decimal location when not passing arguments", function() { + var price = faker.commerce.price(); + + var decimal = "."; + var expected = price.length - 3; + var actual = price.indexOf(decimal); + + assert.equal(actual, expected, "The expected location of the decimal is " + expected + " but it was " + actual + " amount " + price); + }); + + it("should not include a currency symbol by default", function () { + + var amount = faker.commerce.price(); + + var regexp = new RegExp(/[0-9.]/); + + var expected = true; + var actual = regexp.test(amount); + + assert.equal(actual, expected, 'The expected match should not include a currency symbol'); + }); + + it("it should handle negative amounts, but return 0", function () { + + var amount = faker.commerce.price(-200, -1); + + assert.ok(amount); + assert.equal((amount == 0.00), true, "the amount should equal 0"); + }); + }); + +});
\ No newline at end of file |
