UnitTests Sender
This commit is contained in:
154
test/Robot.sendCommand.test.js
Normal file
154
test/Robot.sendCommand.test.js
Normal file
@@ -0,0 +1,154 @@
|
||||
const Robot = require('../robot/Robot')
|
||||
const MockCmdReceiver = require('./helpers/mockCmdReceiver')
|
||||
|
||||
describe('Robot.sendCommand & cmdReceivers', () => {
|
||||
|
||||
let robot
|
||||
|
||||
|
||||
beforeAll(() => {
|
||||
jest.spyOn(console, 'log').mockImplementation(() => {})
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
jest.restoreAllMocks()
|
||||
})
|
||||
|
||||
|
||||
beforeEach(() => {
|
||||
robot = new Robot(10, 10, 5)
|
||||
|
||||
// definierten Startzustand erzwingen
|
||||
robot.xMotor = 1
|
||||
robot.alpha = 2
|
||||
robot.beta = 3
|
||||
robot.a = 4
|
||||
robot.b = 5
|
||||
robot.c = 6
|
||||
robot.eMotor = 7
|
||||
})
|
||||
|
||||
test('sendCommand ruft Receiver.moveTo genau einmal auf', () => {
|
||||
const r = new MockCmdReceiver()
|
||||
|
||||
robot.cmdReceivers.push(r)
|
||||
robot.sendCommand()
|
||||
|
||||
expect(r.callCount).toBe(1)
|
||||
})
|
||||
|
||||
test('moveTo bekommt oldPos und newPos', () => {
|
||||
const r = new MockCmdReceiver()
|
||||
robot.cmdReceivers.push(r)
|
||||
|
||||
robot.sendCommand()
|
||||
|
||||
const call = r.lastCall()
|
||||
expect(call.oldPos).toBeDefined()
|
||||
expect(call.newPos).toBeDefined()
|
||||
})
|
||||
|
||||
test('oldPos und newPos sind unterschiedliche Objekte', () => {
|
||||
const r = new MockCmdReceiver()
|
||||
robot.cmdReceivers.push(r)
|
||||
|
||||
robot.sendCommand()
|
||||
|
||||
const { oldPos, newPos } = r.lastCall()
|
||||
expect(oldPos).not.toBe(newPos)
|
||||
})
|
||||
|
||||
test('oldPos enthält vorherige Motorwerte', () => {
|
||||
const r = new MockCmdReceiver()
|
||||
robot.cmdReceivers.push(r)
|
||||
|
||||
robot.sendCommand()
|
||||
|
||||
const { oldPos } = r.lastCall()
|
||||
expect(oldPos.x).toBe(1)
|
||||
expect(oldPos.y).toBe(2)
|
||||
expect(oldPos.z).toBe(3)
|
||||
})
|
||||
|
||||
test('newPos enthält aktuelle Motorwerte', () => {
|
||||
const r = new MockCmdReceiver()
|
||||
robot.cmdReceivers.push(r)
|
||||
|
||||
robot.xMotor = 10
|
||||
robot.alpha = 20
|
||||
|
||||
robot.sendCommand()
|
||||
|
||||
const { newPos } = r.lastCall()
|
||||
expect(newPos.x).toBe(10)
|
||||
expect(newPos.y).toBe(20)
|
||||
})
|
||||
|
||||
test('mehrere Receiver werden alle benachrichtigt', () => {
|
||||
const r1 = new MockCmdReceiver('r1')
|
||||
const r2 = new MockCmdReceiver('r2')
|
||||
|
||||
robot.cmdReceivers.push(r1, r2)
|
||||
robot.sendCommand()
|
||||
|
||||
expect(r1.callCount).toBe(1)
|
||||
expect(r2.callCount).toBe(1)
|
||||
})
|
||||
|
||||
test('Receiver-Reihenfolge bleibt erhalten', () => {
|
||||
const order = []
|
||||
|
||||
const r1 = { moveTo: () => order.push('r1') }
|
||||
const r2 = { moveTo: () => order.push('r2') }
|
||||
|
||||
robot.cmdReceivers.push(r1, r2)
|
||||
robot.sendCommand()
|
||||
|
||||
expect(order).toEqual(['r1', 'r2'])
|
||||
})
|
||||
|
||||
test('sendCommand funktioniert ohne Receiver (kein Crash)', () => {
|
||||
expect(() => robot.sendCommand()).not.toThrow()
|
||||
})
|
||||
|
||||
test('mehrere sendCommand Aufrufe triggern mehrere Calls', () => {
|
||||
const r = new MockCmdReceiver()
|
||||
robot.cmdReceivers.push(r)
|
||||
|
||||
robot.sendCommand()
|
||||
robot.sendCommand()
|
||||
|
||||
expect(r.callCount).toBe(2)
|
||||
})
|
||||
|
||||
test('MotorPosition wird neu erstellt bei jedem sendCommand', () => {
|
||||
const r = new MockCmdReceiver()
|
||||
robot.cmdReceivers.push(r)
|
||||
|
||||
robot.sendCommand()
|
||||
const first = r.lastCall().newPos
|
||||
|
||||
robot.xMotor = 99
|
||||
robot.sendCommand()
|
||||
const second = r.lastCall().newPos
|
||||
|
||||
expect(first.x).not.toBe(second.x)
|
||||
})
|
||||
|
||||
|
||||
test('sendCommand ruft Receiver auf ohne Log-Ausgabe', () => {
|
||||
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {})
|
||||
|
||||
const r = new MockCmdReceiver()
|
||||
robot.cmdReceivers.push(r)
|
||||
|
||||
robot.sendCommand()
|
||||
|
||||
expect(r.callCount).toBe(1)
|
||||
|
||||
expect(logSpy).toHaveBeenCalled(1)
|
||||
logSpy.mockRestore()
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
23
test/helpers/mockCmdReceiver.js
Normal file
23
test/helpers/mockCmdReceiver.js
Normal file
@@ -0,0 +1,23 @@
|
||||
class MockCmdReceiver {
|
||||
constructor(name = 'receiver') {
|
||||
this.name = name
|
||||
this.calls = []
|
||||
}
|
||||
|
||||
moveTo(oldPos, newPos) {
|
||||
this.calls.push({
|
||||
oldPos,
|
||||
newPos
|
||||
})
|
||||
}
|
||||
|
||||
get callCount() {
|
||||
return this.calls.length
|
||||
}
|
||||
|
||||
lastCall() {
|
||||
return this.calls[this.calls.length - 1]
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MockCmdReceiver
|
||||
Reference in New Issue
Block a user