UNPKG

core-zipcode-br

Version:

A Brazilian ZIP code validation and lookup library. Provides address lookup from various Brazilian CEP services and validates ZIP codes based on the Brazilian format.

55 lines (54 loc) 2.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const cep_service_1 = require("../service/cep.service"); jest.mock('axios'); const mockedAxios = axios_1.default; describe('CepService', () => { let cepService; beforeEach(() => { cepService = new cep_service_1.CepService({ log: false }); }); it('should validate a correct CEP', () => { const isValid = cepService.verify('01001-000'); expect(isValid).toBe(true); }); it('should invalidate an incorrect CEP', () => { const isValid = cepService.verify('invalid-cep'); expect(isValid).toBe(false); }); it('should return address when consulting ViaCEP', async () => { mockedAxios.get.mockResolvedValue({ status: 200, data: { uf: 'SP', localidade: 'São Paulo', logradouro: 'Praça da Sé', bairro: 'Sé', }, }); const address = await cepService.consultViaCep('01001-000'); expect(address).toEqual({ state: 'SP', city: 'São Paulo', street: 'Praça da Sé', neighborhood: 'Sé', zipCode: '01001000', }); }); it('should return null for invalid ViaCEP request', async () => { mockedAxios.get.mockRejectedValue(new Error('API Error')); const address = await cepService.consultViaCep('00000-000'); expect(address).toBeNull(); }); it('should throw an error when ZIP code is not found', async () => { mockedAxios.get.mockResolvedValueOnce({ status: 200, data: { erro: true }, }); await expect(cepService.searchAddressByZipCode('00000-000')).rejects.toThrow('Zip code not found.'); }); });