apphouse
Version:
Component library for React that uses observable state management and theme-able components.
45 lines (39 loc) • 1.18 kB
text/typescript
import { Field } from './Field';
// Mock makeAutoObservable function
jest.mock('mobx', () => ({
makeAutoObservable: jest.fn(),
runInAction: jest.fn()
}));
describe('Field class', () => {
it('should throw an error when setting min value for unsupported input types', () => {
const field = new Field({
id: 'testField',
value: '',
type: 'email',
input: {}
});
expect(() => field.setMin('5')).toThrow(
'setMin can only be used with number and date input types'
);
});
it('should return false when field is required and has no value', () => {
const field = new Field({
id: 'testField',
value: '',
input: { required: true }
});
expect(field.valid).toBe(false);
});
it('should return true when field is valid', () => {
const field = new Field({ id: 'testField', value: 'testValue' });
expect(field.valid).toBe(true);
});
it('should return false when field does not pass validation', () => {
const field = new Field({
id: 'testField',
value: 'testValue',
validation: (value) => value !== 'testValue'
});
expect(field.valid).toBe(false);
});
});