Skip to content

Commit 689514c

Browse files
authored
fix: reflect thread prop in Window component (#1919)
1 parent e9a59ec commit 689514c

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/components/Window/Window.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { PropsWithChildren } from 'react';
2+
import clsx from 'clsx';
23

34
import { StreamMessage, useChannelStateContext } from '../../context/ChannelStateContext';
45

@@ -9,7 +10,7 @@ export type WindowProps<
910
> = {
1011
/** show or hide the window when a thread is active */
1112
hideOnThread?: boolean;
12-
/** optional prop to manually trigger the opening of a thread*/
13+
/** optional prop to force addition of class str-chat__main-panel--hideOnThread to the Window root element */
1314
thread?: StreamMessage<StreamChatGenerics>;
1415
};
1516

@@ -18,15 +19,15 @@ const UnMemoizedWindow = <
1819
>(
1920
props: PropsWithChildren<WindowProps<StreamChatGenerics>>,
2021
) => {
21-
const { children, hideOnThread = false } = props;
22+
const { children, hideOnThread = false, thread: propThread } = props;
2223

23-
const { thread } = useChannelStateContext<StreamChatGenerics>('Window');
24+
const { thread: contextThread } = useChannelStateContext<StreamChatGenerics>('Window');
2425

2526
return (
2627
<div
27-
className={`str-chat__main-panel ${
28-
hideOnThread && thread ? 'str-chat__main-panel--hideOnThread' : ''
29-
}`}
28+
className={clsx('str-chat__main-panel', {
29+
'str-chat__main-panel--hideOnThread': hideOnThread && (contextThread || propThread),
30+
})}
3031
>
3132
{children}
3233
</div>

src/components/Window/__tests__/Window.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const renderComponent = ({ channelStateContextMock, children, props }) =>
1515
);
1616

1717
const thread = generateMessage();
18+
const HIDE_CLASS_NAME = 'str-chat__main-panel--hideOnThread';
1819

1920
describe('Window', () => {
2021
it('should render its children if hideOnThread is false and thread is truthy', () => {
@@ -40,4 +41,30 @@ describe('Window', () => {
4041

4142
expect(getByText('bla')).toBeInTheDocument();
4243
});
44+
it.each([
45+
['not add', 'truthy', 'falsy', 'falsy', true, undefined, undefined],
46+
['add', 'truthy', 'truthy', 'falsy', true, thread, undefined],
47+
['add', 'truthy', 'falsy', 'truthy', true, undefined, thread],
48+
['add', 'truthy', 'truthy', 'truthy', true, thread, thread],
49+
['not add', 'falsy', 'falsy', 'falsy', false, undefined, undefined],
50+
['not add', 'falsy', 'truthy', 'falsy', false, thread, undefined],
51+
['not add', 'falsy', 'falsy', 'truthy', false, undefined, thread],
52+
['not add', 'falsy', 'truthy', 'truthy', false, thread, thread],
53+
])(
54+
'should %s class str-chat__main-panel--hideOnThread if hideOnThread is %s, prop thread is %s, context thread is %s',
55+
(expectation, _, __, ___, hideOnThread, propThread, contextThread) => {
56+
const { container } = renderComponent({
57+
channelStateContextMock: {
58+
thread: contextThread,
59+
},
60+
children: [<div key='bla'>bla</div>],
61+
props: { hideOnThread, thread: propThread },
62+
});
63+
64+
if (expectation === 'add')
65+
expect(container.querySelector(`.${HIDE_CLASS_NAME}`)).toBeInTheDocument();
66+
if (expectation === 'not add')
67+
expect(container.querySelector(`.${HIDE_CLASS_NAME}`)).not.toBeInTheDocument();
68+
},
69+
);
4370
});

0 commit comments

Comments
 (0)