From ee59fa7d9859389a066f05ce077ea16033c62972 Mon Sep 17 00:00:00 2001 From: Drikus Roor Date: Sat, 23 Dec 2023 21:59:30 +0100 Subject: [PATCH] test: Add tests for VisualMatchingPairs component --- .../src/components/PlayButton/PlayCard.js | 22 +++- .../VisualMatchingPairs.js | 11 +- .../VisualMatchingPairs.test.js | 120 ++++++++++++++++++ 3 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 frontend/src/components/VisualMatchingPairs/VisualMatchingPairs.test.js diff --git a/frontend/src/components/PlayButton/PlayCard.js b/frontend/src/components/PlayButton/PlayCard.js index c2aa6bca0..f2839f033 100644 --- a/frontend/src/components/PlayButton/PlayCard.js +++ b/frontend/src/components/PlayButton/PlayCard.js @@ -14,10 +14,24 @@ const PlayCard = ({ onClick, registerUserClicks, playing, section, view }) => { } return ( -
{ - registerUserClicks(event.clientX, event.clientY); - onClick(); - }}> +
{ + registerUserClicks(event.clientX, event.clientY); + onClick(); + }} + role="button" + > {section.turned ? view === MATCHINGPAIRS ? { />
-
Score:
{total}
+
+ Score:
{total} +
@@ -252,7 +254,12 @@ const VisualMatchingPairs = (props) => { ) )} -
+
) } diff --git a/frontend/src/components/VisualMatchingPairs/VisualMatchingPairs.test.js b/frontend/src/components/VisualMatchingPairs/VisualMatchingPairs.test.js new file mode 100644 index 000000000..cf639b375 --- /dev/null +++ b/frontend/src/components/VisualMatchingPairs/VisualMatchingPairs.test.js @@ -0,0 +1,120 @@ +import React from 'react'; +import { render, fireEvent, waitFor } from '@testing-library/react'; +import VisualMatchingPairs from './VisualMatchingPairs'; // Adjust the import path as necessary + +// Mock data for sections +const mockSections = [ + { id: 1, content: 'Card 1', url: '/cat-01.jpg', inactive: false, turned: false, noevents: false, seen: false, group: 1 }, + { id: 2, content: 'Card 2', url: '/cat-02.jpg', inactive: false, turned: false, noevents: false, seen: false, group: 2 }, + { id: 3, content: 'Card 1', url: '/cat-01.jpg', inactive: false, turned: false, noevents: false, seen: false, group: 1 }, + { id: 4, content: 'Card 2', url: '/cat-02.jpg', inactive: false, turned: false, noevents: false, seen: false, group: 2 }, +]; + +describe('VisualMatchingPairs', () => { + it('renders correctly', () => { + const { getByText } = render(); + expect(getByText('Pick a card')).toBeInTheDocument(); + }); + + it('flips a card when clicked', async () => { + const { getAllByTestId } = render(); + const cards = getAllByTestId('play-card'); // Assuming each card is a button, adjust if necessary + + fireEvent.click(cards[0]); + + await waitFor(() => expect(cards[0]).toHaveClass('turned')); + }); + + it('updates score after a match', async () => { + const { getAllByTestId, getByTestId } = render(); + const cards = getAllByTestId('play-card'); + + fireEvent.click(cards[0]); + fireEvent.click(cards[1]); + + await waitFor(() => expect(getByTestId('score')).toBeInTheDocument()); + expect(getByTestId('score')).toHaveTextContent('100'); + }); + + it('does not update score for a non-matching pair', async () => { + const { getAllByTestId, getByTestId } = render(); + const cards = getAllByTestId('play-card'); + + fireEvent.click(cards[0]); + fireEvent.click(cards[1]); + + await waitFor(() => expect(getByTestId('score')).toBeInTheDocument()); + expect(getByTestId('score')).toHaveTextContent('100'); // Assuming 100 is the initial score + }); + + it('ends the game when all pairs are matched', async () => { + const finishedPlaying = jest.fn(); + const submitResult = jest.fn(); + const { getAllByTestId, getByTestId, queryByText } = render( + + ); + const cards = getAllByTestId('play-card'); + + fireEvent.click(cards[0]); + fireEvent.click(cards[2]); + fireEvent.click(getByTestId('overlay')); + fireEvent.click(cards[1]); + fireEvent.click(cards[3]); + fireEvent.click(getByTestId('overlay')); + + await waitFor(() => { + expect(queryByText('Pick a card')).not.toBeInTheDocument(); + expect(submitResult).toHaveBeenCalled(); + }); + }); + + it('disables card clicks in-between turns', async () => { + const { getAllByTestId } = render(); + const cards = getAllByTestId('play-card'); + + await waitFor(() => expect(cards[0]).not.toHaveClass('noevents')); + await waitFor(() => expect(cards[1]).not.toHaveClass('noevents')); + + fireEvent.click(cards[0]); + fireEvent.click(cards[1]); + + await waitFor(() => expect(cards[0]).toHaveClass('noevents')); + await waitFor(() => expect(cards[1]).toHaveClass('noevents')); + }); + + it('calculates the correct score for a match', async () => { + const finishedPlaying = jest.fn(); + const submitResult = jest.fn(); + const { getAllByTestId, getByTestId } = render(); + const cards = getAllByTestId('play-card'); + + fireEvent.click(cards[0]); + fireEvent.click(cards[1]); + fireEvent.click(getByTestId('overlay')); + + // No match, no score + await waitFor(() => expect(getByTestId('score')).toHaveTextContent('100')); + + fireEvent.click(cards[0]); + fireEvent.click(cards[3]); + fireEvent.click(getByTestId('overlay')); + + // No match, no score + await waitFor(() => expect(getByTestId('score')).toHaveTextContent('100')); + + fireEvent.click(cards[0]); + fireEvent.click(cards[2]); + fireEvent.click(getByTestId('overlay')); + + // Lucky match, +10 + await waitFor(() => expect(getByTestId('score')).toHaveTextContent('110')); + + fireEvent.click(cards[1]); + fireEvent.click(cards[3]); + fireEvent.click(getByTestId('overlay')); + + // Known match, +20 + await waitFor(() => expect(getByTestId('score')).toHaveTextContent('130')); + }); +}); +