|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 |
- import getFormValuesDeprecated, {
- getFormValues,
- setFormValues,
- isFieldElement,
- isElementValueIncludedInFormSubmit,
- getValue,
- } from '../../src';
- import * as utils from '../utils'
-
- describe('misc', () => {
- describe('core', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>Misc/Blank</title>
- </head>
- <body>
- <form>
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- `))
-
- it('should call console.warn for deprecated default import usage', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let consoleWarnCalled = false
- const defaultConsoleWarn = console.warn
- console.warn = (...args: unknown[]) => {
- consoleWarnCalled = true
- };
- getFormValuesDeprecated(form, { submitter });
- expect(consoleWarnCalled).toBe(true);
- console.warn = defaultConsoleWarn;
- },
- });
- });
-
- it('should throw an error when providing invalid argument type as form to getFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- getFormValues(0 as unknown as HTMLFormElement, {});
- } catch {
- isThrown = true;
- }
-
- expect(isThrown).toBe(true);
- },
- });
- });
-
- it('should throw an error when providing null as form to getFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- getFormValues(null as unknown as HTMLFormElement, {});
- } catch {
- isThrown = true;
- }
-
- expect(isThrown).toBe(true);
- },
- });
- });
-
- it('should throw an error when providing a different element type as form to getFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- getFormValues(document.body as unknown as HTMLFormElement, {});
- } catch {
- isThrown = true;
- }
-
- expect(isThrown).toBe(true);
- },
- });
- });
-
- it('should throw an error when providing invalid argument type as form to setFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- setFormValues(0 as unknown as HTMLFormElement, {});
- } catch {
- isThrown = true;
- }
-
- expect(isThrown).toBe(true);
- },
- });
- });
-
- it('should throw an error when providing null as form to setFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- setFormValues(null as unknown as HTMLFormElement, {});
- } catch {
- isThrown = true;
- }
-
- expect(isThrown).toBe(true);
- },
- });
- });
-
- it('should throw an error when providing a different element type as form to setFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- setFormValues(document.body as unknown as HTMLFormElement, {});
- } catch {
- isThrown = true;
- }
-
- expect(isThrown).toBe(true);
- },
- });
- });
-
- it('should throw an error when providing invalid argument type as values to setFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- setFormValues(form, 0);
- } catch {
- isThrown = true;
- }
-
- expect(isThrown).toBe(true);
- },
- });
- });
-
- it('should not throw an error when providing null as form to setFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- setFormValues(form, null);
- } catch (e) {
- isThrown = true;
- }
-
- expect(isThrown).toBe(false);
- },
- });
- });
-
- it('should throw an error when providing undefined as form to setFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- setFormValues(form, undefined);
- } catch (e) {
- isThrown = true;
- }
-
- expect(isThrown).toBe(true);
- },
- });
- });
- });
-
- describe('utilities', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>Misc/Utilities</title>
- </head>
- <body>
- <form>
- <input id="input" type="text" name="foobar" />
- <input id="notField" type="text" />
- <input id="disabled" disabled type="text" name="disabled" />
- <meter id="meter" min="1" max="10" value="5" />
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- `));
-
- it('should check for valid field elements value', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- const meter = document.getElementById('meter');
- expect(getValue(meter)).toBe(5);
- },
- });
- });
-
- it('should check for invalid field elements value', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- expect(getValue(document.body)).toBe(null);
- },
- });
- });
-
- it('should check for elements as included fields', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- const input = document.getElementById('input');
- expect(isElementValueIncludedInFormSubmit(input)).toBe(true);
- },
- });
- });
-
- it('should check for elements as excluded fields', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- const notField = document.getElementById('notField');
- expect(isElementValueIncludedInFormSubmit(notField)).toBe(false);
- const disabled = document.getElementById('disabled');
- expect(isElementValueIncludedInFormSubmit(disabled)).toBe(false);
- const meter = document.getElementById('meter');
- expect(isElementValueIncludedInFormSubmit(meter)).toBe(false);
- },
- });
- });
-
- it('should check for elements as valid for fields', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- const input = document.getElementById('input');
- expect(isFieldElement(input)).toBe(true);
- const disabled = document.getElementById('disabled');
- expect(isFieldElement(disabled)).toBe(true);
- },
- });
- });
-
- it('should check for elements as invalid for fields', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- const meter = document.getElementById('meter');
- expect(isFieldElement(meter)).toBe(false);
- const notField = document.getElementById('notField');
- expect(isFieldElement(notField)).toBe(false);
- },
- });
- });
- });
-
- describe('setting values', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>Misc/Blank</title>
- </head>
- <body>
- <form>
- <input type="text" name="foobar" />
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- `))
-
- it('should parse string values for setFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- setFormValues(form, 'foobar=baz');
- } catch (e) {
- isThrown = true;
- }
-
- expect(isThrown).toBe(false);
- expect(getFormValues(form)).toEqual({ foobar: 'baz', });
- },
- })
- });
-
- it('should parse entries values for setFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- setFormValues(form, [['foobar', 'baz']]);
- } catch (e) {
- isThrown = true;
- }
-
- expect(isThrown).toBe(false);
- expect(getFormValues(form)).toEqual({ foobar: 'baz', });
- },
- })
- });
-
- it('should parse URLSearchParams values for setFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- setFormValues(form, new URLSearchParams('foobar=baz'));
- } catch (e) {
- isThrown = true;
- }
-
- expect(isThrown).toBe(false);
- expect(getFormValues(form)).toEqual({ foobar: 'baz', });
- },
- })
- });
-
- it('should parse object values for setFormValues', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- let isThrown = false;
- try {
- setFormValues(form, { foobar: 'baz', });
- } catch (e) {
- isThrown = true;
- }
-
- expect(isThrown).toBe(false);
- expect(getFormValues(form)).toEqual({ foobar: 'baz', });
- },
- })
- });
- });
-
- describe('duplicates', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>Misc/Blank</title>
- </head>
- <body>
- <form>
- <input type="text" name="foobar" />
- <input type="text" name="foobar" />
- <input type="text" name="foobar" />
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- `));
-
- it('should parse duplicates correctly', () => {
- utils.test({
- preAction: (form: HTMLFormElement) => {
- setFormValues(form, { foobar: ['foo', 'bar', 'baz']})
- },
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- expect(getFormValues(form)).toEqual({ foobar: ['foo', 'bar', 'baz'], });
- },
- })
- });
- });
-
- describe('blank', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>Misc/Blank</title>
- </head>
- <body>
- <form>
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- `))
-
- it('should have blank form value', () => {
- utils.test({
- action: (cy: any) => cy.get('[type="submit"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- const before = utils.makeSearchParams(getFormValues(form, { submitter }))
- .toString();
- const after = utils.makeSearchParams(search)
- .toString();
- expect(before)
- .toEqual(after);
- },
- expectedStaticValue: {},
- });
- });
- })
-
- describe('everything', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>Misc/Everything</title>
- </head>
- <body>
- <article>
- <h2></h2>
- <form>
- <fieldset>
- <legend>Name</legend>
- <div>
- <input type="text" placeholder="First Name" name="first_name" />
- </div>
- <div>
- <input type="text" placeholder="Middle Name" name="middle_name" />
- </div>
- <div>
- <input type="text" placeholder="Last Name" name="last_name" />
- </div>
- </fieldset>
- <fieldset>
- <legend>Gender</legend>
- <div>
- <label>
- <input type="radio" name="gender" value="m" />
- Male
- </label>
- <label>
- <input type="radio" name="gender" value="f" />
- Female
- </label>
- </div>
- </fieldset>
- <fieldset>
- <legend>Birthday</legend>
- <div>
- <input type="date" placeholder="Birthday" name="birthday" />
- </div>
- </fieldset>
- <fieldset>
- <legend>Civil Status</legend>
- <div>
- <select name="civil_status">
- <option value="">Select Civil Status</option>
- <option value="single">Single</option>
- <option value="married">Married</option>
- <option value="divorced">Divorced</option>
- <option value="separated">Separated</option>
- </select>
- </div>
- </fieldset>
- <fieldset>
- <legend>New Registration</legend>
- <div>
- <label>
- <input type="checkbox" name="new_registration" />
- New Registration
- </label>
- </div>
- </fieldset>
- <fieldset>
- <legend>Last Appointment Date</legend>
- <div>
- <input type="datetime-local" placeholder="Last Appointment Date" name="last_appointment_datetime" />
- </div>
- </fieldset>
- <fieldset>
- <legend>New Appointment Week</legend>
- <div>
- <input type="week" placeholder="New Appointment Week" name="new_appointment_week" />
- </div>
- </fieldset>
- <fieldset>
- <legend>Start Month</legend>
- <div>
- <input type="month" placeholder="Start Month" name="start_month" />
- </div>
- </fieldset>
- <div>
- <label>
- <input type="checkbox" value="filipino" name="nationality" />
- Filipino
- </label>
- </div>
- <div>
- <input type="number" placeholder="Gross Salary" name="gross" />
- </div>
- <fieldset>
- <legend>
- Default Dependents
- </legend>
- <div>
- <label>
- <input type="radio" value="James" name="dependent" />
- James
- </label>
- <label>
- <input type="radio" value="Jun" name="dependent" />
- Jun
- </label>
- </div>
- </fieldset>
- <div>
- <button type="button" class="dependents">
- Add Dependents
- </button>
- </div>
- <div>
- <textarea name="notes" placeholder="Notes"></textarea>
- </div>
- <fieldset>
- <legend>
- Quality of Service
- </legend>
- <input type="range" min="0" max="10" step="0.5" placeholder="Quality of Service" name="qos" />
- </fieldset>
- <div>
- <button name="submit" value="Hello" type="submit">Hello</button>
- <button name="submit" value="Hi" type="submit">Hi</button>
- </div>
- </form>
- </article>
- <script>
- Array.from(document.getElementsByClassName('dependents')).forEach(d => {
- d.addEventListener('click', e => {
- const container = document.createElement('div')
- const input = document.createElement('input')
- input.name = 'dependent'
- input.type = 'text'
- input.placeholder = 'Dependent'
- container.classList.add('additional-dependent')
- container.appendChild(input)
- e.target.parentElement.parentElement.insertBefore(container, e.target.parentElement)
- })
- })
- </script>
- </body>
- </html>
- `))
-
- it('should have correct form values', () => {
- utils.test({
- action: (cy) => {
- cy.get('[name="first_name"]')
- .type('John')
- cy.get('[name="middle_name"]')
- .type('Marcelo')
- cy.get('[name="last_name"]')
- .type('Dela Cruz')
- cy.get('[name="gender"][value="m"]')
- .check()
- cy.get('[name="birthday"]')
- .type('1989-06-04')
- cy.get('[name="civil_status"]')
- .select('Married')
- cy.get('[name="new_registration"]')
- .check()
- cy.get('[name="last_appointment_datetime"]')
- .type('2001-09-11T06:09')
- cy.get('[name="new_appointment_week"]')
- .type('2001-W51')
- cy.get('[name="start_month"]')
- .type('2002-03')
- cy.get('[name="nationality"][value="filipino"]')
- .check()
- cy.get('[name="gross"]')
- .type('131072')
- cy.get('[name="dependent"][value="Jun"]')
- .check()
-
- cy.get('button.dependents')
- .click()
- cy.get('.additional-dependent [name="dependent"][type="text"]')
- .last()
- .type('Juana')
- cy.get('button.dependents')
- .click()
- cy.get('.additional-dependent [name="dependent"][type="text"]')
- .last()
- .type('Jane')
- cy.get('button.dependents')
- .click()
- cy.get('.additional-dependent [name="dependent"][type="text"]')
- .last()
- .type('Josh')
- cy.get('[name="qos"]')
- .invoke('val', 9.5)
- .trigger('change')
- cy.get('[name="notes"]')
- .type('Test content\n\nNew line\n\nAnother line')
- return cy.get('[name="submit"][value="Hi"]')
- },
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- const before = utils.makeSearchParams(getFormValues(form, { submitter }))
- .toString();
- const after = utils.makeSearchParams(search)
- .toString();
-
- expect(before)
- .toEqual(after);
- },
- expectedStaticValue: 'first_name=John&middle_name=Marcelo&last_name=Dela+Cruz&gender=m&birthday=1989-06-04&civil_status=married&new_registration=on&last_appointment_datetime=2001-09-11T06%3A09&new_appointment_week=2001-W51&start_month=2002-03&nationality=filipino&gross=131072&dependent=Jun¬es=Test+content%0D%0A%0D%0ANew+line%0D%0A%0D%0AAnother+line&qos=9.5&submit=Hi',
- });
- });
-
- it('should have filled form values', () => {
- utils.test({
- action: (cy) => cy.wait(3000).get('[name="submit"][value="Hi"]'),
- test: (form: HTMLFormElement, submitter: any, search: any) => {
- const before = utils.makeSearchParams(getFormValues(form, { submitter }))
- .toString();
- const after = utils.makeSearchParams(search)
- .toString();
-
- expect(before)
- .toEqual(after);
- },
- preAction: (form: HTMLFormElement) => {
- setFormValues(form, {
- first_name: 'John',
- middle_name: 'Marcelo',
- last_name: 'Dela Cruz',
- gender: 'm',
- birthday: new Date('1989-06-04'),
- civil_status: 'married',
- new_registration: 'on',
- last_appointment_datetime: new Date('2001-09-11T06:09:00'),
- new_appointment_week: '2001-W51',
- start_month: '2002-03',
- nationality: 'filipino',
- gross: 131072,
- dependent: 'Jun',
- notes: `Test content
-
- New line
-
- Another line`,
- qos: 9.5,
- });
- },
- expectedStaticValue: 'first_name=John&middle_name=Marcelo&last_name=Dela+Cruz&gender=m&birthday=1989-06-04&civil_status=married&new_registration=on&last_appointment_datetime=2001-09-11T06%3A09&new_appointment_week=2001-W51&start_month=2002-03&nationality=filipino&gross=131072&dependent=Jun¬es=Test+content%0D%0A%0D%0ANew+line%0D%0A%0D%0AAnother+line&qos=9.5&submit=Hi',
- });
- });
- });
-
- // TODO implement tests for multiple values
- });
|