-
Issue #442
+
Issue #442 & #457
@@ -1665,10 +1668,11 @@
12345.67
console.log(`'${AutoNumeric.events.rawValueModified}' sent with`, e); //DEBUG
});
- //-------------- Issue #442
- new AutoNumeric('#issue_442', {
- unformatOnSubmit: AutoNumeric.options.unformatOnSubmit.unformat,
- }).french();
+ //-------------- Issue #442 & #457
+ new AutoNumeric('#issue_442_1', { unformatOnSubmit: AutoNumeric.options.unformatOnSubmit.unformat }).french();
+ new AutoNumeric('#issue_442_2', { unformatOnSubmit: AutoNumeric.options.unformatOnSubmit.keepCurrentValue }).french();
+ new AutoNumeric('#issue_442_3', 22432392.23, { unformatOnSubmit: AutoNumeric.options.unformatOnSubmit.unformat }).french();
+ new AutoNumeric('#issue_442_4', { unformatOnSubmit: AutoNumeric.options.unformatOnSubmit.keepCurrentValue }).french();
const form442 = document.getElementById('issue_442_form');
form442.onsubmit = function(e) {
e.preventDefault();
diff --git a/test/e2e/specs/autoNumeric.e2e.spec.js b/test/e2e/specs/autoNumeric.e2e.spec.js
index d4e97f1b..860bd500 100644
--- a/test/e2e/specs/autoNumeric.e2e.spec.js
+++ b/test/e2e/specs/autoNumeric.e2e.spec.js
@@ -176,7 +176,10 @@ const selectors = {
selectOnFocus38 : '#selectOnFocus38',
selectOnFocus39 : '#selectOnFocus39',
selectOnFocus40 : '#selectOnFocus40',
- issue442 : '#issue_442',
+ issue442One : '#issue_442_1',
+ issue442Two : '#issue_442_2',
+ issue442Three : '#issue_442_3',
+ issue442Four : '#issue_442_4',
issue442Submit : '#issue_442_submit',
issue447 : '#issue_447',
result447 : '#result_447',
@@ -3163,14 +3166,20 @@ describe('`unformatOnSubmit` option', () => {
it('should test for default values', () => {
browser.url(testUrl);
- expect(browser.getValue(selectors.issue442)).toEqual('12.345,67\u202f€');
+ expect(browser.getValue(selectors.issue442One)).toEqual('12.345,67\u202f€');
+ expect(browser.getValue(selectors.issue442Two)).toEqual('1.111.222,21\u202f€');
+ expect(browser.getValue(selectors.issue442Three)).toEqual('22.432.392,23\u202f€');
+ expect(browser.getValue(selectors.issue442Four)).toEqual('0,00\u202f€');
});
- it('should unformat on submit', () => {
+ it('should unformat on submit only the elements that activated the `unformatOnSubmit` option', () => {
const submitButton = $(selectors.issue442Submit);
submitButton.click(); // Submit the form by clicking on the submit button
- expect(browser.getValue(selectors.issue442)).toEqual('12345.67');
+ expect(browser.getValue(selectors.issue442One)).toEqual('12345.67');
+ expect(browser.getValue(selectors.issue442Two)).toEqual('1.111.222,21\u202f€');
+ expect(browser.getValue(selectors.issue442Three)).toEqual('22432392.23');
+ expect(browser.getValue(selectors.issue442Four)).toEqual('0,00\u202f€');
});
});
diff --git a/test/unit/autoNumeric.spec.js b/test/unit/autoNumeric.spec.js
index 15955f61..2bef9462 100644
--- a/test/unit/autoNumeric.spec.js
+++ b/test/unit/autoNumeric.spec.js
@@ -2818,6 +2818,7 @@ describe('autoNumeric options and `options.*` methods', () => {
/*
describe('`unformatOnSubmit` option', () => {
+ //XXX This test cannot be activated since reloading the page in Jasmine test is not allowed
it('should unformat on submit', () => {
// Create the form elements
const newInput = document.createElement('input');
@@ -4677,6 +4678,7 @@ describe('Instantiated autoNumeric functions', () => {
let anInput1;
let anInput2;
let anInput3;
+ let anInput6;
beforeEach(() => { // Initialization
form = document.createElement('form');
@@ -4722,7 +4724,7 @@ describe('Instantiated autoNumeric functions', () => {
anInput1 = new AutoNumeric(input1, anOptions);
anInput2 = new AutoNumeric(input2, anOptions);
anInput3 = new AutoNumeric(input3, anOptions);
- new AutoNumeric(input6, anOptions); // Check that an AutoNumeric-managed input does not show a `0` when the input is empty
+ anInput6 = new AutoNumeric(input6, anOptions); // Check that an AutoNumeric-managed input does not show a `0` when the input is empty
expect(input1.value).toEqual('€ 1.111,11');
expect(anInput1.getFormatted()).toEqual('€ 1.111,11');
@@ -4734,6 +4736,7 @@ describe('Instantiated autoNumeric functions', () => {
anInput1.remove();
anInput2.remove();
anInput3.remove();
+ anInput6.remove();
form.removeChild(input1);
form.removeChild(input2);
form.removeChild(input3);
@@ -5068,6 +5071,45 @@ describe('Instantiated autoNumeric functions', () => {
document.body.removeChild(secondForm);
});
+ it(`should only add a single 'submit' event listener, and remove it when all the AutoNumeric element are removed`, () => {
+ // Get the initial parent form element reference
+ const initialForm = anInput1.form();
+ const formHandlerName = initialForm.dataset.anFormHandler;
+ expect(Number(initialForm.dataset.anCount)).toEqual(4);
+
+ anInput1.wipe();
+ expect(Number(initialForm.dataset.anCount)).toEqual(3);
+ expect(initialForm.dataset.anFormHandler).toEqual(formHandlerName);
+ anInput2.wipe();
+ expect(Number(initialForm.dataset.anCount)).toEqual(2);
+ expect(initialForm.dataset.anFormHandler).toEqual(formHandlerName);
+
+ anInput2 = new AutoNumeric(input2);
+ expect(Number(initialForm.dataset.anCount)).toEqual(3);
+ expect(initialForm.dataset.anFormHandler).toEqual(formHandlerName);
+ anInput2.wipe();
+ expect(Number(initialForm.dataset.anCount)).toEqual(2);
+ expect(initialForm.dataset.anFormHandler).toEqual(formHandlerName);
+ anInput6.wipe();
+ expect(Number(initialForm.dataset.anCount)).toEqual(1);
+ expect(initialForm.dataset.anFormHandler).toEqual(formHandlerName);
+ expect(window.aNFormHandlerMap.has(formHandlerName)).toEqual(true);
+
+ // Remove the very last from child AutoNumeric element
+ anInput3.wipe();
+ expect(initialForm.dataset.anCount).toBeUndefined();
+ expect(initialForm.dataset.anFormHandler).toBeUndefined();
+
+ // Also check that the name does not exist as key in the global map window.aNFormHandlerMap
+ expect(window.aNFormHandlerMap.has(formHandlerName)).toEqual(false);
+
+ // Reinitialize the AutoNumeric object for the `afterEach` un-initialization step
+ anInput1 = new AutoNumeric(input1);
+ anInput2 = new AutoNumeric(input2);
+ anInput3 = new AutoNumeric(input3);
+ anInput6 = new AutoNumeric(input6);
+ });
+
//FIXME Add the tests for : formUnformat, formReformat, formSubmit*
});