2019-11-07 01:17:19 +03:00
import rules from '@/libs/rules'
2020-03-07 17:03:59 +03:00
import FileUpload from '../../src/FileUpload'
2019-11-07 01:17:19 +03:00
2019-11-08 01:03:34 +03:00
/ * *
* Accepted rule
* /
describe ( 'accepted' , ( ) => {
2020-02-26 01:32:40 +03:00
it ( 'passes with true' , async ( ) => expect ( await rules . accepted ( { value : 'yes' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with on' , async ( ) => expect ( await rules . accepted ( { value : 'on' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with 1' , async ( ) => expect ( await rules . accepted ( { value : '1' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with number 1' , async ( ) => expect ( await rules . accepted ( { value : 1 } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with boolean true' , async ( ) => expect ( await rules . accepted ( { value : true } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fail with boolean false' , async ( ) => expect ( await rules . accepted ( { value : false } ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fail with "false"' , async ( ) => expect ( await rules . accepted ( { value : 'false' } ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
} )
/ * *
* Checks if a date is after another date
* /
describe ( 'after' , ( ) => {
const today = new Date ( )
const tomorrow = new Date ( )
const yesterday = new Date ( )
tomorrow . setDate ( today . getDate ( ) + 1 )
yesterday . setDate ( today . getDate ( ) - 1 )
2020-02-26 01:32:40 +03:00
it ( 'passes with tomorrow’ s date object' , async ( ) => expect ( await rules . after ( { value : tomorrow } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with future date' , async ( ) => expect ( await rules . after ( { value : 'January 15, 2999' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with long past date' , async ( ) => expect ( await rules . after ( { value : yesterday } , 'Jan 15, 2000' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails with yesterday’ s date' , async ( ) => expect ( await rules . after ( { value : yesterday } ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails with old date string' , async ( ) => expect ( await rules . after ( { value : 'January, 2000' } ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails with invalid value' , async ( ) => expect ( await rules . after ( { value : '' } ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
} )
/ * *
* Checks if a date is after another date
* /
describe ( 'alpha' , ( ) => {
2020-02-26 01:32:40 +03:00
it ( 'passes with simple string' , async ( ) => expect ( await rules . alpha ( { value : 'abc' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with long string' , async ( ) => expect ( await rules . alpha ( { value : 'lkashdflaosuihdfaisudgflakjsdbflasidufg' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with single character' , async ( ) => expect ( await rules . alpha ( { value : 'z' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with accented character' , async ( ) => expect ( await rules . alpha ( { value : 'jüstin' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with lots of accented characters' , async ( ) => expect ( await rules . alpha ( { value : 'àáâäïíôöÆ' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with lots of accented characters if invalid set' , async ( ) => expect ( await rules . alpha ( { value : 'àáâäïíôöÆ' } , 'russian' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails with lots of accented characters if latin' , async ( ) => expect ( await rules . alpha ( { value : 'àáâäïíôöÆ' } , 'latin' ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails with numbers' , async ( ) => expect ( await rules . alpha ( { value : 'justin83' } ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails with symbols' , async ( ) => expect ( await rules . alpha ( { value : '-justin' } ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
} )
/ * *
* Checks if a date alpha and numeric
* /
describe ( 'alphanumeric' , ( ) => {
2020-02-26 01:32:40 +03:00
it ( 'passes with simple string' , async ( ) => expect ( await rules . alphanumeric ( { value : '567abc' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with long string' , async ( ) => expect ( await rules . alphanumeric ( { value : 'lkashdfla234osuihdfaisudgflakjsdbfla567sidufg' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with single character' , async ( ) => expect ( await rules . alphanumeric ( { value : 'z' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with accented character' , async ( ) => expect ( await rules . alphanumeric ( { value : 'jüst56in' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with lots of accented characters' , async ( ) => expect ( await rules . alphanumeric ( { value : 'àáâ7567567äïíôöÆ' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with lots of accented characters if invalid set' , async ( ) => expect ( await rules . alphanumeric ( { value : '123123àáâäï67íôöÆ' } , 'russian' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails with lots of accented characters if latin' , async ( ) => expect ( await rules . alphanumeric ( { value : 'àáâäï123123íôöÆ' } , 'latin' ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails with decimals in' , async ( ) => expect ( await rules . alphanumeric ( { value : 'abcABC99.123' } ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
} )
2020-03-03 14:53:04 +03:00
/ * *
* Checks if a date is after another date
* /
describe ( 'before' , ( ) => {
const today = new Date ( )
const tomorrow = new Date ( )
const yesterday = new Date ( )
tomorrow . setDate ( today . getDate ( ) + 1 )
yesterday . setDate ( today . getDate ( ) - 1 )
it ( 'fails with tomorrow’ s date object' , async ( ) => expect ( await rules . before ( { value : tomorrow } ) ) . toBe ( false ) )
it ( 'fails with future date' , async ( ) => expect ( await rules . before ( { value : 'January 15, 2999' } ) ) . toBe ( false ) )
it ( 'fails with long past date' , async ( ) => expect ( await rules . before ( { value : yesterday } , 'Jan 15, 2000' ) ) . toBe ( false ) )
it ( 'passes with yesterday’ s date' , async ( ) => expect ( await rules . before ( { value : yesterday } ) ) . toBe ( true ) )
it ( 'passes with old date string' , async ( ) => expect ( await rules . before ( { value : 'January, 2000' } ) ) . toBe ( true ) )
it ( 'fails with invalid value' , async ( ) => expect ( await rules . after ( { value : '' } ) ) . toBe ( false ) )
} )
2019-11-08 01:03:34 +03:00
/ * *
* Checks if between
* /
describe ( 'between' , ( ) => {
2020-02-26 01:32:40 +03:00
it ( 'passes with simple number' , async ( ) => expect ( await rules . between ( { value : 5 } , 0 , 10 ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with simple number string' , async ( ) => expect ( await rules . between ( { value : '5' } , '0' , '10' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with decimal number string' , async ( ) => expect ( await rules . between ( { value : '0.5' } , '0' , '1' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes with string length' , async ( ) => expect ( await rules . between ( { value : 'abc' } , 2 , 4 ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails with string length too long' , async ( ) => expect ( await rules . between ( { value : 'abcdef' } , 2 , 4 ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails with string length too short' , async ( ) => expect ( await rules . between ( { value : 'abc' } , 3 , 10 ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails with number to small' , async ( ) => expect ( await rules . between ( { value : 0 } , 3 , 10 ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails with number to large' , async ( ) => expect ( await rules . between ( { value : 15 } , 3 , 10 ) ) . toBe ( false ) )
2020-03-19 00:15:54 +03:00
it ( 'passes when forced to value' , async ( ) => expect ( await rules . between ( { value : '4' } , 3 , 10 , 'value' ) ) . toBe ( true ) )
it ( 'fails when forced to value' , async ( ) => expect ( await rules . between ( { value : 442 } , 3 , 10 , 'value' ) ) . toBe ( false ) )
it ( 'passes when forced to length' , async ( ) => expect ( await rules . between ( { value : 7442 } , 3 , 10 , 'length' ) ) . toBe ( true ) )
it ( 'fails when forced to length' , async ( ) => expect ( await rules . between ( { value : 6 } , 3 , 10 , 'length' ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
} )
2020-03-03 14:53:04 +03:00
/ * *
* Confirm
* /
describe ( 'confirm' , ( ) => {
it ( 'passes when the values are the same strings' , async ( ) => expect ( await rules . confirm (
{ value : 'abc' , name : 'password' , getFormValues : ( ) => ( { password _confirm : 'abc' } ) }
) ) . toBe ( true ) )
it ( 'passes when the values are the same integers' , async ( ) => expect ( await rules . confirm (
{ value : 4422132 , name : 'xyz' , getFormValues : ( ) => ( { xyz _confirm : 4422132 } ) }
) ) . toBe ( true ) )
it ( 'passes when using a custom field' , async ( ) => expect ( await rules . confirm (
{ value : 4422132 , name : 'name' , getFormValues : ( ) => ( { other _field : 4422132 } ) } ,
'other_field'
) ) . toBe ( true ) )
it ( 'passes when using a field ends in _confirm' , async ( ) => expect ( await rules . confirm (
{ value : '$ecret' , name : 'password_confirm' , getFormValues : ( ) => ( { password : '$ecret' } ) }
) ) . toBe ( true ) )
it ( 'fails when using different strings' , async ( ) => expect ( await rules . confirm (
{ value : 'Justin' , name : 'name' , getFormValues : ( ) => ( { name _confirm : 'Daniel' } ) } ,
) ) . toBe ( false ) )
it ( 'fails when the types are different' , async ( ) => expect ( await rules . confirm (
{ value : '1234' , name : 'num' , getFormValues : ( ) => ( { num _confirm : 1234 } ) } ,
) ) . toBe ( false ) )
} )
/ * *
* Determines if the string is a date
* /
describe ( 'date' , ( ) => {
it ( 'passes with month day year' , async ( ) => expect ( await rules . date ( { value : 'December 17, 2020' } ) ) . toBe ( true ) )
it ( 'passes with month day' , async ( ) => expect ( await rules . date ( { value : 'December 17' } ) ) . toBe ( true ) )
it ( 'passes with short month day' , async ( ) => expect ( await rules . date ( { value : 'Dec 17' } ) ) . toBe ( true ) )
it ( 'passes with short month day' , async ( ) => expect ( await rules . date ( { value : 'Dec 17 12:34:15' } ) ) . toBe ( true ) )
it ( 'passes with out of bounds number' , async ( ) => expect ( await rules . date ( { value : 'January 77' } ) ) . toBe ( true ) )
it ( 'passes with only month' , async ( ) => expect ( await rules . date ( { value : 'January' } ) ) . toBe ( false ) )
it ( 'passes with valid date format' , async ( ) => expect ( await rules . date ( { value : '12/17/1987' } , 'MM/DD/YYYY' ) ) . toBe ( true ) )
it ( 'fails with simple number and date format' , async ( ) => expect ( await rules . date ( { value : '1234' } , 'MM/DD/YYYY' ) ) . toBe ( false ) )
it ( 'fails with only day of week' , async ( ) => expect ( await rules . date ( { value : 'saturday' } ) ) . toBe ( false ) )
it ( 'fails with random string' , async ( ) => expect ( await rules . date ( { value : 'Pepsi 17' } ) ) . toBe ( false ) )
it ( 'fails with random number' , async ( ) => expect ( await rules . date ( { value : '1872301237' } ) ) . toBe ( false ) )
} )
2019-11-08 01:03:34 +03:00
/ * *
* Checks if email .
*
* Note : testing is light , regular expression used is here : http : //jsfiddle.net/ghvj4gy9/embedded/result,js/
* /
describe ( 'email' , ( ) => {
2020-02-26 01:32:40 +03:00
it ( 'passes normal email' , async ( ) => expect ( await rules . email ( { value : 'dev+123@wearebraid.com' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes numeric email' , async ( ) => expect ( await rules . email ( { value : '12345@google.com' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes unicode email' , async ( ) => expect ( await rules . email ( { value : 'àlphä@❤️.ly' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes numeric with new tld' , async ( ) => expect ( await rules . email ( { value : '12345@google.photography' } ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails string without tld' , async ( ) => expect ( await rules . email ( { value : '12345@localhost' } ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails string without invalid name' , async ( ) => expect ( await rules . email ( { value : '1*(123)2345@localhost' } ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
} )
2020-03-03 14:53:04 +03:00
/ * *
* Checks if value ends with a one of the specified Strings .
* /
describe ( 'endsWith' , ( ) => {
it ( 'fails when value ending is not in stack of single value' , async ( ) => {
expect ( await rules . endsWith ( { value : 'andrew@wearebraid.com' } , '@gmail.com' ) ) . toBe ( false )
} )
it ( 'fails when value ending is not in stack of multiple values' , async ( ) => {
expect ( await rules . endsWith ( { value : 'andrew@wearebraid.com' } , '@gmail.com' , '@yahoo.com' ) ) . toBe ( false )
} )
2020-03-04 21:45:37 +03:00
it ( 'fails when passed value is not a string' , async ( ) => {
expect ( await rules . endsWith ( { value : 'andrew@wearebraid.com' } , [ '@gmail.com' , '@wearebraid.com' ] ) ) . toBe ( false )
} )
it ( 'fails when passed value is not a string' , async ( ) => {
expect ( await rules . endsWith ( { value : 'andrew@wearebraid.com' } , { value : '@wearebraid.com' } ) ) . toBe ( false )
} )
it ( 'passes when a string value is present and matched even if non-string values also exist as arguments' , async ( ) => {
expect ( await rules . endsWith ( { value : 'andrew@wearebraid.com' } , { value : 'bad data' } , [ 'no bueno' ] , '@wearebraid.com' ) ) . toBe ( true )
} )
2020-03-03 14:53:04 +03:00
it ( 'passes when stack consists of zero values' , async ( ) => {
expect ( await rules . endsWith ( { value : 'andrew@wearebraid.com' } ) ) . toBe ( true )
} )
it ( 'passes when value ending is in stack of single value' , async ( ) => {
expect ( await rules . endsWith ( { value : 'andrew@wearebraid.com' } , '@wearebraid.com' ) ) . toBe ( true )
} )
it ( 'passes when value ending is in stack of multiple values' , async ( ) => {
expect ( await rules . endsWith ( { value : 'andrew@wearebraid.com' } , '@yahoo.com' , '@wearebraid.com' , '@gmail.com' ) ) . toBe ( true )
} )
} )
/ * *
* In rule
* /
describe ( 'in' , ( ) => {
it ( 'fails when not in stack' , async ( ) => {
expect ( await rules . in ( { value : 'third' } , 'first' , 'second' ) ) . toBe ( false )
} )
it ( 'fails when case sensitive mismatch is in stack' , async ( ) => {
expect ( await rules . in ( { value : 'third' } , 'first' , 'second' , 'Third' ) ) . toBe ( false )
} )
it ( 'fails comparing dissimilar objects' , async ( ) => {
expect ( await rules . in ( { value : { f : 'abc' } } , { a : 'cdf' } , { b : 'abc' } ) ) . toBe ( false )
} )
it ( 'passes when case sensitive match is in stack' , async ( ) => {
expect ( await rules . in ( { value : 'third' } , 'first' , 'second' , 'third' ) ) . toBe ( true )
} )
it ( 'passes a shallow array compare' , async ( ) => {
expect ( await rules . in ( { value : [ 'abc' ] } , [ 'cdf' ] , [ 'abc' ] ) ) . toBe ( true )
} )
it ( 'passes a shallow object compare' , async ( ) => {
expect ( await rules . in ( { value : { f : 'abc' } } , { a : 'cdf' } , { f : 'abc' } , ) ) . toBe ( true )
} )
} )
/ * *
* Matches rule
* /
describe ( 'matches' , ( ) => {
it ( 'simple strings fail if they aren’ t equal' , async ( ) => {
expect ( await rules . matches ( { value : 'third' } , 'first' ) ) . toBe ( false )
} )
it ( 'fails on non matching regex' , async ( ) => {
expect ( await rules . matches ( { value : 'third' } , /^thirds/ ) ) . toBe ( false )
} )
it ( 'passes if simple strings match' , async ( ) => {
expect ( await rules . matches ( { value : 'second' } , 'third' , 'second' ) ) . toBe ( true )
} )
it ( 'passes on matching regex' , async ( ) => {
expect ( await rules . matches ( { value : 'third' } , /^third/ ) ) . toBe ( true )
} )
it ( 'passes on matching mixed regex and string' , async ( ) => {
expect ( await rules . matches ( { value : 'first-fourth' } , 'second' , /^third/ , /fourth$/ ) ) . toBe ( true )
2020-05-15 21:08:58 +03:00
} )
it ( 'fails on a regular expression encoded as a string' , async ( ) => {
expect ( await rules . matches ( { value : 'mypassword' } , '/[0-9]/' ) ) . toBe ( false )
} )
it ( 'passes on a regular expression encoded as a string' , async ( ) => {
expect ( await rules . matches ( { value : 'mypa55word' } , '/[0-9]/' ) ) . toBe ( true )
} )
it ( 'passes on a regular expression containing slashes' , async ( ) => {
expect ( await rules . matches ( { value : 'https://' } , '/https?:///' ) ) . toBe ( true )
2020-03-03 14:53:04 +03:00
} )
} )
2019-11-08 01:03:34 +03:00
/ * *
* Mime types .
* /
describe ( 'mime' , ( ) => {
2019-11-19 15:15:13 +03:00
it ( 'passes basic image/jpeg stack' , async ( ) => {
const fileUpload = new FileUpload ( {
files : [ { type : 'image/jpeg' } ]
} )
2020-02-26 01:32:40 +03:00
expect ( await rules . mime ( { value : fileUpload } , 'image/png' , 'image/jpeg' ) ) . toBe ( true )
2019-11-19 15:15:13 +03:00
} )
it ( 'passes when match is at begining of stack' , async ( ) => {
const fileUpload = new FileUpload ( {
files : [ { type : 'document/pdf' } ]
} )
2020-02-26 01:32:40 +03:00
expect ( await rules . mime ( { value : fileUpload } , 'document/pdf' ) ) . toBe ( true )
2019-11-19 15:15:13 +03:00
} )
2019-11-08 01:03:34 +03:00
2019-11-19 15:15:13 +03:00
it ( 'fails when not in stack' , async ( ) => {
const fileUpload = new FileUpload ( {
files : [ { type : 'application/json' } ]
} )
2020-02-26 01:32:40 +03:00
expect ( await rules . mime ( { value : fileUpload } , 'image/png' , 'image/jpeg' ) ) . toBe ( false )
2019-11-19 15:15:13 +03:00
} )
2019-11-08 01:03:34 +03:00
} )
/ * *
* Minimum .
* /
describe ( 'min' , ( ) => {
2020-02-26 01:32:40 +03:00
it ( 'passes when a number string' , async ( ) => expect ( await rules . min ( { value : '5' } , '5' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes when a number' , async ( ) => expect ( await rules . min ( { value : 6 } , 5 ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes when a string length' , async ( ) => expect ( await rules . min ( { value : 'foobar' } , '6' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes when a array length' , async ( ) => expect ( await rules . min ( { value : Array ( 6 ) } , '6' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes when string is forced to value' , async ( ) => expect ( await rules . min ( { value : 'bcd' } , 'aaa' , 'value' ) ) . toBe ( true ) )
2020-01-28 20:12:08 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when string is forced to lesser value' , async ( ) => expect ( await rules . min ( { value : 'a' } , 'b' , 'value' ) ) . toBe ( false ) )
2020-01-28 20:12:08 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes when a number is forced to length' , async ( ) => expect ( await rules . min ( { value : '000' } , 3 , 'length' ) ) . toBe ( true ) )
2020-01-28 20:12:08 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when a number is forced to length' , async ( ) => expect ( await rules . min ( { value : '44' } , 3 , 'length' ) ) . toBe ( false ) )
2020-01-28 20:12:08 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when a array length' , async ( ) => expect ( await rules . min ( { value : Array ( 6 ) } , '7' ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when a string length' , async ( ) => expect ( await rules . min ( { value : 'bar' } , 4 ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when a number' , async ( ) => expect ( await rules . min ( { value : 3 } , '7' ) ) . toBe ( false ) )
2020-01-28 20:12:08 +03:00
2019-11-08 01:03:34 +03:00
} )
/ * *
* Maximum .
* /
describe ( 'max' , ( ) => {
2020-02-26 01:32:40 +03:00
it ( 'passes when a number string' , async ( ) => expect ( await rules . max ( { value : '5' } , '5' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes when a number' , async ( ) => expect ( await rules . max ( { value : 5 } , 6 ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes when a string length' , async ( ) => expect ( await rules . max ( { value : 'foobar' } , '6' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes when a array length' , async ( ) => expect ( await rules . max ( { value : Array ( 6 ) } , '6' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes when forced to validate on length' , async ( ) => expect ( await rules . max ( { value : 10 } , 3 , 'length' ) ) . toBe ( true ) )
2019-11-15 22:44:01 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes when forced to validate string on value' , async ( ) => expect ( await rules . max ( { value : 'b' } , 'e' , 'value' ) ) . toBe ( true ) )
2019-11-15 22:44:01 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when a array length' , async ( ) => expect ( await rules . max ( { value : Array ( 6 ) } , '5' ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when a string length' , async ( ) => expect ( await rules . max ( { value : 'bar' } , 2 ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when a number' , async ( ) => expect ( await rules . max ( { value : 10 } , '7' ) ) . toBe ( false ) )
2019-11-15 22:44:01 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when a number' , async ( ) => expect ( await rules . max ( { value : 10 } , '7' ) ) . toBe ( false ) )
2019-11-15 22:44:01 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when forced to validate on length' , async ( ) => expect ( await rules . max ( { value : - 10 } , '1' , 'length' ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
} )
/ * *
* Maximum .
* /
describe ( 'not' , ( ) => {
2020-02-26 01:32:40 +03:00
it ( 'passes when a number string' , async ( ) => expect ( await rules . not ( { value : '5' } , '6' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes when a number' , async ( ) => expect ( await rules . not ( { value : 1 } , 30 ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'passes when a string' , async ( ) => expect ( await rules . not ( { value : 'abc' } , 'def' ) ) . toBe ( true ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when a shallow equal array' , async ( ) => expect ( await rules . not ( { value : [ 'abc' ] } , [ 'abc' ] ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when a shallow equal object' , async ( ) => expect ( await rules . not ( { value : { a : 'abc' } } , [ '123' ] , { a : 'abc' } ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
2020-02-26 01:32:40 +03:00
it ( 'fails when string is in stack' , async ( ) => expect ( await rules . not ( { value : 'a' } , 'b' , 'c' , 'd' , 'a' , 'f' ) ) . toBe ( false ) )
} )
/ * *
2020-03-03 14:53:04 +03:00
* Checks if a date is after another date
2020-02-26 01:32:40 +03:00
* /
2020-03-03 14:53:04 +03:00
describe ( 'number' , ( ) => {
it ( 'passes with simple number string' , async ( ) => expect ( await rules . number ( { value : '123' } ) ) . toBe ( true ) )
2020-02-26 01:32:40 +03:00
2020-03-03 14:53:04 +03:00
it ( 'passes with simple number' , async ( ) => expect ( await rules . number ( { value : 19832461234 } ) ) . toBe ( true ) )
2020-02-26 01:32:40 +03:00
2020-03-03 14:53:04 +03:00
it ( 'passes with float' , async ( ) => expect ( await rules . number ( { value : 198.32464 } ) ) . toBe ( true ) )
2020-02-26 01:32:40 +03:00
2020-03-03 14:53:04 +03:00
it ( 'passes with decimal in string' , async ( ) => expect ( await rules . number ( { value : '567.23' } ) ) . toBe ( true ) )
2020-02-26 01:32:40 +03:00
2020-03-03 14:53:04 +03:00
it ( 'fails with comma in number string' , async ( ) => expect ( await rules . number ( { value : '123,456' } ) ) . toBe ( false ) )
2020-02-26 01:32:40 +03:00
2020-03-03 14:53:04 +03:00
it ( 'fails with alpha' , async ( ) => expect ( await rules . number ( { value : '123sdf' } ) ) . toBe ( false ) )
} )
/ * *
* Required rule
* /
describe ( 'required' , ( ) => {
it ( 'fails on empty string' , async ( ) => expect ( await rules . required ( { value : '' } ) ) . toBe ( false ) )
it ( 'fails on empty array' , async ( ) => expect ( await rules . required ( { value : [ ] } ) ) . toBe ( false ) )
it ( 'fails on empty object' , async ( ) => expect ( await rules . required ( { value : { } } ) ) . toBe ( false ) )
it ( 'fails on null' , async ( ) => expect ( await rules . required ( { value : null } ) ) . toBe ( false ) )
it ( 'passes with the number zero' , async ( ) => expect ( await rules . required ( { value : 0 } ) ) . toBe ( true ) )
it ( 'passes with the boolean false' , async ( ) => expect ( await rules . required ( { value : false } ) ) . toBe ( true ) )
it ( 'passes with a non empty array' , async ( ) => expect ( await rules . required ( { value : [ '123' ] } ) ) . toBe ( true ) )
it ( 'passes with a non empty object' , async ( ) => expect ( await rules . required ( { value : { a : 'b' } } ) ) . toBe ( true ) )
it ( 'passes with empty value if second argument is false' , async ( ) => expect ( await rules . required ( { value : '' } , false ) ) . toBe ( true ) )
it ( 'passes with empty value if second argument is false string' , async ( ) => expect ( await rules . required ( { value : '' } , 'false' ) ) . toBe ( true ) )
it ( 'passes with FileUpload' , async ( ) => expect ( await rules . required ( { value : new FileUpload ( { files : [ { name : 'j.png' } ] } ) } ) ) . toBe ( true ) )
it ( 'fails with empty FileUpload' , async ( ) => expect ( await rules . required ( { value : new FileUpload ( { files : [ ] } ) } ) ) . toBe ( false ) )
} )
/ * *
* Checks if value starts with a one of the specified Strings .
* /
describe ( 'startsWith' , ( ) => {
it ( 'fails when value starting is not in stack of single value' , async ( ) => {
expect ( await rules . startsWith ( { value : 'taco tuesday' } , 'pizza' ) ) . toBe ( false )
} )
it ( 'fails when value starting is not in stack of multiple values' , async ( ) => {
expect ( await rules . startsWith ( { value : 'taco tuesday' } , 'pizza' , 'coffee' ) ) . toBe ( false )
} )
2020-03-04 21:45:37 +03:00
it ( 'fails when passed value is not a string' , async ( ) => {
expect ( await rules . startsWith ( { value : 'taco tuesday' } , [ 'taco' , 'pizza' ] ) ) . toBe ( false )
} )
it ( 'fails when passed value is not a string' , async ( ) => {
expect ( await rules . startsWith ( { value : 'taco tuesday' } , { value : 'taco' } ) ) . toBe ( false )
} )
it ( 'passes when a string value is present and matched even if non-string values also exist as arguments' , async ( ) => {
expect ( await rules . startsWith ( { value : 'taco tuesday' } , { value : 'taco' } , [ 'taco' ] , 'taco' ) ) . toBe ( true )
} )
2020-03-03 14:53:04 +03:00
it ( 'passes when stack consists of zero values' , async ( ) => {
expect ( await rules . startsWith ( { value : 'taco tuesday' } ) ) . toBe ( true )
} )
it ( 'passes when value starting is in stack of single value' , async ( ) => {
expect ( await rules . startsWith ( { value : 'taco tuesday' } , 'taco' ) ) . toBe ( true )
} )
it ( 'passes when value starting is in stack of multiple values' , async ( ) => {
expect ( await rules . startsWith ( { value : 'taco tuesday' } , 'pizza' , 'taco' , 'coffee' ) ) . toBe ( true )
} )
} )
/ * *
* Url rule .
*
* Note : these are just sanity checks because the actual package we use is
* well tested : https : //github.com/segmentio/is-url/blob/master/test/index.js
* /
describe ( 'url' , ( ) => {
it ( 'passes with http://google.com' , async ( ) => expect ( await rules . url ( { value : 'http://google.com' } ) ) . toBe ( true ) )
it ( 'fails with google.com' , async ( ) => expect ( await rules . url ( { value : 'google.com' } ) ) . toBe ( false ) )
2019-11-08 01:03:34 +03:00
} )