|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; |
| 12 | + |
| 13 | +import * as Fantom from '@react-native/fantom'; |
| 14 | +import * as React from 'react'; |
| 15 | +import {SectionList, Text} from 'react-native'; |
| 16 | + |
| 17 | +describe('<SectionList>', () => { |
| 18 | + describe('renders empty list', () => { |
| 19 | + it('renders a scroll view with an empty content container', () => { |
| 20 | + const root = Fantom.createRoot(); |
| 21 | + Fantom.runTask(() => { |
| 22 | + root.render( |
| 23 | + <SectionList |
| 24 | + sections={[]} |
| 25 | + renderItem={({item}) => <Text>{item.key}</Text>} |
| 26 | + />, |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + expect( |
| 31 | + JSON.stringify(root.getRenderedOutput({props: []}).toJSX()), |
| 32 | + ).toEqual( |
| 33 | + JSON.stringify( |
| 34 | + <rn-scrollView> |
| 35 | + <rn-view /> |
| 36 | + </rn-scrollView>, |
| 37 | + ), |
| 38 | + ); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('renders section with items', () => { |
| 43 | + it('renders section items inside the scroll view when renderSectionHeader returns null', () => { |
| 44 | + const root = Fantom.createRoot(); |
| 45 | + Fantom.runTask(() => { |
| 46 | + root.render( |
| 47 | + <SectionList |
| 48 | + sections={[{key: 's1', data: [{key: 'i1'}, {key: 'i2'}]}]} |
| 49 | + renderItem={({item}) => <Text>{item.key}</Text>} |
| 50 | + renderSectionHeader={() => null} |
| 51 | + />, |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + expect( |
| 56 | + JSON.stringify(root.getRenderedOutput({props: []}).toJSX()), |
| 57 | + ).toEqual( |
| 58 | + JSON.stringify( |
| 59 | + <rn-scrollView> |
| 60 | + <rn-view> |
| 61 | + <rn-paragraph key="0">i1</rn-paragraph> |
| 62 | + <rn-paragraph key="1">i2</rn-paragraph> |
| 63 | + </rn-view> |
| 64 | + </rn-scrollView>, |
| 65 | + ), |
| 66 | + ); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('renders section header', () => { |
| 71 | + it('renders section header above items', () => { |
| 72 | + const root = Fantom.createRoot(); |
| 73 | + Fantom.runTask(() => { |
| 74 | + root.render( |
| 75 | + <SectionList |
| 76 | + sections={[{key: 's1', data: [{key: 'i1'}, {key: 'i2'}]}]} |
| 77 | + renderItem={({item}) => <Text>{item.key}</Text>} |
| 78 | + renderSectionHeader={({section}) => ( |
| 79 | + <Text>Header: {section.key}</Text> |
| 80 | + )} |
| 81 | + />, |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + const output = JSON.stringify( |
| 86 | + root.getRenderedOutput({props: []}).toJSX(), |
| 87 | + ); |
| 88 | + expect(output).toContain('Header: s1'); |
| 89 | + expect(output).toContain('i1'); |
| 90 | + expect(output).toContain('i2'); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('renders section footer', () => { |
| 95 | + it('renders section footer when there is no data', () => { |
| 96 | + const root = Fantom.createRoot(); |
| 97 | + Fantom.runTask(() => { |
| 98 | + root.render( |
| 99 | + <SectionList |
| 100 | + sections={[{key: 's1', data: []}]} |
| 101 | + renderItem={({item}) => <Text>{item.key}</Text>} |
| 102 | + renderSectionHeader={({section}) => ( |
| 103 | + <Text>Header: {section.key}</Text> |
| 104 | + )} |
| 105 | + renderSectionFooter={({section}) => ( |
| 106 | + <Text>Footer: {section.key}</Text> |
| 107 | + )} |
| 108 | + />, |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + const output = JSON.stringify( |
| 113 | + root.getRenderedOutput({props: []}).toJSX(), |
| 114 | + ); |
| 115 | + expect(output).toContain('Header: s1'); |
| 116 | + expect(output).toContain('Footer: s1'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('renders section footer when there is no data and no header', () => { |
| 120 | + const root = Fantom.createRoot(); |
| 121 | + Fantom.runTask(() => { |
| 122 | + root.render( |
| 123 | + <SectionList |
| 124 | + sections={[{key: 's1', data: []}]} |
| 125 | + renderItem={({item}) => <Text>{item.key}</Text>} |
| 126 | + renderSectionFooter={({section}) => ( |
| 127 | + <Text>Footer: {section.key}</Text> |
| 128 | + )} |
| 129 | + />, |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + const output = JSON.stringify( |
| 134 | + root.getRenderedOutput({props: []}).toJSX(), |
| 135 | + ); |
| 136 | + expect(output).toContain('Footer: s1'); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('renders multiple sections', () => { |
| 141 | + it('renders items from all sections', () => { |
| 142 | + const root = Fantom.createRoot(); |
| 143 | + Fantom.runTask(() => { |
| 144 | + root.render( |
| 145 | + <SectionList |
| 146 | + sections={[ |
| 147 | + {key: 's1', data: [{key: 'i1'}, {key: 'i2'}]}, |
| 148 | + {key: 's2', data: [{key: 'i3'}, {key: 'i4'}]}, |
| 149 | + ]} |
| 150 | + renderItem={({item}) => <Text>{item.key}</Text>} |
| 151 | + renderSectionHeader={() => null} |
| 152 | + />, |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + const output = JSON.stringify( |
| 157 | + root.getRenderedOutput({props: []}).toJSX(), |
| 158 | + ); |
| 159 | + expect(output).toContain('i1'); |
| 160 | + expect(output).toContain('i2'); |
| 161 | + expect(output).toContain('i3'); |
| 162 | + expect(output).toContain('i4'); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe('renders list header and footer', () => { |
| 167 | + it('renders list header and footer with section items', () => { |
| 168 | + const root = Fantom.createRoot(); |
| 169 | + Fantom.runTask(() => { |
| 170 | + root.render( |
| 171 | + <SectionList |
| 172 | + initialNumToRender={Infinity} |
| 173 | + ListHeaderComponent={() => <Text>List Header</Text>} |
| 174 | + ListFooterComponent={() => <Text>List Footer</Text>} |
| 175 | + sections={[{key: 's1', data: [{key: 'i1'}]}]} |
| 176 | + renderItem={({item}) => <Text>{item.key}</Text>} |
| 177 | + renderSectionHeader={() => null} |
| 178 | + />, |
| 179 | + ); |
| 180 | + }); |
| 181 | + |
| 182 | + const output = JSON.stringify( |
| 183 | + root.getRenderedOutput({props: []}).toJSX(), |
| 184 | + ); |
| 185 | + expect(output).toContain('List Header'); |
| 186 | + expect(output).toContain('List Footer'); |
| 187 | + expect(output).toContain('i1'); |
| 188 | + }); |
| 189 | + }); |
| 190 | +}); |
0 commit comments