-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test cases for StylesContext (#236)
- Loading branch information
1 parent
9de0d01
commit 96d9563
Showing
5 changed files
with
92 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters