all files / src/__tests__/ url.spec.js

100% Statements 35/35
100% Branches 4/4
100% Functions 7/7
100% Lines 32/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41     20×                
import assert from 'assert'
import { url } from '../index'
import getErrorId from './helper'
 
const ERROR_ID = 'form.errors.url'
 
function test (value, params) {
  return getErrorId(url(params)(value))
}
 
describe('Validator: url', function() {
  it('should be invalid when `value` is not a valid url', function() {
    assert.equal(ERROR_ID, test(''))
    assert.equal(ERROR_ID, test('http:/:/'))
    assert.equal(ERROR_ID, test('htpp:/www.google.com'))
    assert.equal(ERROR_ID, test('ftp//ft.com'))
  })
  it('should be valid when `value` is a valid url', function() {
    assert.ok(!test('http://google.com'))
    assert.ok(!test('https://www.google.com'))
    assert.ok(!test('ftp://foo:bar@128.193.1.32:3000/foo?foo=bar'))
  })
  it('should be invalid if the protocols don\'t match', function() {
    assert.equal(ERROR_ID, test('http://google.com', { protocol: 'ftp' }))
    assert.equal(ERROR_ID, test('http://google.com', { protocol: ['ftp', 'https'] }))
    assert.equal(ERROR_ID, test('http://google.com', { protocols: 'https' }))
    assert.equal(ERROR_ID, test('http://google.com', { protocols: ['ftp', 'https'] }))
    assert.equal(ERROR_ID, test('https://www.google.com', { protocol: 'ftp' }))
    assert.equal(ERROR_ID, test('ftp://foo:bar@128.193.1.32:3000/foo?foo=bar', { protocol: 'http' }))
  })
  it('should only be valid with certain protocols', function() {
    assert.ok(!test('http://google.com', { protocol: 'http' }))
    assert.ok(!test('http://google.com', { protocol: ['http', 'https'] }))
    assert.ok(!test('http://google.com', { protocol: ['ftp', 'http', 'https'] }))
    assert.ok(!test('http://google.com', { protocols: 'http' }))
    assert.ok(!test('http://google.com', { protocols: ['ftp', 'http', 'https'] }))
    assert.ok(!test('https://www.google.com', { protocol: 'https' }))
    assert.ok(!test('ftp://foo:bar@128.193.1.32:3000/foo?foo=bar', { protocol: 'ftp' }))
  })
})