|
1 |
| -import render from '../src'; |
| 1 | +import { default as defaultRender, render, shallowRender } from '../src'; |
2 | 2 | import { h, Component } from 'preact';
|
3 | 3 | import { expect, use } from 'chai';
|
4 | 4 | import { spy, match } from 'sinon';
|
5 | 5 | import sinonChai from 'sinon-chai';
|
6 | 6 | use(sinonChai);
|
7 | 7 |
|
8 | 8 | describe('render-to-string', () => {
|
9 |
| - describe('Basic JSX', () => { |
10 |
| - it('should render JSX', () => { |
11 |
| - let rendered = render(<div class="foo">bar</div>), |
12 |
| - expected = `<div class="foo">bar</div>`; |
13 |
| - |
14 |
| - expect(rendered).to.equal(expected); |
| 9 | + describe('default()', () => { |
| 10 | + it('should be render()', () => { |
| 11 | + expect(defaultRender).to.equal(render); |
15 | 12 | });
|
| 13 | + }); |
16 | 14 |
|
17 |
| - it('should omit null and undefined attributes', () => { |
18 |
| - let rendered = render(<div a={null} b={undefined} />), |
19 |
| - expected = `<div></div>`; |
20 |
| - |
21 |
| - expect(rendered).to.equal(expected); |
| 15 | + describe('render()', () => { |
| 16 | + it('should be a function', () => { |
| 17 | + expect(render).to.be.a('function'); |
22 | 18 | });
|
23 |
| - }); |
24 | 19 |
|
25 |
| - describe('Functional Components', () => { |
26 |
| - it('should render functional components', () => { |
27 |
| - let Test = spy( ({ foo, children }) => <div foo={foo}>{ children }</div> ); |
| 20 | + describe('Basic JSX', () => { |
| 21 | + it('should render JSX', () => { |
| 22 | + let rendered = render(<div class="foo">bar</div>), |
| 23 | + expected = `<div class="foo">bar</div>`; |
28 | 24 |
|
29 |
| - let rendered = render(<Test foo="test">content</Test>); |
| 25 | + expect(rendered).to.equal(expected); |
| 26 | + }); |
30 | 27 |
|
31 |
| - expect(rendered) |
32 |
| - .to.equal(`<div foo="test">content</div>`); |
| 28 | + it('should omit null and undefined attributes', () => { |
| 29 | + let rendered = render(<div a={null} b={undefined} />), |
| 30 | + expected = `<div></div>`; |
33 | 31 |
|
34 |
| - expect(Test) |
35 |
| - .to.have.been.calledOnce |
36 |
| - .and.calledWithExactly( |
37 |
| - match({ |
38 |
| - foo: 'test', |
39 |
| - children: ['content'] |
40 |
| - }) |
41 |
| - ); |
| 32 | + expect(rendered).to.equal(expected); |
| 33 | + }); |
42 | 34 | });
|
43 | 35 |
|
44 |
| - it('should render functional components within JSX', () => { |
45 |
| - let Test = spy( ({ foo, children }) => <div foo={foo}>{ children }</div> ); |
| 36 | + describe('Functional Components', () => { |
| 37 | + it('should render functional components', () => { |
| 38 | + let Test = spy( ({ foo, children }) => <div foo={foo}>{ children }</div> ); |
46 | 39 |
|
47 |
| - let rendered = render( |
48 |
| - <section> |
49 |
| - <Test foo={1}><span>asdf</span></Test> |
50 |
| - </section> |
51 |
| - ); |
| 40 | + let rendered = render(<Test foo="test">content</Test>); |
| 41 | + |
| 42 | + expect(rendered) |
| 43 | + .to.equal(`<div foo="test">content</div>`); |
| 44 | + |
| 45 | + expect(Test) |
| 46 | + .to.have.been.calledOnce |
| 47 | + .and.calledWithExactly( |
| 48 | + match({ |
| 49 | + foo: 'test', |
| 50 | + children: ['content'] |
| 51 | + }) |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should render functional components within JSX', () => { |
| 56 | + let Test = spy( ({ foo, children }) => <div foo={foo}>{ children }</div> ); |
52 | 57 |
|
53 |
| - expect(rendered) |
54 |
| - .to.equal(`<section><div foo="1"><span>asdf</span></div></section>`); |
55 |
| - |
56 |
| - expect(Test) |
57 |
| - .to.have.been.calledOnce |
58 |
| - .and.calledWithExactly( |
59 |
| - match({ |
60 |
| - foo: 1, |
61 |
| - children: [ |
62 |
| - match({ nodeName:'span', children:['asdf'] }) |
63 |
| - ] |
64 |
| - }) |
| 58 | + let rendered = render( |
| 59 | + <section> |
| 60 | + <Test foo={1}><span>asdf</span></Test> |
| 61 | + </section> |
65 | 62 | );
|
| 63 | + |
| 64 | + expect(rendered) |
| 65 | + .to.equal(`<section><div foo="1"><span>asdf</span></div></section>`); |
| 66 | + |
| 67 | + expect(Test) |
| 68 | + .to.have.been.calledOnce |
| 69 | + .and.calledWithExactly( |
| 70 | + match({ |
| 71 | + foo: 1, |
| 72 | + children: [ |
| 73 | + match({ nodeName:'span', children:['asdf'] }) |
| 74 | + ] |
| 75 | + }) |
| 76 | + ); |
| 77 | + }); |
66 | 78 | });
|
67 |
| - }); |
68 | 79 |
|
69 |
| - describe('Classical Components', () => { |
70 |
| - it('should render classical components', () => { |
71 |
| - class Test extends Component { |
72 |
| - render({ foo, children }, state) { |
73 |
| - return <div foo={foo}>{ children }</div>; |
| 80 | + describe('Classical Components', () => { |
| 81 | + it('should render classical components', () => { |
| 82 | + class Test extends Component { |
| 83 | + render({ foo, children }, state) { |
| 84 | + return <div foo={foo}>{ children }</div>; |
| 85 | + } |
74 | 86 | }
|
75 |
| - } |
76 | 87 |
|
77 |
| - Test = spy(Test); |
78 |
| - spy(Test.prototype, 'render'); |
| 88 | + Test = spy(Test); |
| 89 | + spy(Test.prototype, 'render'); |
79 | 90 |
|
80 |
| - let rendered = render(<Test foo="test">content</Test>); |
| 91 | + let rendered = render(<Test foo="test">content</Test>); |
81 | 92 |
|
82 |
| - expect(rendered) |
83 |
| - .to.equal(`<div foo="test">content</div>`); |
| 93 | + expect(rendered) |
| 94 | + .to.equal(`<div foo="test">content</div>`); |
84 | 95 |
|
85 |
| - expect(Test).to.have.been.calledOnce; |
| 96 | + expect(Test).to.have.been.calledOnce; |
| 97 | + |
| 98 | + expect(Test.prototype.render) |
| 99 | + .to.have.been.calledOnce |
| 100 | + .and.calledWithExactly( |
| 101 | + match({ |
| 102 | + foo: 'test', |
| 103 | + children: ['content'] |
| 104 | + }), |
| 105 | + match({}) // empty state |
| 106 | + ); |
| 107 | + }); |
86 | 108 |
|
87 |
| - expect(Test.prototype.render) |
88 |
| - .to.have.been.calledOnce |
89 |
| - .and.calledWithExactly( |
90 |
| - match({ |
91 |
| - foo: 'test', |
92 |
| - children: ['content'] |
93 |
| - }), |
94 |
| - match({}) // empty state |
| 109 | + it('should render classical components within JSX', () => { |
| 110 | + class Test extends Component { |
| 111 | + render({ foo, children }, state) { |
| 112 | + return <div foo={foo}>{ children }</div>; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + Test = spy(Test); |
| 117 | + spy(Test.prototype, 'render'); |
| 118 | + |
| 119 | + let rendered = render( |
| 120 | + <section> |
| 121 | + <Test foo={1}><span>asdf</span></Test> |
| 122 | + </section> |
95 | 123 | );
|
| 124 | + |
| 125 | + expect(rendered) |
| 126 | + .to.equal(`<section><div foo="1"><span>asdf</span></div></section>`); |
| 127 | + |
| 128 | + expect(Test).to.have.been.calledOnce; |
| 129 | + |
| 130 | + expect(Test.prototype.render) |
| 131 | + .to.have.been.calledOnce |
| 132 | + .and.calledWithExactly( |
| 133 | + match({ |
| 134 | + foo: 1, |
| 135 | + children: [ |
| 136 | + match({ nodeName:'span', children:['asdf'] }) |
| 137 | + ] |
| 138 | + }), |
| 139 | + match({}) // empty state |
| 140 | + ); |
| 141 | + }); |
96 | 142 | });
|
97 | 143 |
|
98 |
| - it('should render classical components within JSX', () => { |
99 |
| - class Test extends Component { |
100 |
| - render({ foo, children }, state) { |
101 |
| - return <div foo={foo}>{ children }</div>; |
102 |
| - } |
103 |
| - } |
| 144 | + describe('className / class massaging', () => { |
| 145 | + it('should render class using className', () => { |
| 146 | + let rendered = render(<div className="foo bar" />); |
| 147 | + expect(rendered).to.equal('<div class="foo bar"></div>'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should render class using class', () => { |
| 151 | + let rendered = render(<div class="foo bar" />); |
| 152 | + expect(rendered).to.equal('<div class="foo bar"></div>'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should prefer className over class', () => { |
| 156 | + let rendered = render(<div class="foo" className="foo bar" />); |
| 157 | + expect(rendered).to.equal('<div class="foo bar"></div>'); |
| 158 | + }); |
| 159 | + }); |
| 160 | + }); |
104 | 161 |
|
105 |
| - Test = spy(Test); |
106 |
| - spy(Test.prototype, 'render'); |
| 162 | + describe('shallowRender()', () => { |
| 163 | + it('should not render nested components', () => { |
| 164 | + let Test = spy( ({ foo, children }) => <div bar={foo}><b>test child</b>{ children }</div> ); |
| 165 | + Test.displayName = 'Test'; |
107 | 166 |
|
108 |
| - let rendered = render( |
| 167 | + let rendered = shallowRender( |
109 | 168 | <section>
|
110 | 169 | <Test foo={1}><span>asdf</span></Test>
|
111 | 170 | </section>
|
112 | 171 | );
|
113 | 172 |
|
114 |
| - expect(rendered) |
115 |
| - .to.equal(`<section><div foo="1"><span>asdf</span></div></section>`); |
116 |
| - |
117 |
| - expect(Test).to.have.been.calledOnce; |
118 |
| - |
119 |
| - expect(Test.prototype.render) |
120 |
| - .to.have.been.calledOnce |
121 |
| - .and.calledWithExactly( |
122 |
| - match({ |
123 |
| - foo: 1, |
124 |
| - children: [ |
125 |
| - match({ nodeName:'span', children:['asdf'] }) |
126 |
| - ] |
127 |
| - }), |
128 |
| - match({}) // empty state |
129 |
| - ); |
| 173 | + expect(rendered).to.equal(`<section><Test foo="1"><span>asdf</span></Test></section>`); |
| 174 | + expect(Test).not.to.have.been.called; |
130 | 175 | });
|
131 |
| - }); |
132 | 176 |
|
133 |
| - describe('className / class massaging', () => { |
134 |
| - it('should render class using className', () => { |
135 |
| - let rendered = render(<div className="foo bar" />); |
136 |
| - expect(rendered).to.equal('<div class="foo bar"></div>'); |
137 |
| - }); |
| 177 | + it('should always render root component', () => { |
| 178 | + let Test = spy( ({ foo, children }) => <div bar={foo}><b>test child</b>{ children }</div> ); |
| 179 | + Test.displayName = 'Test'; |
138 | 180 |
|
139 |
| - it('should render class using class', () => { |
140 |
| - let rendered = render(<div class="foo bar" />); |
141 |
| - expect(rendered).to.equal('<div class="foo bar"></div>'); |
142 |
| - }); |
| 181 | + let rendered = shallowRender( |
| 182 | + <Test foo={1}> |
| 183 | + <span>asdf</span> |
| 184 | + </Test> |
| 185 | + ); |
143 | 186 |
|
144 |
| - it('should prefer className over class', () => { |
145 |
| - let rendered = render(<div class="foo" className="foo bar" />); |
146 |
| - expect(rendered).to.equal('<div class="foo bar"></div>'); |
| 187 | + expect(rendered).to.equal(`<div bar="1"><b>test child</b><span>asdf</span></div>`); |
| 188 | + expect(Test).to.have.been.calledOnce; |
147 | 189 | });
|
148 | 190 | });
|
149 | 191 | });
|
0 commit comments