diff --git a/__tests__/Area.test.tsx b/__tests__/Area.test.tsx index 0be1ce2..d69e904 100644 --- a/__tests__/Area.test.tsx +++ b/__tests__/Area.test.tsx @@ -1,10 +1,20 @@ import React from 'react'; import { mount } from 'enzyme'; -import { ValidatorArea, Validator } from '../src'; -import { ValidatorAreaProps } from '../src/ValidatorArea'; -import required from '../src/rules/required'; +import { Validator } from '@/Validator'; +import { ValidatorArea, ValidatorAreaProps } from '@/components/ValidatorArea'; + +describe('test ValidatorProvider', () => { + beforeEach(() => { + Validator.extend('passes_not', { + passed(): boolean { + return false; + }, + message(): string { + return 'not passed'; + } + }); + }); -describe('test Provider', () => { it('should render input', () => { const area = mount( @@ -64,43 +74,18 @@ describe('test Provider', () => { it('should apply rules on blur', () => { const area = mount( - + ); area.find('input').at(0).simulate('blur'); - expect(area.state().errors[0]).toBe('Test is required'); + expect(area.state().errors[0]).toBe('Not passed'); }); it('should render error when area dirty', async () => { const area = mount( - - {({ errors }) => ( - <> - - {errors.length &&
{errors[0]}
} - - )} -
- ); - - area.find('input').simulate('blur'); - expect(area.find('div').text()).toBe('Test is required'); - }) - - it('should validate element with rule string', () => { - Validator.extend('testrule', { - passed(): boolean { - return false; - }, - message(): string { - return 'string rule passed'; - } - }); - - const area = mount( - + {({ errors }) => ( <> @@ -111,14 +96,14 @@ describe('test Provider', () => { ); area.find('input').simulate('blur'); - expect(area.find('div').text()).toBe('string rule passed'); + expect(area.find('div').text()).toBe('Not passed'); }) it('should call element\'s provided blur along validator blur', () => { const mockFn = jest.fn(); const area = mount( - + ); diff --git a/__tests__/Provider.test.tsx b/__tests__/Provider.test.tsx index c1e30b2..cbc57be 100644 --- a/__tests__/Provider.test.tsx +++ b/__tests__/Provider.test.tsx @@ -1,8 +1,8 @@ import React from 'react'; import { mount } from 'enzyme'; -import { ValidatorArea, ValidatorProvider } from '../src'; -import required from '../src/rules/required'; -import { ValidatorProviderProps } from '../src/Provider'; +import { Validator } from '@/Validator'; +import ValidatorProvider, { ValidatorProviderProps } from '@/components/ValidatorProvider'; +import { ValidatorArea } from '@/components/ValidatorArea'; const tick = () => { @@ -11,31 +11,51 @@ const tick = () => { }) } -describe('test Provider', () => { - it('should render Provider', () => { +describe('test ValidatorProvider', () => { + beforeEach(() => { + Validator.extend('passes_not', { + passed(): boolean { + return false; + }, + message(): string { + return 'not passed'; + } + }); + + Validator.extend('passes', { + passed(): boolean { + return true; + }, + message(): string { + return 'passed'; + } + }); + }); + + it('should render ValidatorProvider', () => { const provider = mount( - + ); expect(provider.instance().props.rules).toBeDefined(); }); - it('should throw error when area with existing name is addeded', () => { - const provider = () => { - mount( - - -
- - -
- - - ); - } - - expect(() => provider()).toThrow('Validation area names should be unique'); - }) + it('should throw error when area with existing name is added', () => { + const provider = () => { + mount( + + +
+ + +
+ + + ); + } + + expect(() => provider()).toThrow('Validation area names should be unique'); + }); it('should render with function as child', () => { const provider = mount( @@ -45,7 +65,7 @@ describe('test Provider', () => { ); expect(provider.find('div').text()).toBe('test'); - }) + }); it('should add an area when provided as child', () => { const provider = mount( @@ -69,10 +89,10 @@ describe('test Provider', () => { {({ validate }) => ( <> - + - +