-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathpostActivity.fail.story.js
More file actions
91 lines (70 loc) · 3.27 KB
/
postActivity.fail.story.js
File metadata and controls
91 lines (70 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import fetch from 'node-fetch';
import { ConnectionStatus } from '../../src/directLine';
import { DirectLineStreaming } from '../../src/directLineStreaming';
import mockObserver from './__setup__/mockObserver';
import setupBotProxy from './__setup__/setupBotProxy';
import waitFor from './__setup__/external/testing-library/waitFor';
const TOKEN_URL = 'https://hawo-mockbot4-token-app.ambitiousflower-67725bfd.westus.azurecontainerapps.io/api/token/directlinease?bot=echo%20bot';
afterEach(() => jest.useRealTimers());
test('should send activity', async () => {
jest.useFakeTimers({ now: 0 });
const onWebSocketSendMessage = jest.fn();
onWebSocketSendMessage.mockImplementation((data, socket, req, next) => next(data, socket, req));
const { domain, token } = await fetch(TOKEN_URL, { method: 'POST' }).then(res => res.json());
const { directLineStreamingURL } = await setupBotProxy({ onWebSocketSendMessage, streamingBotURL: new URL('/', domain).href });
// GIVEN: A Direct Line Streaming chat adapter.
const activityObserver = mockObserver();
const connectionStatusObserver = mockObserver();
const directLine = new DirectLineStreaming({ domain: directLineStreamingURL, token });
directLine.connectionStatus$.subscribe(connectionStatusObserver);
// ---
// WHEN: Connect.
directLine.activity$.subscribe(activityObserver);
// THEN: Should observe "Uninitialized" -> "Connecting" -> "Online".
await waitFor(
() =>
expect(connectionStatusObserver).toHaveProperty('observations', [
[expect.any(Number), 'next', ConnectionStatus.Uninitialized],
[expect.any(Number), 'next', ConnectionStatus.Connecting],
[expect.any(Number), 'next', ConnectionStatus.Online]
]),
{ timeout: 5000 }
);
// THEN: Should receive "Hello and welcome!"
await waitFor(() =>
expect(activityObserver).toHaveProperty('observations', [
[expect.any(Number), 'next', expect.activityContaining('Hello and welcome!')]
])
);
// ---
// GIVEN: Kill connection on next Web Socket message.
// This mimic TCP behavior that disconnection may not be detected until next send.
onWebSocketSendMessage.mockClear();
onWebSocketSendMessage.mockImplementationOnce((_data, socket) => socket.close());
// WHEN: Send a message to the bot.
const postActivityObserver = mockObserver();
directLine
.postActivity({
text: 'Hello, World!',
type: 'message'
})
.subscribe(postActivityObserver);
// THEN: Should send through Web Socket.
await waitFor(() => expect(onWebSocketSendMessage).toBeCalled());
// THEN: Should fail the call.
await waitFor(() =>
expect(postActivityObserver).toHaveProperty('observations', [[expect.any(Number), 'error', expect.any(Error)]])
);
// THEN: Should observe "Connecting" -> "Online" because the chat adapter should reconnect.
await waitFor(
() =>
expect(connectionStatusObserver).toHaveProperty('observations', [
[expect.any(Number), 'next', ConnectionStatus.Uninitialized],
[expect.any(Number), 'next', ConnectionStatus.Connecting],
[expect.any(Number), 'next', ConnectionStatus.Online],
[expect.any(Number), 'next', ConnectionStatus.Connecting],
[expect.any(Number), 'next', ConnectionStatus.Online]
]),
{ timeout: 5_000 }
);
}, 15000);