@khanacademy/wonder-blocks-testing-core 2.2.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @khanacademy/wonder-blocks-testing-core
2
2
 
3
+ ## 3.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - 38c926c: Upgrade WB to using react-router-dom-v5-compat.
8
+
3
9
  ## 2.2.1
4
10
 
5
11
  ### Patch Changes
package/dist/es/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as React from 'react';
2
2
  import { action } from '@storybook/addon-actions';
3
- import { StaticRouter, MemoryRouter, useLocation, Switch, Route } from 'react-router-dom';
3
+ import { StaticRouter, MemoryRouter, Switch, Route } from 'react-router-dom';
4
+ import { CompatRouter, useLocation } from 'react-router-dom-v5-compat';
4
5
  import _extends from '@babel/runtime/helpers/extends';
5
6
  import * as ReactDOMServer from 'react-dom/server';
6
7
 
@@ -305,19 +306,21 @@ const MaybeWithRoute = ({
305
306
  path,
306
307
  configLocation
307
308
  }) => {
308
- const actualLocation = useLocation();
309
- const configuredLocation = typeof configLocation === "string" ? configLocation : configLocation.pathname;
310
309
  if (path == null) {
311
310
  return React.createElement(React.Fragment, null, children);
312
311
  }
312
+ const ErrorElement = () => {
313
+ const actualLocation = useLocation();
314
+ const configuredLocation = typeof configLocation === "string" ? configLocation : configLocation.pathname;
315
+ const errorMessage = `The current location '${actualLocation.pathname}' ` + `does not match the configured path '${path}'. ` + `Did you provide the correct configured ` + `location, '${configuredLocation}', or did the ` + `routing lead to a different place than you ` + `expected?`;
316
+ throw new Error(errorMessage);
317
+ };
313
318
  return React.createElement(Switch, null, React.createElement(Route, {
314
- exact: true,
315
- path: path
316
- }, children), React.createElement(Route, {
319
+ path: path,
320
+ render: () => React.createElement(React.Fragment, null, children)
321
+ }), React.createElement(Route, {
317
322
  path: "*",
318
- render: () => {
319
- throw new Error(`The current location '${actualLocation.pathname}' ` + `does not match the configured path '${path}'. ` + `Did you provide the correct configured ` + `location, '${configuredLocation}', or did the ` + `routing lead to a different place than you ` + `expected?`);
320
- }
323
+ component: ErrorElement
321
324
  }));
322
325
  };
323
326
  const adapter = (children, config) => {
@@ -328,21 +331,30 @@ const adapter = (children, config) => {
328
331
  };
329
332
  }
330
333
  if ("forceStatic" in config && config.forceStatic) {
334
+ if (config.disableCompatRouter) {
335
+ return React.createElement(StaticRouter, {
336
+ location: config.location,
337
+ context: {}
338
+ }, React.createElement(MaybeWithRoute, {
339
+ path: config.path,
340
+ configLocation: config.location
341
+ }, children));
342
+ }
331
343
  return React.createElement(StaticRouter, {
332
344
  location: config.location,
333
345
  context: {}
334
- }, React.createElement(MaybeWithRoute, {
346
+ }, React.createElement(CompatRouter, null, React.createElement(MaybeWithRoute, {
335
347
  path: config.path,
336
348
  configLocation: config.location
337
- }, children));
349
+ }, children)));
338
350
  }
339
351
  if ("location" in config && config.location !== undefined) {
340
352
  return React.createElement(MemoryRouter, {
341
353
  initialEntries: [config.location]
342
- }, React.createElement(MaybeWithRoute, {
354
+ }, React.createElement(CompatRouter, null, React.createElement(MaybeWithRoute, {
343
355
  path: config.path,
344
356
  configLocation: config.location
345
- }, children));
357
+ }, children)));
346
358
  }
347
359
  if (!("initialEntries" in config) || config.initialEntries === undefined) {
348
360
  throw new Error("A location or initial history entries must be provided.");
@@ -354,13 +366,10 @@ const adapter = (children, config) => {
354
366
  if (config.initialIndex != null) {
355
367
  routerProps.initialIndex = config.initialIndex;
356
368
  }
357
- if (config.getUserConfirmation != null) {
358
- routerProps.getUserConfirmation = config.getUserConfirmation;
359
- }
360
- return React.createElement(MemoryRouter, routerProps, React.createElement(MaybeWithRoute, {
369
+ return React.createElement(MemoryRouter, routerProps, React.createElement(CompatRouter, null, React.createElement(MaybeWithRoute, {
361
370
  path: config.path,
362
371
  configLocation: entries[(_config$initialIndex = config.initialIndex) != null ? _config$initialIndex : 0]
363
- }, children));
372
+ }, children)));
364
373
  };
365
374
 
366
375
  const DefaultAdapters = {
@@ -19,11 +19,11 @@ export declare const DefaultAdapters: {
19
19
  readonly router: import("../types").TestHarnessAdapter<string | Readonly<{
20
20
  initialEntries: import("history").LocationDescriptor<unknown>[] | undefined;
21
21
  initialIndex?: number | undefined;
22
- getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined;
23
22
  path?: string;
24
23
  } | {
25
24
  location: import("history").LocationDescriptor;
26
25
  forceStatic: true;
26
+ disableCompatRouter?: boolean;
27
27
  path?: string;
28
28
  } | {
29
29
  location: import("history").LocationDescriptor;
@@ -15,10 +15,6 @@ type Config = Readonly<{
15
15
  * See MemoryRouter prop for initialIndex.
16
16
  */
17
17
  initialIndex?: MemoryRouterProps["initialIndex"];
18
- /**
19
- * See MemoryRouter prop for getUserConfirmation.
20
- */
21
- getUserConfirmation?: MemoryRouterProps["getUserConfirmation"];
22
18
  /**
23
19
  * A path match to use.
24
20
  *
@@ -41,6 +37,15 @@ type Config = Readonly<{
41
37
  * Force the use of a StaticRouter, instead of MemoryRouter.
42
38
  */
43
39
  forceStatic: true;
40
+ /**
41
+ * If true, then we will not use a CompatRouter.
42
+ *
43
+ * NOTE(john): There are cases where we don't want a CompatRouter
44
+ * here as it uses useLayoutEffect, which causes issues in our
45
+ * test environment. Namely, that it generates a warning about
46
+ * the use of useLayoutEffect, which causes an error.
47
+ */
48
+ disableCompatRouter?: boolean;
44
49
  /**
45
50
  * A path match to use.
46
51
  *
@@ -19,11 +19,11 @@ export declare const testHarness: <TProps extends object>(Component: import("rea
19
19
  readonly router: import("./types").TestHarnessAdapter<string | Readonly<{
20
20
  initialEntries: import("history").LocationDescriptor<unknown>[] | undefined;
21
21
  initialIndex?: number | undefined;
22
- getUserConfirmation?: ((message: string, callback: (ok: boolean) => void) => void) | undefined;
23
22
  path?: string;
24
23
  } | {
25
24
  location: import("history").LocationDescriptor;
26
25
  forceStatic: true;
26
+ disableCompatRouter?: boolean;
27
27
  path?: string;
28
28
  } | {
29
29
  location: import("history").LocationDescriptor;
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var React = require('react');
6
6
  var addonActions = require('@storybook/addon-actions');
7
7
  var reactRouterDom = require('react-router-dom');
8
+ var reactRouterDomV5Compat = require('react-router-dom-v5-compat');
8
9
  var _extends = require('@babel/runtime/helpers/extends');
9
10
  var ReactDOMServer = require('react-dom/server');
10
11
 
@@ -333,19 +334,21 @@ const MaybeWithRoute = ({
333
334
  path,
334
335
  configLocation
335
336
  }) => {
336
- const actualLocation = reactRouterDom.useLocation();
337
- const configuredLocation = typeof configLocation === "string" ? configLocation : configLocation.pathname;
338
337
  if (path == null) {
339
338
  return React__namespace.createElement(React__namespace.Fragment, null, children);
340
339
  }
340
+ const ErrorElement = () => {
341
+ const actualLocation = reactRouterDomV5Compat.useLocation();
342
+ const configuredLocation = typeof configLocation === "string" ? configLocation : configLocation.pathname;
343
+ const errorMessage = `The current location '${actualLocation.pathname}' ` + `does not match the configured path '${path}'. ` + `Did you provide the correct configured ` + `location, '${configuredLocation}', or did the ` + `routing lead to a different place than you ` + `expected?`;
344
+ throw new Error(errorMessage);
345
+ };
341
346
  return React__namespace.createElement(reactRouterDom.Switch, null, React__namespace.createElement(reactRouterDom.Route, {
342
- exact: true,
343
- path: path
344
- }, children), React__namespace.createElement(reactRouterDom.Route, {
347
+ path: path,
348
+ render: () => React__namespace.createElement(React__namespace.Fragment, null, children)
349
+ }), React__namespace.createElement(reactRouterDom.Route, {
345
350
  path: "*",
346
- render: () => {
347
- throw new Error(`The current location '${actualLocation.pathname}' ` + `does not match the configured path '${path}'. ` + `Did you provide the correct configured ` + `location, '${configuredLocation}', or did the ` + `routing lead to a different place than you ` + `expected?`);
348
- }
351
+ component: ErrorElement
349
352
  }));
350
353
  };
351
354
  const adapter = (children, config) => {
@@ -356,21 +359,30 @@ const adapter = (children, config) => {
356
359
  };
357
360
  }
358
361
  if ("forceStatic" in config && config.forceStatic) {
362
+ if (config.disableCompatRouter) {
363
+ return React__namespace.createElement(reactRouterDom.StaticRouter, {
364
+ location: config.location,
365
+ context: {}
366
+ }, React__namespace.createElement(MaybeWithRoute, {
367
+ path: config.path,
368
+ configLocation: config.location
369
+ }, children));
370
+ }
359
371
  return React__namespace.createElement(reactRouterDom.StaticRouter, {
360
372
  location: config.location,
361
373
  context: {}
362
- }, React__namespace.createElement(MaybeWithRoute, {
374
+ }, React__namespace.createElement(reactRouterDomV5Compat.CompatRouter, null, React__namespace.createElement(MaybeWithRoute, {
363
375
  path: config.path,
364
376
  configLocation: config.location
365
- }, children));
377
+ }, children)));
366
378
  }
367
379
  if ("location" in config && config.location !== undefined) {
368
380
  return React__namespace.createElement(reactRouterDom.MemoryRouter, {
369
381
  initialEntries: [config.location]
370
- }, React__namespace.createElement(MaybeWithRoute, {
382
+ }, React__namespace.createElement(reactRouterDomV5Compat.CompatRouter, null, React__namespace.createElement(MaybeWithRoute, {
371
383
  path: config.path,
372
384
  configLocation: config.location
373
- }, children));
385
+ }, children)));
374
386
  }
375
387
  if (!("initialEntries" in config) || config.initialEntries === undefined) {
376
388
  throw new Error("A location or initial history entries must be provided.");
@@ -382,13 +394,10 @@ const adapter = (children, config) => {
382
394
  if (config.initialIndex != null) {
383
395
  routerProps.initialIndex = config.initialIndex;
384
396
  }
385
- if (config.getUserConfirmation != null) {
386
- routerProps.getUserConfirmation = config.getUserConfirmation;
387
- }
388
- return React__namespace.createElement(reactRouterDom.MemoryRouter, routerProps, React__namespace.createElement(MaybeWithRoute, {
397
+ return React__namespace.createElement(reactRouterDom.MemoryRouter, routerProps, React__namespace.createElement(reactRouterDomV5Compat.CompatRouter, null, React__namespace.createElement(MaybeWithRoute, {
389
398
  path: config.path,
390
399
  configLocation: entries[(_config$initialIndex = config.initialIndex) != null ? _config$initialIndex : 0]
391
- }, children));
400
+ }, children)));
392
401
  };
393
402
 
394
403
  const DefaultAdapters = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@khanacademy/wonder-blocks-testing-core",
3
- "version": "2.2.1",
3
+ "version": "3.0.0",
4
4
  "design": "v1",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -19,7 +19,8 @@
19
19
  "node-fetch": "^2.6.7",
20
20
  "react": "18.2.0",
21
21
  "react-dom": "18.2.0",
22
- "react-router-dom": "5.3.4"
22
+ "react-router-dom": "5.3.4",
23
+ "react-router-dom-v5-compat": "^6.30.0"
23
24
  },
24
25
  "devDependencies": {
25
26
  "@khanacademy/wonder-stuff-testing": "^3.0.1",