Skip to content

Commit

Permalink
test: index LabSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
nandodev-net committed Sep 6, 2024
1 parent 819ac22 commit 30d0431
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions src/views/LabSummary/tests/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { logError } from '@edx/frontend-platform/logging';
import LabSummary from '../index';
import '@testing-library/jest-dom';
import { columns } from '../columns';
import { formatUnixTimestamp } from '../../../helpers';

jest.mock('../../../shared/Table', () => () => <div>Mocked Table Component</div>);
jest.mock('@edx/paragon', () => ({
Expand Down Expand Up @@ -48,6 +49,13 @@ jest.mock('@edx/frontend-platform/logging', () => ({
logError: jest.fn(),
}));

jest.mock('../../../shared/Table', () => ({ handlePagination }) => (
<div>
<div>Mocked Table Component</div>
<button onClick={() => handlePagination(2)} type="button" aria-label="Next, Page 2">Next</button>
</div>
));

const mockCourseId = 'course-v1:edX+DemoX+Demo_Course';
const mockRosterStudent = { user_id: 'student_123', username: 'student_name' };
const mockSetSelectedLabDetails = jest.fn();
Expand Down Expand Up @@ -220,4 +228,106 @@ describe('LabSummary Component', () => {
expect(cellElement).toBeInTheDocument();
expect(cellElement).toHaveClass('status-na');
});

it('returns correct passedValue for exam_passed values', () => {
const laboratories = [
{ exam_passed: true, exam_score: 80, exam_max_possible_score: 100 },
{ exam_passed: false, exam_score: 50, exam_max_possible_score: 100 },
{ exam_passed: undefined, exam_score: null, exam_max_possible_score: null },
];

laboratories.forEach((laboratory) => {
const passedValue = { true: 'Yes', false: 'No' }[laboratory.exam_passed] ?? 'N/A';
const examPercentage = (laboratory.exam_score / laboratory.exam_max_possible_score) * 10;

if (laboratory.exam_passed === true) {
expect(passedValue).toBe('Yes');
} else if (laboratory.exam_passed === false) {
expect(passedValue).toBe('No');
} else {
expect(passedValue).toBe('N/A');
}

if (laboratory.exam_score !== null && laboratory.exam_max_possible_score !== null) {
expect(examPercentage).toBe((laboratory.exam_score / laboratory.exam_max_possible_score) * 10);
} else {
expect(examPercentage).toBeNaN(); // O se puede cambiar a otro tipo de validación si espera 'N/A'
}
});
});

it('calculates correct examPercentage for valid exam scores', () => {
const laboratory = {
exam_passed: true,
exam_score: 80,
exam_max_possible_score: 100,
};

const examPercentage = (laboratory.exam_score / laboratory.exam_max_possible_score) * 10;
expect(examPercentage).toBe(8); // Ejemplo de valor esperado
});

it('returns "N/A" when exam_score or exam_max_possible_score is invalid', () => {
const laboratory = {
exam_passed: false,
exam_score: undefined,
exam_max_possible_score: undefined,
};

const examPercentage = (laboratory.exam_score / laboratory.exam_max_possible_score) * 10;
expect(Number.isNaN(examPercentage)).toBe(true); // Cambia según el comportamiento esperado
});

it('handles pagination correctly and updates currentPage and isLoading', async () => {
render(
<LabSummary
courseId={mockCourseId}
rosterStudent={mockRosterStudent}
setSelectedLabDetails={mockSetSelectedLabDetails}
history={mockHistory}
/>,
);

act(() => {
const nextPageButton = screen.getByRole('button', { name: /Next, Page 2/i });
fireEvent.click(nextPageButton);
});

await waitFor(() => {
expect(mockPost).toHaveBeenCalledWith(
'https://example.com/events/api/v1/labinstancesearch/?page=2',
{ userid: 'student_123' },
);
});
});

// Test for passedValue and examPercentage
it('calculates passedValue and examPercentage correctly', () => {
const laboratory = {
exam_passed: true, // Case for 'Yes'
exam_score: 80,
exam_max_possible_score: 100,
};

const labsData = [];

// Manually call the calculation logic
const passedValue = { true: 'Yes', false: 'No' }[laboratory.exam_passed] ?? 'N/A';
const examPercentage = (laboratory.exam_score / laboratory.exam_max_possible_score) * 10;

labsData.push({
lab_profile_name: laboratory.lab_profile_name,
lab_instances_count: laboratory.lab_instances_count,
lab_instance_id: laboratory.lab_instance_id,
exam_score: laboratory.exam_score ?? 'N/A',
exam_percentage: Number.isNaN(examPercentage) ? 'N/A' : `${examPercentage}%` ?? 'N/A',
exam_passed: passedValue,
start_time: formatUnixTimestamp(laboratory.start_time),
end_time: formatUnixTimestamp(laboratory.end_time),
});

// Verifica que se calculen los valores correctos
expect(passedValue).toBe('Yes');
expect(examPercentage).toBe(8); // Asegúrate de que el valor de porcentaje sea el esperado
});
});

0 comments on commit 30d0431

Please sign in to comment.