Skip to content

Commit f6dd8ad

Browse files
committed
fix(react): Fix $unmount throwing in a non React test
It's common to call `$unmount` in a `beforeEach` hook to run in every test, and, before, it would throw for tests that never called `$render`.
1 parent 745af61 commit f6dd8ad

File tree

3 files changed

+59
-51
lines changed

3 files changed

+59
-51
lines changed

Diff for: packages/react/src/render.ts

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export function $rerender(element: ReactElement): JQuery {
6464
*/
6565
export function $unmount() {
6666
act(() => {
67+
if (!rootUnmount) {
68+
return;
69+
}
70+
6771
rootUnmount();
6872
});
6973
}

Diff for: packages/react/tests/render.spec.tsx

+1-51
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { useEffect, useState } from 'react';
33
import createReactMock from 'react-mock-component';
44
import { expect } from 'tdd-buffet/expect/jest';
55
import { describe, it } from 'tdd-buffet/suite/node';
6-
import { $render, $rerender, $unmount } from '../src/render';
6+
import { $render, $rerender } from '../src/render';
77

88
describe('$render', () => {
99
it('should render a component', () => {
@@ -105,54 +105,4 @@ describe('$render', () => {
105105

106106
expect($component.text()).toEqual('12');
107107
});
108-
109-
it('should unmount component', () => {
110-
let unmounted = false;
111-
112-
class Unmountable extends React.Component {
113-
render() {
114-
return null;
115-
}
116-
117-
componentWillUnmount() {
118-
unmounted = true;
119-
}
120-
}
121-
122-
$render(<Unmountable />);
123-
$unmount();
124-
125-
expect(unmounted).toBeTruthy();
126-
});
127-
128-
it('should recreate container after unmounting', () => {
129-
const $container = $render(<span>bar</span>);
130-
$unmount();
131-
const $newContainer = $render(<span>baz</span>);
132-
133-
expect($container.text()).toHaveLength(0);
134-
expect($newContainer.text()).toEqual('baz');
135-
});
136-
137-
it('should cleanup after unmounting', () => {
138-
let flushed = false;
139-
140-
const Foo = () => {
141-
useEffect(() => () => {
142-
flushed = true;
143-
});
144-
145-
return null;
146-
};
147-
148-
$render(<Foo />);
149-
$unmount();
150-
151-
expect(flushed).toBeTruthy();
152-
});
153-
154-
it('should not blow up if unmounting nothing', () => {
155-
$unmount();
156-
$unmount();
157-
});
158108
});

Diff for: packages/react/tests/unmount.spec.tsx

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React, { useEffect } from 'react';
2+
import { $render, $unmount } from '../src/render';
3+
4+
describe('unmount', () => {
5+
// This test needs to be first.
6+
it('should not blow up if unmounting nothing', () => {
7+
$unmount();
8+
});
9+
10+
it('should unmount component', () => {
11+
let unmounted = false;
12+
13+
class Unmountable extends React.Component {
14+
render() {
15+
return null;
16+
}
17+
18+
componentWillUnmount() {
19+
unmounted = true;
20+
}
21+
}
22+
23+
$render(<Unmountable />);
24+
$unmount();
25+
26+
expect(unmounted).toBeTruthy();
27+
});
28+
29+
it('should recreate container after unmounting', () => {
30+
const $container = $render(<span>bar</span>);
31+
$unmount();
32+
const $newContainer = $render(<span>baz</span>);
33+
34+
expect($container.text()).toHaveLength(0);
35+
expect($newContainer.text()).toEqual('baz');
36+
});
37+
38+
it('should cleanup after unmounting', () => {
39+
let flushed = false;
40+
41+
const Foo = () => {
42+
useEffect(() => () => {
43+
flushed = true;
44+
});
45+
46+
return null;
47+
};
48+
49+
$render(<Foo />);
50+
$unmount();
51+
52+
expect(flushed).toBeTruthy();
53+
});
54+
});

0 commit comments

Comments
 (0)