Files
geek-calc/tests/integration/ui.test.js
snowprint 54f427ea21
Some checks failed
Deploy to GitHub Pages / build-and-deploy (push) Has been cancelled
init geek calc
2025-10-04 10:53:41 +08:00

105 lines
4.1 KiB
JavaScript

// Integration tests for user scenarios
// These tests should fail initially since the UI controller doesn't exist yet
function testUserScenarios() {
try {
// This will fail since UI components don't exist yet
const calculator = new Calculator();
const rpnCalculator = new RPNCalculator();
const stateManager = new StateManager();
const uiController = new UIController(calculator, rpnCalculator, stateManager);
// Initialize the UI
uiController.init();
// Test basic calculation: 5 + 3 = 8
uiController.handleInput('5');
uiController.handleInput('+');
uiController.handleInput('3');
uiController.handleInput('=');
const result = uiController.getCurrentResult();
if (result !== '8') {
throw new Error(`Basic calculation failed: 5 + 3 should equal 8, got ${result}`);
}
// Test complex expression: (2 + 3) * 4 = 20
uiController.handleInput('(');
uiController.handleInput('2');
uiController.handleInput('+');
uiController.handleInput('3');
uiController.handleInput(')');
uiController.handleInput('*');
uiController.handleInput('4');
uiController.handleInput('=');
const complexResult = uiController.getCurrentResult();
if (complexResult !== '20') {
throw new Error(`Complex calculation failed: (2 + 3) * 4 should equal 20, got ${complexResult}`);
}
// Test percentage: 100 + 10% = 110
uiController.handleInput('100');
uiController.handleInput('+');
uiController.handleInput('10');
uiController.handleInput('%');
uiController.handleInput('=');
const percentResult = uiController.getCurrentResult();
if (percentResult !== '110') {
throw new Error(`Percentage calculation failed: 100 + 10% should equal 110, got ${percentResult}`);
}
// Test RPN mode: 4 ENTER 6 + = 10
uiController.toggleRPNMode();
uiController.handleRPNInput('4');
uiController.handleRPNInput('ENTER'); // Enter to push to stack
uiController.handleRPNInput('6');
uiController.handleRPNInput('ENTER'); // Enter to push to stack
uiController.handleRPNInput('+'); // Add operation
// Check RPN result
const rpnResult = uiController.getCurrentResult();
if (rpnResult !== '10') {
throw new Error(`RPN calculation failed: 4 ENTER 6 + should equal 10, got ${rpnResult}`);
}
// Test history functionality
const history = uiController.getHistory();
if (history.length === 0) {
throw new Error('History functionality not working: no entries found');
}
// Verify that the last calculation is in history
const lastHistoryEntry = history[history.length - 1];
if (!lastHistoryEntry || lastHistoryEntry.result !== 10) {
throw new Error('History functionality not working: last calculation not properly stored');
}
// Test keyboard controls
// This would involve simulating keyboard events
// For simplicity in this test, we'll just verify the keyboard handler exists
if (typeof uiController.handleKeyboardInput !== 'function') {
throw new Error('Keyboard controls not implemented');
}
// Test that clear operation works
uiController.handleInput('C'); // Clear
const clearedResult = uiController.getCurrentResult();
if (clearedResult !== '0') {
throw new Error(`Clear operation failed: should reset to 0, got ${clearedResult}`);
}
return true; // All tests passed
} catch (error) {
return { error: error.message };
}
}
// Add test to global test collection
if (typeof addTest !== 'undefined') {
addTest('User Scenarios Integration Tests', testUserScenarios);
} else {
console.log('Test harness not loaded. Run with test runner.');
}