|
- import { getFormValues, setFormValues } from '../../src';
- import * as utils from '../utils'
-
- describe('number', () => {
- describe('basic', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>Number/Basic</title>
- </head>
- <body>
- <form>
- <label>
- <span>Hello</span>
- <input type="number" name="hello" min="0" max="10" value="5" />
- </label>
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- `))
-
- it('should have single form value', () => {
- utils.test({
- querySubmitter: (cy: any) => cy.get('[type="submit"]'),
- onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
- const before = utils.makeSearchParams(getFormValues(form, { submitter, forceNumberValues: true, }))
- .toString();
- const after = utils.makeSearchParams(search)
- .toString();
- expect(before)
- .toEqual(after);
- },
- expectedStaticValue: {
- hello: '5',
- },
- });
- });
- })
-
- describe('disabled', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>Number/Disabled</title>
- </head>
- <body>
- <form>
- <label>
- <span>Hello</span>
- <input
- type="number" name="hello" min="0" max="10" value="5"
- disabled
- />
- </label>
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- `))
-
- it('should have blank form value', () => {
- utils.test({
- querySubmitter: (cy: any) => cy.get('[type="submit"]'),
- onSubmitted: (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('outside', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>Number/Outside</title>
- </head>
- <body>
- <form id="form">
- <button type="submit">Submit</button>
- </form>
- <label>
- <span>Hello</span>
- <input type="number" name="hello" min="0" max="10" value="5" form="form" />
- </label>
- </body>
- </html>
- `))
-
- it('should have single form value', () => {
- utils.test({
- querySubmitter: (cy: any) => cy.get('[type="submit"]'),
- onSubmitted: (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: {
- hello: '5',
- },
- });
- });
- });
-
- describe('readonly', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>Number/Readonly</title>
- </head>
- <body>
- <form>
- <label>
- <span>Hello</span>
- <input
- type="number" name="hello" min="0" max="10" value="5"
- readonly
- />
- </label>
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- `))
-
- it('should have single form value', () => {
- utils.test({
- querySubmitter: (cy: any) => cy.get('[type="submit"]'),
- onSubmitted: (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: {
- hello: '5',
- },
- });
- });
- });
-
- describe('programmatic value setting', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>Number/Programmatic Value Setting</title>
- </head>
- <body>
- <form>
- <label>
- <span>Hello</span>
- <input type="number" name="hello" />
- </label>
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- `));
-
- it('should have form values set', () => {
- utils.test({
- querySubmitter: (cy: any) => cy.get('[type="submit"]'),
- onLoaded: (form: HTMLFormElement) => {
- setFormValues(form, { hello: 5, })
- },
- onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
- const before = utils.makeSearchParams(getFormValues(form, { submitter, forceNumberValues: true, }))
- .toString();
- const after = utils.makeSearchParams(search)
- .toString();
- expect(before)
- .toEqual(after);
- },
- expectedStaticValue: {
- hello: '5',
- },
- });
- });
- });
-
- describe('duplicate', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>Number/Duplicate</title>
- </head>
- <body>
- <form>
- <label>
- <span>Hello 1</span>
- <input id="hello1" type="number" name="hello" min="0" max="10" value="5" />
- </label>
- <label>
- <span>Hello 2</span>
- <input id="hello2" type="number" name="hello" min="0" max="10" value="7" />
- </label>
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- `));
-
- it('should get both values', () => {
- utils.test({
- querySubmitter: (cy: any) => cy.get('[type="submit"]'),
- onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => {
- const before = utils.makeSearchParams(getFormValues(form, { submitter, forceNumberValues: true, }))
- .toString();
- const after = utils.makeSearchParams(search)
- .toString();
- expect(before)
- .toEqual(after);
- },
- expectedStaticValue: {
- hello: ['5', '7'],
- },
- });
- });
-
- it('should set both values', () => {
- utils.test({
- onLoaded: (form: HTMLFormElement) => {
- setFormValues(form, {
- hello: [4, 2],
- })
- },
- querySubmitter: (cy: any) => cy.get('[type="submit"]'),
- onSubmitted: (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: {
- hello: ['4', '2'],
- },
- });
- });
- });
- })
|