-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(suite-native): tests for useSectionList
- Loading branch information
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
suite-native/module-trading/src/hooks/__tests__/useSectionList.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { BasicProviderForTests, renderHook } from '@suite-native/test-utils'; | ||
|
||
import { SECTION_HEADER_HEIGHT, SectionListData, useSectionList } from '../useSectionList'; | ||
|
||
const renderUseSectionListHook = ( | ||
data: SectionListData<any, any>, | ||
estimatedItemSize: number, | ||
noSingletonSectionHeader: boolean = false, | ||
) => | ||
renderHook( | ||
() => | ||
useSectionList({ | ||
data, | ||
estimatedItemSize, | ||
renderItem: jest.fn(), | ||
keyExtractor: jest.fn(), | ||
noSingletonSectionHeader, | ||
}), | ||
{ | ||
wrapper: BasicProviderForTests, | ||
}, | ||
); | ||
|
||
describe('useSectionList', () => { | ||
const section1 = { | ||
key: 'section1', | ||
label: 'Section 1', | ||
sectionData: { id: 's1' }, | ||
data: ['item1', 'item2'], | ||
}; | ||
|
||
const section2 = { | ||
key: 'section2', | ||
label: 'Section 2', | ||
sectionData: { id: 's2' }, | ||
data: ['item3', 'item4', 'item5'], | ||
}; | ||
|
||
const mockData = [section1, section2]; | ||
|
||
const ITEM_SIZE_50 = 50; | ||
|
||
describe('data transformation', () => { | ||
it('should correctly transform data with section headers', () => { | ||
const { result } = renderUseSectionListHook(mockData, ITEM_SIZE_50); | ||
|
||
const expectedTransformedData = [ | ||
['sectionHeader', 'Section 1', 'section1'], | ||
['item', 'item1', { isFirst: true, isLast: false, sectionData: { id: 's1' } }], | ||
['item', 'item2', { isFirst: false, isLast: true, sectionData: { id: 's1' } }], | ||
['sectionHeader', 'Section 2', 'section2'], | ||
['item', 'item3', { isFirst: true, isLast: false, sectionData: { id: 's2' } }], | ||
['item', 'item4', { isFirst: false, isLast: false, sectionData: { id: 's2' } }], | ||
['item', 'item5', { isFirst: false, isLast: true, sectionData: { id: 's2' } }], | ||
]; | ||
|
||
expect(result.current.data).toEqual(expectedTransformedData); | ||
}); | ||
|
||
it('should handle single section with noSingletonSectionHeader=true', () => { | ||
const { result } = renderUseSectionListHook([section1], ITEM_SIZE_50, true); | ||
|
||
const expectedTransformedData = [ | ||
['item', 'item1', { isFirst: true, isLast: false, sectionData: { id: 's1' } }], | ||
['item', 'item2', { isFirst: false, isLast: true, sectionData: { id: 's1' } }], | ||
]; | ||
|
||
expect(result.current.data).toEqual(expectedTransformedData); | ||
}); | ||
|
||
it('should handle empty data', () => { | ||
const { result } = renderUseSectionListHook([], ITEM_SIZE_50, true); | ||
|
||
expect(result.current.data).toEqual([]); | ||
}); | ||
it('should handle empty sections', () => { | ||
const { result } = renderUseSectionListHook( | ||
[{ ...section1, data: [] }], | ||
ITEM_SIZE_50, | ||
true, | ||
); | ||
|
||
expect(result.current.data).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('sections and items count', () => { | ||
it('should correctly calculate sectionsCount', () => { | ||
const { result } = renderUseSectionListHook(mockData, ITEM_SIZE_50); | ||
|
||
expect(result.current.sectionsCount).toBe(2); | ||
}); | ||
|
||
it('should correctly calculate itemsCount', () => { | ||
const { result } = renderUseSectionListHook(mockData, ITEM_SIZE_50); | ||
|
||
expect(result.current.itemsCount).toBe(5); | ||
}); | ||
}); | ||
|
||
describe('estimatedListSize', () => { | ||
it('should calculate size with section headers', () => { | ||
const { result } = renderUseSectionListHook(mockData, ITEM_SIZE_50); | ||
|
||
expect(result.current.estimatedListSize).toBe( | ||
5 * ITEM_SIZE_50 + 2 * SECTION_HEADER_HEIGHT, | ||
); | ||
}); | ||
|
||
it('should calculate size without section header for singleton', () => { | ||
const { result } = renderUseSectionListHook([section2], ITEM_SIZE_50, true); | ||
|
||
expect(result.current.estimatedListSize).toBe(section2.data.length * ITEM_SIZE_50); | ||
}); | ||
}); | ||
}); |