I'm working on improving my javascript testing skills through katas. I am facing some challenges with a specific one that involves creating a TicketClerk object to handle movie ticket transactions.
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("verifies the initial state of the till as having zero of each denomination", function() {
expect(ticketClerk.till).toEqual({ 25: 0, 50: 0, 100: 0 });
});
});
describe("#sell", function() {
it("ensures 'YES' is returned when able to give change for tickets purchased with 25, 25, 50", function() {
ticketClerk.sell([25, 25, 50, 50]);
expect(ticketClerk.sell).toEqual("YES");
});
it("ensures 'NO' is returned when unable to give change for tickets purchased with 50, 25, 50", function() {
ticketClerk.sell([50, 25, 50]);
expect(ticketClerk.sell).toEqual("NO");
});
});
});
I have additional tests omitted here but the main idea is accepting $25 movie tickets and providing change to customers based on given denominations. The expected output should be "YES" if change can be provided to all customers, and "NO" if not.