Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
compulim committed Sep 22, 2019
1 parent 62ba27b commit 8b435b1
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 29 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions __tests__/setup/marshal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default function marshal(props) {
return (
props &&
Object.keys(props).reduce(
(nextProps, key) => {
const { [key]: value } = props;

if (typeof value === 'function') {
nextProps[key] = `() => ${value.toString()}`;
nextProps.__evalKeys.push(key);
} else {
nextProps[key] = value;
}

return nextProps;
},
{
__evalKeys: []
}
)
);
}
4 changes: 3 additions & 1 deletion __tests__/setup/pageObjects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import sendMessageViaSendBox from './sendMessageViaSendBox';
import sendTextToClipboard from './sendTextToClipboard';
import startSpeechSynthesize from './startSpeechSynthesize';
import typeOnSendBox from './typeOnSendBox';
import updateProps from './updateProps';

function mapMap(map, mapper) {
return Object.keys(map).reduce((final, key) => {
Expand Down Expand Up @@ -51,7 +52,8 @@ export default function pageObjects(driver) {
sendMessageViaSendBox,
sendTextToClipboard,
startSpeechSynthesize,
typeOnSendBox
typeOnSendBox,
updateProps
},
fn => fn.bind(null, driver)
);
Expand Down
7 changes: 7 additions & 0 deletions __tests__/setup/pageObjects/updateProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import marshal from '../marshal';

export default function updateProps(driver, mergeProps) {
return driver.executeScript(mergeProps => {
window.WebChatTest.updateProps(unmarshal(mergeProps));
}, marshal(mergeProps));
}
24 changes: 1 addition & 23 deletions __tests__/setup/setupTestFramework.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import getPort from 'get-port';
import handler from 'serve-handler';

import createPageObjects from './pageObjects/index';
import marshal from './marshal';
import retry from './retry';
import setupTestEnvironment from './setupTestEnvironment';

Expand All @@ -15,29 +16,6 @@ const BROWSER_NAME = process.env.WEBCHAT_TEST_ENV || 'chrome-docker';
// const BROWSER_NAME = 'chrome-local';
const NUM_RETRIES = 3;

function marshal(props) {
return (
props &&
Object.keys(props).reduce(
(nextProps, key) => {
const { [key]: value } = props;

if (typeof value === 'function') {
nextProps[key] = `() => ${value.toString()}`;
nextProps.__evalKeys.push(key);
} else {
nextProps[key] = value;
}

return nextProps;
},
{
__evalKeys: []
}
)
);
}

expect.extend({
toMatchImageSnapshot: configureToMatchImageSnapshot({
customSnapshotsDir: join(__dirname, '../__image_snapshots__', BROWSER_NAME)
Expand Down
24 changes: 19 additions & 5 deletions __tests__/setup/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
await loadScript('/webchat-instrumented.js');

let directLine;

if (!useProductionBot && !createDirectLine) {
directLine = window.MockBotAdapter.createDirectLine({});
} else {
Expand All @@ -118,11 +118,11 @@
throw err;
}
}, 3);

createDirectLine || (createDirectLine = window.WebChat.createDirectLine);
directLine = (createDirectLine || window.WebChat.createDirectLine)({ token });
}

const store = window.WebChatTest.store = window.WebChat.createStore(storeInitialState, store => {
const setupMiddleware = storeMiddleware(store);

Expand All @@ -137,15 +137,29 @@
};
});

window.WebChat.renderWebChat({
props = {
directLine,
store,
styleSet: createStyleSet && createStyleSet(props.styleOptions),
username: 'Happy Web Chat user',
...props
}, document.getElementById('webchat'));
};

renderWebChat = props => {
console.log(props);

window.WebChat.renderWebChat(props, document.getElementById('webchat'));
};

renderWebChat(props);

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

window.WebChatTest.updateProps = mergeProps => {
props = { ...props, ...mergeProps };

renderWebChat(props);
};
}

window.WebChatTest = {
Expand Down
28 changes: 28 additions & 0 deletions __tests__/timestamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { imageSnapshotOptions, timeouts } from './constants.json';

import minNumActivitiesShown from './setup/conditions/minNumActivitiesShown';
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('update timestamp on-the-fly', async () => {
const { driver, pageObjects } = await setupWebDriver();

await driver.wait(uiConnected(), timeouts.directLine);
await pageObjects.sendMessageViaSendBox('echo Hello, World!', { waitForSend: true });

await driver.wait(minNumActivitiesShown(2), timeouts.directLine);

expect(await driver.takeScreenshot()).toMatchImageSnapshot(imageSnapshotOptions);

await pageObjects.updateProps({ locale: 'zh-HK' });

expect(await driver.takeScreenshot()).toMatchImageSnapshot(imageSnapshotOptions);

await pageObjects.updateProps({ locale: 'zh-YUE' });

expect(await driver.takeScreenshot()).toMatchImageSnapshot(imageSnapshotOptions);
});

0 comments on commit 8b435b1

Please sign in to comment.