Skip to content

Commit

Permalink
UITEST-108 export some @bigtest/react functions (#1322)
Browse files Browse the repository at this point in the history
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
zburke committed Jul 13, 2023
1 parent 5b1fde4 commit 5366a36
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 3.1.0 IN PROGRESS

* Export some `@bigtest/react` functions. Refs UITEST-108.

## [3.0.0](https://github.com/folio-org/stripes-testing/tree/v3.0.0) (2021-03-18)
[Full Changelog](https://github.com/folio-org/stripes-testing/compare/v2.0.1...v3.0.0)

Expand Down
48 changes: 48 additions & 0 deletions bigtest/context.js
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;
}
2 changes: 2 additions & 0 deletions bigtest/index.js
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';
87 changes: 87 additions & 0 deletions bigtest/visit.js
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;
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './interactors';
export * from './bigtest';

0 comments on commit 5366a36

Please sign in to comment.