From 5aa46ebb418798180b4559867a670379ac165838 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Thu, 18 Aug 2022 22:14:56 -0500 Subject: [PATCH] test: validate that message is sent --- src/components/Room/Room.test.tsx | 68 ++++++++++++++++++++++++++++--- src/setupTests.ts | 4 ++ 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/components/Room/Room.test.tsx b/src/components/Room/Room.test.tsx index 9e93d8d..1642cd9 100644 --- a/src/components/Room/Room.test.tsx +++ b/src/components/Room/Room.test.tsx @@ -1,32 +1,69 @@ +import { PropsWithChildren } from 'react' import { render, screen } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { MemoryRouter as Router, Route, Routes } from 'react-router-dom' import { Room } from './' -const getRoomStub = () => { +const mockSender = jest.fn() + +jest.mock('trystero', () => ({ + joinRoom: () => ({ + makeAction: () => [mockSender, () => {}, () => {}], + ping: () => Promise.resolve(0), + leave: () => {}, + getPeers: () => [], + addStream: () => [Promise.resolve()], + removeStream: () => {}, + addTrack: () => [Promise.resolve()], + removeTrack: () => {}, + replaceTrack: () => [Promise.resolve()], + onPeerJoin: () => {}, + onPeerLeave: () => {}, + onPeerStream: () => {}, + onPeerTrack: () => {}, + }), +})) + +const RouteStub = ({ children }: PropsWithChildren) => { return ( - }> + ) } +jest.useFakeTimers().setSystemTime(100) + describe('Room', () => { test('is available', () => { - render(getRoomStub()) + render( + + + + ) }) test('send button is disabled', () => { - render(getRoomStub()) + render( + + + + ) + const sendButton = screen.getByText('Send') expect(sendButton).toBeDisabled() }) test('inputting text enabled send button', () => { - render(getRoomStub()) + render( + + + + ) + const sendButton = screen.getByText('Send') const textInput = screen.getByPlaceholderText('Your message') userEvent.type(textInput, 'hello') @@ -34,11 +71,30 @@ describe('Room', () => { }) test('sending a message clears the text input', () => { - render(getRoomStub()) + render( + + + + ) + const sendButton = screen.getByText('Send') const textInput = screen.getByPlaceholderText('Your message') userEvent.type(textInput, 'hello') userEvent.click(sendButton) expect(textInput).toHaveValue('') }) + + test('message is sent to peer', () => { + render( + + + + ) + + const sendButton = screen.getByText('Send') + const textInput = screen.getByPlaceholderText('Your message') + userEvent.type(textInput, 'hello') + userEvent.click(sendButton) + expect(mockSender).toHaveBeenCalledWith({ text: 'hello', timeSent: 100 }) + }) }) diff --git a/src/setupTests.ts b/src/setupTests.ts index 52aaef1..c61fade 100644 --- a/src/setupTests.ts +++ b/src/setupTests.ts @@ -3,3 +3,7 @@ // expect(element).toHaveTextContent(/react/i) // learn more: https://github.com/testing-library/jest-dom import '@testing-library/jest-dom' + +afterEach(() => { + jest.restoreAllMocks() +})