|
| 1 | +import React, { Component } from 'react' |
| 2 | +import { render } from 'react-dom' |
| 3 | +import { renderToString } from 'react-dom/server' |
| 4 | +import { JSDOM } from 'jsdom' |
| 5 | + |
| 6 | +import Style from '../UniversalStyle' |
| 7 | + |
| 8 | +afterEach(() => { |
| 9 | + global.window = undefined |
| 10 | + global.document = undefined |
| 11 | +}) |
| 12 | + |
| 13 | +describe('UniversalComponent Component', () => { |
| 14 | + it('should render a style element on the server', () => { |
| 15 | + const css = '.class1 { color: red } .class2 { color: blue }' |
| 16 | + |
| 17 | + const markup = renderToString(<Style css={css} />) |
| 18 | + expect(markup).toMatchSnapshot() |
| 19 | + }) |
| 20 | + |
| 21 | + it('should render a style element on the client', () => { |
| 22 | + const { window } = new JSDOM('<html><body></body></html>') |
| 23 | + |
| 24 | + global.window = window |
| 25 | + global.document = window.document |
| 26 | + |
| 27 | + const div = document.createElement('div') |
| 28 | + const css = '.class1 { color: red } .class2 { color: blue }' |
| 29 | + |
| 30 | + render(<Style css={css} />, div) |
| 31 | + |
| 32 | + expect(div.innerHTML).toMatchSnapshot() |
| 33 | + expect(document.head.childNodes.length).toBe(0) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should move the style to the document.head if the component unmounts', () => { |
| 37 | + const { window } = new JSDOM('<html><body></body></html>') |
| 38 | + |
| 39 | + global.window = window |
| 40 | + global.document = window.document |
| 41 | + |
| 42 | + const div = document.createElement('div') |
| 43 | + const css = '.class1 { color: red } .class2 { color: blue }' |
| 44 | + |
| 45 | + class Wrapper extends Component { |
| 46 | + constructor(props, context) { |
| 47 | + super(props, context) |
| 48 | + |
| 49 | + this.state = { |
| 50 | + isVisible: true, |
| 51 | + } |
| 52 | + |
| 53 | + window.hide = () => { |
| 54 | + this.setState({ isVisible: false }) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + render() { |
| 59 | + if (this.state.isVisible) { |
| 60 | + return <Style css={css} /> |
| 61 | + } |
| 62 | + |
| 63 | + return null |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + render(<Wrapper />, div) |
| 68 | + |
| 69 | + expect(div.innerHTML).toMatchSnapshot() |
| 70 | + expect(document.head.childNodes.length).toBe(0) |
| 71 | + |
| 72 | + window.hide() |
| 73 | + |
| 74 | + expect(div.innerHTML).toBe('') |
| 75 | + expect(document.head.childNodes.length).toBe(1) |
| 76 | + expect( |
| 77 | + document.getElementById('__react_css_component_id-0').textContent |
| 78 | + ).toBe(css) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should move each different style to the document.head if the component unmounts', () => { |
| 82 | + const { window } = new JSDOM('<html><body></body></html>') |
| 83 | + |
| 84 | + global.window = window |
| 85 | + global.document = window.document |
| 86 | + |
| 87 | + const div = document.createElement('div') |
| 88 | + const css1 = '.class1 { color: red } .class2 { color: blue }' |
| 89 | + const css2 = '.class3 { color: green }' |
| 90 | + |
| 91 | + class Wrapper extends Component { |
| 92 | + constructor(props, context) { |
| 93 | + super(props, context) |
| 94 | + |
| 95 | + this.state = { |
| 96 | + isVisible: true, |
| 97 | + } |
| 98 | + |
| 99 | + window.hide = () => { |
| 100 | + this.setState({ isVisible: false }) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + render() { |
| 105 | + if (this.state.isVisible) { |
| 106 | + return [ |
| 107 | + <Style key="style-1" css={css1} />, |
| 108 | + <Style key="style-2" css={css2} />, |
| 109 | + ] |
| 110 | + } |
| 111 | + |
| 112 | + return null |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + render(<Wrapper />, div) |
| 117 | + |
| 118 | + expect(div.innerHTML).toMatchSnapshot() |
| 119 | + expect(document.head.childNodes.length).toBe(0) |
| 120 | + |
| 121 | + window.hide() |
| 122 | + |
| 123 | + expect(div.innerHTML).toBe('') |
| 124 | + expect(document.head.childNodes.length).toBe(2) |
| 125 | + expect( |
| 126 | + document.getElementById('__react_css_component_id-0').textContent |
| 127 | + ).toBe(css1) |
| 128 | + expect( |
| 129 | + document.getElementById('__react_css_component_id-1').textContent |
| 130 | + ).toBe(css2) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should only move the style once to the document.head', () => { |
| 134 | + const { window } = new JSDOM('<html><body></body></html>') |
| 135 | + |
| 136 | + global.window = window |
| 137 | + global.document = window.document |
| 138 | + |
| 139 | + const div = document.createElement('div') |
| 140 | + const css = '.class1 { color: red } .class2 { color: blue }' |
| 141 | + |
| 142 | + class Wrapper extends Component { |
| 143 | + constructor(props, context) { |
| 144 | + super(props, context) |
| 145 | + |
| 146 | + this.state = { |
| 147 | + isVisible: true, |
| 148 | + } |
| 149 | + |
| 150 | + window.hide = () => { |
| 151 | + this.setState({ isVisible: false }) |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + render() { |
| 156 | + if (this.state.isVisible) { |
| 157 | + return [ |
| 158 | + <Style key="style-1" css={css} />, |
| 159 | + <Style key="style-2" css={css} />, |
| 160 | + ] |
| 161 | + } |
| 162 | + |
| 163 | + return null |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + render(<Wrapper />, div) |
| 168 | + |
| 169 | + expect(div.innerHTML).toMatchSnapshot() |
| 170 | + expect(document.head.childNodes.length).toBe(0) |
| 171 | + |
| 172 | + window.hide() |
| 173 | + |
| 174 | + expect(div.innerHTML).toBe('') |
| 175 | + expect(document.head.childNodes.length).toBe(1) |
| 176 | + expect( |
| 177 | + document.getElementById('__react_css_component_id-0').textContent |
| 178 | + ).toBe(css) |
| 179 | + }) |
| 180 | +}) |
0 commit comments