-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add localTimestamp and localTimezone (#3896)
* Add localTimestamp and localTimezone * Update entry * Fix test break * Add sendDevToolsCommand * Clean up * Clean up
- Loading branch information
Showing
21 changed files
with
18,288 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
__tests__/html/directLine.postActivity.localTimezone.noIntl.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
5 changes: 5 additions & 0 deletions
5
__tests__/html/directLine.postActivity.localTimezone.noIntl.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
Oops, something went wrong.