Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated testing with Jest #44

Open
efilion opened this issue Aug 4, 2022 · 2 comments
Open

Automated testing with Jest #44

efilion opened this issue Aug 4, 2022 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@efilion
Copy link

efilion commented Aug 4, 2022

Had a hell of a time trying to figure this one out. Thought I would share the solution I found here to help anyone else trying to set up automated Jest tests with ReactChessboard.

Solution for writing automated tests with Jest

  1. npx create-react-app react-chessboard-jest-testing, cd react-chessboard-jest-testing
  2. npm i react-chessboard
  3. npm i --save-dev react-dnd-test-utils react-dnd-test-backend

Getting Jest to play nicely with ESM modules

package.json

{
  ...
  "jest": {
    "transformIgnorePatterns": [
      "/node_modules/(?!(@?react-dnd[^/]*|dnd-core)/)"
    ]
  }
}

Writing the test

src/App.test.js

import React from 'react';
import { act } from 'react-dom/test-utils';
import { cleanup, render } from '@testing-library/react';
import { fireDragDrop, tick } from 'react-dnd-test-utils';
import { Chessboard as ReactChessboard } from 'react-chessboard';

/* act and await tick to ensure setup animation completes  
 * (and sets waitingForAnimation to false) before running assertions
 */
beforeEach(async () => {
  await act(async () => {
    const props = {
      position: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", // starting position
      onPieceDrop: () => true,
      animationDuration: 0, // **important** to not slow down your tests
    }
    render (<ReactChessboard {...props} />);
    await tick();
  })
})

afterEach(() => {
  cleanup()
})

test('e2 to e4', async () => {
  let e2pawn = document.querySelector('[data-square=e2] [draggable]');
  let e4square = document.querySelector('[data-square=e4]')

  expect(e2pawn).toBeInTheDocument();
  expect(e4square.querySelector('[draggable]')).not.toBeInTheDocument();
  await fireDragDrop(e2pawn, e4square);
  expect(e2pawn).not.toBeInTheDocument();
  expect(e4square.querySelector('[draggable]')).toBeInTheDocument();
})

Tested on:

Brainstorming possible improvements

Using querySelector is not necessarily a best practice. By adding aria roles and labels, those calls could be replaced with the more standard testing-library queries.

test('can find white pawn', () => {
  render(
    <>
      <div role="gridcell" aria-label="e2 square">
          <div role="widget" aria-label="white e2 pawn" />
      </div>
    </>
  )

  let e2pawn = screen.getByRole('widget', { name: /white e2 pawn/i });
  expect(e2pawn).toBeInTheDocument();
})

I'm by no means an expert on accessibility. I went to both chess.com and lichess.org to see how they approach this, and it seems that they don't yet have any support for screen readers. Would be nice to get input from someone more knowledgeable about accessibility before making these changes and to think more broadly about the experience for visually impaired players.

@Clariity
Copy link
Owner

Clariity commented Aug 5, 2022

This is incredible, I've only had a brief read through as I'm very busy lately but when I get time I will try to turn this into some documentation that everyone can reference if they need to test, and I'll try to have a think about how testing could be improved/made easier as you suggested

Thank you so much for documenting this

@Clariity Clariity added the documentation Improvements or additions to documentation label Aug 5, 2022
@rahulbhai9
Copy link

Hello, I am Rahul a blind person. I am regular screen reader user. As mentioned here I also notice that the library does not work well with the screen readers. . This can have huge impact. any website wich uses this library, will be completely in accessible for blind people like me. If allowed ,I would like to work and help in fixing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants