Skip to content

Commit

Permalink
Add localTimestamp and localTimezone (#3896)
Browse files Browse the repository at this point in the history
* Add localTimestamp and localTimezone

* Update entry

* Fix test break

* Add sendDevToolsCommand

* Clean up

* Clean up
  • Loading branch information
compulim authored May 20, 2021
1 parent d00b76c commit 880f817
Show file tree
Hide file tree
Showing 21 changed files with 18,288 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Cleanup repo URLs to point to main branch, by [@corinagum](https://github.com/corinagum), in PR [#3870](https://github.com/microsoft/BotFramework-WebChat/pull/3870)
- Resolved [#3557](https://github.com/microsoft/BotFramework-WebChat/issues/3557) and [#3736](https://github.com/microsoft/BotFramework-WebChat/issues/3736). Improved test harness and added browser pooling, by [@compulim](https://github.com/compulim), in PR [#3871](https://github.com/microsoft/BotFramework-WebChat/pull/3871)
- Resolved [#3788](https://github.com/microsoft/BotFramework-WebChat/issues/3788). Added `localTimestamp` and `localTimezone` (if available) to all outgoing activities, by [@compulim](https://github.com/compulim), in PR [#3896](https://github.com/microsoft/BotFramework-WebChat/pull/3896)

### Fixed

Expand Down
55 changes: 55 additions & 0 deletions __tests__/html/directLine.postActivity.localTimezone.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<link href="/assets/index.css" rel="stylesheet" type="text/css" />
<script crossorigin="anonymous" src="/test-harness.js"></script>
<script crossorigin="anonymous" src="/test-page-object.js"></script>
<script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
</head>
<body>
<div id="webchat"></div>
<script>
run(async function () {
await host.sendDevToolsCommand('Emulation.setTimezoneOverride', { timezoneId: 'America/Los_Angeles' });

const clock = lolex.install();

// Before running the test, the browser should have setup with time zone of GMT-08, at epoch time.
// Note the getTimezoneOffset() is correctly returns positive number for a negative offset.
expect(new Date().getTimezoneOffset()).toBe(480);

const directLine = testHelpers.createDirectLineWithTranscript([], {
overridePostActivity: activity => {
const id = '0';

expect(activity).toHaveProperty('timestamp', '1970-01-01T00:00:00.600Z');

expect(activity).toHaveProperty('localTimestamp', '1969-12-31T16:00:00.600-08:00');
expect(activity).toHaveProperty('localTimezone', 'America/Los_Angeles');

directLine.activityDeferredObservable.next({ ...activity, id });

return Observable.from([id]);
}
});

WebChat.renderWebChat(
{
directLine,
store: testHelpers.createStore()
},
document.getElementById('webchat')
);

await pageConditions.webChatRendered();

clock.tick(600);

// Wait for "Connecting..." message to dismiss
await pageConditions.uiConnected();

await pageObjects.sendMessageViaSendBox('Hello, World!');
});
</script>
</body>
</html>
5 changes: 5 additions & 0 deletions __tests__/html/directLine.postActivity.localTimezone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @jest-environment ./packages/test/harness/src/host/jest/WebDriverEnvironment.js */

describe('postActivity', () => {
test('should send localTimestamp and localTimezone', () => runHTML('directLine.postActivity.localTimezone.html'));
});
59 changes: 59 additions & 0 deletions __tests__/html/directLine.postActivity.localTimezone.noIntl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<link href="/assets/index.css" rel="stylesheet" type="text/css" />
<script crossorigin="anonymous" src="/test-harness.js"></script>
<script crossorigin="anonymous" src="/test-page-object.js"></script>
<script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
</head>
<body>
<div id="webchat"></div>
<script>
run(async function () {
await host.sendDevToolsCommand('Emulation.setTimezoneOverride', { timezoneId: 'America/Los_Angeles' });

const clock = lolex.install();

// Before running the test, the browser should have setup with time zone of GMT-08, at epoch time.
// Note the getTimezoneOffset() is correctly returns positive number for a negative offset.
expect(new Date().getTimezoneOffset()).toBe(480);

// Remove "Intl" object to disable local time zone name.
window.Intl = undefined;

const directLine = testHelpers.createDirectLineWithTranscript([], {
overridePostActivity: activity => {
const id = '0';

expect(activity).toHaveProperty('timestamp', '1970-01-01T00:00:00.600Z');
expect(activity).toHaveProperty('localTimestamp', '1969-12-31T16:00:00.600-08:00');

// Without "Intl" object, local time zone name cannot be retrieved.
expect(activity.localTimezone).toBeUndefined();

directLine.activityDeferredObservable.next({ ...activity, id });

return Observable.from([id]);
}
});

WebChat.renderWebChat(
{
directLine,
store: testHelpers.createStore()
},
document.getElementById('webchat')
);

await pageConditions.webChatRendered();

clock.tick(600);

// Wait for "Connecting..." message to dismiss
await pageConditions.uiConnected();

await pageObjects.sendMessageViaSendBox('Hello, World!');
});
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @jest-environment ./packages/test/harness/src/host/jest/WebDriverEnvironment.js */

describe('postActivity', () => {
test('should send localTimestamp only if Intl global is undefined', () => runHTML('directLine.postActivity.localTimezone.noIntl.html'));
});
18 changes: 18 additions & 0 deletions __tests__/setup/jestNodeEnvironmentWithTimezone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Adopted from https://stackoverflow.com/questions/56261381/how-do-i-set-a-timezone-in-my-jest-config.
const NodeEnvironment = require('jest-environment-node');

/**
* Timezone-aware Jest environment. Supports `@timezone` JSDoc
* pragma within test suites to set timezone.
*/
module.exports = class TimezoneAwareNodeEnvironment extends NodeEnvironment {
constructor(config, context) {
// Allow test suites to change timezone, even if TZ is passed in a script.
// Falls back to existing TZ environment variable or UTC if no timezone is specified.
// IMPORTANT: This must happen before super(config) is called, otherwise
// it doesn't work.
process.env.TZ = context.docblockPragmas.timezone || process.env.TZ || 'UTC';

super(config);
}
};
Loading

0 comments on commit 880f817

Please sign in to comment.