Download react-hook-form/src/__tests__/logic/getCheckboxValue.test.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 5.63 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/src/__tests__/logic/getCheckboxValue.test.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/src/__tests__/logic/getCheckboxValue.test.ts
-
curl -L -o getCheckboxValue.test.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/src/__tests__/logic/getCheckboxValue.test.ts
5.63 kB
| import getCheckboxValue from '../../logic/getCheckboxValue'; | |
| describe('getCheckboxValue', () => { | |
| it('should return default value if not valid or empty options', () => { | |
| expect(getCheckboxValue(undefined)).toEqual({ | |
| value: false, | |
| isValid: false, | |
| }); | |
| }); | |
| it('should return checked value if single checkbox is checked', () => { | |
| expect( | |
| getCheckboxValue([ | |
| { | |
| name: 'bill', | |
| checked: true, | |
| value: '3', | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '3' }, | |
| }, | |
| ]), | |
| ).toEqual({ value: '3', isValid: true }); | |
| }); | |
| it('should return true if single checkbox is checked and has no value', () => { | |
| expect( | |
| // @ts-expect-error this is a mock for html input | |
| getCheckboxValue([{ name: 'bill', checked: true, attributes: {} }]), | |
| ).toEqual({ value: true, isValid: true }); | |
| }); | |
| it('should return true if single checkbox is checked and has empty value', () => { | |
| expect( | |
| getCheckboxValue([ | |
| { | |
| name: 'bill', | |
| checked: true, | |
| value: '', | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: 'test' }, | |
| }, | |
| ]), | |
| ).toEqual({ value: true, isValid: true }); | |
| expect( | |
| getCheckboxValue([ | |
| { | |
| name: 'bill', | |
| checked: true, | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: 'test' }, | |
| }, | |
| ]), | |
| ).toEqual({ value: true, isValid: true }); | |
| }); | |
| it('should return false if single checkbox is un-checked', () => { | |
| expect( | |
| getCheckboxValue([ | |
| { | |
| name: 'bill', | |
| checked: false, | |
| // @ts-expect-error this is a mock for html input | |
| attributes: {}, | |
| }, | |
| ]), | |
| ).toEqual({ value: false, isValid: false }); | |
| }); | |
| it('should return multiple selected values', () => { | |
| expect( | |
| getCheckboxValue([ | |
| { | |
| name: 'bill', | |
| checked: true, | |
| value: '2', | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '2' }, | |
| }, | |
| { | |
| name: 'bill', | |
| checked: true, | |
| value: '3', | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '3' }, | |
| }, | |
| ]), | |
| ).toEqual({ value: ['2', '3'], isValid: true }); | |
| }); | |
| it('should return values for checked boxes only', () => { | |
| expect( | |
| getCheckboxValue([ | |
| { | |
| name: 'bill', | |
| checked: false, | |
| value: '2', | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '2' }, | |
| }, | |
| { | |
| name: 'bill', | |
| checked: true, | |
| value: '3', | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '3' }, | |
| }, | |
| { | |
| name: 'bill', | |
| checked: false, | |
| value: '4', | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '4' }, | |
| }, | |
| ]), | |
| ).toEqual({ value: ['3'], isValid: true }); | |
| }); | |
| it('should return empty array for multi checkbox with no checked box', () => { | |
| expect( | |
| getCheckboxValue([ | |
| { | |
| name: 'bill', | |
| checked: false, | |
| value: '2', | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '2' }, | |
| }, | |
| { | |
| name: 'bill', | |
| checked: false, | |
| value: '3', | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '3' }, | |
| }, | |
| ]), | |
| ).toEqual({ value: [], isValid: false }); | |
| }); | |
| it('should not return error when check box ref is undefined', () => { | |
| expect( | |
| getCheckboxValue([ | |
| // @ts-expect-error this is a mock for html input | |
| undefined, | |
| { | |
| name: 'bill', | |
| checked: false, | |
| value: '2', | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '2' }, | |
| }, | |
| ]), | |
| ).toEqual({ value: [], isValid: false }); | |
| }); | |
| it('should return disabled input result', () => { | |
| expect( | |
| getCheckboxValue([ | |
| { | |
| name: 'bill', | |
| checked: true, | |
| value: '2', | |
| disabled: true, | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '2' }, | |
| }, | |
| { | |
| name: 'bill', | |
| checked: true, | |
| value: '3', | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '3' }, | |
| }, | |
| ]), | |
| ).toEqual({ | |
| value: ['3'], | |
| isValid: true, | |
| }); | |
| expect( | |
| getCheckboxValue([ | |
| { | |
| name: 'bill', | |
| checked: true, | |
| value: '2', | |
| disabled: true, | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '2' }, | |
| }, | |
| { | |
| name: 'bill', | |
| disabled: true, | |
| checked: true, | |
| value: '3', | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '3' }, | |
| }, | |
| ]), | |
| ).toEqual({ | |
| value: [], | |
| isValid: false, | |
| }); | |
| expect( | |
| getCheckboxValue([ | |
| { | |
| name: 'bill', | |
| checked: true, | |
| value: '2', | |
| disabled: true, | |
| // @ts-expect-error this is a mock for html input | |
| attributes: { value: '2' }, | |
| }, | |
| ]), | |
| ).toEqual({ | |
| value: false, | |
| isValid: false, | |
| }); | |
| }); | |
| }); | |