Skip to content

Commit 1191759

Browse files
authored
fix: can not remove style correctly in qiankun context (#373)
* fix: can not remove style correctly in qiankun context * chore: add ut * chore: fix ci
1 parent 428089a commit 1191759

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/Dom/dynamicCSS.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ function findExistNode(key: string, option: Options = {}) {
102102

103103
export function removeCSS(key: string, option: Options = {}) {
104104
const existNode = findExistNode(key, option);
105-
existNode?.parentNode?.removeChild(existNode);
105+
if (existNode) {
106+
const container = getContainer(option);
107+
container.removeChild(existNode);
108+
}
106109
}
107110

108111
/**
@@ -116,10 +119,17 @@ function syncRealContainer(container: Element, option: Options) {
116119
const placeholderStyle = injectCSS('', option);
117120
const { parentNode } = placeholderStyle;
118121
containerCache.set(container, parentNode);
119-
parentNode.removeChild(placeholderStyle);
122+
container.removeChild(placeholderStyle);
120123
}
121124
}
122125

126+
/**
127+
* manually clear container cache to avoid global cache in unit testes
128+
*/
129+
export function clearContainerCache() {
130+
containerCache.clear();
131+
}
132+
123133
export function updateCSS(css: string, key: string, option: Options = {}) {
124134
const container = getContainer(option);
125135

tests/dynamicCSS.test.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
/* eslint-disable no-eval */
2-
import { injectCSS, updateCSS, removeCSS } from '../src/Dom/dynamicCSS';
2+
import {
3+
injectCSS,
4+
updateCSS,
5+
removeCSS,
6+
clearContainerCache,
7+
} from '../src/Dom/dynamicCSS';
38

49
const TEST_STYLE = '.bamboo { context: "light" }';
510

@@ -171,15 +176,20 @@ describe('dynamicCSS', () => {
171176

172177
describe('qiankun', () => {
173178
let originAppendChild: typeof document.head.appendChild;
179+
let originRemoveChild: typeof document.head.removeChild;
174180
let targetContainer: HTMLElement;
175181

176182
beforeAll(() => {
183+
clearContainerCache();
177184
originAppendChild = document.head.appendChild;
185+
originRemoveChild = document.head.removeChild;
178186
document.head.appendChild = ele => targetContainer.appendChild(ele);
187+
document.head.removeChild = ele => targetContainer.removeChild(ele);
179188
});
180189

181190
afterAll(() => {
182191
document.head.appendChild = originAppendChild;
192+
document.head.removeChild = originRemoveChild;
183193
});
184194

185195
it('updateCSS', () => {
@@ -191,7 +201,12 @@ describe('dynamicCSS', () => {
191201
expect(document.head.contains(firstStyle)).toBeFalsy();
192202
expect(targetContainer.contains(firstStyle)).toBeTruthy();
193203

194-
// Mock qiankun uninstall & reinstall
204+
// Mock qiankun uninstall
205+
removeCSS('qiankun');
206+
expect(document.head.contains(firstStyle)).toBeFalsy();
207+
expect(targetContainer.contains(firstStyle)).toBeFalsy();
208+
209+
// Mock qiankun reinstall
195210
document.body.removeChild(targetContainer);
196211
targetContainer = document.createElement('div');
197212
document.body.appendChild(targetContainer);

0 commit comments

Comments
 (0)