|
| 1 | +import render from '../src'; |
| 2 | +import { h, Component } from 'preact'; |
| 3 | +import { expect, use } from 'chai'; |
| 4 | +import { spy, match } from 'sinon'; |
| 5 | +import sinonChai from 'sinon-chai'; |
| 6 | +use(sinonChai); |
| 7 | + |
| 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); |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('Functional Components', () => { |
| 19 | + it('should render functional components', () => { |
| 20 | + let Test = spy( ({ foo, children }) => <div foo={foo}>{ children }</div> ); |
| 21 | + |
| 22 | + let rendered = render(<Test foo="test">content</Test>); |
| 23 | + |
| 24 | + expect(rendered) |
| 25 | + .to.equal(`<div foo="test">content</div>`); |
| 26 | + |
| 27 | + expect(Test) |
| 28 | + .to.have.been.calledOnce |
| 29 | + .and.calledWithExactly( |
| 30 | + match({ |
| 31 | + foo: 'test', |
| 32 | + children: ['content'] |
| 33 | + }) |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should render functional components within JSX', () => { |
| 38 | + let Test = spy( ({ foo, children }) => <div foo={foo}>{ children }</div> ); |
| 39 | + |
| 40 | + let rendered = render( |
| 41 | + <section> |
| 42 | + <Test foo={1}><span>asdf</span></Test> |
| 43 | + </section> |
| 44 | + ); |
| 45 | + |
| 46 | + expect(rendered) |
| 47 | + .to.equal(`<section><div foo="1"><span>asdf</span></div></section>`); |
| 48 | + |
| 49 | + expect(Test) |
| 50 | + .to.have.been.calledOnce |
| 51 | + .and.calledWithExactly( |
| 52 | + match({ |
| 53 | + foo: 1, |
| 54 | + children: [ |
| 55 | + match({ nodeName:'span', children:['asdf'] }) |
| 56 | + ] |
| 57 | + }) |
| 58 | + ); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('Classical Components', () => { |
| 63 | + it('should render classical components', () => { |
| 64 | + class Test extends Component { |
| 65 | + render({ foo, children }, state) { |
| 66 | + return <div foo={foo}>{ children }</div>; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Test = spy(Test); |
| 71 | + spy(Test.prototype, 'render'); |
| 72 | + |
| 73 | + let rendered = render(<Test foo="test">content</Test>); |
| 74 | + |
| 75 | + expect(rendered) |
| 76 | + .to.equal(`<div foo="test">content</div>`); |
| 77 | + |
| 78 | + expect(Test).to.have.been.calledOnce; |
| 79 | + |
| 80 | + expect(Test.prototype.render) |
| 81 | + .to.have.been.calledOnce |
| 82 | + .and.calledWithExactly( |
| 83 | + match({ |
| 84 | + foo: 'test', |
| 85 | + children: ['content'] |
| 86 | + }), |
| 87 | + match({}) // empty state |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should render classical components within JSX', () => { |
| 92 | + class Test extends Component { |
| 93 | + render({ foo, children }, state) { |
| 94 | + return <div foo={foo}>{ children }</div>; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + Test = spy(Test); |
| 99 | + spy(Test.prototype, 'render'); |
| 100 | + |
| 101 | + let rendered = render( |
| 102 | + <section> |
| 103 | + <Test foo={1}><span>asdf</span></Test> |
| 104 | + </section> |
| 105 | + ); |
| 106 | + |
| 107 | + expect(rendered) |
| 108 | + .to.equal(`<section><div foo="1"><span>asdf</span></div></section>`); |
| 109 | + |
| 110 | + expect(Test).to.have.been.calledOnce; |
| 111 | + |
| 112 | + expect(Test.prototype.render) |
| 113 | + .to.have.been.calledOnce |
| 114 | + .and.calledWithExactly( |
| 115 | + match({ |
| 116 | + foo: 1, |
| 117 | + children: [ |
| 118 | + match({ nodeName:'span', children:['asdf'] }) |
| 119 | + ] |
| 120 | + }), |
| 121 | + match({}) // empty state |
| 122 | + ); |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments