|
| 1 | +import { h, createElement, render, hydrate, Fragment } from 'preact'; |
| 2 | +import { setupScratch, teardown } from '../_util/helpers'; |
| 3 | + |
| 4 | +/** @jsx createElement */ |
| 5 | +/** @jsxFrag Fragment */ |
| 6 | + |
| 7 | +const COMMENT = '!--'; |
| 8 | + |
| 9 | +describe.only('keys', () => { |
| 10 | + /** @type {HTMLDivElement} */ |
| 11 | + let scratch; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + scratch = setupScratch(); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + teardown(scratch); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should not render comments', () => { |
| 22 | + render(h(COMMENT, null, 'test'), scratch); |
| 23 | + expect(scratch.innerHTML).to.equal('<!--test-->'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should render comments in elements', () => { |
| 27 | + render(<div>{h(COMMENT, null, 'test')}</div>, scratch); |
| 28 | + expect(scratch.innerHTML).to.equal('<div><!--test--></div>'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should render Components that return comments', () => { |
| 32 | + function App() { |
| 33 | + return h(COMMENT, null, 'test'); |
| 34 | + } |
| 35 | + render(<App />, scratch); |
| 36 | + expect(scratch.innerHTML).to.equal('<!--test-->'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should render Fragments that wrap comments', () => { |
| 40 | + function App() { |
| 41 | + return <Fragment>{h(COMMENT, null, 'test')}</Fragment>; |
| 42 | + } |
| 43 | + render(<App />, scratch); |
| 44 | + expect(scratch.innerHTML).to.equal('<!--test-->'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should render components that use comments to delimit start and end of a component', () => { |
| 48 | + function App() { |
| 49 | + return ( |
| 50 | + <div> |
| 51 | + {h(COMMENT, null, 'start')} |
| 52 | + <div>test</div> |
| 53 | + {h(COMMENT, null, 'end')} |
| 54 | + </div> |
| 55 | + ); |
| 56 | + } |
| 57 | + render(<App />, scratch); |
| 58 | + expect(scratch.innerHTML).to.equal( |
| 59 | + '<div><!--start--><div>test</div><!--end--></div>' |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should render components that use comments to delimit start and end of a component with a Fragment', () => { |
| 64 | + function App() { |
| 65 | + return ( |
| 66 | + <Fragment> |
| 67 | + {h(COMMENT, null, 'start')} |
| 68 | + <div>test</div> |
| 69 | + {h(COMMENT, null, 'end')} |
| 70 | + </Fragment> |
| 71 | + ); |
| 72 | + } |
| 73 | + render(<App />, scratch); |
| 74 | + expect(scratch.innerHTML).to.equal('<!--start--><div>test</div><!--end-->'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should move comments to the correct location when moving a component', () => { |
| 78 | + function Child() { |
| 79 | + return ( |
| 80 | + <> |
| 81 | + {h(COMMENT, null, 'start')} |
| 82 | + <div>test</div> |
| 83 | + {h(COMMENT, null, 'end')} |
| 84 | + </> |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + /** @type {(props: { move?: boolean }) => any} */ |
| 89 | + function App({ move = false }) { |
| 90 | + if (move) { |
| 91 | + return [ |
| 92 | + <div key="a">a</div>, |
| 93 | + <Child key="child" />, |
| 94 | + <div key="b">b</div> |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | + return [ |
| 99 | + <Child key="child" />, |
| 100 | + <div key="a">a</div>, |
| 101 | + <div key="b">b</div> |
| 102 | + ]; |
| 103 | + } |
| 104 | + |
| 105 | + const childHTML = '<!--start--><div>test</div><!--end-->'; |
| 106 | + |
| 107 | + render(<App />, scratch); |
| 108 | + expect(scratch.innerHTML).to.equal(`${childHTML}<div>a</div><div>b</div>`); |
| 109 | + |
| 110 | + render(<App move />, scratch); |
| 111 | + expect(scratch.innerHTML).to.equal(`<div>a</div>${childHTML}<div>b</div>`); |
| 112 | + |
| 113 | + render(<App />, scratch); |
| 114 | + expect(scratch.innerHTML).to.equal(`${childHTML}<div>a</div><div>b</div>`); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should correctly show hide DOM around comments', () => { |
| 118 | + function App({ show = false }) { |
| 119 | + return ( |
| 120 | + <> |
| 121 | + {h(COMMENT, null, 'start')} |
| 122 | + {show && <div>test</div>} |
| 123 | + {h(COMMENT, null, 'end')} |
| 124 | + </> |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + render(<App />, scratch); |
| 129 | + expect(scratch.innerHTML).to.equal('<!--start--><!--end-->'); |
| 130 | + |
| 131 | + render(<App show />, scratch); |
| 132 | + expect(scratch.innerHTML).to.equal('<!--start--><div>test</div><!--end-->'); |
| 133 | + |
| 134 | + render(<App />, scratch); |
| 135 | + expect(scratch.innerHTML).to.equal('<!--start--><!--end-->'); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should hydrate comments VNodes', () => { |
| 139 | + scratch.innerHTML = '<div><!--test--></div>'; |
| 140 | + hydrate(<div>{h(COMMENT, null, 'test')}</div>, scratch); |
| 141 | + expect(scratch.innerHTML).to.equal('<div><!--test--></div>'); |
| 142 | + }); |
| 143 | +}); |
0 commit comments