Icontem

File: test/rules/dateAfterToday.js

Recommend this page to a friend!
  Classes of Harcharan Singh  >  Node Input Validator  >  test/rules/dateAfterToday.js  >  Download  
File: test/rules/dateAfterToday.js
Role: Example script
Content type: text/plain
Description: Example script
Class: Node Input Validator
Validate submitted input values in Node.js
Author: By
Last change: nxt
Date: 9 months ago
Size: 1,564 bytes
 

Contents

Class file image Download
const assert = require('assert');

const moment = require('moment');
const { Validator } = require('../../lib/index');

describe('#dateAfterToday', () => {
  it('should pass with valida date', async () => {
    const v = new Validator(
      {
        dob: moment().add(2, 'days').format('YYYY-MM-DD'),
      }, {
        dob: 'required|dateFormat:YYYY-MM-DD|dateAfterToday:1,days',
      },
    );

    const matched = await v.check();
    assert.equal(matched, true);
  });

  it('should pass with optional seed', async () => {
    const v = new Validator(
      {
        dob: moment().add(2, 'days').format('YYYY-MM-DD'),
      }, {
        dob: 'required|dateFormat:YYYY-MM-DD|dateAfterToday:1',
      },
    );

    const matched = await v.check();
    assert.equal(matched, true);
  });


  it('should fail', async () => {
    const v = new Validator(
      {
        dob: '2019-02-28',
      }, {
        dob: 'required|dateFormat:YYYY-MM-DD|dateAfterToday:2,days',
      },
    );

    const matched = await v.check();

    assert.equal(matched, false);
  });

  it('message should exist', async () => {
    const v = new Validator(
      {
        dob: '2019-02-28',
      }, {
        dob: 'required|dateFormat:YYYY-MM-DD|dateAfterToday:2,days',
      },
    );

    const matched = await v.check();

    assert.equal(matched, false);
    assert.equal(
      v.errors.dob.message,
      v.getExistinParsedMessage({
        rule: 'dateAfterToday',
        value: '2019-02-28',
        attr: 'dob',
        args: ['2', 'days'],
      }),
    );
  });
});