Skip to content

Feature/check rule applied #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions __tests__/Area.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Validator } from '@/Validator';
import { ValidatorArea, ValidatorAreaProps } from '@/components/ValidatorArea';
import ValidatorProvider, { ValidatorProviderProps } from '@/components/ValidatorProvider';
import { ProviderScope } from '@/ProviderScope';
import required from '@/rules/required';

const tick = () => {
return new Promise(resolve => {
Expand All @@ -21,6 +22,7 @@ describe('test ValidatorProvider', () => {
return 'not passed';
}
});
Validator.extend('required', required)
});

it('should render input', () => {
Expand Down Expand Up @@ -83,7 +85,7 @@ describe('test ValidatorProvider', () => {
it('should apply rules on blur', () => {
const area = mount<ValidatorArea, ValidatorAreaProps>(
<ValidatorArea rules="passes_not">
<input name="test" />
<input name="test" value="test" />
</ValidatorArea>
);

Expand All @@ -107,7 +109,7 @@ describe('test ValidatorProvider', () => {
<ValidatorArea rules="passes_not">
{({ errors }) => (
<>
<input name="test" />
<input name="test" value="test" />
{errors.length && <div>{errors[0]}</div>}
</>
)}
Expand All @@ -123,7 +125,7 @@ describe('test ValidatorProvider', () => {

const area = mount<ValidatorArea, ValidatorAreaProps>(
<ValidatorArea rules="passes_not">
<input name="test" onBlur={mockFn} />
<input name="test" onBlur={mockFn} value="test" />
</ValidatorArea>
);

Expand All @@ -137,7 +139,7 @@ describe('test ValidatorProvider', () => {
return validator.refs().length === 2;
},
message(): string {
return 'test';
return '';
}
}))
const mockFn = jest.fn();
Expand All @@ -147,10 +149,10 @@ describe('test ValidatorProvider', () => {
{({ validate }: ProviderScope) => (
<>
<ValidatorArea name="test1">
<input value="" />
<input value="test" />
</ValidatorArea>
<ValidatorArea>
<input value="" name="test2" />
<input value="test" name="test2" />
</ValidatorArea>
<button onClick={() => validate(mockFn)} />
</>
Expand Down Expand Up @@ -180,11 +182,11 @@ describe('test ValidatorProvider', () => {
{({ validate }: ProviderScope) => (
<>
<ValidatorArea name="test1">
<input value="" />
<input value="" />
<input value="test" />
<input value="test" />
</ValidatorArea>
<ValidatorArea>
<input value="" name="test2" />
<input value="test" name="test2" />
</ValidatorArea>
<button onClick={() => validate(mockFn)} />
</>
Expand Down Expand Up @@ -213,10 +215,10 @@ describe('test ValidatorProvider', () => {
{({ validate }: ProviderScope) => (
<>
<ValidatorArea name="test1">
<input value="" />
<input value="test" />
</ValidatorArea>
<ValidatorArea>
<input value="" name="test2" />
<input value="test" name="test2" />
</ValidatorArea>
<button onClick={() => validate(mockFn)} />
</>
Expand All @@ -243,7 +245,7 @@ describe('test ValidatorProvider', () => {

const area = mount<ValidatorArea, ValidatorAreaProps>(
<ValidatorArea rules="no_other_areas">
<input name="test" onBlur={mockFn} />
<input name="test" value="test" onBlur={mockFn} />
</ValidatorArea>
);

Expand All @@ -270,10 +272,10 @@ describe('test ValidatorProvider', () => {
{({ validate }: ProviderScope) => (
<>
<ValidatorArea name="test1">
<textarea value="" />
<textarea value="test" />
</ValidatorArea>
<ValidatorArea>
<input value="" name="test2" />
<input value="test" name="test2" />
</ValidatorArea>
<button onClick={() => validate(mockFn)} />
</>
Expand All @@ -298,7 +300,7 @@ describe('test ValidatorProvider', () => {

const area = mount<ValidatorArea, ValidatorAreaProps>(
<ValidatorArea validationName="Foo" rules="passes_not">
<input name="test" />
<input name="test" value="test" />
</ValidatorArea>
);

Expand Down
4 changes: 2 additions & 2 deletions __tests__/Provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ describe('test ValidatorProvider', () => {
{({ validate }) => (
<>
<ValidatorArea rules="passes_not" name="test1">
<input value="" />
<input value="test" />
</ValidatorArea>
<ValidatorArea rules="passes_not" name="test2">
<input value="" />
<input value="test" />
</ValidatorArea>
<button onClick={() => validate(mockFn)} />
</>
Expand Down
49 changes: 44 additions & 5 deletions __tests__/Validator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ describe('test validator', () => {

Validator.extend('testRule', rule);

expect(Validator.hasRule('testRule')).toBeTruthy();
expect(Validator.ruleExists('testRule')).toBeTruthy();
});

it('should validate rules as string', () => {
const input = document.createElement<'input'>('input');
input.value = 'test';

const validator = new Validator(
[
document.createElement<'input'>('input')
input
],
'rule_one|rule_two',
'test'
Expand All @@ -60,9 +63,11 @@ describe('test validator', () => {
});

it('should validate rules as array', () => {
const input = document.createElement<'input'>('input');
input.value = 'test';
const validator = new Validator(
[
document.createElement<'input'>('input')
input
],
['rule_one', 'rule_two'],
'test'
Expand All @@ -73,9 +78,12 @@ describe('test validator', () => {
});

it('should validate with parameters', () => {
const input = document.createElement<'input'>('input');
input.value = 'test';

const validator = new Validator(
[
document.createElement<'input'>('input')
input
],
['rule_with_params:1,2,3,4'],
'test'
Expand All @@ -86,10 +94,12 @@ describe('test validator', () => {
});

it('throws an exception when rule is not found', () => {
const input = document.createElement<'input'>('input');
input.value = 'test';
const throws = () => {
const validator = new Validator(
[
document.createElement<'input'>('input')
input
],
['not_existing_rule'],
'test'
Expand Down Expand Up @@ -118,5 +128,34 @@ describe('test validator', () => {
}

expect(() => throws()).toThrowError('Areas are only available when validating React components.')
});

it('should be able to check if the value is required', () => {
const input = document.createElement<'input'>('input');
input.value = 'test';
Validator.extend('check_if_required', (validator: Validator) => ({
passed(elements: HTMLElement[]): boolean {
return !elements.every((element: HTMLElement) => validator.shouldValidate(element));
},
message(): string {
return 'Value is false negative required'
}
}));

const validator = new Validator(
[
input
],
['required', 'check_if_required'],
'test'
);

validator.validate();

expect(validator.getErrors()[0]).toBe('Value is false negative required');
})

it('should return empty array when no refs provided', () => {

})
});
70 changes: 52 additions & 18 deletions __tests__/rules/max.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { mount } from 'enzyme';
import max from '@/rules/max';
import { Validator } from '@/Validator';
import { ValidatorArea, ValidatorAreaProps } from '@/components/ValidatorArea';
import { IncorrectArgumentTypeError } from '@/rules';

describe('test max rule', () => {
beforeEach(() => {
Expand All @@ -11,39 +12,72 @@ describe('test max rule', () => {

it('should always validate inputs and not validate non-inputs', () => {
const input = document.createElement('input');
input.value = '5';

const throwsArgumentError = () => {
const validator = new Validator([
input
], ['max:foo'],
'validate_throws'
);
validator.validate();
}
const meter = document.createElement('meter');
const output = document.createElement('output');
const progress = document.createElement('progress');
const canvas = document.createElement('canvas');
input.value = '7';
output.value = '7';
meter.value = 6;
meter.max = 10;
progress.value = 6;

const validator_input = new Validator([
input
],
['max:4'],
'validator_input');
['max:5'],
'');

const progress = document.createElement('progress');
progress.value = 5;
const validator_meter = new Validator([
meter
],
['max:5'],
'');

const validator_output = new Validator([
output
],
['max:5'],
'');

const validator_progress = new Validator([
progress
],
['max:4'],
'validate_progress');
['max:5'],
'');

const validator_canvas = new Validator([
canvas
],
['max:5'],
'');

expect(() => throwsArgumentError()).toThrowError('max rule has incorrect argument foo. Expected a number.')
const validator_wrong_arg = new Validator([
input
],
['max:foo'],
'');

validator_input.validate();
expect(validator_input.getErrors().length).toBe(1);

validator_meter.validate();
expect(validator_meter.getErrors().length).toBe(1);

validator_output.validate();
expect(validator_output.getErrors().length).toBe(1);

validator_progress.validate();
expect(validator_progress.getErrors().length).toBe(0);
expect(validator_progress.getErrors().length).toBe(1);

validator_canvas.validate();
expect(validator_canvas.getErrors().length).toBe(0);

const throwsInvalidArgument = () => {
validator_wrong_arg.validate();
}

expect(() => throwsInvalidArgument()).toThrowError(IncorrectArgumentTypeError);
});

it('should validate select', () => {
Expand Down
Loading