Socialify

Folder ..

Viewing commerce.unit.js
143 lines (103 loc) • 4.6 KB

  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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
if (typeof module !== 'undefined') {
  var assert = require('assert'),
    sinon = require('sinon'),
    blaver = require('../index');
}

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

  describe("color()", function() {
    it("returns random value from commerce.color array", function() {
      var color = blaver.commerce.color();
      assert.ok(blaver.definitions.commerce.color.indexOf(color) !== -1);
    });
  });

  describe("department(max, fixedValue)", function() {

    it("should use the default amounts when not passing arguments", function() {
      var department = blaver.commerce.department();
      assert.ok(department.split(" ").length === 1);
    });

    /*

    it("should return only one value if we specify a maximum of one", function() {
        sinon.spy(blaver.random, 'arrayElement');

        var department = blaver.commerce.department(1);

        assert.strictEqual(department.split(" ").length, 1);
        assert.ok(blaver.random.arrayElement.calledOnce);

        blaver.random.arrayElement.restore();
    });

    it("should return the maximum value if we specify the fixed value", function() {
        sinon.spy(blaver.random, 'arrayElement');

        var department = blaver.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 arrayElement has been called exactly or more than 5 times
        assert.ok(blaver.random.arrayElement.callCount >= 5);

        blaver.random.arrayElement.restore();
    });
    */
  });

  describe("productName()", function() {
    it("returns name comprising of an adjective, material and product", function() {
      sinon.spy(blaver.random, 'arrayElement');
      sinon.spy(blaver.commerce, 'productAdjective');
      sinon.spy(blaver.commerce, 'productMaterial');
      sinon.spy(blaver.commerce, 'product');
      var name = blaver.commerce.productName();

      assert.ok(name.split(' ').length >= 3);
      assert.ok(blaver.random.arrayElement.calledThrice);
      assert.ok(blaver.commerce.productAdjective.calledOnce);
      assert.ok(blaver.commerce.productMaterial.calledOnce);
      assert.ok(blaver.commerce.product.calledOnce);

      blaver.random.arrayElement.restore();
      blaver.commerce.productAdjective.restore();
      blaver.commerce.productMaterial.restore();
      blaver.commerce.product.restore();
    });
  });

  describe("price(min, max, dec, symbol)", function() {
    it("should use the default amounts when not passing arguments", function() {
      var price = blaver.commerce.price();

      assert.ok(price);
      assert.strictEqual((price > 0), true, "the amount should be greater than 0");
      assert.strictEqual((price < 1001), true, "the amount should be less than 1000");
    });

    it("should use the default decimal location when not passing arguments", function() {
      var price = blaver.commerce.price();

      var decimal = ".";
      var expected = price.length - 3;
      var actual = price.indexOf(decimal);

      assert.strictEqual(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 = blaver.commerce.price();

      var regexp = new RegExp(/[0-9.]/);

      var expected = true;
      var actual = regexp.test(amount);

      assert.strictEqual(actual, expected, 'The expected match should not include a currency symbol');
    });

    it("it should handle negative amounts, but return 0", function () {

      var amount = blaver.commerce.price(-200, -1);

      assert.ok(amount);
      assert.strictEqual((amount == 0.00), true, "the amount should equal 0");
    });

    it("it should handle argument dec", function () {

      var price = blaver.commerce.price(100, 100, 1);

      assert.ok(price);
      assert.strictEqual(price , '100.0', "the price should be equal 100.0");
    });

    it("it should handle argument dec = 0", function () {

      var price = blaver.commerce.price(100, 100, 0);

      assert.ok(price);
      assert.strictEqual(price , '100', "the price should be equal 100");
    });

  });

  describe("productDescription()", function() {
    it("returns a random product description", function() {
      sinon.spy(blaver.commerce, 'productDescription');
      var description = blaver.commerce.productDescription();

      assert.ok(typeof description === 'string');
      assert.ok(blaver.commerce.productDescription.calledOnce);

      blaver.commerce.productDescription.restore();
    });
  });

});