-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a test for start/stop monitored vessels fetching
- Loading branch information
1 parent
122ff0c
commit 87a9236
Showing
5 changed files
with
93 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
{ | ||
"editor.defaultFormatter": "esbenp.prettier-vscode", | ||
"editor.formatOnSave": true | ||
"editor.formatOnSave": true, | ||
"[typescript]": { | ||
"editor.defaultFormatter": "esbenp.prettier-vscode" | ||
}, | ||
"[typescriptreact]": { | ||
"editor.defaultFormatter": "esbenp.prettier-vscode" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
import GRPCClientHandler from '../implementations/GRPCClientHandler'; | ||
import { AISServiceClientImpl } from '../../proto/AIS-protobuf/ais'; | ||
import GRPCClientHandler from '../implementations/GRPCClientHandler' | ||
import { AISServiceClientImpl, VesselInfoResponse } from '../../proto/AIS-protobuf/ais' | ||
|
||
// Create a mock implementation of Rpc | ||
const mockClient: jest.Mocked<AISServiceClientImpl> = { | ||
GetVesselInfo: jest.fn(), | ||
} as unknown as jest.Mocked<AISServiceClientImpl>; | ||
GetVesselInfo: jest.fn(), | ||
} as unknown as jest.Mocked<AISServiceClientImpl> | ||
|
||
jest.mock('../../proto/AIS-protobuf/ais'); | ||
jest.mock('../../proto/AIS-protobuf/ais') | ||
|
||
describe('GRPCClientHandler', () => { | ||
let clientHandler: GRPCClientHandler; | ||
let clientHandler: GRPCClientHandler | ||
|
||
beforeEach(() => { | ||
clientHandler = new GRPCClientHandler(mockClient); | ||
}); | ||
beforeEach(() => { | ||
clientHandler = new GRPCClientHandler(mockClient) | ||
}) | ||
|
||
it('should return detailed vessel information', async () => { | ||
const mockResponse = { mmsi: 123456789, name: 'Test Vessel', shipType: 'Cargo' } as any; // Mock the actual response | ||
mockClient.GetVesselInfo.mockResolvedValue(mockResponse); | ||
it('should return detailed vessel information', async () => { | ||
const mockResponse = { mmsi: 123456789, name: 'Test Vessel', shipType: 'Cargo' } as VesselInfoResponse // Mock the actual response | ||
mockClient.GetVesselInfo.mockResolvedValue(mockResponse) | ||
|
||
const result = await clientHandler.getVesselInfo({ mmsi: 123456789, timestamp: 1609459200 }); | ||
expect(result).toEqual({ | ||
mmsi: 123456789, | ||
name: 'Test Vessel', | ||
shipType: 'Cargo', | ||
imo: undefined, | ||
callSign: undefined, | ||
width: undefined, | ||
length: undefined, | ||
positionFixingDevice: undefined, | ||
}); | ||
}); | ||
}); | ||
const result = await clientHandler.getVesselInfo({ mmsi: 123456789, timestamp: 1609459200 }) | ||
expect(result).toEqual({ | ||
mmsi: 123456789, | ||
name: 'Test Vessel', | ||
shipType: 'Cargo', | ||
imo: undefined, | ||
callSign: undefined, | ||
width: undefined, | ||
length: undefined, | ||
positionFixingDevice: undefined, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,61 @@ | ||
import StreamManager, { SIMPLE_FETCH_INTERVAL, MONITORED_FETCH_INTERVAL } from '../implementations/StreamManager'; | ||
import { IClientHandler } from '../interfaces/IClientHandler'; | ||
import { ISimpleVessel } from '../models/simpleVessel'; | ||
import { IMonitoredVessel } from '../models/monitoredVessel'; | ||
import { RefObject } from 'react'; | ||
import StreamManager from '../implementations/StreamManager' | ||
import { IClientHandler } from '../interfaces/IClientHandler' | ||
import { ISimpleVessel } from '../models/simpleVessel' | ||
import { IMonitoredVessel } from '../models/monitoredVessel' | ||
import { RefObject } from 'react' | ||
|
||
jest.mock('../interfaces/IClientHandler'); | ||
jest.mock('../interfaces/IClientHandler') | ||
|
||
describe('StreamManager', () => { | ||
let mockClientHandler: jest.Mocked<IClientHandler>; | ||
let setAllVessels: jest.Mock<React.Dispatch<React.SetStateAction<ISimpleVessel[]>>>; | ||
let setMonitoredVessels: jest.Mock<React.Dispatch<React.SetStateAction<IMonitoredVessel[]>>>; | ||
let dateTimeRef: RefObject<Date>; | ||
let streamManager: StreamManager; | ||
|
||
beforeEach(() => { | ||
mockClientHandler = { | ||
getSimpleVessles: jest.fn().mockResolvedValue([{ mmsi: 123456, location: { point: { lon: 1, lat: 2 }, timestamp: new Date(), heading: 45 } }]), | ||
getMonitoredVessels: jest.fn().mockResolvedValue([{ mmsi: 123456, trustworthiness: 0.9, reason: 'Test' }]), | ||
} as unknown as jest.Mocked<IClientHandler>; | ||
|
||
setAllVessels = jest.fn(); | ||
setMonitoredVessels = jest.fn(); | ||
dateTimeRef = { current: new Date() } as RefObject<Date>; | ||
|
||
streamManager = new StreamManager(mockClientHandler, setAllVessels, setMonitoredVessels, dateTimeRef); | ||
}); | ||
|
||
it('should start and stop simple vessel fetching', async () => { | ||
jest.useFakeTimers(); | ||
jest.spyOn(global, 'setTimeout') | ||
streamManager.startSimpleVesselFetching(); | ||
expect(mockClientHandler.getSimpleVessles).toHaveBeenCalledTimes(1); // Called once | ||
|
||
// jest.advanceTimersByTime(SIMPLE_FETCH_INTERVAL); // Simulate 5 seconds for the next call | ||
// expect(mockClientHandler.getSimpleVessles).toHaveBeenCalledTimes(2); // Called in loop | ||
|
||
streamManager.stopSimpleVesselFetching(); // Stop the loop | ||
// jest.clearAllTimers(); | ||
}); | ||
|
||
it('should handle monitoring zone change and stop fetching if zone is invalid', () => { | ||
streamManager.onMonitoringZoneChange(undefined); | ||
expect(setMonitoredVessels).toHaveBeenCalledWith([]); | ||
}); | ||
}); | ||
let mockClientHandler: jest.Mocked<IClientHandler> | ||
let setAllVessels: jest.Mock<React.Dispatch<React.SetStateAction<ISimpleVessel[]>>> | ||
let setMonitoredVessels: jest.Mock<React.Dispatch<React.SetStateAction<IMonitoredVessel[]>>> | ||
let dateTimeRef: RefObject<Date> | ||
let streamManager: StreamManager | ||
|
||
beforeEach(() => { | ||
mockClientHandler = { | ||
getSimpleVessles: jest | ||
.fn() | ||
.mockResolvedValue([ | ||
{ mmsi: 123456, location: { point: { lon: 1, lat: 2 }, timestamp: new Date(), heading: 45 } }, | ||
]), | ||
getMonitoredVessels: jest.fn().mockResolvedValue([{ mmsi: 123456, trustworthiness: 0.9, reason: 'Test' }]), | ||
} as unknown as jest.Mocked<IClientHandler> | ||
|
||
setAllVessels = jest.fn() | ||
setMonitoredVessels = jest.fn() | ||
dateTimeRef = { current: new Date() } as RefObject<Date> | ||
|
||
streamManager = new StreamManager(mockClientHandler, setAllVessels, setMonitoredVessels, dateTimeRef) | ||
}) | ||
|
||
it('should start and stop simple vessel fetching', async () => { | ||
streamManager.startSimpleVesselFetching() | ||
expect(mockClientHandler.getSimpleVessles).toHaveBeenCalledTimes(1) // Called once | ||
streamManager.stopSimpleVesselFetching() // Stop the loop | ||
}) | ||
|
||
it('should start and stop monitored vessel fetching', async () => { | ||
// Start fetching | ||
streamManager.onMonitoringZoneChange([ | ||
{ lon: 1, lat: 2 }, | ||
{ lon: 3, lat: 4 }, | ||
{ lon: 5, lat: 6 }, | ||
{ lon: 7, lat: 8 }, | ||
]) | ||
expect(mockClientHandler.getMonitoredVessels).toHaveBeenCalledTimes(1) | ||
|
||
// Clear call count | ||
mockClientHandler.getMonitoredVessels.mockClear() | ||
|
||
// Stop fetching | ||
streamManager.onMonitoringZoneChange(undefined) | ||
expect(mockClientHandler.getMonitoredVessels).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should handle monitoring zone change and stop fetching if zone is invalid', () => { | ||
streamManager.onMonitoringZoneChange(undefined) | ||
expect(setMonitoredVessels).toHaveBeenCalledWith([]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters