Skip to content

Instantly share code, notes, and snippets.

@rquadling
Created January 10, 2018 17:54
Show Gist options
  • Save rquadling/457b5f4a7a3b01bd9df68e623bdc8fc5 to your computer and use it in GitHub Desktop.
Save rquadling/457b5f4a7a3b01bd9df68e623bdc8fc5 to your computer and use it in GitHub Desktop.
Nodeunit test
'use strict';
require('../../../bootstrap.js');
require(rootPath + '/src/js/lib/Calculator.js');
var calculatorDataProvider = {
'Empty calculator': {
amountDue: 0.00,
amountTenderedAfterAmountDueSet: 0.00,
changeGivenAfterAmountDueSet: 0.00,
amountTendered: 0.00,
changeGiven: 0.00,
amountTenderedAfterClear: 0.00,
changeGivenAfterClear: 0.00
},
'Sale of 9.99 with 15.00 tendered and 5.01 change given': {
amountDue: 9.99,
amountTenderedAfterAmountDueSet: 0.00,
changeGivenAfterAmountDueSet: 0.00,
amountTendered: 15.00,
changeGiven: 5.01,
amountTenderedAfterClear: 0.00,
changeGivenAfterClear: 0.00
},
'Refund of 9.99 with no tendering and 9.99 change given': {
amountDue: -9.99,
amountTenderedAfterAmountDueSet: 0.00,
changeGivenAfterAmountDueSet: 9.99,
amountTendered: 0.00,
changeGiven: 9.99,
amountTenderedAfterClear: 0.00,
changeGivenAfterClear: 9.99
},
'Refund of 9.99 with 0.01 tender and 10.00 change given': {
amountDue: -9.99,
amountTenderedAfterAmountDueSet: 0.00,
changeGivenAfterAmountDueSet: 9.99,
amountTendered: 0.01,
changeGiven: 10.00,
amountTenderedAfterClear: 0.00,
changeGivenAfterClear: 9.99
}
};
exports.CalculatorTest = {
withoutCalculationCallback: dataProvider(
calculatorDataProvider,
function (test, data, testName) {
test.expect(8);
// New calculator should be 0 and 0
var calculator = new DigiTickets.Calculator();
test.equals(calculator.getAmountTendered(), 0.00,
'Amount Tendered starts at 0.00 for ' + testName);
test.equals(calculator.getChange(), 0.00,
'Change Given starts at 0.00 for ' + testName);
calculator.setAmountDue(data.amountDue);
test.equals(calculator.getAmountTendered(), data.amountTenderedAfterAmountDueSet,
'Amount Tendered is ' + data.amountTenderedAfterAmountDueSet + ' after Amount Due set for ' + testName);
test.equals(calculator.getChange(), data.changeGivenAfterAmountDueSet,
'Change Given still ' + data.changeGivenAfterAmountDueSet + ' after Amount Due set for ' + testName);
calculator.setAmountTendered(data.amountTendered);
test.equals(calculator.getAmountTendered(), data.amountTendered,
'Amount Tendered set to ' + data.amountTendered + ' for ' + testName);
test.equals(calculator.getChange(), data.changeGiven,
'Change Given calculated to be ' + data.changeGiven + ' for ' + testName);
calculator.clear();
test.equals(calculator.getAmountTendered(), data.amountTenderedAfterClear,
'Amount Tendered back to ' + data.amountTenderedAfterClear + ' once cleared for ' + testName);
test.equals(calculator.getChange(), data.changeGivenAfterClear,
'Change Given back to ' + data.changeGivenAfterClear + ' once cleared for ' + testName);
test.done();
}
),
withCalculationCallback: dataProvider(
calculatorDataProvider,
function (test, data, testName) {
var callbacksCalled = 0;
var returnedCalculator = null;
test.expect(18);
// New calculator should be 0 and 0
var calculator = new DigiTickets.Calculator(function (calculator) {
++callbacksCalled;
returnedCalculator = calculator;
});
test.equals(calculator.getAmountTendered(), 0.00,
'Amount Tendered starts at 0.00 for ' + testName);
test.equals(calculator.getChange(), 0.00,
'Change Given starts at 0.00 for ' + testName);
test.equals(callbacksCalled, 0,
'Callback called 0 times for ' + testName);
calculator.setAmountDue(data.amountDue);
test.equals(calculator.getAmountTendered(), data.amountTenderedAfterAmountDueSet,
'Amount Tendered is ' + data.amountTenderedAfterAmountDueSet + ' after Amount Due set for ' + testName);
test.equals(calculator.getChange(), data.changeGivenAfterAmountDueSet,
'Change Given still ' + data.changeGivenAfterAmountDueSet + ' after Amount Due set for ' + testName);
test.equals(callbacksCalled, 1,
'Callback called 1 times for ' + testName);
test.equals(returnedCalculator.getAmountTendered(), data.amountTenderedAfterAmountDueSet,
'Callback Amount Tendered still ' + data.amountTenderedAfterAmountDueSet + ' after Amount Due set for ' + testName);
test.equals(returnedCalculator.getChange(), data.changeGivenAfterAmountDueSet,
'Callback Change Given still ' + data.changeGivenAfterAmountDueSet + ' after Amount Due set for ' + testName);
calculator.setAmountTendered(data.amountTendered);
test.equals(calculator.getAmountTendered(), data.amountTendered,
'Amount Tendered set to ' + data.amountTendered);
test.equals(calculator.getChange(), data.changeGiven,
'Change Given calculated to be ' + data.changeGiven);
test.equals(callbacksCalled, 2,
'Callback called 2 times for ' + testName);
test.equals(returnedCalculator.getAmountTendered(), data.amountTendered,
'Callback Amount Tendered set to ' + data.amountTendered);
test.equals(returnedCalculator.getChange(), data.changeGiven,
'Callback Change Given calculated to be ' + data.changeGiven);
calculator.clear();
test.equals(calculator.getAmountTendered(), data.amountTenderedAfterClear,
'Amount Tendered back to ' + data.amountTenderedAfterClear + ' once cleared for ' + testName);
test.equals(calculator.getChange(), data.changeGivenAfterClear,
'Change Given back to ' + data.changeGivenAfterClear + ' once cleared for ' + testName);
test.equals(callbacksCalled, 3,
'Callback called 3 times for ' + testName);
test.equals(returnedCalculator.getAmountTendered(), data.amountTenderedAfterClear,
'Callback Amount Tendered back to ' + data.amountTenderedAfterClear + ' once cleared for ' + testName);
test.equals(returnedCalculator.getChange(), data.changeGivenAfterClear,
'Callback Change Given back to ' + data.changeGivenAfterClear + ' once cleared for ' + testName);
test.done();
}
)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment