|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import getFormValues from '../../src'
- import * as utils from '../utils'
-
- describe('file', () => {
- describe('single', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>File/Single</title>
- </head>
- <body>
- <form>
- <label>
- <span>Hello</span>
- <input type="file" name="hello" />
- </label>
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- `))
-
- 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',
- }
- );
- })
-
- it('should retrieve the file list upon setting appropriate option', () => {
- utils.test(
- (cy: any) => {
- cy
- .get('[name="hello"]')
- .attachFile('uploads/data.json')
-
- return cy.get('[type="submit"]')
- },
- (form: HTMLFormElement, submitter: any) => {
- const formValues = getFormValues(form, {submitter, getFileObjects: true})
- expect(formValues.hello[0].name).toBe('data.json')
- //expect(before).toEqual(after);
- },
- );
- })
- })
-
- describe('multiple', () => {
- beforeEach(utils.setup(`
- <!DOCTYPE html>
- <html lang="en-PH">
- <head>
- <meta charset="UTF-8">
- <title>File/Multiple</title>
- </head>
- <body>
- <form>
- <label>
- <span>Hello</span>
- <input type="file" name="hello" multiple />
- </label>
- <button type="submit">Submit</button>
- </form>
- </body>
- </html>
- `))
-
- 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', 'uploads/data2.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&hello=data2.json',
- );
- })
-
- it('should retrieve the file list upon setting appropriate option', () => {
- utils.test(
- (cy: any) => {
- cy
- .get('[name="hello"]')
- .attachFile(['uploads/data.json', 'uploads/data2.json'])
-
- return cy.get('[type="submit"]')
- },
- (form: HTMLFormElement, submitter: any) => {
- const formValues = getFormValues(form, {submitter, getFileObjects: true})
- expect(formValues.hello[0].name).toBe('data.json')
- expect(formValues.hello[1].name).toBe('data2.json')
- },
- );
- })
- })
- })
|