Adicionado validacao do cpf

This commit is contained in:
2024-05-28 20:04:17 -04:00
parent 10a916b119
commit 7ceaf76c31
27 changed files with 1227 additions and 3 deletions

View File

@@ -0,0 +1,65 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cnpj_1 = __importDefault(require("../src/validator/cnpj"));
const emptyCnpj = '';
const validCnpjMasked = '31.257.435/0001-40';
const validCnpjUnmasked = '31257435000140';
const invalidCnpjMasked = '31.257.435/0001-4';
const invalidCnpjUnmasked = '3125743500014';
describe('CNPJ Validation Test', () => {
it('Should validate a valid CNPJ number', () => {
const isValid = cnpj_1.default.validate(emptyCnpj);
expect(isValid).toBeFalsy();
});
it('Should validate a valid CNPJ masked number', () => {
const isValid = cnpj_1.default.validate(validCnpjMasked);
expect(isValid).toBeTruthy();
});
it('Should validate a valid CNPJ unmasked number', () => {
const isValid = cnpj_1.default.validate(validCnpjUnmasked);
expect(isValid).toBeTruthy();
});
it('Should validate a invalid CNPJ masked number', () => {
const isValid = cnpj_1.default.validate(invalidCnpjMasked);
expect(isValid).toBeFalsy();
});
it('Should validate a invalid CNPJ unmasked number', () => {
const isValid = cnpj_1.default.validate(invalidCnpjUnmasked);
expect(isValid).toBeFalsy();
});
});
describe('CNPJ Mask Test', () => {
it('Should have mask on a valid cnpj or return empty string', () => {
const newCnpj = cnpj_1.default.mask(validCnpjUnmasked);
expect(newCnpj).toBe(validCnpjMasked);
});
it('Should return empty string on invalid CNPJ', () => {
const newCnpj = cnpj_1.default.mask(invalidCnpjUnmasked);
expect(newCnpj).toBe('');
});
});
describe('CNPJ Unmask Test', () => {
it('Should be unmasked string on a valid cnpj or return empty string', () => {
const newCnpj = cnpj_1.default.unmask(validCnpjMasked);
expect(newCnpj).toBe(validCnpjUnmasked);
});
it('Should return empty string on invalid CNPJ', () => {
const newCnpj = cnpj_1.default.unmask(invalidCnpjUnmasked);
expect(newCnpj).toBe('');
});
});
describe('CNPJ Generator Test', () => {
it('Should generate a valid cnpj', () => {
const newCnpj = cnpj_1.default.generate();
const isValid = cnpj_1.default.validate(newCnpj);
expect(isValid).toBeTruthy();
});
it('Should generate a valid cnpj and formmat', () => {
const newCnpj = cnpj_1.default.generate({ format: true });
const isValid = cnpj_1.default.validate(newCnpj);
expect(isValid).toBeTruthy();
});
});

View File

@@ -0,0 +1,74 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cpf_1 = __importDefault(require("../src/validator/cpf"));
const emptyCpf = '';
const validCpf = '047.145.880-52';
const validOnlyNumbers = '04714588052';
const invalidCpfLength = '047.145.880-5';
const invalidCpf = '047.145.880-54';
const invalidOnlyNumbers = '04714588054';
describe('CPF Validation test', () => {
it('Correct check if CPF is null or empty', () => {
const isEmpty = cpf_1.default.validate(emptyCpf);
expect(isEmpty).toBeFalsy();
});
it('Correct check if CPF provided is valid', () => {
const isValid = cpf_1.default.validate(validCpf);
expect(isValid).toBeTruthy();
});
it('Correct check if CPF provided is invalid', () => {
const isValid = cpf_1.default.validate(invalidCpf);
expect(isValid).toBeFalsy();
});
it('Correct check if CPF provided has correct length', () => {
const isValid = cpf_1.default.validate(invalidCpfLength);
expect(isValid).toBeFalsy();
});
it('Correct check if CPF provided is 00000000000 ', () => {
const isValid = cpf_1.default.validate('00000000000');
expect(isValid).toBeFalsy();
});
});
describe('CPF Mask test', () => {
it('Correct check if CPF is null or empty', () => {
const isEmpty = cpf_1.default.mask(emptyCpf);
expect(isEmpty).toBe('');
});
it('Correct check if CPF is masked in the right format ', () => {
const valid = cpf_1.default.mask(validOnlyNumbers);
expect(valid).toBe(validCpf);
});
it('Correct check if invalid CPF returns empty string', () => {
const invalid = cpf_1.default.mask(invalidOnlyNumbers);
expect(invalid).toBe('');
});
});
describe('CPF Unmask test', () => {
it('Correct check if CPF is null or empty', () => {
const isEmpty = cpf_1.default.unmask(emptyCpf);
expect(isEmpty).toBe('');
});
it('Correct check if CPF unmasked (only numbers)', () => {
const valid = cpf_1.default.unmask(validCpf);
expect(valid).toBe(validOnlyNumbers);
});
it('Correct check if invalid CPF returns empty string', () => {
const invalid = cpf_1.default.unmask(invalidOnlyNumbers);
expect(invalid).toBe('');
});
});
describe('CPF Generator test', () => {
it('Correct check if generated CPF is valid and unmasked', () => {
const cpfGenerated = cpf_1.default.generate({ mask: true });
const isValid = cpf_1.default.validate(cpfGenerated);
expect(isValid).toBeTruthy();
});
it('Correct check if generated CPF is valid and masked', () => {
const cpfGenerated = cpf_1.default.generate({ mask: false });
const isValid = cpf_1.default.validate(cpfGenerated);
expect(isValid).toBeTruthy();
});
});