Icontem

File: test/rules/size.js

Recommend this page to a friend!
  Classes of Harcharan Singh  >  Node Input Validator  >  test/rules/size.js  >  Download  
File: test/rules/size.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: 2,926 bytes
 

Contents

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


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

describe('size', () => {
  it('should pass with buffer', async () => {
    const v = new Validator(
      { file: fs.readFileSync('./test/stubs/file-small.png') }, { file: 'size:4kb' },
    );


    const matched = await v.check();

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

  it('should pass with buffer using min and max seed', async () => {
    const v = new Validator(
      { file: fs.readFileSync('./test/stubs/file-small.png') }, { file: 'size:4kb,2kb' },
    );


    const matched = await v.check();

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

  it('should fail min size', async () => {
    const v = new Validator(
      { file: fs.readFileSync('./test/stubs/file-small.png') }, { file: 'size:5kb,4kb' },
    );


    const matched = await v.check();

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

  it('should fail max size', async () => {
    const v = new Validator(
      { file: fs.readFileSync('./test/stubs/file-small.png') }, { file: 'size:,4kb' },
    );


    const matched = await v.check();

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

  it('should pass with path', async () => {
    const v = new Validator(
      { file: { path: './test/stubs/file-small.png' } }, { file: 'size:4kb' },
    );


    const matched = await v.check();

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

  it('should fails', async () => {
    const v = new Validator(
      { file: { buffer: fs.readFileSync('./test/stubs/file-small.png') } }, { file: 'size:1kb' },
    );


    const matched = await v.check();

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

  it('should faile with path', async () => {
    const v = new Validator(
      { file: './test/stubs/file-small.png' }, { file: 'size:1kb' }, { 'file.size': 'Max 1kb size is allowed.' },
    );

    const matched = await v.check();

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

  it('should faile with path', async () => {
    const v = new Validator(
      { file: { size: 2300 } }, { file: 'size:5kb,4kb' },
    );

    const matched = await v.check();

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

  it('should throw exception invalid context', async () => {
    try {
      const v = new Validator(
        { file: { ok: true } }, { file: 'size:1kb' },
      );

      await v.check();
      throw new Error('Invalid context.');
    } catch (e) {
      assert.equal(e, 'Error: Size rule only accepts Buffer,file path or size property in file object.');
    }
  });

  it('message should exist', async () => {
    const v = new Validator(
      { file: './test/stubs/file-small.png' }, { file: 'size:1kb' },
    );


    const matched = await v.check();

    assert.equal(matched, false);
    assert.equal(
      v.errors.file.message,
      v.getExistinParsedMessage({
        rule: 'size',
        value: '',
        attr: 'file',
        args: ['1kb'],
      }),
    );
  });
});