|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | + |
| 7 | +const { Assert: BaseAssert, Violation } = require('validator.js'); |
| 8 | +const CpfNumberAssert = require('../../src/asserts/cpf-number-assert'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Extend `Assert` with `CpfNumberAssert`. |
| 12 | + */ |
| 13 | + |
| 14 | +const Assert = BaseAssert.extend({ |
| 15 | + CpfNumber: CpfNumberAssert |
| 16 | +}); |
| 17 | + |
| 18 | +/** |
| 19 | + * Test `CpfNumberAssert`. |
| 20 | + */ |
| 21 | + |
| 22 | +describe('CpfNumberAssert', () => { |
| 23 | + it('should throw an error if the input value is not a string', () => { |
| 24 | + try { |
| 25 | + Assert.cpfNumber().validate(); |
| 26 | + |
| 27 | + fail(); |
| 28 | + } catch (e) { |
| 29 | + expect(e).toBeInstanceOf(Violation); |
| 30 | + expect(e.show().assert).toBe('CpfNumber'); |
| 31 | + expect(e.value).toBeUndefined(); |
| 32 | + expect(e.violation.value).toBe('must_be_a_string'); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + it('should throw an error if `cpf` is invalid', () => { |
| 37 | + try { |
| 38 | + Assert.cpfNumber().validate('123'); |
| 39 | + |
| 40 | + fail(); |
| 41 | + } catch (e) { |
| 42 | + expect(e).toBeInstanceOf(Violation); |
| 43 | + expect(e.value).toBe('123'); |
| 44 | + expect(e.violation.value).toBe('must_be_a_valid_cpf_number'); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + it('should throw an error if `cpf` is invalid but correctly formatted', () => { |
| 49 | + try { |
| 50 | + Assert.cpfNumber().validate('564.232.751-31'); |
| 51 | + |
| 52 | + fail(); |
| 53 | + } catch (e) { |
| 54 | + expect(e).toBeInstanceOf(Violation); |
| 55 | + expect(e.value).toBe('564.232.751-31'); |
| 56 | + expect(e.violation.value).toBe('must_be_a_valid_cpf_number'); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + it('should accept a valid `cpf` not formatted', () => { |
| 61 | + Assert.cpfNumber().validate('43228987264'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should accept a valid `cpf` correctly formatted', () => { |
| 65 | + Assert.cpfNumber().validate('432.289.872-64'); |
| 66 | + }); |
| 67 | +}); |
0 commit comments