From 2089f146ef166960bcd0ae05f6d03b50cd4fa1e7 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sun, 12 Mar 2023 10:57:52 +0800 Subject: [PATCH] Update tests, fix multiple value setting Make tests more comprehensive. --- cypress/integration/date.test.ts | 4 +- cypress/integration/datetime-local.test.ts | 4 +- cypress/integration/file.test.ts | 22 +- cypress/integration/misc.test.ts | 179 ++++++++++- cypress/integration/month.test.ts | 265 ++++++++++++++++ cypress/integration/number.test.ts | 265 ++++++++++++++++ cypress/integration/range.test.ts | 265 ++++++++++++++++ cypress/integration/text.test.ts | 86 +---- cypress/integration/textarea.test.ts | 349 +++++++++++++++++++++ cypress/integration/week.test.ts | 265 ++++++++++++++++ cypress/utils/index.ts | 2 +- package.json | 2 +- src/index.ts | 116 ++++--- 13 files changed, 1688 insertions(+), 136 deletions(-) create mode 100644 cypress/integration/month.test.ts create mode 100644 cypress/integration/number.test.ts create mode 100644 cypress/integration/range.test.ts create mode 100644 cypress/integration/textarea.test.ts create mode 100644 cypress/integration/week.test.ts diff --git a/cypress/integration/date.test.ts b/cypress/integration/date.test.ts index 1cce143..43b91d4 100644 --- a/cypress/integration/date.test.ts +++ b/cypress/integration/date.test.ts @@ -123,7 +123,7 @@ describe('date', () => { - Text/Readonly + Date/Readonly
@@ -164,7 +164,7 @@ describe('date', () => { - Text/Basic + Date/Programmatic Value Setting diff --git a/cypress/integration/datetime-local.test.ts b/cypress/integration/datetime-local.test.ts index 141d6fa..c920605 100644 --- a/cypress/integration/datetime-local.test.ts +++ b/cypress/integration/datetime-local.test.ts @@ -123,7 +123,7 @@ describe('date', () => { - Text/Readonly + Datetime-Local/Readonly @@ -164,7 +164,7 @@ describe('date', () => { - Text/Basic + Datetime-Local/Programmatic Value Setting diff --git a/cypress/integration/file.test.ts b/cypress/integration/file.test.ts index 03781ea..4fe2717 100644 --- a/cypress/integration/file.test.ts +++ b/cypress/integration/file.test.ts @@ -1,4 +1,4 @@ -import { getFormValues } from '../../src' +import { getFormValues, setFormValues } from '../../src'; import * as utils from '../utils' describe('file', () => { @@ -83,6 +83,26 @@ describe('file', () => { }, }); }) + + it('should do nothing when attempting to set the value of the file', () => { + utils.test({ + preAction: (form: HTMLFormElement) => { + setFormValues(form, { hello: 'data.json' }); + }, + action: (cy: any) => cy.get('[type="submit"]'), + test: (form: HTMLFormElement, submitter: any) => { + const formValues = getFormValues( + form, + { + submitter, + getFileObjects: true + } + ) + expect(formValues.hello).toBeDefined() + }, + expectedStaticValue: {}, + }); + }); }) describe('multiple', () => { diff --git a/cypress/integration/misc.test.ts b/cypress/integration/misc.test.ts index ffe243d..24f969f 100644 --- a/cypress/integration/misc.test.ts +++ b/cypress/integration/misc.test.ts @@ -1,7 +1,184 @@ -import { getFormValues, setFormValues } from '../../src'; +import getFormValuesDeprecated, { getFormValues, setFormValues } 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('blank', () => { beforeEach(utils.setup(` diff --git a/cypress/integration/month.test.ts b/cypress/integration/month.test.ts new file mode 100644 index 0000000..765cfea --- /dev/null +++ b/cypress/integration/month.test.ts @@ -0,0 +1,265 @@ +import { getFormValues, setFormValues } from '../../src'; +import * as utils from '../utils' + +describe('month', () => { + describe('basic', () => { + beforeEach(utils.setup(` + + + + + Month/Basic + + +
+ + +
+ + + `)) + + it('should have single 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: { + hello: '2003-05', + }, + }); + }); + }) + + describe('disabled', () => { + beforeEach(utils.setup(` + + + + + Month/Disabled + + +
+ + +
+ + + `)) + + 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('outside', () => { + beforeEach(utils.setup(` + + + + + Month/Outside + + +
+ +
+ + + + `)) + + it('should have single 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: { + hello: '2003-05', + }, + }); + }); + }); + + describe('readonly', () => { + beforeEach(utils.setup(` + + + + + Text/Readonly + + +
+ + +
+ + + `)) + + it('should have single 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: { + hello: '2003-05', + }, + }); + }); + }); + + describe('programmatic value setting', () => { + beforeEach(utils.setup(` + + + + + Month/Programmatic Value Setting + + +
+ + +
+ + + `)); + + it('should have form values set', () => { + utils.test({ + action: (cy: any) => cy.get('[type="submit"]'), + preAction: (form: HTMLFormElement) => { + setFormValues(form, { hello: '2003-05', }) + }, + 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: { + hello: '2003-05', + }, + }); + }); + }); + + describe('duplicate', () => { + beforeEach(utils.setup(` + + + + + Month/Duplicate + + +
+ + + +
+ + + `)); + + it('should get both values', () => { + 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: { + hello: ['2003-05', '2003-06'], + }, + }); + }); + + it('should set both values', () => { + utils.test({ + preAction: (form: HTMLFormElement) => { + setFormValues(form, { + hello: ['2003-04', '2003-02'], + }) + }, + 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: { + hello: ['2003-04', '2003-02'], + }, + }); + }); + }); +}) diff --git a/cypress/integration/number.test.ts b/cypress/integration/number.test.ts new file mode 100644 index 0000000..b28d8ea --- /dev/null +++ b/cypress/integration/number.test.ts @@ -0,0 +1,265 @@ +import { getFormValues, setFormValues } from '../../src'; +import * as utils from '../utils' + +describe('number', () => { + describe('basic', () => { + beforeEach(utils.setup(` + + + + + Number/Basic + + +
+ + +
+ + + `)) + + it('should have single 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, forceNumberValues: true, })) + .toString(); + const after = utils.makeSearchParams(search) + .toString(); + expect(before) + .toEqual(after); + }, + expectedStaticValue: { + hello: '5', + }, + }); + }); + }) + + describe('disabled', () => { + beforeEach(utils.setup(` + + + + + Number/Disabled + + +
+ + +
+ + + `)) + + 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('outside', () => { + beforeEach(utils.setup(` + + + + + Number/Outside + + +
+ +
+ + + + `)) + + it('should have single 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: { + hello: '5', + }, + }); + }); + }); + + describe('readonly', () => { + beforeEach(utils.setup(` + + + + + Number/Readonly + + +
+ + +
+ + + `)) + + it('should have single 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: { + hello: '5', + }, + }); + }); + }); + + describe('programmatic value setting', () => { + beforeEach(utils.setup(` + + + + + Number/Programmatic Value Setting + + +
+ + +
+ + + `)); + + it('should have form values set', () => { + utils.test({ + action: (cy: any) => cy.get('[type="submit"]'), + preAction: (form: HTMLFormElement) => { + setFormValues(form, { hello: 5, }) + }, + test: (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(` + + + + + Number/Duplicate + + +
+ + + +
+ + + `)); + + it('should get both values', () => { + utils.test({ + action: (cy: any) => cy.get('[type="submit"]'), + test: (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({ + preAction: (form: HTMLFormElement) => { + setFormValues(form, { + hello: [4, 2], + }) + }, + 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: { + hello: ['4', '2'], + }, + }); + }); + }); +}) diff --git a/cypress/integration/range.test.ts b/cypress/integration/range.test.ts new file mode 100644 index 0000000..f9b73c4 --- /dev/null +++ b/cypress/integration/range.test.ts @@ -0,0 +1,265 @@ +import { getFormValues, setFormValues } from '../../src'; +import * as utils from '../utils' + +describe('range', () => { + describe('basic', () => { + beforeEach(utils.setup(` + + + + + Range/Basic + + +
+ + +
+ + + `)) + + it('should have single 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, forceNumberValues: true, })) + .toString(); + const after = utils.makeSearchParams(search) + .toString(); + expect(before) + .toEqual(after); + }, + expectedStaticValue: { + hello: '5', + }, + }); + }); + }) + + describe('disabled', () => { + beforeEach(utils.setup(` + + + + + Range/Disabled + + +
+ + +
+ + + `)) + + 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('outside', () => { + beforeEach(utils.setup(` + + + + + Range/Outside + + +
+ +
+ + + + `)) + + it('should have single 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: { + hello: '5', + }, + }); + }); + }); + + describe('readonly', () => { + beforeEach(utils.setup(` + + + + + Range/Readonly + + +
+ + +
+ + + `)) + + it('should have single 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: { + hello: '5', + }, + }); + }); + }); + + describe('programmatic value setting', () => { + beforeEach(utils.setup(` + + + + + Range/Programmatic Value Setting + + +
+ + +
+ + + `)); + + it('should have form values set', () => { + utils.test({ + action: (cy: any) => cy.get('[type="submit"]'), + preAction: (form: HTMLFormElement) => { + setFormValues(form, { hello: 5, }) + }, + test: (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(` + + + + + Range/Duplicate + + +
+ + + +
+ + + `)); + + it('should get both values', () => { + utils.test({ + action: (cy: any) => cy.get('[type="submit"]'), + test: (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({ + preAction: (form: HTMLFormElement) => { + setFormValues(form, { + hello: [4, 2], + }) + }, + 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: { + hello: ['4', '2'], + }, + }); + }); + }); +}) diff --git a/cypress/integration/text.test.ts b/cypress/integration/text.test.ts index 4403567..94fe112 100644 --- a/cypress/integration/text.test.ts +++ b/cypress/integration/text.test.ts @@ -1,4 +1,4 @@ -import { getFormValues, LineEnding, setFormValues } from '../../src'; +import { getFormValues, setFormValues } from '../../src'; import * as utils from '../utils' describe('text', () => { @@ -199,90 +199,6 @@ describe('text', () => { }); }); - describe('textarea', () => { - beforeEach(utils.setup(` - - - - - Text/Textarea - - -
- - -
- - - `)); - - it('should read LF line breaks', () => { - utils.test({ - action: (cy: any) => { - cy.get('[name="hello"]') - .type('Hi\nHello', { parseSpecialCharSequences: false }) - return cy.get('[type="submit"]') - }, - test: (form: HTMLFormElement, submitter: any, search: any) => { - const before = utils.makeSearchParams(getFormValues(form, { submitter, lineEndings: LineEnding.LF })) - .toString(); - const after = utils.makeSearchParams(search) - .toString(); - expect(before) - .toEqual(after); - }, - expectedStaticValue: { - hello: 'Hi\nHello', - }, - }); - }); - - it('should read CR line breaks', () => { - utils.test({ - action: (cy: any) => { - cy.get('[name="hello"]') - .type('Hi\rHello', { parseSpecialCharSequences: false }) - return cy.get('[type="submit"]') - }, - test: (form: HTMLFormElement, submitter: any, search: any) => { - const before = utils.makeSearchParams(getFormValues(form, { submitter, lineEndings: LineEnding.CR })) - .toString(); - const after = utils.makeSearchParams(search) - .toString(); - expect(before) - .toEqual(after); - }, - expectedStaticValue: { - hello: 'Hi\rHello', - }, - }); - }); - - it('should read CRLF line breaks', () => { - utils.test({ - action: (cy: any) => { - cy.get('[name="hello"]') - .type('Hi\r\nHello', { parseSpecialCharSequences: false }) - return cy.get('[type="submit"]') - }, - test: (form: HTMLFormElement, submitter: any, search: any) => { - const before = utils.makeSearchParams(getFormValues(form, { submitter, lineEndings: LineEnding.CRLF })) - .toString(); - const after = utils.makeSearchParams(search) - .toString(); - expect(before) - .toEqual(after); - }, - expectedStaticValue: { - hello: 'Hi\r\nHello', - }, - }); - }); - }); - describe('duplicate', () => { beforeEach(utils.setup(` diff --git a/cypress/integration/textarea.test.ts b/cypress/integration/textarea.test.ts new file mode 100644 index 0000000..5fdf85d --- /dev/null +++ b/cypress/integration/textarea.test.ts @@ -0,0 +1,349 @@ +import { getFormValues, LineEnding, setFormValues } from '../../src'; +import * as utils from '../utils' + +describe('textarea', () => { + describe('basic', () => { + beforeEach(utils.setup(` + + + + + Textarea/Basic + + +
+ + +
+ + + `)) + + it('should have single 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: { + hello: 'Hi', + }, + }); + }); + }) + + describe('disabled', () => { + beforeEach(utils.setup(` + + + + + Textarea/Disabled + + +
+ + +
+ + + `)) + + 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('outside', () => { + beforeEach(utils.setup(` + + + + + Textarea/Outside + + +
+ +
+ + + + `)) + + it('should have single 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: { + hello: 'Hi', + }, + }); + }); + }); + + describe('readonly', () => { + beforeEach(utils.setup(` + + + + + Textarea/Readonly + + +
+ + +
+ + + `)) + + it('should have single 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: { + hello: 'Hi', + }, + }); + }); + }); + + describe('programmatic value setting', () => { + beforeEach(utils.setup(` + + + + + Textarea/Programmatic Value Setting + + +
+ + +
+ + + `)); + + it('should have form values set', () => { + utils.test({ + action: (cy: any) => cy.get('[type="submit"]'), + preAction: (form: HTMLFormElement) => { + setFormValues(form, { hello: '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: { + hello: 'Hi', + }, + }); + }); + }); + + describe('lines', () => { + beforeEach(utils.setup(` + + + + + Textarea/Lines + + +
+ + +
+ + + `)); + + it('should read LF line breaks', () => { + utils.test({ + action: (cy: any) => { + cy.get('[name="hello"]') + .type('Hi\nHello', { parseSpecialCharSequences: false }) + return cy.get('[type="submit"]') + }, + test: (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, { submitter, lineEndings: LineEnding.LF })) + .toString(); + const after = utils.makeSearchParams(search) + .toString(); + expect(before) + .toEqual(after); + }, + expectedStaticValue: { + hello: 'Hi\nHello', + }, + }); + }); + + it('should read CR line breaks', () => { + utils.test({ + action: (cy: any) => { + cy.get('[name="hello"]') + .type('Hi\rHello', { parseSpecialCharSequences: false }) + return cy.get('[type="submit"]') + }, + test: (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, { submitter, lineEndings: LineEnding.CR })) + .toString(); + const after = utils.makeSearchParams(search) + .toString(); + expect(before) + .toEqual(after); + }, + expectedStaticValue: { + hello: 'Hi\rHello', + }, + }); + }); + + it('should read CRLF line breaks', () => { + utils.test({ + action: (cy: any) => { + cy.get('[name="hello"]') + .type('Hi\r\nHello', { parseSpecialCharSequences: false }) + return cy.get('[type="submit"]') + }, + test: (form: HTMLFormElement, submitter: any, search: any) => { + const before = utils.makeSearchParams(getFormValues(form, { submitter, lineEndings: LineEnding.CRLF })) + .toString(); + const after = utils.makeSearchParams(search) + .toString(); + expect(before) + .toEqual(after); + }, + expectedStaticValue: { + hello: 'Hi\r\nHello', + }, + }); + }); + }); + + describe('duplicate', () => { + beforeEach(utils.setup(` + + + + + Textarea/Duplicate + + +
+ + + +
+ + + `)); + + it('should get both values', () => { + 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: { + hello: ['value', 'another value'], + }, + }); + }); + + it('should set both values', () => { + utils.test({ + preAction: (form: HTMLFormElement) => { + setFormValues(form, { + hello: ['new value 1', 'another value 2'], + }) + }, + 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: { + hello: ['new value 1', 'another value 2'], + }, + }); + }); + }); +}) diff --git a/cypress/integration/week.test.ts b/cypress/integration/week.test.ts new file mode 100644 index 0000000..df7e643 --- /dev/null +++ b/cypress/integration/week.test.ts @@ -0,0 +1,265 @@ +import { getFormValues, setFormValues } from '../../src'; +import * as utils from '../utils' + +describe('week', () => { + describe('basic', () => { + beforeEach(utils.setup(` + + + + + Week/Basic + + +
+ + +
+ + + `)) + + it('should have single 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: { + hello: '2003-W25', + }, + }); + }); + }) + + describe('disabled', () => { + beforeEach(utils.setup(` + + + + + Week/Disabled + + +
+ + +
+ + + `)) + + 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('outside', () => { + beforeEach(utils.setup(` + + + + + Week/Outside + + +
+ +
+ + + + `)) + + it('should have single 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: { + hello: '2003-W25', + }, + }); + }); + }); + + describe('readonly', () => { + beforeEach(utils.setup(` + + + + + Text/Readonly + + +
+ + +
+ + + `)) + + it('should have single 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: { + hello: '2003-W25', + }, + }); + }); + }); + + describe('programmatic value setting', () => { + beforeEach(utils.setup(` + + + + + Week/Programmatic Value Setting + + +
+ + +
+ + + `)); + + it('should have form values set', () => { + utils.test({ + action: (cy: any) => cy.get('[type="submit"]'), + preAction: (form: HTMLFormElement) => { + setFormValues(form, { hello: '2003-W25', }) + }, + 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: { + hello: '2003-W25', + }, + }); + }); + }); + + describe('duplicate', () => { + beforeEach(utils.setup(` + + + + + Week/Duplicate + + +
+ + + +
+ + + `)); + + it('should get both values', () => { + 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: { + hello: ['2003-W25', '2003-W30'], + }, + }); + }); + + it('should set both values', () => { + utils.test({ + preAction: (form: HTMLFormElement) => { + setFormValues(form, { + hello: ['2003-W40', '2003-W50'], + }) + }, + 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: { + hello: ['2003-W40', '2003-W50'], + }, + }); + }); + }); +}) diff --git a/cypress/utils/index.ts b/cypress/utils/index.ts index 654437a..6dc5182 100644 --- a/cypress/utils/index.ts +++ b/cypress/utils/index.ts @@ -106,7 +106,7 @@ export const makeSearchParams = (beforeValues: Record | string) const theValue = !Array.isArray(value) ? [value] : value theValue.forEach(v => { let processedLineBreaks = v - if (typeof cy !== 'undefined') { + if (typeof cy !== 'undefined' && typeof v === 'string') { let forceLineBreaks: string; // TODO make this foolproof diff --git a/package.json b/package.json index 0571e47..918fc28 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "dev": "pridepack dev", "test:jsdom": "vitest", "test:cypress": "cypress run", - "test:cpanel": "cypress open" + "cypress:cpanel": "cypress open" }, "private": false, "description": "Extract and set form values through the DOM.", diff --git a/src/index.ts b/src/index.ts index 0e64ac2..400544b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,11 +16,6 @@ export enum LineEnding { CRLF = '\r\n', } -/** - * Type for a placeholder object value. - */ -type PlaceholderObject = Record - /** * Tag name for the `` element. */ @@ -96,36 +91,36 @@ const getTextAreaFieldValue = ( * Sets the value of a `