removed jest fully and fixed final tests in libs

This commit is contained in:
Bojan Kucera 2025-06-04 17:06:23 -04:00
parent 1899078523
commit 3d910a13e0
8 changed files with 35 additions and 744 deletions

View file

@ -1,23 +0,0 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Notifications } from './notifications';
describe('Notifications', () => {
let component: Notifications;
let fixture: ComponentFixture<Notifications>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [Notifications]
})
.compileComponents();
fixture = TestBed.createComponent(Notifications);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View file

@ -1,244 +0,0 @@
import { TestBed, ComponentFixture, fakeAsync, tick } from '@angular/core/testing';
import { WebSocketService } from '../../services/websocket.service';
import { StrategyService, TradingStrategy } from '../../services/strategy.service';
import { StrategyDetailsComponent } from '../../pages/strategies/strategy-details/strategy-details.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { MatDialogModule } from '@angular/material/dialog';
import { CommonModule } from '@angular/common';
import { BehaviorSubject, Subject } from 'rxjs';
import { WebSocketMessage } from '../../services/websocket.service';
describe('StrategyDetails WebSocket Integration', () => {
let component: StrategyDetailsComponent;
let fixture: ComponentFixture<StrategyDetailsComponent>;
let webSocketServiceSpy: jasmine.SpyObj<WebSocketService>;
let strategyServiceSpy: jasmine.SpyObj<StrategyService>;
// Mock data
const mockStrategy: TradingStrategy = {
id: 'test-strategy',
name: 'Test Strategy',
description: 'A test strategy',
status: 'INACTIVE',
type: 'MovingAverageCrossover',
symbols: ['AAPL', 'MSFT', 'GOOGL'],
parameters: {
shortPeriod: 10,
longPeriod: 30
},
performance: {
totalTrades: 100,
winRate: 0.6,
totalReturn: 0.15,
sharpeRatio: 1.2,
maxDrawdown: 0.05
},
createdAt: new Date('2023-01-01'),
updatedAt: new Date('2023-01-10')
};
// Create mock subjects for WebSocket messages
const mockStrategySubject = new Subject<WebSocketMessage>();
beforeEach(async () => {
// Create spies for services
webSocketServiceSpy = jasmine.createSpyObj('WebSocketService', [
'getStrategyUpdates',
'getStrategySignals',
'getStrategyTrades',
'getAllStrategyMessages',
'sendMessage'
]);
strategyServiceSpy = jasmine.createSpyObj('StrategyService', [
'startStrategy',
'stopStrategy',
'pauseStrategy'
]);
// Setup spy return values
webSocketServiceSpy.getStrategyUpdates.and.returnValue(mockStrategySubject.asObservable());
webSocketServiceSpy.getStrategySignals.and.returnValue(mockStrategySubject.asObservable());
webSocketServiceSpy.getStrategyTrades.and.returnValue(mockStrategySubject.asObservable());
webSocketServiceSpy.getAllStrategyMessages.and.returnValue(mockStrategySubject.asObservable());
strategyServiceSpy.startStrategy.and.returnValue(
new BehaviorSubject({ success: true, data: { ...mockStrategy, status: 'ACTIVE' } })
);
strategyServiceSpy.pauseStrategy.and.returnValue(
new BehaviorSubject({ success: true, data: { ...mockStrategy, status: 'PAUSED' } })
);
strategyServiceSpy.stopStrategy.and.returnValue(
new BehaviorSubject({ success: true, data: { ...mockStrategy, status: 'INACTIVE' } })
);
await TestBed.configureTestingModule({
imports: [
CommonModule,
HttpClientTestingModule,
MatDialogModule
],
declarations: [
StrategyDetailsComponent
],
providers: [
{ provide: WebSocketService, useValue: webSocketServiceSpy },
{ provide: StrategyService, useValue: strategyServiceSpy }
]
}).compileComponents();
fixture = TestBed.createComponent(StrategyDetailsComponent);
component = fixture.componentInstance;
component.strategy = { ...mockStrategy };
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should subscribe to WebSocket updates when strategy changes', () => {
// Arrange & Act
component.ngOnChanges({
strategy: {
currentValue: mockStrategy,
previousValue: null,
firstChange: true,
isFirstChange: () => true
}
});
// Assert
expect(webSocketServiceSpy.getStrategySignals).toHaveBeenCalledWith(mockStrategy.id);
expect(webSocketServiceSpy.getStrategyTrades).toHaveBeenCalledWith(mockStrategy.id);
});
it('should update signals when receiving new signal WebSocket message', fakeAsync(() => {
// Arrange
component.signals = [];
component.ngOnChanges({
strategy: {
currentValue: mockStrategy,
previousValue: null,
firstChange: true,
isFirstChange: () => true
}
});
// Act: Simulate receiving a WebSocket signal message
const mockSignal = {
type: 'strategy_signal',
timestamp: new Date().toISOString(),
data: {
strategyId: mockStrategy.id,
symbol: 'AAPL',
action: 'BUY',
price: 150.5,
quantity: 10,
confidence: 0.85
}
};
mockStrategySubject.next(mockSignal);
tick();
// Assert
expect(component.signals.length).toBeGreaterThan(0);
expect(component.signals[0].symbol).toBe('AAPL');
expect(component.signals[0].action).toBe('BUY');
}));
it('should update trades when receiving new trade WebSocket message', fakeAsync(() => {
// Arrange
component.trades = [];
component.ngOnChanges({
strategy: {
currentValue: mockStrategy,
previousValue: null,
firstChange: true,
isFirstChange: () => true
}
});
// Act: Simulate receiving a WebSocket trade message
const mockTrade = {
type: 'strategy_trade',
timestamp: new Date().toISOString(),
data: {
strategyId: mockStrategy.id,
symbol: 'MSFT',
entryPrice: 290.50,
entryTime: new Date().toISOString(),
exitPrice: 295.25,
exitTime: new Date().toISOString(),
quantity: 5,
pnl: 23.75,
pnlPercent: 1.63
}
};
mockStrategySubject.next(mockTrade);
tick();
// Assert
expect(component.trades.length).toBeGreaterThan(0);
expect(component.trades[0].symbol).toBe('MSFT');
expect(component.trades[0].pnl).toBeCloseTo(23.75);
}));
it('should update strategy status when receiving status update message', fakeAsync(() => {
// Arrange
component.strategy = { ...mockStrategy, status: 'INACTIVE' };
component.ngOnChanges({
strategy: {
currentValue: component.strategy,
previousValue: null,
firstChange: true,
isFirstChange: () => true
}
});
// Act: Simulate receiving a WebSocket status update message
const mockStatusUpdate = {
type: 'strategy_update',
timestamp: new Date().toISOString(),
data: {
strategyId: mockStrategy.id,
status: 'ACTIVE'
}
};
mockStrategySubject.next(mockStatusUpdate);
tick();
// Assert
expect(component.strategy.status).toBe('ACTIVE');
}));
it('should call startStrategy service method when activateStrategy is called', () => {
// Arrange & Act
component.activateStrategy();
// Assert
expect(strategyServiceSpy.startStrategy).toHaveBeenCalledWith(mockStrategy.id);
expect(component.strategy.status).toBe('ACTIVE');
});
it('should call pauseStrategy service method when pauseStrategy is called', () => {
// Arrange & Act
component.pauseStrategy();
// Assert
expect(strategyServiceSpy.pauseStrategy).toHaveBeenCalledWith(mockStrategy.id);
expect(component.strategy.status).toBe('PAUSED');
});
it('should call stopStrategy service method when stopStrategy is called', () => {
// Arrange & Act
component.stopStrategy();
// Assert
expect(strategyServiceSpy.stopStrategy).toHaveBeenCalledWith(mockStrategy.id);
expect(component.strategy.status).toBe('INACTIVE');
});
});