Skip to content

Commit 7a89d46

Browse files
authored
Migrate Chess Example Tests from PR (#3025)
* test: merge in testing-library based tests for the chessboard example (@ryota-murakami ) * refactor: remove file * chore: cut semver file
1 parent d12b62d commit 7a89d46

24 files changed

+568
-103
lines changed

.pnp.js

+67
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

.yarn/versions/b852359f.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
releases:
2+
react-dnd-examples-decorators: patch
3+
react-dnd-examples-hooks: patch
4+
5+
declined:
6+
- react-dnd-documentation

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@
6868
"@babel/preset-typescript": "^7.12.13",
6969
"@commitlint/cli": "^11.0.0",
7070
"@commitlint/config-conventional": "^11.0.0",
71+
"@testing-library/jest-dom": "^5.11.9",
7172
"@testing-library/react": "^11.2.5",
7273
"@types/jest": "^26.0.20",
7374
"@types/node": "^14.14.25",
75+
"@types/testing-library__jest-dom": "^5",
7476
"@typescript-eslint/eslint-plugin": "^4.15.0",
7577
"@typescript-eslint/parser": "^4.15.0",
7678
"@wojtekmaj/enzyme-adapter-react-17": "^0.4.1",

packages/examples-decorators/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333
},
3434
"devDependencies": {
3535
"@react-dnd/build": "workspace:packages/build",
36+
"@testing-library/jest-dom": "^5.11.9",
3637
"@testing-library/react": "^11.2.5",
3738
"@types/enzyme": "^3.10.8",
3839
"@types/faker": "^5.1.6",
3940
"@types/jest": "^26.0.20",
4041
"@types/lodash": "^4.14.168",
4142
"@types/react": "^17.0.1",
4243
"@types/react-dom": "^17.0.0",
44+
"@types/testing-library__jest-dom": "^5",
4345
"enzyme": "^3.11.0",
4446
"gulp": "^4.0.2",
4547
"npm-run-all": "^4.1.5",

packages/examples-decorators/src/00-chessboard/Board.tsx

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { CSSProperties, FC } from 'react'
1+
import { CSSProperties, FC, useEffect, useState } from 'react'
22
import BoardSquare from './BoardSquare'
3+
import { Game, Position } from './Game'
34
import { Piece } from './Piece'
45

56
export interface BoardProps {
6-
knightPosition: [number, number]
7+
game: Game
78
}
89

910
/** Styling properties applied to the board element */
@@ -20,16 +21,19 @@ const squareStyle: CSSProperties = { width: '12.5%', height: '12.5%' }
2021
* The chessboard component
2122
* @param props The react props
2223
*/
23-
export const Board: FC<BoardProps> = ({
24-
knightPosition: [knightX, knightY],
25-
}) => {
24+
export const Board: FC<BoardProps> = ({ game }) => {
25+
const [[knightX, knightY], setKnightPos] = useState<Position>(
26+
game.knightPosition,
27+
)
28+
useEffect(() => game.observe(setKnightPos))
29+
2630
function renderSquare(i: number) {
2731
const x = i % 8
2832
const y = Math.floor(i / 8)
2933

3034
return (
3135
<div key={i} style={squareStyle}>
32-
<BoardSquare x={x} y={y}>
36+
<BoardSquare x={x} y={y} game={game}>
3337
<Piece isKnight={x === knightX && y === knightY} />
3438
</BoardSquare>
3539
</div>

packages/examples-decorators/src/00-chessboard/BoardSquare.tsx

+9-8
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import {
66
ConnectDropTarget,
77
} from 'react-dnd'
88
import { Square } from './Square'
9-
import { canMoveKnight, moveKnight } from './Game'
109
import { ItemTypes } from './ItemTypes'
11-
import { Overlay } from './Overlay'
10+
import { Overlay, OverlayType } from './Overlay'
11+
import { Game } from './Game'
1212

1313
export interface BoardSquareProps {
1414
x: number
1515
y: number
1616
children: any
17+
game: Game
1718

1819
// Collected Props
1920
isOver: boolean
@@ -37,20 +38,20 @@ const BoardSquare: FC<BoardSquareProps> = ({
3738
}) => {
3839
const black = (x + y) % 2 === 1
3940
return connectDropTarget(
40-
<div style={boardSquareStyle}>
41+
<div role="Space" data-testid={`(${x},${y})`} style={boardSquareStyle}>
4142
<Square black={black}>{children}</Square>
42-
{isOver && !canDrop && <Overlay color="red" />}
43-
{!isOver && canDrop && <Overlay color="yellow" />}
44-
{isOver && canDrop && <Overlay color="green" />}
43+
{isOver && !canDrop && <Overlay type={OverlayType.IllegalMoveHover} />}
44+
{!isOver && canDrop && <Overlay type={OverlayType.PossibleMove} />}
45+
{isOver && canDrop && <Overlay type={OverlayType.LegalMoveHover} />}
4546
</div>,
4647
)
4748
}
4849

4950
export default DropTarget(
5051
ItemTypes.KNIGHT,
5152
{
52-
canDrop: (props: BoardSquareProps) => canMoveKnight(props.x, props.y),
53-
drop: (props: BoardSquareProps) => moveKnight(props.x, props.y),
53+
canDrop: ({ game, x, y }: BoardSquareProps) => game.canMoveKnight(x, y),
54+
drop: ({ game, x, y }: BoardSquareProps) => game.moveKnight(x, y),
5455
},
5556
(connect: DropTargetConnector, monitor: DropTargetMonitor) => {
5657
return {
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
1-
let knightPosition: [number, number] = [1, 7]
2-
let observers: PositionObserver[] = []
3-
export type PositionObserver = ((position: [number, number]) => void) | null
1+
export type Position = [number, number]
2+
export type PositionObserver = ((position: Position) => void) | null
43

5-
function emitChange() {
6-
observers.forEach((o) => o && o(knightPosition))
7-
}
4+
export class Game {
5+
public knightPosition: Position = [1, 7]
6+
private observers: PositionObserver[] = []
87

9-
export function observe(o: PositionObserver): () => void {
10-
observers.push(o)
11-
emitChange()
8+
public observe(o: PositionObserver): () => void {
9+
this.observers.push(o)
10+
this.emitChange()
1211

13-
return (): void => {
14-
observers = observers.filter((t) => t !== o)
12+
return (): void => {
13+
this.observers = this.observers.filter((t) => t !== o)
14+
}
1515
}
16-
}
1716

18-
export function canMoveKnight(toX: number, toY: number): boolean {
19-
const [x, y] = knightPosition
20-
const dx = toX - x
21-
const dy = toY - y
17+
public moveKnight(toX: number, toY: number): void {
18+
this.knightPosition = [toX, toY]
19+
this.emitChange()
20+
}
2221

23-
return (
24-
(Math.abs(dx) === 2 && Math.abs(dy) === 1) ||
25-
(Math.abs(dx) === 1 && Math.abs(dy) === 2)
26-
)
27-
}
22+
public canMoveKnight(toX: number, toY: number): boolean {
23+
const [x, y] = this.knightPosition
24+
const dx = toX - x
25+
const dy = toY - y
2826

29-
export function moveKnight(toX: number, toY: number): void {
30-
knightPosition = [toX, toY]
31-
emitChange()
27+
return (
28+
(Math.abs(dx) === 2 && Math.abs(dy) === 1) ||
29+
(Math.abs(dx) === 1 && Math.abs(dy) === 2)
30+
)
31+
}
32+
33+
private emitChange() {
34+
const pos = this.knightPosition
35+
this.observers.forEach((o) => o && o(pos))
36+
}
3237
}

packages/examples-decorators/src/00-chessboard/Overlay.tsx

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import { FC } from 'react'
22

3+
export enum OverlayType {
4+
IllegalMoveHover = 'Illegal',
5+
LegalMoveHover = 'Legal',
6+
PossibleMove = 'Possible',
7+
}
38
export interface OverlayProps {
4-
color: string
9+
type: OverlayType
510
}
611

7-
export const Overlay: FC<OverlayProps> = ({ color }) => {
12+
export const Overlay: FC<OverlayProps> = ({ type }) => {
13+
const color = getOverlayColor(type)
814
return (
915
<div
16+
className="overlay"
17+
role={type}
1018
style={{
1119
position: 'absolute',
1220
top: 0,
@@ -20,3 +28,14 @@ export const Overlay: FC<OverlayProps> = ({ color }) => {
2028
/>
2129
)
2230
}
31+
32+
function getOverlayColor(type: OverlayType): string {
33+
switch (type) {
34+
case OverlayType.IllegalMoveHover:
35+
return 'red'
36+
case OverlayType.LegalMoveHover:
37+
return 'green'
38+
case OverlayType.PossibleMove:
39+
return 'yellow'
40+
}
41+
}

0 commit comments

Comments
 (0)