Skip to content

Commit 6bbb8e2

Browse files
author
Joel Denning
authored
Fix error when parent app unmounts parcel before Vue component unmount (#121)
* Fix error when parent app unmounts parcel before Vue component unmount * Self review
1 parent 44fc2fe commit 6bbb8e2

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/parcel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default {
6565
});
6666
},
6767
singleSpaUnmount() {
68-
if (this.parcel) {
68+
if (this.parcel && this.parcel.getStatus() === "MOUNTED") {
6969
return this.parcel.unmount();
7070
}
7171
},

src/parcel.test.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import { mount } from "@vue/test-utils";
2-
import { mountRootParcel } from "single-spa";
2+
import {
3+
mountRootParcel,
4+
registerApplication,
5+
start,
6+
triggerAppChange,
7+
unregisterApplication,
8+
} from "single-spa";
39
import Parcel from "./parcel.js";
410

511
describe("Parcel", () => {
612
let wrapper;
713

14+
beforeAll(() => {
15+
start();
16+
});
17+
818
afterEach(() => {
919
if (wrapper) {
1020
wrapper.destroy();
@@ -213,6 +223,47 @@ describe("Parcel", () => {
213223
expect(config.mounted).toBe(true);
214224
expect(wrapper.find("button#parcel").exists()).toBe(true);
215225
});
226+
227+
it(`doesn't throw error if parent application is unmounted`, async () => {
228+
let appMounted = true;
229+
const config = createParcelConfig();
230+
231+
registerApplication({
232+
name: "parent-app-unmount",
233+
activeWhen() {
234+
return appMounted;
235+
},
236+
app: {
237+
async bootstrap() {},
238+
async mount(props) {
239+
wrapper = await mount(Parcel, {
240+
propsData: {
241+
config,
242+
mountParcel: props.mountParcel,
243+
},
244+
});
245+
},
246+
async unmount() {},
247+
},
248+
});
249+
250+
await triggerAppChange();
251+
await tick();
252+
253+
expect(config.mounted).toBe(true);
254+
255+
appMounted = false;
256+
257+
await triggerAppChange();
258+
259+
expect(config.mounted).toBe(false);
260+
261+
// This is what caused the error in https://github.com/single-spa/single-spa-vue/pull/95
262+
// Trying to unmount the vue component after the single-spa app already unmounted the parcel
263+
await wrapper.destroy();
264+
265+
unregisterApplication("parent-app-unmount");
266+
});
216267
});
217268

218269
function createParcelConfig(opts = {}) {

0 commit comments

Comments
 (0)