import { getFormValues, setFormValues } from '../../src';
import * as utils from '../utils'
describe('date', () => {
describe('basic', () => {
beforeEach(utils.setup(`
Datetime-Local/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-06T13:37',
},
});
});
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(`
Datetime-Local/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(`
Datetime-Local/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-20T13:37',
},
});
});
});
describe('readonly', () => {
beforeEach(utils.setup(`
Datetime-Local/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-11T13:37',
},
});
});
});
describe('programmatic value setting', () => {
beforeEach(utils.setup(`
Datetime-Local/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-01T13:37'), })
},
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-01T13:37',
},
});
});
});
describe('duplicate', () => {
beforeEach(utils.setup(`
Datetime-Local/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-07T13:37', '2008-08-08T13:37'],
},
});
});
it('should set both values', () => {
utils.test({
onLoaded: (form: HTMLFormElement) => {
setFormValues(form, {
hello: ['2006-06-06T13:37', '2005-05-05T13:37'],
})
},
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-06T13:37', '2005-05-05T13:37'],
},
});
});
});
})