From 6ffa9f63f9f1bd4bdbbf25a10e2af8c1a94094a4 Mon Sep 17 00:00:00 2001 From: Suvrat1629 Date: Mon, 21 Oct 2024 17:15:08 +0530 Subject: [PATCH] added testcases for BotStatesContext --- .../context/BotStatesContext.test.tsx | 73 +++++++++++++++++++ package-lock.json | 1 + 2 files changed, 74 insertions(+) create mode 100644 __tests__/components/context/BotStatesContext.test.tsx diff --git a/__tests__/components/context/BotStatesContext.test.tsx b/__tests__/components/context/BotStatesContext.test.tsx new file mode 100644 index 00000000..cc33bfb9 --- /dev/null +++ b/__tests__/components/context/BotStatesContext.test.tsx @@ -0,0 +1,73 @@ +import React from 'react'; +import { render, screen, fireEvent } from '@testing-library/react'; +import { BotStatesProvider, useBotStatesContext } from '../../../src/context/BotStatesContext'; +import '@testing-library/jest-dom'; + + +const TestComponent = () => { + const { + isBotTyping, + setIsBotTyping, + isChatWindowOpen, + setIsChatWindowOpen, + } = useBotStatesContext(); + + return ( +
+

Bot Typing: {isBotTyping ? 'Yes' : 'No'}

+ +

Chat Window Open: {isChatWindowOpen ? 'Yes' : 'No'}

+ +
+ ); +}; + +describe('BotStatesProvider', () => { + const renderWithProvider = (children: React.ReactNode, settings = {}) => { + return render( + + {children} + + ); + }; + + it('should initialize with default values', () => { + renderWithProvider(); + + expect(screen.getByText(/Bot Typing: No/i)).toBeInTheDocument(); + expect(screen.getByText(/Chat Window Open: No/i)).toBeInTheDocument(); + }); + + it('should toggle bot typing state', () => { + renderWithProvider(); + + const toggleButton = screen.getByRole('button', { name: /Toggle Bot Typing/i }); + fireEvent.click(toggleButton); + + expect(screen.getByText(/Bot Typing: Yes/i)).toBeInTheDocument(); + }); + + it('should toggle chat window state', () => { + renderWithProvider(); + + const toggleButton = screen.getByRole('button', { name: /Toggle Chat Window/i }); + fireEvent.click(toggleButton); + + expect(screen.getByText(/Chat Window Open: Yes/i)).toBeInTheDocument(); + }); + + it('should respect initial settings', () => { + const settings = { + chatWindow: { defaultOpen: true }, + audio: { defaultToggledOn: true }, + }; + + renderWithProvider(, settings); + + expect(screen.getByText(/Chat Window Open: Yes/i)).toBeInTheDocument(); + }); +}); diff --git a/package-lock.json b/package-lock.json index 7472cf88..516b0880 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2340,6 +2340,7 @@ "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.13.tgz", "integrity": "sha512-wd+MVEZCHt23V0/L642O5APvspWply/rGY5BcW4SUETo2UzPU3Z26qr8jC2qxpimI2jjx9h7+2cj2FwIr01bXg==", "dev": true, + "license": "MIT", "dependencies": { "expect": "^29.0.0", "pretty-format": "^29.0.0"