Skip to content

Commit

Permalink
added testcases for BotStatesContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Suvrat1629 committed Oct 21, 2024
1 parent fa4fa25 commit 6ffa9f6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
73 changes: 73 additions & 0 deletions __tests__/components/context/BotStatesContext.test.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<p>Bot Typing: {isBotTyping ? 'Yes' : 'No'}</p>
<button onClick={() => setIsBotTyping(!isBotTyping)}>
Toggle Bot Typing
</button>
<p>Chat Window Open: {isChatWindowOpen ? 'Yes' : 'No'}</p>
<button onClick={() => setIsChatWindowOpen(!isChatWindowOpen)}>
Toggle Chat Window
</button>
</div>
);
};

describe('BotStatesProvider', () => {
const renderWithProvider = (children: React.ReactNode, settings = {}) => {
return render(
<BotStatesProvider settings={settings}>
{children}
</BotStatesProvider>
);
};

it('should initialize with default values', () => {
renderWithProvider(<TestComponent />);

expect(screen.getByText(/Bot Typing: No/i)).toBeInTheDocument();
expect(screen.getByText(/Chat Window Open: No/i)).toBeInTheDocument();
});

it('should toggle bot typing state', () => {
renderWithProvider(<TestComponent />);

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(<TestComponent />);

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(<TestComponent />, settings);

expect(screen.getByText(/Chat Window Open: Yes/i)).toBeInTheDocument();
});
});
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6ffa9f6

Please sign in to comment.