-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathpostActivity.success.story.js
More file actions
86 lines (69 loc) · 2.94 KB
/
postActivity.success.story.js
File metadata and controls
86 lines (69 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
import fetch from 'node-fetch';
import { ConnectionStatus } from '../../src/directLine';
import { DirectLineStreaming } from '../../src/directLineStreaming';
import activityTimestampComparer from './__setup__/activityTimestampComparer';
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 { 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 }
);
// THEN: Should receive "Hello and welcome!"
await waitFor(() =>
expect(activityObserver).toHaveProperty('observations', [
[expect.any(Number), 'next', expect.activityContaining('Hello and welcome!')]
])
);
// ---
// WHEN: Send a message to the bot.
const postActivityObserver = mockObserver();
directLine
.postActivity({
text: 'Hello, World!',
type: 'message'
})
.subscribe(postActivityObserver);
// THEN: Should send successfully and completed the observable.
await waitFor(() =>
expect(postActivityObserver).toHaveProperty('observations', [
[expect.any(Number), 'next', expect.any(String)][(expect.any(Number), 'complete')]
])
);
// THEN: Bot should reply and the activity should echo back.
await waitFor(
() =>
expect([...activityObserver.observations].sort(([, , x], [, , y]) => activityTimestampComparer(x, y))).toEqual([
[expect.any(Number), 'next', expect.activityContaining('Hello and welcome!')],
[
expect.any(Number),
'next',
expect.activityContaining('Hello, World!', {
id: postActivityObserver.observations[0][2]
})
],
[expect.any(Number), 'next', expect.activityContaining('Echo: Hello, World!')]
]),
{ timeout: 5000 }
);
}, 15000);