Skip to content

Commit

Permalink
test(react-native-li): enhance test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamr committed Apr 13, 2021
1 parent 2315975 commit 0e56c6b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/react-native-li/src/MarkedListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default function MarkedListItem({
const markerString = counterRenderer.renderMarker(index + startIndex);
return (
<View
testID="marked-list-item"
style={[rtlLineReversed ? styles.lineRtl : styles.lineLtr, style]}
key={index}>
{renderMarker({
Expand Down
6 changes: 5 additions & 1 deletion packages/react-native-li/src/MarkerBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ import { MarkerBoxProps } from './shared-types';
* @public
*/
export default function MarkerBox({ style, markerString }: MarkerBoxProps) {
return <Text style={style}>{markerString}</Text>;
return (
<Text testID="marker-box" style={style}>
{markerString}
</Text>
);
}
35 changes: 34 additions & 1 deletion packages/react-native-li/src/__tests__/MarkedList.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { render } from '@testing-library/react-native';
import MarkedList from '../MarkedList';
import { Text } from 'react-native';
import { StyleSheet, Text } from 'react-native';
import * as presets from '@jsamr/counter-style/presets';

describe('MarkedList', () => {
Expand All @@ -18,4 +18,37 @@ describe('MarkedList', () => {
const list = UNSAFE_getByType(MarkedList);
expect(list.children.length).toBe(100);
});

it('should reverse list items with rtlLineReversed', () => {
const { getByTestId } = render(
<MarkedList counterRenderer={presets.disc} rtlLineReversed>
<Text style={{ flexShrink: 1 }}>Lorem</Text>
</MarkedList>
);
const li = getByTestId('marked-list-item');
expect(StyleSheet.flatten(li.props.style)).toMatchObject({
flexDirection: 'row-reverse'
});
});
it('should reverse marker prefix, counter and suffix order with rtlMarkerReversed', () => {
const { getByTestId } = render(
<MarkedList counterRenderer={presets.disc} rtlMarkerReversed>
<Text style={{ flexShrink: 1 }}>Lorem</Text>
</MarkedList>
);
const mb = getByTestId('marker-box');
expect(mb.props.children).toBe(' •');
});
it('should allow to customize rtlMarkerReversed', () => {
const { getByTestId } = render(
<MarkedList
counterRenderer={presets.decimal}
startIndex={10}
rtlMarkerReversed={{ reverseCounter: true, reversePrefix: false }}>
<Text style={{ flexShrink: 1 }}>Lorem</Text>
</MarkedList>
);
const mb = getByTestId('marker-box');
expect(mb.props.children).toBe(' .01');
});
});
8 changes: 8 additions & 0 deletions packages/react-native-li/src/__tests__/useMarkedList.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ describe('useMarkedList', () => {
);
expect(result.current.maxNumOfCodepoints).toBe(expectedLen);
});
it('should work with 0 lengths', () => {
const expectedLen = counterRenderer.maxMarkerLenInRange(2, 1);
const { result } = renderHook(() =>
//@ts-expect-error
useMarkedList({ counterRenderer, startIndex: 2 })
);
expect(result.current.maxNumOfCodepoints).toBe(expectedLen);
});
});
3 changes: 2 additions & 1 deletion packages/react-native-li/src/useMarkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export default function useMarkedList({
computeMarkerBoxWidth = defaultComputeMarkerBoxWidth
}: MarkedListProps & { length: number }): Omit<MarkedListItemProps, 'index'> {
const maxNumOfCodepoints = useMemo(
() => counterRenderer.maxMarkerLenInRange(startIndex, startIndex + length - 1),
() =>
counterRenderer.maxMarkerLenInRange(startIndex, startIndex + length - 1),
[counterRenderer, length, startIndex]
);
const syntheticRtlLineReversed = !I18nManager.isRTL && rtlLineReversed;
Expand Down

0 comments on commit 0e56c6b

Please sign in to comment.