|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | + |
| 7 | +const { Assert: BaseAssert, Violation } = require('validator.js'); |
| 8 | +const { describe, it } = require('node:test'); |
| 9 | +const NotPoBoxAssert = require('../../src/asserts/not-po-box-assert.js'); |
| 10 | + |
| 11 | +/** |
| 12 | + * Extend `Assert` with `NotPoBox`. |
| 13 | + */ |
| 14 | + |
| 15 | +const Assert = BaseAssert.extend({ |
| 16 | + NotPoBox: NotPoBoxAssert |
| 17 | +}); |
| 18 | + |
| 19 | +/** |
| 20 | + * Instances. |
| 21 | + */ |
| 22 | + |
| 23 | +const addresses = { |
| 24 | + allowed: [ |
| 25 | + `123 Boxing Street`, |
| 26 | + `34 Box Handler Road`, |
| 27 | + `424 Po Dance drive`, |
| 28 | + `234 P.O.D. Wasn't Terrible Circle`, |
| 29 | + `345 Box St, Main St`, |
| 30 | + `345 Po St, Main St`, |
| 31 | + `POSTAL NOTHING 234 Main St`, |
| 32 | + `p.o.BOX ABC, Main St`, |
| 33 | + `Cuerpo 2` |
| 34 | + ], |
| 35 | + disallowed: [ |
| 36 | + `P.O.BOX 234 Main St`, |
| 37 | + `P.O. BOX 234 Main St`, |
| 38 | + `Main St, P.O.BOX 234`, |
| 39 | + `POST OFFICE BOX 234 Main St`, |
| 40 | + `PO BOX 234 Main St`, |
| 41 | + `po box 234 Main St`, |
| 42 | + `po Box 234, 453 Main St`, |
| 43 | + `Postal Office BOX 234, Main St`, |
| 44 | + `POST BOX 234, Main St`, |
| 45 | + `POST BOX NO 234, Main St`, |
| 46 | + `POSTAL BOX 234, Main St`, |
| 47 | + `PO 234, 345 Main St`, |
| 48 | + `Postfach 8 15`, |
| 49 | + `Postbus 90222`, |
| 50 | + `Postboks 447 Sentrum`, |
| 51 | + `BP 13210`, |
| 52 | + `Apartado 0832` |
| 53 | + ] |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * Test `NotPoBoxAssert`. |
| 58 | + */ |
| 59 | + |
| 60 | +describe('NotPoBoxAssert', () => { |
| 61 | + it('should throw an error if value is not provided', ({ assert }) => { |
| 62 | + try { |
| 63 | + Assert.notPoBox().validate(); |
| 64 | + |
| 65 | + assert.fail(); |
| 66 | + } catch (e) { |
| 67 | + assert.ok(e instanceof Violation); |
| 68 | + assert.equal(e.show().assert, 'NotPoBox'); |
| 69 | + assert.equal(e.show().violation.value, 'must_be_a_string'); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + addresses.disallowed.forEach(address => { |
| 74 | + it(`should throw an error for address \`${address}\``, ({ assert }) => { |
| 75 | + try { |
| 76 | + Assert.notPoBox().validate(address); |
| 77 | + assert.fail(); |
| 78 | + } catch (e) { |
| 79 | + assert.ok(e instanceof Violation); |
| 80 | + assert.equal(e.show().assert, 'NotPoBox'); |
| 81 | + assert.equal(e.show().value, address); |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + addresses.allowed.forEach(address => { |
| 87 | + it(`should accept address \`${address}\``, ({ assert }) => { |
| 88 | + assert.doesNotThrow(() => { |
| 89 | + Assert.notPoBox().validate(address); |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments