From 5f1a94052c15d054080e49dd45462279675d1704 Mon Sep 17 00:00:00 2001 From: Eran Machiels Date: Sat, 26 Sep 2020 16:02:08 +0200 Subject: [PATCH 1/5] Added access to validator, area and provider (when used) within validation rules --- __tests__/Area.test.tsx | 128 +++++++++++++++++++++++++++ __tests__/Validator.test.tsx | 17 +++- src/Rule.ts | 15 +++- src/Validator.ts | 40 +++++++-- src/ValidatorContext.ts | 5 +- src/components/ValidatorArea.tsx | 43 ++++++--- src/components/ValidatorProvider.tsx | 22 ++++- 7 files changed, 249 insertions(+), 21 deletions(-) diff --git a/__tests__/Area.test.tsx b/__tests__/Area.test.tsx index d69e904..814840a 100644 --- a/__tests__/Area.test.tsx +++ b/__tests__/Area.test.tsx @@ -2,6 +2,14 @@ import React from 'react'; import { mount } from 'enzyme'; import { Validator } from '@/Validator'; import { ValidatorArea, ValidatorAreaProps } from '@/components/ValidatorArea'; +import ValidatorProvider, { ValidatorProviderProps } from '@/components/ValidatorProvider'; +import { ProviderScope } from '@/ProviderScope'; + +const tick = () => { + return new Promise(resolve => { + setTimeout(resolve, 0); + }) +} describe('test ValidatorProvider', () => { beforeEach(() => { @@ -111,4 +119,124 @@ describe('test ValidatorProvider', () => { area.find('input').simulate('blur'); expect(mockFn).toBeCalled(); }); + + it('should get all input refs from the provider', async () => { + Validator.extend('test_all', (validator: Validator) => ({ + passed(): boolean { + return validator.refs().length === 2; + }, + message(): string { + return 'test'; + } + })) + const mockFn = jest.fn(); + + const provider = mount( + + {({ validate }: ProviderScope) => ( + <> + + + + + + +