|
| 1 | + |
| 2 | +/** |
| 3 | + * Module dependencies. |
| 4 | + */ |
| 5 | + |
| 6 | +var Assert = require('validator.js').Assert; |
| 7 | +var Validator = require('validator.js').Validator; |
| 8 | +var Violation = require('validator.js').Violation; |
| 9 | +var assert = require('../../lib/asserts/country-assert'); |
| 10 | +var should = require('should'); |
| 11 | + |
| 12 | +/** |
| 13 | + * Test `CountryAssert`. |
| 14 | + */ |
| 15 | + |
| 16 | +describe('CountryAssert', function() { |
| 17 | + before(function() { |
| 18 | + Assert.prototype.Country = assert; |
| 19 | + }); |
| 20 | + |
| 21 | + it('should throw an error if the input value is not a string', function() { |
| 22 | + var choices = [[], {}, 123]; |
| 23 | + |
| 24 | + choices.forEach(function(choice) { |
| 25 | + try { |
| 26 | + new Assert().Country().validate(choice); |
| 27 | + |
| 28 | + should.fail(); |
| 29 | + } catch (e) { |
| 30 | + e.should.be.instanceOf(Violation); |
| 31 | + /* jshint camelcase: false */ |
| 32 | + e.violation.value.should.equal(Validator.errorCode.must_be_a_string); |
| 33 | + /* jshint camelcase: true */ |
| 34 | + } |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should throw an error if country is invalid', function() { |
| 39 | + try { |
| 40 | + new Assert().Country().validate('FOO'); |
| 41 | + |
| 42 | + should.fail(); |
| 43 | + } catch (e) { |
| 44 | + e.should.be.instanceOf(Violation); |
| 45 | + e.value.should.equal('FOO'); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + it('should expose `assert` equal to `Country`', function() { |
| 50 | + try { |
| 51 | + new Assert().Country().validate([]); |
| 52 | + |
| 53 | + should.fail(); |
| 54 | + } catch(e) { |
| 55 | + e.show().assert.should.equal('Country'); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + it('should accept an `alpha-3` code', function() { |
| 60 | + new Assert().Country().validate('PRT'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should accept an `alpha-3` code belonging to a division', function() { |
| 64 | + new Assert().Country().validate('SHN'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should accept an `alpha-2` code', function() { |
| 68 | + new Assert().Country().validate('PT'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should accept an `alpha-2` code belonging to a division', function() { |
| 72 | + new Assert().Country().validate('BQ'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should accept a country `official` name', function() { |
| 76 | + new Assert().Country().validate('Portuguese Republic'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should accept a country `common` name', function() { |
| 80 | + new Assert().Country().validate('Portugal'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should accept a country `altSpelling` name', function() { |
| 84 | + new Assert().Country().validate('República Portuguesa'); |
| 85 | + }); |
| 86 | +}); |
0 commit comments