From 5366a364d49a133fcc63bd31ba12acf2a1923848 Mon Sep 17 00:00:00 2001 From: Zak Burke Date: Thu, 13 Jul 2023 14:56:59 -0400 Subject: [PATCH] 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 e9482a1c69f6626357f6f2b40345fe427d009c9d) --- CHANGELOG.md | 2 ++ bigtest/context.js | 48 +++++++++++++++++++++++++ bigtest/index.js | 2 ++ bigtest/visit.js | 87 ++++++++++++++++++++++++++++++++++++++++++++++ index.js | 1 + 5 files changed, 140 insertions(+) create mode 100644 bigtest/context.js create mode 100644 bigtest/index.js create mode 100644 bigtest/visit.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 59eb1156fd..6e64368b79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/bigtest/context.js b/bigtest/context.js new file mode 100644 index 0000000000..1b1cb599db --- /dev/null +++ b/bigtest/context.js @@ -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; +} diff --git a/bigtest/index.js b/bigtest/index.js new file mode 100644 index 0000000000..d72f999adc --- /dev/null +++ b/bigtest/index.js @@ -0,0 +1,2 @@ +export { clearContext, getContext, setContext } from './context'; +export { visit, goBack, goForward, location } from './visit'; diff --git a/bigtest/visit.js b/bigtest/visit.js new file mode 100644 index 0000000000..fba83e2ac8 --- /dev/null +++ b/bigtest/visit.js @@ -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; +} diff --git a/index.js b/index.js index 4fb9fc6d6b..3f58709606 100644 --- a/index.js +++ b/index.js @@ -1 +1,2 @@ export * from './interactors'; +export * from './bigtest';