diff --git a/src/app/__tests__/registros.spec.tsx b/src/app/__tests__/registros.spec.tsx index 7ae3019f..0590afad 100644 --- a/src/app/__tests__/registros.spec.tsx +++ b/src/app/__tests__/registros.spec.tsx @@ -1,15 +1,13 @@ import React from 'react'; -import { render, waitFor, fireEvent } from '@testing-library/react-native'; +import { render, waitFor } from '@testing-library/react-native'; import Registros from '../private/tabs/registros'; import AsyncStorage from '@react-native-async-storage/async-storage'; import { IIdoso } from '../interfaces/idoso.interface'; -import { IMetrica } from '../interfaces/metricas.interface'; -import { Q } from '@nozbe/watermelondb'; +import { IMetrica, EMetricas } from '../interfaces/metricas.interface'; // Import EMetricas import database from '../db'; -import { getAllMetrica } from '../services/metrica.service'; import Toast from 'react-native-toast-message'; -// Mocking the necessary components and services +// Mocking necessary components and services jest.mock('@react-native-async-storage/async-storage', () => ({ getItem: jest.fn(), })); @@ -26,19 +24,36 @@ jest.mock('react-native-toast-message'); describe('Registros', () => { const mockUser = { id: 'user1', nome: 'User Test' }; - const mockIdoso: IIdoso = { id: 'idoso1', nome: 'Idoso Test', foto: null }; - const mockMetricas: IMetrica[] = [{ _raw: { idoso_id: 'idoso1' } }]; + const mockIdoso: IIdoso = { id: 'idoso1', nome: 'Idoso Test', foto: undefined }; + const mockMetricas: IMetrica[] = [ + { id: 1, idIdoso: 'idoso1', categoria: EMetricas.PESO, valorMaximo: '75' } // Use EMetricas.PESO + ]; beforeEach(() => { - // Clear mocks before each test jest.clearAllMocks(); }); it('should render the user and idoso information when both are available', async () => { + // Mocking AsyncStorage to return user and idoso (AsyncStorage.getItem as jest.Mock) .mockResolvedValueOnce(JSON.stringify(mockUser)) // Mocking user .mockResolvedValueOnce(JSON.stringify(mockIdoso)); // Mocking idoso - const { getByText, getByTestId } = render(); -  }); -}); \ No newline at end of file + // Mocking the database query to return some metricas + (database.get as jest.Mock).mockReturnValue({ + query: jest.fn().mockReturnValue({ + fetch: jest.fn().mockResolvedValueOnce(mockMetricas), // Returning mock metrics + }), + }); + + const { getByText, getByTestId, queryByTestId } = render(); + + // Wait for the user and idoso info to be rendered + await waitFor(() => { + expect(getByText('Idoso Test')).toBeTruthy(); // Verifying the idoso name appears + }); + + // Check that the "no photo" placeholder is displayed (since foto is null) + const noPhotoIcon = queryByTestId('no-photo-icon'); + }); +}); diff --git a/src/app/private/tabs/registros.tsx b/src/app/private/tabs/registros.tsx index 294c5673..aa17bfb3 100644 --- a/src/app/private/tabs/registros.tsx +++ b/src/app/private/tabs/registros.tsx @@ -62,19 +62,34 @@ export default function Registros() { const visualizarMetrica = (item: IMetrica) => { router.push({ pathname: "private/pages/visualizarMetrica", - params: item._raw, + params: { + id: item.id, + idIdoso: item.idIdoso, + categoria: item.categoria, + valorMaximo: item.valorMaximo, + }, }); }; + const getMetricas = async () => { - if (idoso == undefined) return; - + if (!idoso) return; + const metricasCollection = database.get('metrica'); - + try { setLoading(true); const idosoMetricas = await metricasCollection.query(Q.where('idoso_id', idoso.id)).fetch(); - setMetricas(idosoMetricas); + + // Map the WatermelonDB models to your IMetrica interface + const metricasData: IMetrica[] = idosoMetricas.map((metrica: any) => ({ + id: metrica._raw.id, // Assuming `id` is a field in the _raw object + idIdoso: metrica._raw.idoso_id, + categoria: metrica._raw.categoria, + valorMaximo: metrica._raw.valorMaximo, + })); + + setMetricas(metricasData); } catch (err) { console.log("Erro ao obter metricas do idoso:", err); } finally {