-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UITEST-108 export some @bigtest/react functions (#1322)
Provide the react-agnostic `@bigtest/react` functions so that we can build other test infrastructure on top of them without bringing along `@bigtest/react` and its react v16 peer-deps (and thus its build warnings about unsatisfied dependencies). Refs UITEST-108 (cherry picked from commit e9482a1)
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 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,48 @@ | ||
// local context where various things can be persisted using the | ||
// helper functions below | ||
let context = Object.create(null); | ||
|
||
/** | ||
* Clears everything from the current context by creating a brand new | ||
* empty context. | ||
* | ||
* @private | ||
*/ | ||
export function clearContext() { | ||
context = Object.create(null); | ||
} | ||
|
||
/** | ||
* Adds things to the context by key, value. | ||
* | ||
* @private | ||
* @param {Object} newContexts - Hash of things to store in the | ||
* current context | ||
*/ | ||
export function setContext(newContext) { | ||
Object.assign(context, newContext); | ||
} | ||
|
||
/** | ||
* Retrieves the value of a specific key in the current context. | ||
* | ||
* @private | ||
* @param {String} key - The key of the value to retrieve | ||
* @param {String|Boolean} [error] - Specific error message to throw | ||
* @returns Value of `key` within the current context | ||
* @throws {Error} when `key` is not fonud in the current context | ||
*/ | ||
export function getContext(key, error) { | ||
let warn = error; | ||
if (typeof error === 'undefined') { | ||
warn = `no ${key} context, make sure \`setupAppForTesting\` was called`; | ||
} | ||
|
||
if (key in context) { | ||
return context[key]; | ||
} else if (warn) { | ||
throw new Error(warn); | ||
} | ||
|
||
return null; | ||
} |
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,2 @@ | ||
export { clearContext, getContext, setContext } from './context'; | ||
export { visit, goBack, goForward, location } from './visit'; |
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,87 @@ | ||
import { getContext } from './context'; | ||
|
||
/** | ||
* Uses the history context setup during `setupAppForTesting` and | ||
* calls `push` with the provided location argument. | ||
* | ||
* ``` javascript | ||
* // must be called to setup the `history` context | ||
* setupAppForTesting(App) | ||
* | ||
* // calls `history.push()` | ||
* visit('/someroute') | ||
* visit({ pathname: '/foo', search: '?bar' }) | ||
* ``` | ||
* | ||
* @function visit | ||
* @param {Object|String} location - Argument for `history.push()` | ||
* @throws {Error} When `setupAppForTesting` was not called | ||
*/ | ||
export function visit(l) { | ||
getContext('history').push(l); | ||
} | ||
|
||
/** | ||
* Uses the history context setup during `setupAppForTesting` and | ||
* calls the `goBack` method. | ||
* | ||
* ``` javascript | ||
* // must be called to setup the `history` context | ||
* setupAppForTesting(App) | ||
* | ||
* // go to a route | ||
* visit('/someroute') | ||
* | ||
* // go back | ||
* goBack() | ||
* ``` | ||
* | ||
* @function goBack | ||
* @throws {Error} When `setupAppForTesting` was not called | ||
*/ | ||
export function goBack() { | ||
getContext('history').goBack(); | ||
} | ||
|
||
/** | ||
* Uses the history context setup during `setupAppForTesting` and | ||
* calls the `goForward` method. | ||
* | ||
* ``` javascript | ||
* // must be called to setup the `history` context | ||
* setupAppForTesting(App) | ||
* | ||
* // go to a route, and back | ||
* visit('/someroute') | ||
* goBack() | ||
* | ||
* // go back to `/someroute` | ||
* goForward() | ||
* ``` | ||
* | ||
* @function goForward | ||
* @throws {Error} When `setupAppForTesting` was not called | ||
*/ | ||
export function goForward() { | ||
getContext('history').goForward(); | ||
} | ||
|
||
/** | ||
* Uses the history context setup during `setupAppForTesting` | ||
* and returns the current location. | ||
* | ||
* ``` javascript | ||
* // must be called to setup the `history` context | ||
* setupAppForTesting(App) | ||
* visit('/someroute') | ||
* | ||
* location().pathname | ||
* //=> "/someroute" | ||
* ``` | ||
* | ||
* @returns {Object} Current history location object | ||
* @throws {Error} When `setupAppForTesting` was not called | ||
*/ | ||
export function location() { | ||
return getContext('history').location; | ||
} |
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 +1,2 @@ | ||
export * from './interactors'; | ||
export * from './bigtest'; |