Skip to content

Commit

Permalink
Added useEffects but this breaks tests
Browse files Browse the repository at this point in the history
This is an ongoing issue with shallow wrapping and the useEffect hook unfortunately! See enzymejs/enzyme#2011
  • Loading branch information
TimGuiteDiamond committed Dec 12, 2019
1 parent 2d8f8b3 commit e412e07
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
41 changes: 26 additions & 15 deletions src/components/FromBob/fromBob.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Provide the same component as fromJson but converting bob files and
providing a useful widget dictionary */

import { useState } from "react";
import { useState, useEffect } from "react";
import PropTypes from "prop-types";
import log from "loglevel";

Expand Down Expand Up @@ -59,20 +59,31 @@ export const WidgetFromBob = (
// Extract props
let { file, macroMap } = props;

if (file !== renderedFile) {
fetch(file)
.then(x => new Promise(resolve => setTimeout(() => resolve(x), 5000)))
.then(
(response): Promise<any> => {
return (response as Response).text();
}
)
.then((bob): void => {
setBob(bob);
setFile(file);
setMacros(macroMap as MacroMap);
});
}
useEffect(() => {
// Will be set on the first render
let mounted = true;
if (file !== renderedFile) {
fetch(file)
.then(
(response): Promise<any> => {
return response.json();
}
)
.then((bob): void => {
// Check component is still mounted when result comes back
if (mounted) {
setBob(bob);
setFile(file);
setMacros(macroMap as MacroMap);
}
});
}

// Clean up function
return () => {
mounted = false;
};
});

if (macroMap !== currentMacros) {
setMacros(macroMap as MacroMap);
Expand Down
4 changes: 1 addition & 3 deletions src/components/FromJson/fromJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { DynamicPageWidget } from "../DynamicPage/dynamicPage";
import { WidgetFromBob } from "../FromBob/fromBob";
import { GroupingContainer } from "../GroupingContainer/groupingContainer";
import { WidgetPropType, InferWidgetProps } from "../Widget/widget";
import { resolve } from "dns";

const EMPTY_WIDGET: WidgetDescription = {
type: "empty",
Expand Down Expand Up @@ -58,10 +57,9 @@ export const WidgetFromJson = (
let mounted = true;
if (file !== renderedFile) {
fetch(file)
.then(x => new Promise(resolve => setTimeout(() => resolve(x), 5000)))
.then(
(response): Promise<any> => {
return (response as Response).json();
return response.json();
}
)
.then((json): void => {
Expand Down

0 comments on commit e412e07

Please sign in to comment.