Skip to content

Commit 96a7c68

Browse files
Updating tests and docs
1 parent d32a221 commit 96a7c68

File tree

7 files changed

+39
-228
lines changed

7 files changed

+39
-228
lines changed

CHANGELOG.md

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,57 +23,6 @@
2323

2424
- On upgrading to this release, ensure events and any custom handling functions (ex: `onAddedToChannel` or `onMessageNew`) are properly processed and update the list UI as expected.
2525

26-
*ChannelContext*
27-
28-
- We have split the `ChannelContext` into three separate contexts to further modularize the code and reduce renders as items in context change. The following contexts now contain the following values, previously all held within the `ChannelContext`:
29-
30-
- `ChannelContext`:
31-
32-
- `channel`
33-
- `disabled`
34-
- `EmptyStateIndicator`
35-
- `error`
36-
- `eventHistory`
37-
- `lastRead`
38-
- `loading`
39-
- `markRead`
40-
- `members`
41-
- `read`
42-
- `setLastRead`
43-
- `typing`
44-
- `watcherCount`
45-
- `watchers`
46-
47-
- `MessagesContext`
48-
49-
- `Attachment`
50-
- `clearEditingState`
51-
- `editing`
52-
- `editMessage`
53-
- `emojiData`
54-
- `hasMore`
55-
- `loadingMore`
56-
- `loadMore`
57-
- `Message`
58-
- `messages`
59-
- `removeMessage`
60-
- `retrySendMessage`
61-
- `sendMessage`
62-
- `setEditingState`
63-
- `updateMessage`
64-
65-
- `ThreadContext`
66-
67-
- `closeThread`
68-
- `loadMoreThread`
69-
- `openThread`
70-
- `thread`
71-
- `threadHasMore`
72-
- `threadLoadingMore`
73-
- `threadMessages`
74-
75-
- All contexts are exported and any values can be accessed through a higher order component (ex: `withMessagesContext`) or the `useContext` hook (ex: `const { messages } = useContext(MessagesContext);`)
76-
7726
*FileUploadPreview*
7827

7928
- We fixed a bug for being unable to remove a file from the `MessageInput` and made it consistent to `ImageUploadPreview`

src/components/Channel/__tests__/Channel.test.js

Lines changed: 24 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@ import { getTestClientWithUser } from 'mock-builders/mock';
1313

1414
import Channel from '../Channel';
1515

16-
import Attachment from '../../Attachment/Attachment';
1716
import Chat from '../../Chat/Chat';
1817

19-
import {
20-
ChannelContext,
21-
MessagesContext,
22-
ThreadContext,
23-
} from '../../../context';
18+
import { ChannelContext } from '../../../context';
2419

2520
// This component is used for performing effects in a component that consumes ChannelContext,
2621
// i.e. making use of the callbacks & values provided by the Channel component.
@@ -173,17 +168,13 @@ describe('Channel', () => {
173168
const hasThread = jest.fn();
174169
// this renders Channel, calls openThread from a child context consumer with a message,
175170
// and then calls hasThread with the thread id if it was set.
176-
renderComponent(
177-
{ channel },
178-
({ openThread, thread }) => {
179-
if (!thread) {
180-
openThread(threadMessage);
181-
} else {
182-
hasThread(thread.id);
183-
}
184-
},
185-
ThreadContext,
186-
);
171+
renderComponent({ channel }, ({ openThread, thread }) => {
172+
if (!thread) {
173+
openThread(threadMessage);
174+
} else {
175+
hasThread(thread.id);
176+
}
177+
});
187178
await waitFor(() =>
188179
expect(hasThread).toHaveBeenCalledWith(threadMessage.id),
189180
);
@@ -220,7 +211,6 @@ describe('Channel', () => {
220211
channelHasMore = hasMore;
221212
}
222213
},
223-
MessagesContext,
224214
);
225215
await waitFor(() => expect(channelHasMore).toBe(false));
226216
});
@@ -230,14 +220,10 @@ describe('Channel', () => {
230220

231221
const newMessages = [generateMessage()];
232222

233-
renderComponent(
234-
{ channel },
235-
({ loadMore }) => {
236-
useMockedApis(chatClient, [queryChannelWithNewMessages(newMessages)]);
237-
loadMore(limit);
238-
},
239-
MessagesContext,
240-
);
223+
renderComponent({ channel }, ({ loadMore }) => {
224+
useMockedApis(chatClient, [queryChannelWithNewMessages(newMessages)]);
225+
loadMore(limit);
226+
});
241227

242228
await waitFor(() => expect(channelQuerySpy).toHaveBeenCalled());
243229
});
@@ -246,16 +232,12 @@ describe('Channel', () => {
246232
const queryPromise = new Promise(() => {});
247233
let isLoadingMore = false;
248234

249-
renderComponent(
250-
{ channel },
251-
({ loadingMore, loadMore }) => {
252-
// return a promise that hasn't resolved yet, so loadMore will be stuck in the 'await' part of the function
253-
jest.spyOn(channel, 'query').mockImplementationOnce(() => queryPromise);
254-
loadMore();
255-
isLoadingMore = loadingMore;
256-
},
257-
MessagesContext,
258-
);
235+
renderComponent({ channel }, ({ loadingMore, loadMore }) => {
236+
// return a promise that hasn't resolved yet, so loadMore will be stuck in the 'await' part of the function
237+
jest.spyOn(channel, 'query').mockImplementationOnce(() => queryPromise);
238+
loadMore();
239+
isLoadingMore = loadingMore;
240+
});
259241

260242
await waitFor(() => expect(isLoadingMore).toBe(true));
261243
});
@@ -265,13 +247,9 @@ describe('Channel', () => {
265247
const updatedMessage = { ...messages[0], text: newText };
266248
const clientUpdateMessageSpy = jest.spyOn(chatClient, 'updateMessage');
267249

268-
renderComponent(
269-
{ channel },
270-
({ editMessage }) => {
271-
editMessage(updatedMessage);
272-
},
273-
MessagesContext,
274-
);
250+
renderComponent({ channel }, ({ editMessage }) => {
251+
editMessage(updatedMessage);
252+
});
275253

276254
await waitFor(() =>
277255
expect(clientUpdateMessageSpy).toHaveBeenCalledWith(updatedMessage),
@@ -280,13 +258,9 @@ describe('Channel', () => {
280258

281259
it('should use doUpdateMessageRequest for the editMessage callback if provided', async () => {
282260
const doUpdateMessageRequest = jest.fn((channelId, message) => message);
283-
renderComponent(
284-
{ channel, doUpdateMessageRequest },
285-
({ editMessage }) => {
286-
editMessage(messages[0]);
287-
},
288-
MessagesContext,
289-
);
261+
renderComponent({ channel, doUpdateMessageRequest }, ({ editMessage }) => {
262+
editMessage(messages[0]);
263+
});
290264

291265
await waitFor(() =>
292266
expect(doUpdateMessageRequest).toHaveBeenCalledWith(
@@ -311,7 +285,6 @@ describe('Channel', () => {
311285
allMessagesRemoved = true;
312286
}
313287
},
314-
MessagesContext,
315288
);
316289

317290
await waitFor(() => {
@@ -361,88 +334,4 @@ describe('Channel', () => {
361334
});
362335
});
363336
});
364-
365-
describe('MessagesContext', () => {
366-
it('renders children without crashing', async () => {
367-
const { getByTestId } = render(
368-
<MessagesContext.Provider>
369-
<View testID='children' />
370-
</MessagesContext.Provider>,
371-
);
372-
373-
await waitFor(() => expect(getByTestId('children')).toBeTruthy());
374-
});
375-
376-
it('exposes the messages context', async () => {
377-
let context;
378-
379-
const mockContext = {
380-
Attachment,
381-
editing: false,
382-
messages,
383-
sendMessage: () => {},
384-
};
385-
386-
render(
387-
<MessagesContext.Provider value={mockContext}>
388-
<ContextConsumer
389-
context={MessagesContext}
390-
fn={(ctx) => {
391-
context = ctx;
392-
}}
393-
></ContextConsumer>
394-
</MessagesContext.Provider>,
395-
);
396-
397-
await waitFor(() => {
398-
expect(context).toBeInstanceOf(Object);
399-
expect(context.Attachment).toBeInstanceOf(Function);
400-
expect(context.editing).toBe(false);
401-
expect(context.messages).toBeInstanceOf(Array);
402-
expect(context.sendMessage).toBeInstanceOf(Function);
403-
});
404-
});
405-
});
406-
407-
describe('ThreadContext', () => {
408-
it('renders children without crashing', async () => {
409-
const { getByTestId } = render(
410-
<ThreadContext.Provider>
411-
<View testID='children' />
412-
</ThreadContext.Provider>,
413-
);
414-
415-
await waitFor(() => expect(getByTestId('children')).toBeTruthy());
416-
});
417-
418-
it('exposes the thread context', async () => {
419-
let context;
420-
421-
const mockContext = {
422-
openThread: () => {},
423-
thread: {},
424-
threadHasMore: true,
425-
threadLoadingMore: false,
426-
};
427-
428-
render(
429-
<ThreadContext.Provider value={mockContext}>
430-
<ContextConsumer
431-
context={ThreadContext}
432-
fn={(ctx) => {
433-
context = ctx;
434-
}}
435-
></ContextConsumer>
436-
</ThreadContext.Provider>,
437-
);
438-
439-
await waitFor(() => {
440-
expect(context).toBeInstanceOf(Object);
441-
expect(context.openThread).toBeInstanceOf(Function);
442-
expect(context.thread).toBeInstanceOf(Object);
443-
expect(context.threadHasMore).toBe(true);
444-
expect(context.threadLoadingMore).toBe(false);
445-
});
446-
});
447-
});
448337
});

src/components/MessageInput/__tests__/SendButton.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import { fireEvent, render, waitFor } from '@testing-library/react-native';
33

44
import SendButton from '../SendButton';
55

6-
import { MessagesContext } from '../../../context';
6+
import { ChannelContext } from '../../../context';
77

88
describe('SendButton', () => {
99
it('should render a non-editing enabled SendButton', async () => {
1010
const sendMessage = jest.fn();
1111

1212
const { getByTestId, queryByTestId, toJSON } = render(
13-
<MessagesContext.Provider value={{ editing: false }}>
13+
<ChannelContext.Provider value={{ editing: false }}>
1414
<SendButton sendMessage={sendMessage} />
15-
</MessagesContext.Provider>,
15+
</ChannelContext.Provider>,
1616
);
1717

1818
await waitFor(() => {
@@ -35,9 +35,9 @@ describe('SendButton', () => {
3535
const sendMessage = jest.fn();
3636

3737
const { getByTestId, queryByTestId, toJSON } = render(
38-
<MessagesContext.Provider value={{ editing: false }}>
38+
<ChannelContext.Provider value={{ editing: false }}>
3939
<SendButton disabled sendMessage={sendMessage} />
40-
</MessagesContext.Provider>,
40+
</ChannelContext.Provider>,
4141
);
4242

4343
await waitFor(() => {
@@ -60,9 +60,9 @@ describe('SendButton', () => {
6060
const sendMessage = jest.fn();
6161

6262
const { getByTestId, queryByTestId, toJSON } = render(
63-
<MessagesContext.Provider value={{ editing: true }}>
63+
<ChannelContext.Provider value={{ editing: true }}>
6464
<SendButton sendMessage={sendMessage} />
65-
</MessagesContext.Provider>,
65+
</ChannelContext.Provider>,
6666
);
6767

6868
await waitFor(() => {
@@ -85,9 +85,9 @@ describe('SendButton', () => {
8585
const sendMessage = jest.fn();
8686

8787
const { getByTestId, queryByTestId, toJSON } = render(
88-
<MessagesContext.Provider value={{ editing: true }}>
88+
<ChannelContext.Provider value={{ editing: true }}>
8989
<SendButton disabled sendMessage={sendMessage} />
90-
</MessagesContext.Provider>,
90+
</ChannelContext.Provider>,
9191
);
9292

9393
await waitFor(() => {

src/components/Thread/__tests__/Thread.test.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ import Thread from '../Thread';
1818
import Channel from '../../Channel/Channel';
1919
import Chat from '../../Chat/Chat';
2020

21-
import {
22-
ChannelContext,
23-
ThreadContext,
24-
TranslationContext,
25-
} from '../../../context';
21+
import { ChannelContext, TranslationContext } from '../../../context';
2622
import { Streami18n } from '../../../utils/Streami18n';
2723

2824
const StreamReactNativeNamespace = '9b244ee4-7d69-4d7b-ae23-cf89e9f7b035';
@@ -61,12 +57,12 @@ describe('Thread', () => {
6157
<Chat client={chatClient}>
6258
<TranslationContext.Provider value={{ ...translators, t }}>
6359
<Channel channel={channel} client={chatClient} thread={thread}>
64-
<ThreadContext.Consumer>
60+
<ChannelContext.Consumer>
6561
{(c) => {
6662
openThread = c.openThread;
6763
return <Thread thread={thread} />;
6864
}}
69-
</ThreadContext.Consumer>
65+
</ChannelContext.Consumer>
7066
</Channel>
7167
</TranslationContext.Provider>
7268
</Chat>,

src/components/docs/Channel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
></div>
1616
```
1717

18-
**Note:** The Channel component provides access to the values stored in [ChannelContext](#channelcontext), [MessagesContext](#messagescontext), and [ThreadContext](#threadcontext) and exposes the [withChannelContext](#withchannelcontext), [withMessagesContext](#withmessagescontext), and [withThreadContext](#withthreadcontext) higher order components.
18+
**Note:** The Channel component provides access to the values stored in [ChannelContext](#channelcontext) and exposes the [withChannelContext](#withchannelcontext) higher order components.
1919

2020
The example below shows how to write a component that consumes a context through a higher order component.
2121

src/components/docs/SendButton.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Disabled send button
1313
Send edited message button
1414

1515
```js
16-
<MessagesContext.Provider value={{ editing: true }}>
16+
<ChannelContext.Provider value={{ editing: true }}>
1717
<SendButton sendMessage={() => {}} />
18-
</MessagesContext.Provider>
18+
</ChannelContext.Provider>
1919
```

0 commit comments

Comments
 (0)