|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | + |
| 7 | +const { Assert: BaseAssert, Validator, Violation } = require('validator.js'); |
| 8 | +const CaZipCodeAssert = require('../../src/asserts/ca-zip-code-assert'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Extend `Assert` with `CaZipCodeAssert`. |
| 12 | + */ |
| 13 | + |
| 14 | +const Assert = BaseAssert.extend({ |
| 15 | + CaZipCode: CaZipCodeAssert |
| 16 | +}); |
| 17 | + |
| 18 | +/** |
| 19 | + * Test `CaZipCodeAssert`. |
| 20 | + */ |
| 21 | + |
| 22 | +describe('CaZipCodeAssert', () => { |
| 23 | + it('should throw an error if the input value is not a string', () => { |
| 24 | + const choices = [[], {}, 123]; |
| 25 | + |
| 26 | + choices.forEach(choice => { |
| 27 | + try { |
| 28 | + Assert.caZipCode().validate(choice); |
| 29 | + |
| 30 | + fail(); |
| 31 | + } catch (e) { |
| 32 | + expect(e).toBeInstanceOf(Violation); |
| 33 | + expect(e.violation.value).toBe(Validator.errorCode.must_be_a_string); |
| 34 | + } |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should throw an error if `value` is invalid', () => { |
| 39 | + const choices = ['#', '1A1A9B', 'A1AA1A', 'D1A9B9']; |
| 40 | + |
| 41 | + choices.forEach(choice => { |
| 42 | + try { |
| 43 | + Assert.caZipCode().validate(choice); |
| 44 | + |
| 45 | + fail(); |
| 46 | + } catch (e) { |
| 47 | + expect(e).toBeInstanceOf(Violation); |
| 48 | + expect(e.show().assert).toBe('CaZipCode'); |
| 49 | + } |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should accept a valid zip code', () => { |
| 54 | + ['A1A1A1', 'A1A9B9', 'A1A9b9', 'A1a9B9', 'C9A2M6', 'C9A 2M6', 'T7A-2D3', 'a1A9B9'].forEach(choice => { |
| 55 | + Assert.caZipCode().validate(choice); |
| 56 | + }); |
| 57 | + }); |
| 58 | +}); |
0 commit comments