-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathcardActionMiddleware.js
More file actions
156 lines (125 loc) · 4.99 KB
/
cardActionMiddleware.js
File metadata and controls
156 lines (125 loc) · 4.99 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { By } from 'selenium-webdriver';
import { imageSnapshotOptions, timeouts } from './constants.json';
import allOutgoingActivitiesSent from './setup/conditions/allOutgoingActivitiesSent';
import getTranscript from './setup/elements/getTranscript';
import minNumActivitiesShown from './setup/conditions/minNumActivitiesShown.js';
import suggestedActionsShown from './setup/conditions/suggestedActionsShown';
import uiConnected from './setup/conditions/uiConnected';
// selenium-webdriver API doc:
// https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_WebDriver.html
jest.setTimeout(timeouts.test);
test('card action "openUrl"', async () => {
const { driver, pageObjects } = await setupWebDriver({
props: {
cardActionMiddleware:
({ dispatch }) =>
next =>
({ cardAction }) => {
if (cardAction.type === 'openUrl') {
dispatch({
type: 'WEB_CHAT/SEND_MESSAGE',
payload: {
text: `Navigating to ${cardAction.value}`
}
});
} else {
return next(cardAction);
}
}
}
});
await driver.wait(uiConnected(), timeouts.directLine);
await pageObjects.sendMessageViaSendBox('card-actions', { waitForSend: true });
await driver.wait(suggestedActionsShown(), timeouts.directLine);
const openUrlButton = await driver.findElement(By.css('[role="form"] ul > li:first-child button'));
await openUrlButton.click();
await driver.wait(minNumActivitiesShown(4), timeouts.directLine);
await driver.wait(allOutgoingActivitiesSent(), timeouts.directLine);
const base64PNG = await driver.takeScreenshot();
expect(base64PNG).toMatchImageSnapshot(imageSnapshotOptions);
});
test('card action "signin"', async () => {
const { driver, pageObjects } = await setupWebDriver({
props: {
cardActionMiddleware:
({ dispatch }) =>
next =>
({ cardAction, getSignInUrl }) => {
if (cardAction.type === 'signin') {
getSignInUrl().then(url => {
dispatch({
type: 'WEB_CHAT/SEND_MESSAGE',
payload: {
text: `Signing into ${new URL(url).host}`
}
});
});
} else {
return next(cardAction);
}
}
},
useProductionBot: true
});
await driver.wait(uiConnected(), timeouts.directLine);
await pageObjects.sendMessageViaSendBox('oauth', { waitForSend: true });
await driver.wait(minNumActivitiesShown(2), timeouts.directLine);
const transcript = await getTranscript(driver);
const openUrlButton = await transcript.findElement(By.css('.webchat__bubble__content button'));
await openUrlButton.click();
await driver.wait(minNumActivitiesShown(4), timeouts.directLine);
await driver.wait(allOutgoingActivitiesSent(), timeouts.directLine);
// When the "Sign in" button is clicked, the focus move to it, need to blur it.
await driver.executeScript(() => {
const { activeElement } = document;
activeElement && activeElement.blur();
});
const base64PNG = await driver.takeScreenshot();
expect(base64PNG).toMatchImageSnapshot(imageSnapshotOptions);
});
test('card action "signin" when directLine.getSessionId is falsy', async () => {
const { driver, pageObjects } = await setupWebDriver({
disableNoMagicCode: true,
props: {
cardActionMiddleware:
({ dispatch }) =>
next =>
({ cardAction, getSignInUrl }) => {
if (cardAction.type === 'signin') {
Promise.resolve(getSignInUrl()).then(url => {
dispatch({
type: 'WEB_CHAT/SEND_MESSAGE',
payload: {
text: `Signing into ${new URL(url).host}`
}
});
});
} else {
return next(cardAction);
}
}
},
useProductionBot: true
});
await driver.wait(uiConnected(), timeouts.directLine);
await pageObjects.sendMessageViaSendBox('oauth', { waitForSend: true });
await driver.wait(minNumActivitiesShown(2), timeouts.directLine);
const transcript = await getTranscript(driver);
const openUrlButton = await transcript.findElement(By.css('.webchat__bubble__content button'));
await openUrlButton.click();
await driver.wait(minNumActivitiesShown(4), timeouts.directLine);
await driver.wait(allOutgoingActivitiesSent(), timeouts.directLine);
// When the "Sign in" button is clicked, the focus move to it, need to blur it.
await driver.executeScript(() => {
const { activeElement } = document;
activeElement && activeElement.blur();
});
const base64PNG = await driver.takeScreenshot();
expect(base64PNG).toMatchImageSnapshot(imageSnapshotOptions);
await expect(pageObjects.getConsoleErrors()).resolves.toEqual([]);
expect(
(await pageObjects.getConsoleWarnings()).includes(
'botframework-webchat: OAuth is not supported on this Direct Line adapter.'
)
).toBe(true);
});