samedi 10 février 2018

Tests not returning expected result - ticket seller kata

I'm trying to practice my javascript testing by doing some katas. I don't quite understand why this isn't working, I managed to do a ruby version quite easily with a similar sell function.

ticketClark.js

var TicketClerk = function() {
  this.till = { 25: 0, 50: 0, 100: 0 };
};

TicketClerk.prototype.sell = function(array) {
  for (var i = 0; i < array.length; i++) {
    if (this.canMakeChange(array[i])) {
      this.giveChange(array[i]);
    } else {
      return "NO";
    }
  }
  return "YES";
};

TicketClerk.prototype.canMakeChange = function(note) {
  if (note === 50) {
    return this.till[25] > 0;
  }
  if (note === 100) {
    return this.canGiveFiftyTwentyFive() || this.canGiveThreeTwentyFives();
  }
  return true;
};

TicketClerk.prototype.giveChange = function(note) {
  if (note === 25) {
    this.till[25]++;
  }
  if (note === 50) {
    this.till[25]--;
    this.till[50]++;
  }
  if (note === 100 && this.canGiveFiftyTwentyFive()) {
    this.till[25]--;
    this.till[50]--;
    this.till[100]++;
  }
  if (note === 100 && this.canGiveThreeTwentyFives()) {
    this.till[25] -= 3;
    this.till[100]++;
  }
};

TicketClerk.prototype.canGiveThreeTwentyFives = function() {
  return this.till[25] > 2;
};

TicketClerk.prototype.canGiveFiftyTwentyFive = function() {
  return this.till[25] > 0 && this.till[50] > 0;
};

test.js

describe("#TicketClerk", function() {
  beforeEach(function() {
    ticketClerk = new TicketClerk();
  });
  describe("#initialize", function() {
    it("shows a hash of the money in the till, which starts with zero of each denominator", function() {
      expect(ticketClerk.till).toEqual({ 25: 0, 50: 0, 100: 0 });
    });
  });

  describe("#sell", function() {
    it("entering 25, 25, 50 should return 'YES' as it can give change", function() {
      ticketClerk.sell([25, 25, 50, 50]);
      expect(ticketClerk.sell).toEqual("YES");
    });
    it("entering 50, 25, 50 should return 'NO' as it cannot give change", function() {
      ticketClerk.sell([50, 25, 50]);
      expect(ticketClerk.sell).toEqual("NO");
    });
  });
});

I left out my other tests, I don't think its needed. The Kata is about accepting movie tickets at $25, and give change to an array of customers. It should return "YES" if you can give change to everyone, and "NO" if you cannot.

Aucun commentaire:

Enregistrer un commentaire