From 118147836fd8457cf15a136675d040c47fcd26c6 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sun, 16 May 2021 12:29:55 +0800 Subject: [PATCH] Restructure tests, add other scenarios Handle selects, as well as file uploads. --- README.md | 4 + cypress.json | 7 +- .../fixtures/templates/blank.html | 0 .../fixtures/templates/everything.html | 0 .../templates/single-disabled-input.html | 0 .../fixtures/templates/single-file-input.html | 16 ++ ...e-input-with-double-button-submitters.html | 0 ...le-input-with-double-input-submitters.html | 0 .../fixtures/templates/single-input.html | 0 .../templates/single-multiple-file-input.html | 16 ++ .../templates/single-multiple-select.html | 21 ++ .../single-outside-input-and-submitter.html | 15 ++ .../templates/single-outside-input.html | 18 ++ .../templates/single-readonly-input.html | 0 cypress/fixtures/templates/single-select.html | 20 ++ cypress/fixtures/uploads/data.json | 3 + {test => cypress}/integration/blank.test.ts | 7 +- .../integration/everything.test.ts | 7 +- cypress/integration/single.test.ts | 200 ++++++++++++++++++ {test => cypress}/plugins/index.ts | 0 {test => cypress}/support/commands.ts | 3 + {test => cypress}/support/index.ts | 1 - cypress/tsconfig.json | 2 +- package.json | 41 ++-- src/index.ts | 60 +++++- test/integration/single.test.ts | 94 -------- test/utils/search.ts | 0 yarn.lock | 5 + 28 files changed, 402 insertions(+), 138 deletions(-) rename {test => cypress}/fixtures/templates/blank.html (100%) rename {test => cypress}/fixtures/templates/everything.html (100%) rename {test => cypress}/fixtures/templates/single-disabled-input.html (100%) create mode 100644 cypress/fixtures/templates/single-file-input.html rename {test => cypress}/fixtures/templates/single-input-with-double-button-submitters.html (100%) rename {test => cypress}/fixtures/templates/single-input-with-double-input-submitters.html (100%) rename {test => cypress}/fixtures/templates/single-input.html (100%) create mode 100644 cypress/fixtures/templates/single-multiple-file-input.html create mode 100644 cypress/fixtures/templates/single-multiple-select.html create mode 100644 cypress/fixtures/templates/single-outside-input-and-submitter.html create mode 100644 cypress/fixtures/templates/single-outside-input.html rename {test => cypress}/fixtures/templates/single-readonly-input.html (100%) create mode 100644 cypress/fixtures/templates/single-select.html create mode 100644 cypress/fixtures/uploads/data.json rename {test => cypress}/integration/blank.test.ts (65%) rename {test => cypress}/integration/everything.test.ts (88%) create mode 100644 cypress/integration/single.test.ts rename {test => cypress}/plugins/index.ts (100%) rename {test => cypress}/support/commands.ts (93%) rename {test => cypress}/support/index.ts (95%) delete mode 100644 test/integration/single.test.ts delete mode 100644 test/utils/search.ts diff --git a/README.md b/README.md index 2db757a..698da75 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ Extract form values through the DOM. +## Motivation + +There are many ways to lay out forms. + ## Installation The library is not yet out on any package manager. Installing through the URL is the preferred way. diff --git a/cypress.json b/cypress.json index d9bbdde..0967ef4 100644 --- a/cypress.json +++ b/cypress.json @@ -1,6 +1 @@ -{ - "fixturesFolder": "test/fixtures", - "integrationFolder": "test/integration", - "pluginsFile": "test/plugins/index.ts", - "supportFile": "test/support/index.ts" -} +{} diff --git a/test/fixtures/templates/blank.html b/cypress/fixtures/templates/blank.html similarity index 100% rename from test/fixtures/templates/blank.html rename to cypress/fixtures/templates/blank.html diff --git a/test/fixtures/templates/everything.html b/cypress/fixtures/templates/everything.html similarity index 100% rename from test/fixtures/templates/everything.html rename to cypress/fixtures/templates/everything.html diff --git a/test/fixtures/templates/single-disabled-input.html b/cypress/fixtures/templates/single-disabled-input.html similarity index 100% rename from test/fixtures/templates/single-disabled-input.html rename to cypress/fixtures/templates/single-disabled-input.html diff --git a/cypress/fixtures/templates/single-file-input.html b/cypress/fixtures/templates/single-file-input.html new file mode 100644 index 0000000..75d862a --- /dev/null +++ b/cypress/fixtures/templates/single-file-input.html @@ -0,0 +1,16 @@ + + + + + Title + + +
+ + +
+ + diff --git a/test/fixtures/templates/single-input-with-double-button-submitters.html b/cypress/fixtures/templates/single-input-with-double-button-submitters.html similarity index 100% rename from test/fixtures/templates/single-input-with-double-button-submitters.html rename to cypress/fixtures/templates/single-input-with-double-button-submitters.html diff --git a/test/fixtures/templates/single-input-with-double-input-submitters.html b/cypress/fixtures/templates/single-input-with-double-input-submitters.html similarity index 100% rename from test/fixtures/templates/single-input-with-double-input-submitters.html rename to cypress/fixtures/templates/single-input-with-double-input-submitters.html diff --git a/test/fixtures/templates/single-input.html b/cypress/fixtures/templates/single-input.html similarity index 100% rename from test/fixtures/templates/single-input.html rename to cypress/fixtures/templates/single-input.html diff --git a/cypress/fixtures/templates/single-multiple-file-input.html b/cypress/fixtures/templates/single-multiple-file-input.html new file mode 100644 index 0000000..633b1fd --- /dev/null +++ b/cypress/fixtures/templates/single-multiple-file-input.html @@ -0,0 +1,16 @@ + + + + + Title + + +
+ + +
+ + diff --git a/cypress/fixtures/templates/single-multiple-select.html b/cypress/fixtures/templates/single-multiple-select.html new file mode 100644 index 0000000..08e64d2 --- /dev/null +++ b/cypress/fixtures/templates/single-multiple-select.html @@ -0,0 +1,21 @@ + + + + + Title + + +
+ + +
+ + diff --git a/cypress/fixtures/templates/single-outside-input-and-submitter.html b/cypress/fixtures/templates/single-outside-input-and-submitter.html new file mode 100644 index 0000000..17b8808 --- /dev/null +++ b/cypress/fixtures/templates/single-outside-input-and-submitter.html @@ -0,0 +1,15 @@ + + + + + Title + + +
+ + + + diff --git a/cypress/fixtures/templates/single-outside-input.html b/cypress/fixtures/templates/single-outside-input.html new file mode 100644 index 0000000..4553eb1 --- /dev/null +++ b/cypress/fixtures/templates/single-outside-input.html @@ -0,0 +1,18 @@ + + + + + Title + + +
+ +
+ + + diff --git a/test/fixtures/templates/single-readonly-input.html b/cypress/fixtures/templates/single-readonly-input.html similarity index 100% rename from test/fixtures/templates/single-readonly-input.html rename to cypress/fixtures/templates/single-readonly-input.html diff --git a/cypress/fixtures/templates/single-select.html b/cypress/fixtures/templates/single-select.html new file mode 100644 index 0000000..1f71ded --- /dev/null +++ b/cypress/fixtures/templates/single-select.html @@ -0,0 +1,20 @@ + + + + + Title + + +
+ + +
+ + diff --git a/cypress/fixtures/uploads/data.json b/cypress/fixtures/uploads/data.json new file mode 100644 index 0000000..8b32dd1 --- /dev/null +++ b/cypress/fixtures/uploads/data.json @@ -0,0 +1,3 @@ +{ + "hello": "hi" +} diff --git a/test/integration/blank.test.ts b/cypress/integration/blank.test.ts similarity index 65% rename from test/integration/blank.test.ts rename to cypress/integration/blank.test.ts index 619356b..b1bd3ea 100644 --- a/test/integration/blank.test.ts +++ b/cypress/integration/blank.test.ts @@ -1,8 +1,5 @@ -/// -/// - import getFormValues from '../../src' -import * as utils from '../utils' +import * as utils from '../../test/utils' describe('blank template', () => { beforeEach(utils.setup('blank')) @@ -11,7 +8,7 @@ describe('blank template', () => { utils.test( (cy: any) => cy.get('[type="submit"]'), (form: HTMLFormElement, submitter: any, search: any) => { - const before = utils.makeSearchParams(getFormValues(form, submitter)).toString(); + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); const after = utils.makeSearchParams(search).toString(); expect(before).toEqual(after); }, diff --git a/test/integration/everything.test.ts b/cypress/integration/everything.test.ts similarity index 88% rename from test/integration/everything.test.ts rename to cypress/integration/everything.test.ts index 5468b8e..2d43985 100644 --- a/test/integration/everything.test.ts +++ b/cypress/integration/everything.test.ts @@ -1,8 +1,5 @@ -/// -/// - import getFormValues from '../../src' -import * as utils from '../utils'; +import * as utils from '../../test/utils'; describe('blank template', () => { beforeEach(utils.setup('everything')) @@ -31,7 +28,7 @@ describe('blank template', () => { return cy.get('[name="submit"][value="Hi"]') }, (form: HTMLFormElement, submitter: any, search: any) => { - const before = utils.makeSearchParams(getFormValues(form, submitter)).toString(); + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); const after = utils.makeSearchParams(search).toString(); expect(before).toEqual(after); }, diff --git a/cypress/integration/single.test.ts b/cypress/integration/single.test.ts new file mode 100644 index 0000000..c82209a --- /dev/null +++ b/cypress/integration/single.test.ts @@ -0,0 +1,200 @@ +import getFormValues from '../../src' +import * as utils from '../../test/utils'; + +describe('single input template', () => { + beforeEach(utils.setup('single-input')) + + it('should have single form value', () => { + utils.test( + (cy: any) => cy.get('[type="submit"]'), + (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); + const after = utils.makeSearchParams(search).toString(); + expect(before).toEqual(after); + }, + { + hello: 'Hi', + } + ); + }); +}) + +describe('single outside input template', () => { + beforeEach(utils.setup('single-outside-input')) + + it('should have single form value', () => { + utils.test( + (cy: any) => cy.get('[type="submit"]'), + (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); + const after = utils.makeSearchParams(search).toString(); + expect(before).toEqual(after); + }, + { + hello: 'Hi', + } + ); + }); +}) + +describe('single outside input and submitter template', () => { + beforeEach(utils.setup('single-outside-input-and-submitter')) + + it('should have single form value', () => { + utils.test( + (cy: any) => cy.get('[type="submit"]'), + (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); + const after = utils.makeSearchParams(search).toString(); + expect(before).toEqual(after); + }, + { + hello: 'Hi', + } + ); + }); +}) + +describe('single readonly template', () => { + beforeEach(utils.setup('single-readonly-input')) + + it('should have single form value', () => { + utils.test( + (cy: any) => cy.get('[type="submit"]'), + (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); + const after = utils.makeSearchParams(search).toString(); + expect(before).toEqual(after); + }, + { + hello: 'Hi', + } + ); + }); +}) + +describe('single disabled template', () => { + beforeEach(utils.setup('single-disabled-input')) + + it('should have blank form value', () => { + utils.test( + (cy: any) => cy.get('[type="submit"]'), + (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); + const after = utils.makeSearchParams(search).toString(); + expect(before).toEqual(after); + }, + {} + ); + }); +}) + +describe('single input with double button submitters template', () => { + beforeEach(utils.setup('single-input-with-double-button-submitters')) + + it('should have double form values', () => { + utils.test( + (cy: any) => cy.get('[name="action"][value="Foo"]'), + (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); + const after = utils.makeSearchParams(search).toString(); + expect(before).toEqual(after); + }, + { + hello: 'Hi', + action: 'Foo', + } + ); + }); +}) + +describe('single input with double input submitters template', () => { + beforeEach(utils.setup('single-input-with-double-input-submitters')) + + it('should have double form values', () => { + utils.test( + (cy: any) => cy.get('[name="action"][value="Bar"]'), + (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); + const after = utils.makeSearchParams(search).toString(); + expect(before).toEqual(after); + }, + { + hello: 'Hi', + action: 'Bar', + } + ); + }); +}) + +describe('single select template', () => { + beforeEach(utils.setup('single-select')) + + it('should have single form value', () => { + utils.test( + (cy: any) => cy.get('[type="submit"]'), + (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); + const after = utils.makeSearchParams(search).toString(); + expect(before).toEqual(after); + }, + { + hello: 'Baz', + } + ); + }); +}) + +describe('single multiple select template', () => { + beforeEach(utils.setup('single-multiple-select')) + + it('should have single form value', () => { + utils.test( + (cy: any) => cy.get('[type="submit"]'), + (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); + const after = utils.makeSearchParams(search).toString(); + expect(before).toEqual(after); + }, + { + hello: 'Bar,Quux', + } + ); + }); +}) + +describe('single file input template', () => { + beforeEach(utils.setup('single-file-input')) + + it('should have no form values when no file is selected', () => { + utils.test( + (cy: any) => cy.get('[type="submit"]'), + (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); + const after = utils.makeSearchParams(search).toString(); + expect(before).toEqual(after); + }, + {} + ); + }) + + it('should have single form value when a file is selected', () => { + utils.test( + (cy: any) => { + cy + .get('[name="hello"]') + .attachFile('uploads/data.json') + + return cy.get('[type="submit"]') + }, + (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, {submitter})).toString(); + const after = utils.makeSearchParams(search).toString(); + expect(before).toEqual(after); + }, + { + hello: 'data.json', + } + ); + }) +}) diff --git a/test/plugins/index.ts b/cypress/plugins/index.ts similarity index 100% rename from test/plugins/index.ts rename to cypress/plugins/index.ts diff --git a/test/support/commands.ts b/cypress/support/commands.ts similarity index 93% rename from test/support/commands.ts rename to cypress/support/commands.ts index 119ab03..7c9d2f1 100644 --- a/test/support/commands.ts +++ b/cypress/support/commands.ts @@ -23,3 +23,6 @@ // // -- This will overwrite an existing command -- // Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) + +import 'cypress-jest-adapter' +import 'cypress-file-upload' diff --git a/test/support/index.ts b/cypress/support/index.ts similarity index 95% rename from test/support/index.ts rename to cypress/support/index.ts index ed026ad..bd55847 100644 --- a/test/support/index.ts +++ b/cypress/support/index.ts @@ -15,7 +15,6 @@ // Import commands.ts using ES2015 syntax: import './commands' -import 'cypress-jest-adapter' // Alternatively you can use CommonJS syntax: // require('./commands') diff --git a/cypress/tsconfig.json b/cypress/tsconfig.json index 20dff1e..fa1cd16 100644 --- a/cypress/tsconfig.json +++ b/cypress/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "es5", "lib": ["es5", "dom"], - "types": ["cypress"] + "types": ["cypress", "cypress-file-upload", "cypress-jest-adapter"] }, "include": ["**/*.ts"] } diff --git a/package.json b/package.json index b481515..272580a 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.0", + "version": "0.2.0", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", @@ -7,25 +7,25 @@ "dist", "src" ], - "publishing": { - "github": { - "repository": "https://github.com/TheoryOfNekomata/formxtr.git", - "publishConfig": { - "registry": "https://npm.pkg.github.com" - } - }, - "master": { - "repository": "https://code.modal.sh/TheoryOfNekomata/formxtr.git", - "publishConfig": { - "registry": "https://js.pack.modal.sh" - } - }, - "npm": { - "publishConfig": { - "registry": "https://registry.npmjs.com" - } - } - }, + "publishing": { + "github": { + "repository": "https://github.com/TheoryOfNekomata/formxtr.git", + "publishConfig": { + "registry": "https://npm.pkg.github.com" + } + }, + "master": { + "repository": "https://code.modal.sh/TheoryOfNekomata/formxtr.git", + "publishConfig": { + "registry": "https://js.pack.modal.sh" + } + }, + "npm": { + "publishConfig": { + "registry": "https://registry.npmjs.com" + } + } + }, "engines": { "node": ">=10" }, @@ -68,6 +68,7 @@ "@size-limit/preset-small-lib": "^4.10.2", "@types/jsdom": "^16.2.10", "cypress": "^7.2.0", + "cypress-file-upload": "^5.0.7", "cypress-jest-adapter": "^0.1.1", "husky": "^6.0.0", "jsdom": "^16.5.3", diff --git a/src/index.ts b/src/index.ts index d1684b3..5e04396 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,20 @@ +/** + * Type for valid field elements. + * + * + */ + type HTMLFieldElement = HTMLInputElement | HTMLButtonElement | HTMLSelectElement | HTMLTextAreaElement +/** + * Type for valid submitter elements. + * + * Only the