Skip to content

Instantly share code, notes, and snippets.

@davilera
Last active Dec 22, 2016
Embed
What would you like to do?
function getVat( quantity ) {
if ( typeof quantity === 'string' ) {
// Remove thousands separator.
quantity = quantity.replace( /\./g, '' )
// Remove decimals separator.
quantity = quantity.replace( ',', '.' )
// Remove currency.
quantity = quantity.replace( /[^0-9.]/g, '' );
}//end if
return quantity * 0.21;
}//end getVat()
QUnit.module( 'The function "getVat"', {}, function() {
QUnit.module( 'when the quantity is an integer', {}, function() {
QUnit.test( 'should return 21% of the given value', function( assert ) {
var result = getVat( 1000 );
assert.equal( result, 210 );
});
});
QUnit.module( 'when the quantity is a string', {}, function() {
QUnit.test( 'should accept values that use dot as the thousands separator and return 21% of the given value', function( assert ) {
var result = getVat( '1.000' );
assert.equal( result, 210 );
});
QUnit.test( 'should accept values that use comma as the decimal separator and return 21% of the given value', function( assert ) {
var result = getVat( '100,00' );
assert.equal( result, 21 );
});
// ...
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>QUnit Example</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.0.1.css">
<script src="functions.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.0.1.js"></script>
<script src="tests.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment