@sjsilva-libs/validation
Version:
Validação de CNPJ e CPF
45 lines (35 loc) • 1.51 kB
text/typescript
import { describe, it, expect } from 'vitest'
import { isCPF } from './index'
describe('isCPF', () => {
const validStrCPF = '529.982.247-25' // Valid CPF
const invalidStrCPF = '529.982.247-26' // Invalid CPF (wrong check digits)
const bigStrCPF = '529.982.247-255' // CPF with more than 11 characters
const smallStrCPF = '529.982.247' // CPF with fewer than 11 characters
const cpfAllSameDigits = '111.111.111-11' // CPF with all digits the same
const cpfWithNonNumericChars = '529.982.247-2a' // CPF with non-numeric characters
const cpfSecondRest10 = '123.456.789-09' // CPF where the second rest is 10 or 11
it('CPF must be valid', () => {
expect(isCPF(validStrCPF)).toBe(true)
})
it('CPF must be invalid', () => {
expect(isCPF(invalidStrCPF)).toBe(false)
})
it.each([null, undefined, '', ' '])("CPF must be informed: '%s'", (cpf) => {
expect(isCPF(cpf)).toBe(false)
})
it.each([bigStrCPF, smallStrCPF])('CPF must have 11 chars: %s', (cpf) => {
expect(isCPF(cpf)).toBe(false)
})
it('CPF with all digits the same must be invalid', () => {
expect(isCPF(cpfAllSameDigits)).toBe(false)
})
it('CPF with non-numeric characters must be invalid', () => {
expect(isCPF(cpfWithNonNumericChars)).toBe(false)
})
it('CPF with correct second check digit must be valid', () => {
expect(isCPF(validStrCPF)).toBe(true)
})
it('CPF where the second rest is 10 or 11 must be valid', () => {
expect(isCPF(cpfSecondRest10)).toBe(true)
})
})