Skip to content

Commit e1be59f

Browse files
authored
Remove sc style nodes on cleanup (#382)
* Remove sc style nodes on cleanup * Add resetStyleSheet test * Remove unneeded 'expect' import
1 parent 90f8813 commit e1be59f

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ const sheet = mainSheet || masterSheet;
1111
const isServer = () => typeof document === 'undefined';
1212

1313
const resetStyleSheet = () => {
14+
if (!isServer()) {
15+
const scStyles = document.querySelectorAll('style[data-styled-version]')
16+
for (const item of scStyles) {
17+
item.parentElement.removeChild(item)
18+
}
19+
}
20+
1421
sheet.names = new Map();
1522
sheet.clearTag();
1623
};

test/utils.spec.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const {
2-
__PRIVATE__: { mainSheet, masterSheet },
3-
} = require('styled-components');
4-
5-
const { getHashes } = require('../src/utils');
1+
import styled, { __PRIVATE__ } from 'styled-components'
2+
import { render } from '@testing-library/react';
3+
import React from 'react'
4+
import { getHashes, resetStyleSheet } from '../src/utils';
65

6+
const { mainSheet, masterSheet } = __PRIVATE__
77
const sheet = mainSheet || masterSheet;
88

99
it('extracts hashes', () => {
@@ -36,3 +36,23 @@ it('extracts hashes', () => {
3636

3737
expect(getHashes()).toEqual(['sc-1', 'a', 'sc-2', 'b', 'c', 'sc-3', 'd', 'e']);
3838
});
39+
40+
it('resets style sheets', () => {
41+
const Component = styled.div`
42+
background-color: orange;
43+
`
44+
45+
render(<Component />)
46+
47+
expect(
48+
document.querySelectorAll('style[data-styled-version]').length,
49+
).not.toBe(0)
50+
expect(sheet.names.size).not.toBe(0)
51+
52+
resetStyleSheet()
53+
54+
expect(
55+
document.querySelectorAll('style[data-styled-version]').length,
56+
).toBe(0)
57+
expect(sheet.names.size).toBe(0)
58+
})

0 commit comments

Comments
 (0)