2018-01-31 01:21:21 +03:00
|
|
|
import test from 'ava'
|
|
|
|
import f from '../dist'
|
|
|
|
|
|
|
|
const rules = f.rules
|
2018-02-01 18:47:11 +03:00
|
|
|
const error = ({field, value, label}) => {
|
2018-01-31 01:21:21 +03:00
|
|
|
return `${field}${label}`
|
|
|
|
}
|
|
|
|
|
|
|
|
test('test required rule failure', async t => {
|
2018-02-01 18:47:11 +03:00
|
|
|
let v = await rules.required({field: 'name', value: '', error, label: 'xyz'})
|
2018-01-31 01:21:21 +03:00
|
|
|
t.is('string', typeof v)
|
|
|
|
t.is('namexyz', v)
|
|
|
|
})
|
|
|
|
|
2018-11-20 19:51:32 +03:00
|
|
|
test('test number no failure on empty', async t => {
|
|
|
|
let v = await rules.number({field: 'name', value: '', error, label: 'xyz'})
|
|
|
|
t.is(false, v)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('test number failure not number', async t => {
|
|
|
|
let v = await rules.number({field: 'name', value: 't', error, label: 'xyz'})
|
|
|
|
t.is('string', typeof v)
|
|
|
|
t.is('namexyz', v)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('test number is typeof number', async t => {
|
|
|
|
let v = await rules.number({field: 'name', value: '3', error, label: 'xyz'})
|
|
|
|
t.is(false, v)
|
|
|
|
})
|
|
|
|
|
2018-02-01 01:20:29 +03:00
|
|
|
test('test required rule empty array failure', async t => {
|
2018-02-01 18:47:11 +03:00
|
|
|
let v = await rules.required({field: 'name', value: [], error, label: 'xyz'})
|
2018-02-01 01:20:29 +03:00
|
|
|
t.is('namexyz', v)
|
|
|
|
})
|
|
|
|
|
2018-01-31 01:21:21 +03:00
|
|
|
test('test required rule passes', async t => {
|
|
|
|
t.is(false, await rules.required({field: 'name', value: 'Justin'}))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('test email rule with valid email', async t => {
|
|
|
|
t.is(false, await rules.email({field: 'email', value: 'valid@example.com'}))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('test email rule with invalid email', async t => {
|
2018-02-01 18:47:11 +03:00
|
|
|
t.is('email123', await rules.email({field: 'email', value: 'invalid@example', error, label: '123'}))
|
2018-01-31 01:21:21 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
test('test email with empty email', async t => {
|
|
|
|
t.is(false, await rules.email({field: 'email', value: '', error}))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('test confirmed passes', async t => {
|
|
|
|
t.is(false, await rules.confirmed({
|
|
|
|
field: 'password',
|
|
|
|
value: 'password',
|
2018-02-01 18:47:11 +03:00
|
|
|
label: '123',
|
2018-01-31 01:21:21 +03:00
|
|
|
error,
|
|
|
|
values: {password_confirmation: 'password'}
|
2018-02-01 18:47:11 +03:00
|
|
|
}))
|
2018-01-31 01:21:21 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
test('test confirmed passes custom field', async t => {
|
|
|
|
t.is(false, await rules.confirmed({
|
|
|
|
field: 'password',
|
|
|
|
value: 'password',
|
2018-02-01 18:47:11 +03:00
|
|
|
label: '123',
|
2018-01-31 01:21:21 +03:00
|
|
|
error,
|
|
|
|
values: {customfield: 'password'}
|
2018-02-01 18:47:11 +03:00
|
|
|
}, 'customfield'))
|
2018-01-31 01:21:21 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
test('test confirmed fails', async t => {
|
|
|
|
t.is('password123', await rules.confirmed({
|
|
|
|
field: 'password',
|
|
|
|
value: 'password',
|
2018-02-01 18:47:11 +03:00
|
|
|
label: '123',
|
2018-01-31 01:21:21 +03:00
|
|
|
error,
|
|
|
|
values: {password_confirmation: 'pAssword'}
|
2018-02-01 18:47:11 +03:00
|
|
|
}))
|
2018-01-31 01:21:21 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
test('test empty confirmed passes', async t => {
|
|
|
|
t.is(false, await rules.confirmed({
|
|
|
|
field: 'password',
|
|
|
|
value: '',
|
2018-02-01 18:47:11 +03:00
|
|
|
label: '123',
|
2018-01-31 01:21:21 +03:00
|
|
|
error,
|
|
|
|
values: {password_confirmation: ''}
|
2018-02-01 18:47:11 +03:00
|
|
|
}))
|
2018-01-31 01:21:21 +03:00
|
|
|
})
|