Skip to content

Commit

Permalink
test: Check that the 'no photo' placeholder is displayed
Browse files Browse the repository at this point in the history
Co-Authored-By: Natalia Rodrigues <Natytotherodrigues@gmail.com>
  • Loading branch information
GabrielSMonteiro and Natyrodrigues committed Sep 19, 2024
1 parent 747d829 commit 2c7703a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
37 changes: 26 additions & 11 deletions src/app/__tests__/registros.spec.tsx
Original file line number Diff line number Diff line change
@@ -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(),
}));
Expand All @@ -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(<Registros />);
  });
});
// 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(<Registros />);

// 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');
});
});
25 changes: 20 additions & 5 deletions src/app/private/tabs/registros.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2c7703a

Please sign in to comment.