explodorama / files / test / reveal.test.ts
reveal.test.ts
| 1 | import { describe, expect, it } from 'vitest' |
| 2 | import { revealShift } from '../src/render/reveal' |
| 3 | |
| 4 | // A 9 x 13 view centred on (10, 20): its edges are at x 5.5..14.5 and y 13.5..26.5. |
| 5 | const aim = { x: 10, y: 20 } |
| 6 | const shift = (x: number, y: number) => revealShift(aim, { x, y }, 9, 13) |
| 7 | |
| 8 | describe('revealShift', () => { |
| 9 | it('leaves a tile alone while it is more than a tile from the edge', () => { |
| 10 | expect(shift(10, 20)).toEqual({ x: 0, y: 0 }) |
| 11 | expect(shift(13, 25)).toEqual({ x: 0, y: 0 }) |
| 12 | }) |
| 13 | it('moves the view once the tile is within a tile of the edge', () => { |
| 14 | expect(shift(14, 20)).toEqual({ x: 1, y: 0 }) |
| 15 | expect(shift(6, 20)).toEqual({ x: -1, y: 0 }) |
| 16 | expect(shift(10, 14)).toEqual({ x: 0, y: -1 }) |
| 17 | }) |
| 18 | it('moves both axes at a corner, and far tiles all the way', () => { |
| 19 | expect(shift(16, 28)).toEqual({ x: 3, y: 3 }) |
| 20 | }) |
| 21 | }) |
| 22 | |
| 23 | describe('revealShift on a board', () => { |
| 24 | it('gives only the shift the board allows', () => { |
| 25 | // The view's right edge is at 14.5; a board 15 wide lets it go 0.5 further, not 1. |
| 26 | expect(revealShift(aim, { x: 14, y: 20 }, 9, 13, 1, { w: 15, h: 40 })).toEqual({ x: 0.5, y: 0 }) |
| 27 | }) |
| 28 | it('is zero when the view is at the board edge, or the board is smaller than the view', () => { |
| 29 | expect(revealShift({ x: 10.5, y: 20 }, { x: 14, y: 20 }, 9, 13, 1, { w: 15, h: 40 })).toEqual({ x: 0, y: 0 }) |
| 30 | expect(revealShift({ x: 4, y: 20 }, { x: 7, y: 20 }, 9, 13, 1, { w: 8, h: 40 })).toEqual({ x: 0, y: 0 }) |
| 31 | }) |
| 32 | }) |