-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from ES2-UFPI/59-validar-dados-de-entrada-ex-h…
…orário-válido-dosagem-permitida-back-endfront-end ✨ feat: Implementação da Validar dados de entrada para cadastro de medicações + Testes Front-end
- Loading branch information
Showing
4 changed files
with
7,715 additions
and
6,371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, waitFor } from '@testing-library/react-native'; | ||
import RegisterMedication from '../app/(tabs)/add' | ||
import axios from 'axios'; | ||
import { Alert } from 'react-native'; | ||
|
||
jest.mock('axios'); | ||
jest.mock('expo-router', () => ({ | ||
Link: ({ children }) => children, | ||
})); | ||
jest.mock('expo-font', () => ({ | ||
useFonts: () => [true], | ||
})); | ||
jest.mock('@expo-google-fonts/katibeh', () => ({ | ||
Katibeh_400Regular: 'mocked-font', | ||
})); | ||
// Mock do Alert | ||
jest.spyOn(Alert, 'alert'); | ||
|
||
describe('RegisterMedication', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('renderiza todos os campos do formulário', () => { | ||
const { getByPlaceholderText, getByText } = render(<RegisterMedication />); | ||
|
||
expect(getByPlaceholderText('Nome do medicamento')).toBeTruthy(); | ||
expect(getByPlaceholderText('Nome do laboratório')).toBeTruthy(); | ||
expect(getByText('Dosagem')).toBeTruthy(); | ||
expect(getByText('Data de início')).toBeTruthy(); | ||
expect(getByText('Horário de início')).toBeTruthy(); | ||
expect(getByText('Frequência')).toBeTruthy(); | ||
}); | ||
|
||
test('mostra erro quando campos obrigatórios estão vazios', async () => { | ||
const { getByText } = render(<RegisterMedication />); | ||
|
||
fireEvent.press(getByText('Confirmar')); | ||
|
||
await waitFor(() => { | ||
expect(Alert.alert).toHaveBeenCalledWith('Erro', 'Por favor, preencha todos os campos obrigatórios.'); | ||
}); | ||
}); | ||
|
||
test('valida dosagem inválida', async () => { | ||
const { getByPlaceholderText, getByText } = render(<RegisterMedication />); | ||
|
||
fireEvent.changeText(getByPlaceholderText('Nome do medicamento'), 'Dipirona'); | ||
fireEvent.changeText(getByPlaceholderText('Quantidade de medicamento por dose (ex.: 1, 2, 0.5)'), '101'); | ||
|
||
fireEvent.press(getByText('Confirmar')); | ||
|
||
await waitFor(() => { | ||
expect(Alert.alert).toHaveBeenCalledWith('Erro', 'Por favor, insira uma dosagem válida.'); | ||
}); | ||
}); | ||
|
||
test('cadastra medicação com sucesso', async () => { | ||
const mockMedicamentoResponse = { data: { id: 1 } }; | ||
(axios.post as jest.Mock) | ||
.mockResolvedValueOnce(mockMedicamentoResponse) | ||
.mockResolvedValueOnce({ data: { success: true } }); | ||
|
||
const { getByPlaceholderText, getByText } = render(<RegisterMedication />); | ||
|
||
// Preenche todos os campos obrigatórios | ||
fireEvent.changeText(getByPlaceholderText('Nome do medicamento'), 'Dipirona'); | ||
fireEvent.changeText(getByPlaceholderText('Nome do laboratório'), 'EMS'); | ||
fireEvent.changeText(getByPlaceholderText('Quantidade de medicamento por dose (ex.: 1, 2, 0.5)'), '1'); | ||
fireEvent.changeText(getByPlaceholderText('Quantidade de comprimidos'), '30'); | ||
|
||
// Seleciona frequência | ||
const picker = getByText('Selecionar frequência'); | ||
fireEvent(picker, 'onValueChange', '12'); | ||
|
||
fireEvent.press(getByText('Confirmar')); | ||
|
||
await waitFor(() => { | ||
expect(axios.post).toHaveBeenCalledWith('http://localhost:8080/medicamentos', { | ||
nome: 'Dipirona', | ||
laboratorio: 'EMS' | ||
}); | ||
expect(axios.post).toHaveBeenCalledWith('http://localhost:8080/usuarios-medicamentos/1', expect.any(Object)); | ||
expect(Alert.alert).toHaveBeenCalledWith('Sucesso', 'Medicação cadastrada com sucesso!'); | ||
}, { timeout: 3000 }); | ||
}); | ||
|
||
test('trata erro na API corretamente', async () => { | ||
(axios.post as jest.Mock).mockRejectedValueOnce(new Error('Erro de API')); | ||
|
||
const { getByPlaceholderText, getByText } = render(<RegisterMedication />); | ||
|
||
fireEvent.changeText(getByPlaceholderText('Nome do medicamento'), 'Dipirona'); | ||
fireEvent.changeText(getByPlaceholderText('Nome do laboratório'), 'EMS'); | ||
fireEvent.changeText(getByPlaceholderText('Quantidade de medicamento por dose (ex.: 1, 2, 0.5)'), '1'); | ||
fireEvent.changeText(getByPlaceholderText('Quantidade de comprimidos'), '30'); | ||
|
||
fireEvent.press(getByText('Confirmar')); | ||
|
||
await waitFor(() => { | ||
expect(Alert.alert).toHaveBeenCalledWith('Erro', 'Não foi possível cadastrar a medicação. Tente novamente.'); | ||
}); | ||
}); | ||
|
||
test('limpa formulário ao pressionar cancelar', () => { | ||
const { getByPlaceholderText, getByText } = render(<RegisterMedication />); | ||
|
||
fireEvent.changeText(getByPlaceholderText('Nome do medicamento'), 'Dipirona'); | ||
fireEvent.press(getByText('Cancelar')); | ||
|
||
expect(getByPlaceholderText('Nome do medicamento').props.value).toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.