|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { render, screen, fireEvent } from "@testing-library/react"; |
| 3 | +import { JostickController } from "./jostick-controller"; |
| 4 | +import "@testing-library/jest-dom"; |
| 5 | + |
| 6 | +describe("JostickController", () => { |
| 7 | + const defaultMessage = { |
| 8 | + joystickDirection: "IDLE", |
| 9 | + buttonJostick: "off", |
| 10 | + remoteGyroscope: "LEVEL", |
| 11 | + }; |
| 12 | + |
| 13 | + const mockOnDirection = vi.fn(); |
| 14 | + |
| 15 | + it("renders all control buttons", () => { |
| 16 | + render( |
| 17 | + <JostickController |
| 18 | + recibedMessage={defaultMessage} |
| 19 | + onDirection={mockOnDirection} |
| 20 | + /> |
| 21 | + ); |
| 22 | + |
| 23 | + expect(screen.getByTestId("joystick-arriba")).toBeInTheDocument(); |
| 24 | + expect(screen.getByTestId("joystick-abajo")).toBeInTheDocument(); |
| 25 | + expect(screen.getByTestId("joystick-izquierda")).toBeInTheDocument(); |
| 26 | + expect(screen.getByTestId("joystick-derecha")).toBeInTheDocument(); |
| 27 | + expect(screen.getByTestId("joystick-center")).toBeInTheDocument(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("calls onDirection with 'Arriba' when the top button is clicked", () => { |
| 31 | + render( |
| 32 | + <JostickController |
| 33 | + recibedMessage={defaultMessage} |
| 34 | + onDirection={mockOnDirection} |
| 35 | + /> |
| 36 | + ); |
| 37 | + |
| 38 | + fireEvent.click(screen.getByTestId("joystick-arriba")); |
| 39 | + expect(mockOnDirection).toHaveBeenCalledWith("Arriba"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("calls onDirection with 'Abajo' when the bottom button is clicked", () => { |
| 43 | + render( |
| 44 | + <JostickController |
| 45 | + recibedMessage={defaultMessage} |
| 46 | + onDirection={mockOnDirection} |
| 47 | + /> |
| 48 | + ); |
| 49 | + |
| 50 | + fireEvent.click(screen.getByTestId("joystick-abajo")); |
| 51 | + expect(mockOnDirection).toHaveBeenCalledWith("Abajo"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("calls onDirection with 'Izquierda' when the left button is clicked", () => { |
| 55 | + render( |
| 56 | + <JostickController |
| 57 | + recibedMessage={defaultMessage} |
| 58 | + onDirection={mockOnDirection} |
| 59 | + /> |
| 60 | + ); |
| 61 | + |
| 62 | + fireEvent.click(screen.getByTestId("joystick-izquierda")); |
| 63 | + expect(mockOnDirection).toHaveBeenCalledWith("Izquierda"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("calls onDirection with 'Derecha' when the right button is clicked", () => { |
| 67 | + render( |
| 68 | + <JostickController |
| 69 | + recibedMessage={defaultMessage} |
| 70 | + onDirection={mockOnDirection} |
| 71 | + /> |
| 72 | + ); |
| 73 | + |
| 74 | + fireEvent.click(screen.getByTestId("joystick-derecha")); |
| 75 | + expect(mockOnDirection).toHaveBeenCalledWith("Derecha"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("calls onDirection with 'CENTER' when the center button is clicked", () => { |
| 79 | + render( |
| 80 | + <JostickController |
| 81 | + recibedMessage={defaultMessage} |
| 82 | + onDirection={mockOnDirection} |
| 83 | + /> |
| 84 | + ); |
| 85 | + |
| 86 | + fireEvent.click(screen.getByTestId("joystick-center")); |
| 87 | + expect(mockOnDirection).toHaveBeenCalledWith("CENTER"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("highlights the 'Arriba' button when joystickDirection is 'Arriba'", () => { |
| 91 | + const activeMessage = { ...defaultMessage, joystickDirection: "Arriba" }; |
| 92 | + render( |
| 93 | + <JostickController |
| 94 | + recibedMessage={activeMessage} |
| 95 | + onDirection={mockOnDirection} |
| 96 | + /> |
| 97 | + ); |
| 98 | + |
| 99 | + expect(screen.getByTestId("joystick-arriba")).toHaveClass("active"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("highlights the 'Izquierda' button when remoteGyroscope is 'Izquierda'", () => { |
| 103 | + const activeMessage = { ...defaultMessage, remoteGyroscope: "Izquierda" }; |
| 104 | + render( |
| 105 | + <JostickController |
| 106 | + recibedMessage={activeMessage} |
| 107 | + onDirection={mockOnDirection} |
| 108 | + /> |
| 109 | + ); |
| 110 | + |
| 111 | + expect(screen.getByTestId("joystick-izquierda")).toHaveClass("active"); |
| 112 | + }); |
| 113 | + |
| 114 | + it("highlights the center button when buttonJostick is 'on'", () => { |
| 115 | + const activeMessage = { ...defaultMessage, buttonJostick: "on" }; |
| 116 | + render( |
| 117 | + <JostickController |
| 118 | + recibedMessage={activeMessage} |
| 119 | + onDirection={mockOnDirection} |
| 120 | + /> |
| 121 | + ); |
| 122 | + |
| 123 | + expect(screen.getByTestId("joystick-center")).toHaveClass("active"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("applies the provided id to the container", () => { |
| 127 | + render( |
| 128 | + <JostickController |
| 129 | + id="custom-joystick-id" |
| 130 | + recibedMessage={defaultMessage} |
| 131 | + onDirection={mockOnDirection} |
| 132 | + /> |
| 133 | + ); |
| 134 | + |
| 135 | + const container = screen.getByTestId("jostick-container"); |
| 136 | + expect(container).toHaveAttribute("id", "custom-joystick-id"); |
| 137 | + }); |
| 138 | +}); |
0 commit comments