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 16 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();
});
});
60 changes: 60 additions & 0 deletions __tests__/context/StylesContext.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import { render, screen, act } from '@testing-library/react';
import { StylesProvider, useStylesContext } from '../../src/context/StylesContext';
import { DefaultStyles } from "../../src/constants/internal/DefaultStyles";
import '@testing-library/jest-dom';


// Mocking a child component to test context
const MockChild = () => {
const { styles, setStyles } = useStylesContext();

return (
<div>
<p>Current Styles: {JSON.stringify(styles)}</p>
<button onClick={() => setStyles({ ...styles, chatWindowStyle: { backgroundColor: 'blue' } })}>
Change Chat Window Style
</button>
</div>
);
};

// Test suite for StylesProvider
describe('StylesProvider', () => {
beforeEach(() => {
jest.clearAllMocks();
});

test('provides default styles', () => {
render(
<StylesProvider styles={DefaultStyles} setStyles={jest.fn()}>
<MockChild />
</StylesProvider>
);

// Check if the default styles are provided
const stylesElement = screen.getByText(`Current Styles: ${JSON.stringify(DefaultStyles)}`);
expect(stylesElement).toBeInTheDocument();
});

test('allows updating styles through setStyles', () => {
const setStylesMock = jest.fn();
render(
<StylesProvider styles={DefaultStyles} setStyles={setStylesMock}>
<MockChild />
</StylesProvider>
);

// Click the button to change styles
const button = screen.getByText('Change Chat Window Style');
act(() => {
button.click();
});

// Verify that setStyles was called with the updated styles
expect(setStylesMock).toHaveBeenCalledWith({
...DefaultStyles,
chatWindowStyle: { backgroundColor: 'blue' },
});
});
});
31 changes: 27 additions & 4 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@
"react-dom": ">=16.14.0 <20.0.0 || ^19.0.0-0"
},
"devDependencies": {
"@testing-library/jest-dom": "^6.5.0",
"@testing-library/jest-dom": "^6.6.2",
"@testing-library/react": "^16.0.1",
"@types/dom-speech-recognition": "^0.0.4",
"@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