Skip to content

Commit

Permalink
Added test cases for StylesContext (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
Suvrat1629 authored Oct 26, 2024
1 parent 9de0d01 commit 96d9563
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
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

0 comments on commit 96d9563

Please sign in to comment.