Skip to content

Commit

Permalink
Remove sc style nodes on cleanup (styled-components#382)
Browse files Browse the repository at this point in the history
* Remove sc style nodes on cleanup

* Add resetStyleSheet test

* Remove unneeded 'expect' import
  • Loading branch information
atsikov committed Jul 12, 2021
1 parent 90f8813 commit e1be59f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ const sheet = mainSheet || masterSheet;
const isServer = () => typeof document === 'undefined';

const resetStyleSheet = () => {
if (!isServer()) {
const scStyles = document.querySelectorAll('style[data-styled-version]')
for (const item of scStyles) {
item.parentElement.removeChild(item)
}
}

sheet.names = new Map();
sheet.clearTag();
};
Expand Down
30 changes: 25 additions & 5 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const {
__PRIVATE__: { mainSheet, masterSheet },
} = require('styled-components');

const { getHashes } = require('../src/utils');
import styled, { __PRIVATE__ } from 'styled-components'
import { render } from '@testing-library/react';
import React from 'react'
import { getHashes, resetStyleSheet } from '../src/utils';

const { mainSheet, masterSheet } = __PRIVATE__
const sheet = mainSheet || masterSheet;

it('extracts hashes', () => {
Expand Down Expand Up @@ -36,3 +36,23 @@ it('extracts hashes', () => {

expect(getHashes()).toEqual(['sc-1', 'a', 'sc-2', 'b', 'c', 'sc-3', 'd', 'e']);
});

it('resets style sheets', () => {
const Component = styled.div`
background-color: orange;
`

render(<Component />)

expect(
document.querySelectorAll('style[data-styled-version]').length,
).not.toBe(0)
expect(sheet.names.size).not.toBe(0)

resetStyleSheet()

expect(
document.querySelectorAll('style[data-styled-version]').length,
).toBe(0)
expect(sheet.names.size).toBe(0)
})

0 comments on commit e1be59f

Please sign in to comment.