Skip to content

Commit

Permalink
Add tests for send typing indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
compulim committed Jan 27, 2019
1 parent dbaf7f8 commit fc20bb0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
41 changes: 41 additions & 0 deletions __tests__/sendTypingIndicator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { By, Key } from 'selenium-webdriver';

import directLineConnected from './setup/conditions/directLineConnected';
import minNumActivitiesReached from './setup/conditions/minNumActivitiesReached';
import webChatLoaded from './setup/conditions/webChatLoaded';

// selenium-webdriver API doc:
// https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_WebDriver.html

test('Send typing indicator', async () => {
const { driver } = await setupWebDriver({ props: { sendTypingIndicator: true } });

await driver.wait(webChatLoaded(), 2000);
await driver.wait(directLineConnected(), 2000);

const input = await driver.findElement(By.tagName('input[type="text"]'));

await input.sendKeys('echo-typing', Key.RETURN);
await driver.wait(minNumActivitiesReached(3), 2000);
await input.sendKeys('ABC');

// Typing indicator takes longer to come back
await driver.wait(minNumActivitiesReached(4), 5000);
}, 60000);

// TODO: [P3] Take this deprecation code out when releasing on or after January 13 2020
test('Send typing indicator using deprecated props', async () => {
const { driver } = await setupWebDriver({ props: { sendTyping: true } });

await driver.wait(webChatLoaded(), 2000);
await driver.wait(directLineConnected(), 2000);

const input = await driver.findElement(By.tagName('input[type="text"]'));

await input.sendKeys('echo-typing', Key.RETURN);
await driver.wait(minNumActivitiesReached(3), 2000);
await input.sendKeys('ABC');

// Typing indicator takes longer to come back
await driver.wait(minNumActivitiesReached(4), 5000);
}, 60000);
9 changes: 6 additions & 3 deletions __tests__/setup/conditions/directLineConnected.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Condition } from 'selenium-webdriver';

export default function () {
return new Condition('Waiting for Direct Line to connect', async driver => {
return await driver.executeScript(() => ~window.WebChatTest.actions.findIndex(({ type }) => type === 'DIRECT_LINE/CONNECT_FULFILLED'));
});
return new Condition('Waiting for Direct Line to connect with a welcome message', async driver =>
await driver.executeScript(() =>
!!~window.WebChatTest.actions.findIndex(({ type }) => type === 'DIRECT_LINE/CONNECT_FULFILLED')
&& !!document.querySelector(`[role="listitem"]:nth-child(1)`)
)
);
}
10 changes: 10 additions & 0 deletions __tests__/setup/conditions/typingActivityReceived.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Condition } from 'selenium-webdriver';

export default function () {
return new Condition('Waiting for incoming typing activity', async driver => {
await driver.executeScript(() =>
// TODO: [P2] We should use activities selector from core
!!~window.WebChatTest.store.getState().activities.findIndex(({ type }) => type === 'typing')
)
);
}
8 changes: 4 additions & 4 deletions __tests__/setup/setupTestFramework.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ expect.extend({
let driverPromise;
let serverPromise;

global.setupWebDriver = async () => {
global.setupWebDriver = async ({ props } = {}) => {
if (!driverPromise) {
driverPromise = (async () => {
let { baseURL, builder } = await setupTestEnvironment(BROWSER_NAME, new Builder());
Expand All @@ -36,10 +36,10 @@ global.setupWebDriver = async () => {
await driver.get(baseURL);
}

await driver.executeScript(coverage => {
await driver.executeScript((coverage, options) => {
window.__coverage__ = coverage;
main();
}, global.__coverage__);
main(options);
}, global.__coverage__, { props });

return { driver };
})();
Expand Down
5 changes: 3 additions & 2 deletions __tests__/setup/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<script>
window.WebChatTest = { actions: [] };

function main() {
function main({ props } = {}) {
const webChatScript = document.createElement('script');

webChatScript.setAttribute('src', '/webchat-instrumented.js');
Expand All @@ -78,7 +78,8 @@
// webSocket: false
// })
directLine: window.WebChat.createDirectLine({ token }),
store
store,
...props
}, document.getElementById('webchat'));

document.querySelector('#webchat > *').focus();
Expand Down

0 comments on commit fc20bb0

Please sign in to comment.