|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
1 | 4 | import { JSDOM } from 'jsdom';
|
2 |
| -import { absoluteToStylesheet, _isBlockedElement } from '../src/snapshot'; |
| 5 | +import { |
| 6 | + absoluteToStylesheet, |
| 7 | + serializeNodeWithId, |
| 8 | + _isBlockedElement, |
| 9 | +} from '../src/snapshot'; |
| 10 | +import { serializedNodeWithId } from '../src/types'; |
3 | 11 |
|
4 | 12 | describe('absolute url to stylesheet', () => {
|
5 | 13 | const href = 'http://localhost/css/style.css';
|
@@ -126,3 +134,48 @@ describe('isBlockedElement()', () => {
|
126 | 134 | ).toEqual(true);
|
127 | 135 | });
|
128 | 136 | });
|
| 137 | + |
| 138 | +describe('style elements', () => { |
| 139 | + const serializeNode = (node: Node): serializedNodeWithId | null => { |
| 140 | + return serializeNodeWithId(node, { |
| 141 | + doc: document, |
| 142 | + map: {}, |
| 143 | + blockClass: 'blockblock', |
| 144 | + blockSelector: null, |
| 145 | + maskTextClass: 'maskmask', |
| 146 | + maskTextSelector: null, |
| 147 | + skipChild: false, |
| 148 | + inlineStylesheet: true, |
| 149 | + maskTextFn: undefined, |
| 150 | + maskInputFn: undefined, |
| 151 | + slimDOMOptions: {}, |
| 152 | + }); |
| 153 | + }; |
| 154 | + |
| 155 | + const render = (html: string): HTMLStyleElement => { |
| 156 | + document.write(html); |
| 157 | + return document.querySelector('style')!; |
| 158 | + }; |
| 159 | + |
| 160 | + it('should serialize all rules of stylesheet when the sheet has a single child node', () => { |
| 161 | + const styleEl = render(`<style>body { color: red; }</style>`); |
| 162 | + styleEl.sheet?.insertRule('section { color: blue; }'); |
| 163 | + expect(serializeNode(styleEl.childNodes[0])).toMatchObject({ |
| 164 | + isStyle: true, |
| 165 | + rootId: undefined, |
| 166 | + textContent: 'section {color: blue;}body {color: red;}', |
| 167 | + type: 3, |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should serialize individual text nodes on stylesheets with multiple child nodes', () => { |
| 172 | + const styleEl = render(`<style>body { color: red; }</style>`); |
| 173 | + styleEl.append(document.createTextNode('section { color: blue; }')); |
| 174 | + expect(serializeNode(styleEl.childNodes[1])).toMatchObject({ |
| 175 | + isStyle: true, |
| 176 | + rootId: undefined, |
| 177 | + textContent: 'section { color: blue; }', |
| 178 | + type: 3, |
| 179 | + }); |
| 180 | + }); |
| 181 | +}); |
0 commit comments