fix bug comma

This commit is contained in:
2020-07-12 15:04:12 +07:00
parent e132f3b5c9
commit c27b800795
66 changed files with 15559 additions and 16 deletions

View File

@@ -0,0 +1,31 @@
"use strict";
module("change");
test("it calls the change event when the field's value is changed", function() {
var input = $("#input1").maskMoney(),
changeWasCalled = false;
input.change(function() {
changeWasCalled = true;
});
input.val("0.01");
input.trigger("focus");
keypress(input, 1);
input.trigger("blur");
ok(changeWasCalled, "change was called");
equal(input.val(), "0.11", "changed value");
});
test("it doesn't call the change event when the field's value is unchanged", function() {
var input = $("#input1").maskMoney(),
changeWasCalled = false;
input.change(function() {
changeWasCalled = true;
});
input.val("0.01");
input.trigger("focus");
input.trigger("blur");
ok(!changeWasCalled, "change was not called");
equal(input.val(), "0.01", "changed value");
});

View File

@@ -0,0 +1,27 @@
"use strict";
// stop()/start() is used when you have async tests. since we use setTimeout({}, 0)
// to call mask() inside cutPasteEvent function, we need to use them
module("cut & paste");
test("when cuting", function() {
stop();
var input = $("#input1").maskMoney();
input.val("12345678");
input.trigger("cut");
setTimeout( function() {
start();
equal(input.val(), "12,345,678.00", "format the value of the field");
}, 1);
});
test("when pasting", function() {
stop();
var input = $("#input1").maskMoney();
input.val("12345678");
input.trigger("paste");
setTimeout( function() {
start();
equal(input.val(), "12,345,678.00", "format the value of the field");
}, 1);
});

View File

@@ -0,0 +1,27 @@
"use strict";
module("data-dash api");
test("with field configured using data-* attributes", function() {
var input = $("#input3").maskMoney();
input.val("12345678");
input.trigger("focus");
equal(input.val(), "R$12.345.678,00", "configure maskMoney using data-* attributes");
});
test("configuring a setting that isn't a single word", function() {
var input = $("#input3");
input.attr("data-allow-zero", "true");
input.maskMoney();
input.val("0");
input.trigger("mask");
equal(input.val(), "R$0,00", "it works by using dashes-pattern instead of camelCase");
});
test("allow to configure multiple fields using data-* attributes", function() {
var input = $(".multiple-dash").maskMoney();
input.val("12345678");
input.trigger("focus");
equal($("#input3").val(), "R$12.345.678,00", "configure maskMoney using data-* attributes");
equal($("#input4").val(), "U$12,345,678.00", "configure maskMoney using data-* attributes");
});

View File

@@ -0,0 +1,17 @@
"use strict";
module("empty");
test("allow empty inputs", function() {
var input = $("#input1").maskMoney({ precision: 2, allowEmpty: true });
input.val("");
input.trigger("focus");
equal(input.val(), "", "allow empty inputs");
});
test("don't allow empty inputs", function() {
var input = $("#input1").maskMoney({ precision: 2, allowEmpty: false });
input.val("");
input.trigger("focus");
equal(input.val(), "0.00", "don't allow empty inputs");
});

View File

@@ -0,0 +1,9 @@
"use strict";
module("focus");
test("with default mask", function() {
var input = $("#input1").maskMoney();
input.val("12345678");
input.trigger("focus");
equal(input.val(), "12,345,678.00", "format the value of the field on focus");
});

View File

@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery maskMoney Test Suite</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.23.1.css">
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="http://code.jquery.com/qunit/qunit-1.23.1.js"></script>
<script src="../src/jquery.maskMoney.js"></script>
<script src="util.js"></script>
<script src="init_and_destroy_test.js"></script>
<script src="mask_test.js"></script>
<script src="data_dash_test.js"></script>
<script src="unmasked_test.js"></script>
<script src="focus_test.js"></script>
<script src="change_test.js"></script>
<script src="typing_test.js"></script>
<script src="cut_paste_test.js"></script>
<script src="selection_test.js"></script>
<script src="empty_test.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture">
<input type="text" id="input1" class="all"/>
<input type="text" id="input2" class="all"/>
<input type="text" class="multiple-dash" id="input3" data-prefix="R$" data-thousands="." data-decimal=","/>
<input type="text" class="multiple-dash" id="input4" data-prefix="U$" data-thousands="," data-decimal="."/>
</div>
</body>
</html>

View File

@@ -0,0 +1,26 @@
"use strict";
test("chainable", function() {
ok($("#input1").maskMoney().val("123"), "can be chained");
equal($("#input1").val(), "123", "class was added correctly from chaining");
});
test("init", function() {
var input = $("#input1").maskMoney(),
events;
events = jQuery._data(input.get(0), "events");
ok(events.blur, "attached the blur event");
ok(events.keypress, "attached the keypress event");
ok(events.keydown, "attached the keydown event");
ok(events.focus, "attached the focus event");
ok(events.click, "attached the click event");
});
test("destroy", function() {
var input = $("#input1").maskMoney(),
events;
input.maskMoney("destroy");
events = jQuery._data(input.get(0), "events");
equal(events, undefined, "destroy method removed all attached events");
});

View File

@@ -0,0 +1,82 @@
"use strict";
module("mask");
test("when triggered in a empty field", function() {
var input = $("#input1").maskMoney();
input.val("");
input.maskMoney("mask");
equal(input.val(), "0.00", "set zero as the value");
});
test("with a lot of leading zeros", function() {
var input = $("#input1").maskMoney();
input.val("000000000123");
input.maskMoney("mask");
equal(input.val(), "123.00", "removes the unecessary zeroes");
});
QUnit.skip("with a pre-formatted number", function() {
var input = $("#input1").maskMoney();
input.val("123,45");
input.maskMoney("mask");
equal(input.val(), "123.45", "keeps the number precision");
});
QUnit.skip("with a pre-formatted number smaller than the set precision", function() {
var input = $("#input1").maskMoney();
input.val("123,4");
input.maskMoney("mask");
equal(input.val(), "123.40", "keeps the number precision");
});
test("with negative symbol and a field that doesn't allow negative ", function() {
var input = $("#input1").maskMoney({allowNegative: false});
input.val("-123");
input.maskMoney("mask");
equal(input.val(), "123.00", "removes negative symbol");
});
QUnit.skip("with a number as parameter", function() {
var input = $("#input1").maskMoney();
input.maskMoney("mask", 123456.70);
equal(input.val(), "123,456.70");
});
test("with a number as parameter", function() {
var input = $("#input1").maskMoney({ precision: 0 });
input.maskMoney("mask", 1000);
equal(input.val(), "1,000");
});
test("with a decimal number as parameter", function() {
var input = $("#input1").maskMoney();
input.maskMoney("mask", 1.1);
equal(input.val(), "1.10");
});
test("with a decimal number as parameter and different symbol", function() {
var input = $("#input1").maskMoney({thousands: ".", decimal: ","});
input.maskMoney("mask", 2001.1);
equal(input.val(), "2.001,10");
});
test("without a decimal number as parameter and different symbol", function() {
var input = $("#input1").maskMoney({thousands: ".", decimal: ","});
input.maskMoney("mask", 2001);
equal(input.val(), "2.001,00");
});
test("with a negative number as parameter", function() {
var input = $("#input1").maskMoney({allowNegative: true});
input.maskMoney("mask", -123456.78);
equal(input.val(), "-123,456.78");
});
test("with a suffix", function() {
var input = $("#input1").maskMoney({suffix: " €"});
input.maskMoney("mask", 20316.22);
equal(input.val(), "20,316.22 €");
});

View File

@@ -0,0 +1,19 @@
"use strict";
module("double click selection");
test("when double clicking", function() {
var input = $("#input1").maskMoney();
input.val("123.45");
input.maskMoney("mask");
input.trigger("dblclick");
equal(input.val(), "123.45", "the content does not change");
});
test("when double clicking and hitting delete", function() {
var input = $("#input1").maskMoney();
input.val("12345");
input.maskMoney("mask");
input.trigger("dblclick");
keydown(input, "backspace");
equal(input.val(), "0.00", "all the content is cleared");
});

View File

@@ -0,0 +1,36 @@
"use strict";
module("typing");
test("accepts keys in sequence", function() {
var input = $("#input1").maskMoney();
input.trigger("focus");
keypress(input, 1);
keypress(input, 2);
keypress(input, 3);
keypress(input, 4);
keypress(input, 5);
keypress(input, 6);
equal(input.val(), "1,234.56", "accept the input and format correctly");
});
test("with a suffix", function() {
var input = $("#input1").maskMoney({suffix: " €"});
input.trigger("focus");
keypress(input, 1);
keypress(input, 2);
keypress(input, 3);
keypress(input, 4);
keypress(input, 5);
equal(input.val(), "123.45 €", "accept the input and format correctly");
});
test("with a pre-set value", function() {
var input = $("#input1").maskMoney();
input.val("1");
input.trigger("focus");
keypress(input, 2);
equal(input.val(), "10.02", "accept the input and format correctly");
});

View File

@@ -0,0 +1,68 @@
"use strict";
module("unmasked");
test("with prefix", function() {
var input = $("#input1"),
unmasked;
input.val("+ 123.456,78");
unmasked = input.maskMoney("unmasked")[0];
equal(unmasked, 123456.78, "unmask method return the correct number when the field value has prefix");
});
test("with suffix", function() {
var input = $("#input1"),
unmasked;
input.val("123.456,78 €");
unmasked = input.maskMoney("unmasked")[0];
equal(unmasked, 123456.78, "unmask method return the correct number when the field value has suffix");
});
test("with prefix and suffix", function() {
var input = $("#input1"),
unmasked;
input.val("+ 123.456,78 €");
unmasked = input.maskMoney("unmasked")[0];
equal(unmasked, 123456.78, "unmask method return the correct number when the field value has prefix and suffix");
});
test("with negative number", function() {
var input = $("#input1"),
unmasked;
input.val("-R$ 123.456,78");
unmasked = input.maskMoney("unmasked")[0];
equal(unmasked, -123456.78, "unmask method return the correct number when the field value has prefix and suffix");
});
test("with collection of fields", function() {
var input = $(".all"),
unmaskedCollection;
$("#input1").val("+ 123.456,78 €");
$("#input2").val("R$ 876.543,21");
unmaskedCollection = input.maskMoney("unmasked").get();
deepEqual(unmaskedCollection, [123456.78, 876543.21], "unmask method return the correct number when the field value has prefix and suffix");
});
test("without options", function() {
var input = $("#input1"),
unmasked;
input.val("123,456");
unmasked = input.maskMoney("unmaskedWithOptions")[0];
equal(unmasked, 123456, "unmask method return the correct number even when options are not passed");
});
test("with decimal part", function() {
var input = $("#input1"),
unmasked;
input.val("123,456.01");
unmasked = input.maskMoney("unmaskedWithOptions")[0];
equal(unmasked, 123456.01, "unmask method return the correct number when the field value has a decimal part");
});
test("with options specified", function() {
var input = $("#input1"),
unmasked;
input.val("123.456");
input.maskMoney({ thousandsForUnmasked : "\\.", thousands:"." });
unmasked = input.maskMoney("unmaskedWithOptions")[0];
equal(unmasked, 123456, "unmask method return the correct number when options are set");
});

View File

@@ -0,0 +1,26 @@
"use strict";
var KEY_TO_KEYCODE_MAP = {
0: 48, 1: 49,
2: 50, 3: 51,
4: 52, 5: 53,
6: 54, 7: 55,
8: 56, 9: 57,
"shift": 16, "ctrl": 17, "alt": 18,
"backspace": 8, "tab": 9,
"enter": 13, "esc": 27, "space": 32,
"left": 37, "up": 38,
"right": 39, "down": 40,
"delete": 46,
"home": 36, "end": 35,
",": 188, ".": 190,
"-": 189, "=": 187,
};
function keypress(input, key) {
input.trigger($.Event("keypress", { keyCode: KEY_TO_KEYCODE_MAP[key] } ));
}
function keydown(input, key) {
input.trigger($.Event("keydown", { keyCode: KEY_TO_KEYCODE_MAP[key] } ));
}