import getFormValuesDeprecated, {
getFormValues,
setFormValues,
isFieldElement,
isElementValueIncludedInFormSubmit,
getValue,
} from '../../src';
import * as utils from '../utils'
describe('misc', () => {
describe('core', () => {
beforeEach(utils.setup(`
Misc/Blank
`))
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(`
Misc/Utilities
`));
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(`
Misc/Blank
`))
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(`
Misc/Blank
`));
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(`
Misc/Blank
`))
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(`
Misc/Everything
`))
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
});