import { getFormValues, setFormValues } from '../../src'; import * as utils from '../utils' describe('date', () => { describe('basic', () => { beforeEach(utils.setup(` Date/Basic
`)) it('should have single form value', () => { utils.test({ actionBeforeSubmit: (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: '2003-04-06', }, }); }); it('should enable Date representation', () => { utils.test({ actionBeforeSubmit: (cy: any) => cy.get('[type="submit"]'), onSubmitted: (form: HTMLFormElement, submitter: any, search: any) => { const values = getFormValues(form, { submitter, forceDateValues: true }); // somehow, checking instanceof Date fails here, because we're using an artificial date // object? const testDate = new Date(values.hello as Date); expect((values.hello as Date).getTime()).toBe(testDate.getTime()); }, }); }); }) describe('disabled', () => { beforeEach(utils.setup(` Date/Disabled
`)) it('should have blank form value', () => { utils.test({ actionBeforeSubmit: (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(` Date/Outside
`)) it('should have single form value', () => { utils.test({ actionBeforeSubmit: (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: '2003-04-20', }, }); }); }); describe('readonly', () => { beforeEach(utils.setup(` Date/Readonly
`)) it('should have single form value', () => { utils.test({ actionBeforeSubmit: (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: '2003-11-11', }, }); }); }); describe('programmatic value setting', () => { beforeEach(utils.setup(` Date/Programmatic Value Setting
`)); it('should have form values set', () => { utils.test({ actionBeforeSubmit: (cy: any) => cy.get('[type="submit"]'), onLoaded: (form: HTMLFormElement) => { setFormValues(form, { hello: new Date('2000-01-01'), }) }, 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: '2000-01-01', }, }); }); }); describe('duplicate', () => { beforeEach(utils.setup(` Date/Duplicate
`)); it('should get both values', () => { utils.test({ actionBeforeSubmit: (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: ['2007-07-07', '2008-08-08'], }, }); }); it('should set both values', () => { utils.test({ onLoaded: (form: HTMLFormElement) => { setFormValues(form, { hello: ['2006-06-06', '2005-05-05'], }) }, actionBeforeSubmit: (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: ['2006-06-06', '2005-05-05'], }, }); }); }); })