This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[terra-functional-testing] Create a setLocale utility that updates te…
…rra-application's locale (#524) * [placeholder] initialize dispatchCustomEvent. * [placeholder] attach to terra global. perhaps this is unnecessary. * Revert "[placeholder] attach to terra global. perhaps this is unnecessary." This reverts commit 093efe9. * remove afterCommand short circuit * add custom event dispatcher fixture * update jest tests * add wdio test * reword unit tests * add utility to terra global and update to object based api. add doc. export via index file. * remove console.log. revert back to single it block * [placeholder] add setLocale utility; expose via terra global; update doc * split customEventDispatcher into separate file. add unit test for setLocale * update doc and error log * correct typo in doc * rename utility to setApplicationLocale. fix failing wdio test. * rename setLocale fn to setApplicationLocale
- Loading branch information
Showing
10 changed files
with
149 additions
and
3 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
packages/terra-functional-testing/src/commands/utils/dispatchCustomEvent.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,21 @@ | ||
/** | ||
* Utility to send a custom event containing metadata. | ||
* @param {string} options.name - name of event | ||
* @param {object} options.metaData - metadata pertaining to event | ||
*/ | ||
const dispatchCustomEvent = (options) => { | ||
const { name, metaData } = options; | ||
try { | ||
global.browser.execute((eventName, eventMetaData) => { | ||
/* If IE support is removed, convert below to use event constructors. */ | ||
const event = document.createEvent('Event'); | ||
event.initEvent(eventName, true, true); | ||
event.metaData = eventMetaData; | ||
window.dispatchEvent(event); | ||
}, name, metaData); | ||
} catch (error) { | ||
throw new Error(`dispatchCustomEvent failed: ${error}`); | ||
} | ||
}; | ||
|
||
module.exports = dispatchCustomEvent; |
2 changes: 2 additions & 0 deletions
2
packages/terra-functional-testing/src/commands/utils/index.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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
const describeViewports = require('./describeViewports'); | ||
const getViewports = require('./getViewports'); | ||
const hideInputCaret = require('./hideInputCaret'); | ||
const setApplicationLocale = require('./setApplicationLocale'); | ||
const setViewport = require('./setViewport'); | ||
|
||
module.exports = { | ||
describeViewports, | ||
getViewports, | ||
hideInputCaret, | ||
setApplicationLocale, | ||
setViewport, | ||
}; |
24 changes: 24 additions & 0 deletions
24
packages/terra-functional-testing/src/commands/utils/setApplicationLocale.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,24 @@ | ||
const { Logger } = require('@cerner/terra-cli'); | ||
const dispatchCustomEvent = require('./dispatchCustomEvent'); | ||
|
||
const logger = new Logger({ prefix: '[terra-functional-testing:setApplicationLocale]' }); | ||
|
||
/** | ||
* Updates terra-application's locale via custom event | ||
* @param {string} locale - name of locale to update to | ||
*/ | ||
const setApplicationLocale = (locale) => { | ||
if (typeof locale === 'undefined' || !locale) { | ||
logger.error(`Unsupported locale supplied to setApplicationLocale. "${locale}" is not a valid locale.`); | ||
return; | ||
} | ||
|
||
const event = { | ||
name: 'applicationBase.testOverride', | ||
metaData: { locale }, | ||
}; | ||
|
||
dispatchCustomEvent(event); | ||
}; | ||
|
||
module.exports = setApplicationLocale; |
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
15 changes: 15 additions & 0 deletions
15
packages/terra-functional-testing/tests/fixtures/dispatch-custom-event.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,15 @@ | ||
<!doctype html> | ||
<html lang="<%= htmlWebpackPlugin.options.lang %>" dir="ltr"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="x-ua-compatible" content="ie=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>Dispatch Custom Event</title> | ||
</head> | ||
<body style="height: 100%;"> | ||
<div> | ||
Test Custom Event Dispatcher | ||
<p id="custom-event-paragraph"></p> | ||
</div> | ||
</body> | ||
</html> |
22 changes: 22 additions & 0 deletions
22
packages/terra-functional-testing/tests/jest/commands/utils/dispatchCustomEvent.test.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,22 @@ | ||
const dispatchCustomEvent = require('../../../../src/commands/utils/dispatchCustomEvent'); | ||
|
||
describe('dispatchCustomEvent', () => { | ||
it('executes a specified function via browser.execute', () => { | ||
const mockFn = jest.fn(); | ||
global.browser = { | ||
execute: mockFn, | ||
}; | ||
|
||
dispatchCustomEvent({ name: 'mock' }); | ||
expect(mockFn).toBeCalled(); | ||
}); | ||
|
||
it('throws an error outputted by browser.execute', () => { | ||
const mockError = new Error('mock error'); | ||
global.browser = { | ||
execute: () => { throw mockError; }, | ||
}; | ||
|
||
expect(() => dispatchCustomEvent({ name: 'mock' })).toThrow(`dispatchCustomEvent failed: ${mockError}`); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
packages/terra-functional-testing/tests/jest/commands/utils/setLocale.test.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,15 @@ | ||
const setApplicationLocale = require('../../../../src/commands/utils/setApplicationLocale'); | ||
const dispatchCustomEvent = require('../../../../src/commands/utils/dispatchCustomEvent'); | ||
|
||
jest.mock('../../../../src/commands/utils/dispatchCustomEvent'); | ||
|
||
describe('setApplicationLocale', () => { | ||
it('executes dispatchCustomEvent with a specified locale', () => { | ||
setApplicationLocale('ar'); | ||
const expectedEvent = { | ||
name: 'applicationBase.testOverride', | ||
metaData: { locale: 'ar' }, | ||
}; | ||
expect(dispatchCustomEvent).toHaveBeenCalledWith(expectedEvent); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/terra-functional-testing/tests/wdio/dispatch-custom-event-spec.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,28 @@ | ||
/* global $ */ | ||
const dispatchCustomEvent = require('../../src/commands/utils/dispatchCustomEvent'); | ||
|
||
describe('dispatchCustomEvent', () => { | ||
it('sends a custom event that injects a string into a paragraph', () => { | ||
browser.url('/dispatch-custom-event.html'); | ||
|
||
/* Setup event listener that injects a string into a paragraph. */ | ||
browser.execute(() => { | ||
const eventListenerHandler = (event) => { | ||
const paragraph = document.getElementById('custom-event-paragraph'); | ||
const { metaData } = event; | ||
const { injectedString } = metaData; | ||
paragraph.textContent = injectedString; | ||
}; | ||
|
||
window.addEventListener('mockCustomEvent', eventListenerHandler); | ||
}); | ||
|
||
const injectedString = 'mock'; | ||
dispatchCustomEvent({ | ||
name: 'mockCustomEvent', | ||
metaData: { injectedString }, | ||
}); | ||
const actualString = $('#custom-event-paragraph').getText(); | ||
expect(actualString).toEqual(injectedString); | ||
}); | ||
}); |
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