Skip to content

Commit

Permalink
tests: improve coverage and fixes new interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Jan 2, 2025
1 parent 3b88021 commit 31f861f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
15 changes: 15 additions & 0 deletions tests/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EventGpz } from "../src/interface";

const coreJoyMock = (o: any) => core['Joy'](o)
const coreBtnMock = (o: any) => core['Btn'](o)
const coreBtn4Mock = (o: any) => core['Btn4'](o)

test("core joy: 1100 -> 000 (touch)", () => {
const gpz = {
Expand Down Expand Up @@ -34,3 +35,17 @@ test("core gpz: click on button (touch)", () => {
expect(gpz.stateOld).toEqual([false])
expect(gpz.stateNew).toEqual([true])
})

test("core btn4: gamepad input updates state", () => {
const gpz = {
from: EventGpz.Gamepad,
buttons: [true, false, true],
stateOld: [false, false, false],
stateNew: [false, false, false],
};

coreBtn4Mock(gpz)

expect(gpz.stateOld).toEqual([false, false, false])
expect(gpz.stateNew).toEqual([true, false, true])
})
21 changes: 21 additions & 0 deletions tests/draw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { draw, drawCircle } from "../src/draw"
const drawJoyMock = (o: any) => draw['Joy'](o)
const drawBtnMock = (o: any) => draw['Btn'](o)
const drawDpadMock = (o: any) => draw['Dpad'](o)
const drawBtn4Mock = (o: any) => draw['Btn4'](o)
const drawCircleMock = (a: any, b: any, c: any, d: any, e: any) => drawCircle(a, b, e, {x: c, y: d})

test("drawCircle should draw a filled circle with the correct attributes", () => {
Expand Down Expand Up @@ -106,3 +107,23 @@ test("Dpad should draw correctly based on stateNew", () => {

expect(gpz.ctx2d.clearRect).toHaveBeenCalledWith(0, 0, 100, 100)
})

test("Btn4 should draw correctly based on stateNew", () => {
const gpz = {
canvas: { width: 100, height: 100, dataset: { gpzSize: '16', color: '#aaaaaa80' } },
stateNew: [false, false, true, false],
ctx2d: {
fillStyle: "",
beginPath: jest.fn(),
arc: jest.fn(),
fill: jest.fn(),
stroke: jest.fn(),
closePath: jest.fn(),
clearRect: jest.fn()
},
}

drawBtn4Mock(gpz)

expect(gpz.ctx2d.clearRect).toHaveBeenCalledWith(0, 0, 100, 100)
})
6 changes: 4 additions & 2 deletions tests/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ test('handleGamepadAxis should return an array of Vector2d', () => {
test('handleGamepadButtons should return an array of pressed button indices', () => {
const gamepad = {buttons: [{ pressed: false }, { pressed: false }, { pressed: true }]}
const result = handleGamepadButtonsMock(gamepad)
expect(result).toHaveLength(1)
expect(result[0]).toBe(2)
expect(result).toHaveLength(3)
expect(result[0]).toBe(false)
expect(result[1]).toBe(false)
expect(result[2]).toBe(true)
})

0 comments on commit 31f861f

Please sign in to comment.