This repository has been archived on 2026-05-05. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
moebiusl 218d15ff51 Merge gmpl main2 (#37)
* addPage for gmpl

* implement gmpl full_functional

* final

* del unused variable

* change type of variable

* change test

* code fix

* code fix

* fixing var error (var -> let)

---------

Co-authored-by: SinusFox <61253950+SinusFox@users.noreply.github.com>
2024-10-11 23:25:56 +02:00

111 lines
3.8 KiB
JavaScript

import { render, fireEvent, screen } from '@testing-library/react';
import {
customLog,
customLogClear,
getTranslation,
isInputValidRegex,
isInputFilled,
downloadLPFormatting,
downloadLP,
calculate_click
} from '../src/app/scripts';
import Home from '../src/app/page'
import text from '../src/app/lang';
// Mocking GLPKAPI and console log
jest.mock('../src/solver/glpk.min.js', () => ({
LPF_ECOND: 2,
}));
// Mocking console.log
const consoleLogMock = jest.spyOn(console, 'log').mockImplementation(() => {});
// Mock useRouter to avoid invariant error
jest.mock('next/navigation', () => ({
useRouter: jest.fn(() => ({
push: jest.fn(), // Mock the 'push' function
})),
}));
beforeEach(() => {
document.body.innerHTML = `
<div>
<select id="language_current">
<option value="eng">English</option>
</select>
<textarea id="objective"></textarea>
<textarea id="subject"></textarea>
<textarea id="bounds"></textarea>
<textarea id="vars"></textarea>
<select id="maxminswitch">
<option value="maximize">Maximize</option>
<option value="minimize">Minimize</option>
</select>
<div id="out"></div>
</div>
`;
jest.clearAllMocks(); // Clear any previous mocks
});
test('customLog should append message to output box', () => {
const message = 'Test message';
customLog(message);
const outputElement = document.getElementById('out');
expect(outputElement.innerHTML).toContain(message);
});
test('customLogClear should clear the output box', () => {
const message = 'Test message';
customLog(message);
customLogClear();
const outputElement = document.getElementById('out');
expect(outputElement.innerHTML).toBe('');
});
test('getTranslation should return translation based on selected language', () => {
const result = getTranslation('header_title');
expect(result).toBe(text('eng', 'header_title')); // Assuming text function provides correct translation
});
test('isInputValidRegex should validate input regex correctly', () => {
expect(isInputValidRegex("x + y", "+1 x + 2 y <= 15\n+3 x + 1 y <= 20", "x >= 0\ny >= 0", "x\ny")).toBe(true);
expect(isInputValidRegex("x + y", "+1 x + 2 y <= 15\n+3 x + 1 y <= 20", "x >= 0\ny >= 0", "")).toBe(false); // Invalid objective
});
test('isInputFilled should check for filled inputs', () => {
expect(isInputFilled('3x + 5y', 'x + y <= 10', 'x <= 5', 'x\ny')).toBe(true);
expect(isInputFilled('', 'x + y <= 10', 'x <= 5', 'x\ny')).toBe(false); // Objective empty
});
test('downloadLPFormatting should format LP correctly', () => {
const formattedLP = downloadLPFormatting('3x + 5y', 'x + y <= 10', 'x <= 5');
expect(formattedLP).toContain('obj: 3x + 5y');
expect(formattedLP).toContain('Subject To');
expect(formattedLP).toContain('Bounds');
});
test('calculate_click should display "Calculating" in the output box', () => {
render(<Home />);
// Spy on customLog and customLogClear to prevent actual logging and check the calls
const mockClear = jest.spyOn({ customLogClear }, 'customLogClear').mockImplementation();
const mockLog = jest.spyOn({ customLog }, 'customLog').mockImplementation();
// Set valid inputs
document.getElementById('objective').value = '3x + 5y';
document.getElementById('subject').value = 'x + y <= 10';
document.getElementById('bounds').value = 'x <= 5';
document.getElementById('vars').value = 'x\ny';
// Simulate the button click to trigger calculation
fireEvent.click(screen.getByText('Calculate'));
// Check the contents of out box
const outputElement = document.getElementById('out');
expect(outputElement.innerHTML).toContain('Calculating');
// Clear mock
mockClear.mockRestore();
mockLog.mockRestore();
});