|
| 1 | + |
| 2 | +/** |
| 3 | + * Module dependencies. |
| 4 | + */ |
| 5 | + |
| 6 | +import { Assert as BaseAssert, Violation } from 'validator.js'; |
| 7 | +import TaxpayerIdentificationNumberAssert from '../../src/asserts/taxpayer-identification-number-assert'; |
| 8 | +import should from 'should'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Extend `Assert` with `TaxpayerIdentificationNumberAssert`. |
| 12 | + */ |
| 13 | + |
| 14 | +const Assert = BaseAssert.extend({ |
| 15 | + TaxpayerIdentificationNumber: TaxpayerIdentificationNumberAssert |
| 16 | +}); |
| 17 | + |
| 18 | +/** |
| 19 | + * Test `TaxpayerIdentificationNumberAssert`. |
| 20 | + */ |
| 21 | + |
| 22 | +describe('TaxpayerIdentificationNumberAssert', () => { |
| 23 | + it('should throw an error if the input value is not a string', () => { |
| 24 | + [{}, []].forEach((choice) => { |
| 25 | + try { |
| 26 | + new Assert().TaxpayerIdentificationNumber().validate(choice); |
| 27 | + |
| 28 | + should.fail(); |
| 29 | + } catch (e) { |
| 30 | + e.should.be.instanceOf(Violation); |
| 31 | + e.violation.value.should.equal('must_be_a_string'); |
| 32 | + } |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should throw an error if the input value is not a valid `tin`', () => { |
| 37 | + try { |
| 38 | + new Assert().TaxpayerIdentificationNumber().validate('foobar'); |
| 39 | + |
| 40 | + should.fail(); |
| 41 | + } catch (e) { |
| 42 | + e.should.be.instanceOf(Violation); |
| 43 | + e.show().value.should.equal('foobar'); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + it('should throw an error if the input value is not a correctly formatted `tin`', () => { |
| 48 | + try { |
| 49 | + new Assert().TaxpayerIdentificationNumber().validate('1-2-3456 789'); |
| 50 | + |
| 51 | + should.fail(); |
| 52 | + } catch (e) { |
| 53 | + e.should.be.instanceOf(Violation); |
| 54 | + e.show().value.should.equal('1-2-3456 789'); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + it('should expose `assert` equal to `TaxpayerIdentificationNumber`', () => { |
| 59 | + try { |
| 60 | + new Assert().TaxpayerIdentificationNumber().validate('1-2-3456 789'); |
| 61 | + |
| 62 | + should.fail(); |
| 63 | + } catch (e) { |
| 64 | + e.show().assert.should.equal('TaxpayerIdentificationNumber'); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + it('should accept a valid `tin`', () => { |
| 69 | + new Assert().TaxpayerIdentificationNumber().validate('123456789'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should accept a correctly formatted `tin`', () => { |
| 73 | + new Assert().TaxpayerIdentificationNumber().validate('123-45-6789'); |
| 74 | + }); |
| 75 | +}); |
0 commit comments