-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathend.story.js
More file actions
90 lines (71 loc) · 2.94 KB
/
end.story.js
File metadata and controls
90 lines (71 loc) · 2.94 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
import { ConnectionStatus } from '../../src/directLine';
import { DirectLineStreaming } from '../../src/directLineStreaming';
import waitFor from './__setup__/external/testing-library/waitFor';
import mockObserver from './__setup__/mockObserver';
import setupBotProxy from './__setup__/setupBotProxy';
const TOKEN_URL =
'https://hawo-mockbot4-token-app.ambitiousflower-67725bfd.westus.azurecontainerapps.io/api/token/directlinease?bot=echo%20bot';
afterEach(() => jest.useRealTimers());
test('should connect', async () => {
jest.useFakeTimers({ now: 0 });
const { domain, token } = await fetch(TOKEN_URL, { method: 'POST' }).then(res => res.json());
const { directLineStreamingURL } = await setupBotProxy({ 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 }
);
// ---
// WHEN: Call end().
directLine.end();
// THEN: Should observe "Uninitialized" -> "Connecting" -> "Online" -> "Ended" -> Complete.
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.Ended],
[expect.any(Number), 'complete']
])
);
// ---
// WHEN: Send a message after disconnection.
const postActivityObserver = mockObserver();
directLine
.postActivity({
text: 'Hello, World!',
type: 'message'
})
.subscribe(postActivityObserver);
// THEN: Should fail all postActivity() calls.
await waitFor(() =>
expect(postActivityObserver).toHaveProperty('observations', [[expect.any(Number), 'error', expect.any(Error)]])
);
// THEN: Should complete activity$.
await waitFor(() =>
expect(activityObserver).toHaveProperty('observations', [
[expect.any(Number), 'next', expect.activityContaining('Hello and welcome!')],
[expect.any(Number), 'complete']
])
);
// THEN: Call reconnect() should throw.
expect(() =>
directLine.reconnect({
conversationId: directLine.conversationId,
token: directLine.token
})
).toThrow('Connection has ended.');
});