From e9c8b9cc03dc376ac97c5d3179e979e065f41ac4 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 12 May 2022 16:29:23 +0200 Subject: [PATCH] MOBILE-4069 test: Add unit tests to CoreForms singleton --- src/core/singletons/form.ts | 2 +- src/core/singletons/tests/form.test.ts | 134 +++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/core/singletons/tests/form.test.ts diff --git a/src/core/singletons/form.ts b/src/core/singletons/form.ts index f4beefa7c..a40eb91f8 100644 --- a/src/core/singletons/form.ts +++ b/src/core/singletons/form.ts @@ -70,7 +70,7 @@ export class CoreForms { CoreEvents.trigger(CoreEvents.FORM_ACTION, { action: CoreEventFormAction.CANCEL, - form: formRef.nativeElement, + form: formRef.nativeElement || formRef, }, siteId); } diff --git a/src/core/singletons/tests/form.test.ts b/src/core/singletons/tests/form.test.ts new file mode 100644 index 000000000..bbdb9ca2b --- /dev/null +++ b/src/core/singletons/tests/form.test.ts @@ -0,0 +1,134 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { ElementRef } from '@angular/core'; +import { CoreEventFormAction, CoreEvents } from '@singletons/events'; +import { CoreForms } from '@singletons/form'; + +const createInputElement = (type: string, name: string, value = ''): HTMLInputElement => { + const input = document.createElement('input'); + input.type = type; + input.name = name; + input.value = value; + + return input; +}; + +describe('CoreForms singleton', () => { + + it('gets data from form', () => { + // Create several types of inputs. + const textInput = createInputElement('text', 'mytext'); + const firstRadio = createInputElement('radio', 'myradio', 'firstradio'); + const secondRadio = createInputElement('radio', 'myradio', 'secondradio'); + const checkbox = createInputElement('checkbox', 'mycheckbox'); + const hiddenInput = createInputElement('hidden', 'myhidden', 'hiddenvalue'); + const submitInput = createInputElement('submit', 'submit'); + + const textarea = document.createElement('textarea'); + textarea.name = 'mytextarea'; + + const select = document.createElement('select'); + select.name = 'myselect'; + const firstOption = document.createElement('option'); + firstOption.value = 'firstoption'; + const secondOption = document.createElement('option'); + secondOption.value = 'secondoption'; + select.appendChild(firstOption); + select.appendChild(secondOption); + + // Create a form with the inputs. + const form = document.createElement('form'); + form.appendChild(textInput); + form.appendChild(firstRadio); + form.appendChild(secondRadio); + form.appendChild(checkbox); + form.appendChild(hiddenInput); + form.appendChild(submitInput); + form.appendChild(textarea); + form.appendChild(select); + + // Test data is retrieved. + const values: Record = { + mytext: '', + mycheckbox: false, + myhidden: 'hiddenvalue', + mytextarea: '', + myselect: 'firstoption', + }; + + expect(CoreForms.getDataFromForm(form)).toEqual(values); + + // Change some values and test again. + textInput.value = values.mytext = 'a value'; + select.value = values.myselect = 'secondoption'; + firstRadio.checked = true; + values.myradio = 'firstradio'; + checkbox.checked = values.mycheckbox = true; + textarea.value = values.mytextarea = 'textarea value'; + + expect(CoreForms.getDataFromForm(form)).toEqual(values); + }); + + it('triggers form action events', () => { + const form = document.createElement('form'); + const formElRef = new ElementRef(form); + const siteId = 'site-id'; + const callback = jest.fn(); + const secondCallback = jest.fn(); + + CoreEvents.on(CoreEvents.FORM_ACTION, callback, siteId); + CoreEvents.on(CoreEvents.FORM_ACTION, secondCallback, 'another-site'); + + CoreForms.triggerFormCancelledEvent(form, siteId); + expect(callback).toHaveBeenCalledWith({ + action: CoreEventFormAction.CANCEL, + form, + siteId, + }); + + CoreForms.triggerFormCancelledEvent(formElRef, siteId); + expect(callback).toHaveBeenCalledWith({ + action: CoreEventFormAction.CANCEL, + form, + siteId, + }); + + CoreForms.triggerFormSubmittedEvent(form, true, siteId); + expect(callback).toHaveBeenCalledWith({ + action: CoreEventFormAction.SUBMIT, + form, + online: true, + siteId, + }); + + CoreForms.triggerFormSubmittedEvent(form, false, siteId); + expect(callback).toHaveBeenCalledWith({ + action: CoreEventFormAction.SUBMIT, + form, + online: false, + siteId, + }); + + CoreForms.triggerFormSubmittedEvent(formElRef, true, siteId); + expect(callback).toHaveBeenCalledWith({ + action: CoreEventFormAction.SUBMIT, + form, + online: true, + siteId, + }); + expect(secondCallback).not.toHaveBeenCalled(); + }); + +});