Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added testcases for BotStatesContext #242

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions __tests__/context/BotStatesContext.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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();
});
});
23 changes: 23 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@types/jest": "^29.5.13",
"@types/react": "^18.3.6",
"@types/react-dom": "^18.3.0",
"@types/testing-library__react-hooks": "^3.4.1",
"@typescript-eslint/eslint-plugin": "^5.60.1",
"@vitejs/plugin-react": "^4.0.4",
"cypress": "^13.14.2",
Expand Down