@sjsilva-libs/validation
Version:
Validação de CNPJ e CPF
49 lines (41 loc) • 2.24 kB
text/typescript
import { describe, it, expect } from "vitest";
import { isCNPJ } from "./index";
// Test suite for the isCNPJ function
describe('isCNPJ', () => {
// Define all test cases and variables at the beginning
const validStrCNPJ = '00.776.574/0006-60'; // Valid CNPJ
const invalidStrCNPJ = '00.776.574/0006-61'; // Invalid CNPJ (wrong check digits)
const bigStrCNPJ = '00.776.574/0006-611'; // CNPJ with more than 14 characters
const smallStrCNPJ = '00.776.574/0006'; // CNPJ with fewer than 14 characters
const cnpjEspecialFirstSum = "12345678000195"; // CNPJ where the first sum % 11 is less than 2
const cnpjNormalSum = "11444777000161"; // CNPJ where the first sum % 11 is greater than or equal to 2
const cnpjEspecialSecondSum = "98765432000198"; // CNPJ where the second sum % 11 is less than 2
// Test case: Valid CNPJ should return true
it('CNPJ must be valid', () => {
expect(isCNPJ(validStrCNPJ)).toBe(true);
});
// Test case: Invalid CNPJ should return false
it('CNPJ must be invalid', () => {
expect(isCNPJ(invalidStrCNPJ)).toBe(false);
});
// Test case: CNPJ must be provided (null, undefined, empty string, or spaces should return false)
it.each([null, undefined, '', ' '])("CNPJ must be informed: '%s'", (cnpj) => {
expect(isCNPJ(cnpj)).toBe(false);
});
// Test case: CNPJ must have exactly 14 characters (no more, no less)
it.each([bigStrCNPJ, smallStrCNPJ])('CNPJ must have 14 chars: %s', (cnpj) => {
expect(isCNPJ(cnpj)).toBe(false);
});
// Test case: Validate a CNPJ where the first sum % 11 is less than 2
it("Should validate a CNPJ where sum % 11 is less than 2", () => {
expect(isCNPJ(cnpjEspecialFirstSum)).toBe(true);
});
// Test case: Validate a CNPJ where the first sum % 11 is greater than or equal to 2
it("Should validate a CNPJ where sum % 11 is greater than or equal to 2", () => {
expect(isCNPJ(cnpjNormalSum)).toBe(true);
});
// Test case: Validate a CNPJ where the second sum % 11 is less than 2
it("Should validate a CNPJ where the second sum % 11 is less than 2", () => {
expect(isCNPJ(cnpjEspecialSecondSum)).toBe(true);
});
});