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 test cases for StylesContext #236

Merged
merged 16 commits into from
Oct 26, 2024
Merged
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
1 change: 1 addition & 0 deletions __tests__/context/MessagesContext.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'

import { expect } from '@jest/globals'
import { act, render, screen } from '@testing-library/react'
import '@testing-library/jest-dom/jest-globals'
Expand Down
1 change: 1 addition & 0 deletions __tests__/context/PathsContext.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'

import { expect } from '@jest/globals'
import { act, render, screen } from '@testing-library/react'
import '@testing-library/jest-dom/jest-globals'
Expand Down
61 changes: 61 additions & 0 deletions __tests__/context/StylesContext.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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' },
});
});
});
32 changes: 28 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"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",
Expand Down