|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | + |
| 3 | +import axios from 'axios'; |
| 4 | + |
| 5 | +// Smart service availability check (IBGE bloqueia os runners do GH Actions) |
| 6 | +let shouldSkipTests = false; |
| 7 | + |
| 8 | +try { |
| 9 | + const response = await axios.get( |
| 10 | + 'https://servicodados.ibge.gov.br/api/v1/localidades/regioes', |
| 11 | + { timeout: 5000 } |
| 12 | + ); |
| 13 | + if (response.status !== 200) { |
| 14 | + shouldSkipTests = true; |
| 15 | + } |
| 16 | +} catch (error) { |
| 17 | + shouldSkipTests = true; |
| 18 | +} |
| 19 | + |
| 20 | +describe.skipIf(shouldSkipTests)('/ibge/regioes/v1 (E2E)', () => { |
| 21 | + test('Utilizando um Codigo válido: 1', async () => { |
| 22 | + const requestUrl = `${global.SERVER_URL}/api/ibge/regioes/v1/1`; |
| 23 | + const response = await axios.get(requestUrl); |
| 24 | + |
| 25 | + expect(response.status).toBe(200); |
| 26 | + expect(response.data).toEqual( |
| 27 | + expect.objectContaining({ |
| 28 | + id: expect.any(Number), |
| 29 | + sigla: expect.any(String), |
| 30 | + nome: expect.any(String), |
| 31 | + }) |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + test('Buscando todas as regiões', async () => { |
| 36 | + const requestUrl = `${global.SERVER_URL}/api/ibge/regioes/v1`; |
| 37 | + const response = await axios.get(requestUrl); |
| 38 | + |
| 39 | + expect(response.status).toBe(200); |
| 40 | + expect(response.data).toEqual( |
| 41 | + expect.arrayContaining([ |
| 42 | + expect.objectContaining({ |
| 43 | + id: expect.any(Number), |
| 44 | + sigla: expect.any(String), |
| 45 | + nome: expect.any(String), |
| 46 | + }), |
| 47 | + ]) |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + test('Utilizando um Codigo inexistente ou inválido: 99', async () => { |
| 52 | + const requestUrl = `${global.SERVER_URL}/api/ibge/regioes/v1/99`; |
| 53 | + |
| 54 | + try { |
| 55 | + await axios.get(requestUrl); |
| 56 | + } catch (error) { |
| 57 | + const { response } = error; |
| 58 | + expect(response.status).toBe(404); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + test('Utilizando uma Sigla válida: S', async () => { |
| 63 | + const requestUrl = `${global.SERVER_URL}/api/ibge/regioes/v1/S`; |
| 64 | + const response = await axios.get(requestUrl); |
| 65 | + |
| 66 | + expect(response.status).toBe(200); |
| 67 | + expect(response.data).toEqual( |
| 68 | + expect.objectContaining({ |
| 69 | + id: expect.any(Number), |
| 70 | + sigla: expect.any(String), |
| 71 | + nome: expect.any(String), |
| 72 | + }) |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + test('Utilizando um sigla inexistente ou inválida: PP', async () => { |
| 77 | + const requestUrl = `${global.SERVER_URL}/api/ibge/regioes/v1/PP`; |
| 78 | + |
| 79 | + try { |
| 80 | + await axios.get(requestUrl); |
| 81 | + } catch (error) { |
| 82 | + const { response } = error; |
| 83 | + expect(response.status).toBe(404); |
| 84 | + expect(response.data).toMatchObject({ |
| 85 | + name: 'NotFoundError', |
| 86 | + message: 'Região não encontrada.', |
| 87 | + type: 'not_found', |
| 88 | + }); |
| 89 | + } |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
0 commit comments