Skip to content

Commit

Permalink
Use lolex
Browse files Browse the repository at this point in the history
  • Loading branch information
compulim committed Apr 26, 2019
1 parent f05bb8f commit c2f3d53
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
20 changes: 12 additions & 8 deletions __tests__/offlineUI.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { By, Condition, Key } from 'selenium-webdriver';

import { imageSnapshotOptions, timeouts } from './constants.json';
import actionDispatched from './setup/conditions/actionDispatched';
import minNumActivitiesShown from './setup/conditions/minNumActivitiesShown';
import staticSpinner from './setup/assets/staticSpinner';
import uiConnected from './setup/conditions/uiConnected';
Expand Down Expand Up @@ -307,18 +308,21 @@ describe('offline UI', async () => {
};
},
pingBotOnLoad: false,
setup: () => new Promise(resolve => {
const scriptElement = document.createElement('script');
setup: () =>
Promise.all([
window.WebChatTest.loadScript('https://unpkg.com/core-js@2.6.3/client/core.min.js'),
window.WebChatTest.loadScript('https://unpkg.com/lolex@4.0.1/lolex.js')
]).then(() => {
window.WebChatTest.clock = lolex.install();
})
});

scriptElement.onload = resolve;
scriptElement.setAttribute('src', 'https://unpkg.com/core-js@2.6.3/client/core.min.js');
await driver.wait(actionDispatched('DIRECT_LINE/RECONNECT_PENDING'), timeouts.directLine);

document.head.appendChild(scriptElement);
})
await driver.executeScript(() => {
window.WebChatTest.clock.tick(17000);
});

await driver.sleep(17000);

const base64PNG = await driver.takeScreenshot();

expect(base64PNG).toMatchImageSnapshot(imageSnapshotOptions);
Expand Down
7 changes: 7 additions & 0 deletions __tests__/setup/conditions/actionDispatched.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Condition } from 'selenium-webdriver';

export default function (type) {
return new Condition('Action to dispatch', async driver =>
await driver.executeScript(type => ~window.WebChatTest.actions.findIndex(({ type: target }) => target === type), type)
);
}
10 changes: 7 additions & 3 deletions __tests__/setup/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
<body>
<div id="webchat" role="main"></div>
<script>
window.WebChatTest = { actions: [] };

function unmarshal({ __evalKeys, ...obj } = {}) {
__evalKeys && __evalKeys.forEach(key => {
obj[key] = eval(obj[key])();
Expand All @@ -67,7 +65,8 @@
return new Promise((resolve, reject) => {
const script = document.createElement('script');

script.setAttribute('src', '/webchat-instrumented.js');
script.setAttribute('async', '');
script.setAttribute('src', src);
script.addEventListener('load', resolve);
script.addEventListener('error', ({ error }) => reject(error));

Expand Down Expand Up @@ -142,6 +141,11 @@

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

window.WebChatTest = {
actions: [],
loadScript
};
</script>
</body>
</html>

0 comments on commit c2f3d53

Please sign in to comment.