|
| 1 | +import type { ViewStyle } from 'react-native'; |
| 2 | + |
| 3 | +import { splitStyles } from '../splitStyles'; |
| 4 | + |
| 5 | +describe('splitStyles', () => { |
| 6 | + const styles: Readonly<ViewStyle> = Object.freeze({ |
| 7 | + backgroundColor: 'red', |
| 8 | + marginTop: 1, |
| 9 | + marginBottom: 2, |
| 10 | + marginLeft: 3, |
| 11 | + padding: 4, |
| 12 | + borderTopLeftRadius: 5, |
| 13 | + borderTopRightRadius: 6, |
| 14 | + right: 4, |
| 15 | + }); |
| 16 | + |
| 17 | + it('splits margins, paddings, and border radiuses correctly', () => { |
| 18 | + const marginPredicate = (style: string) => style.startsWith('margin'); |
| 19 | + const paddingPredicate = (style: string) => style.startsWith('padding'); |
| 20 | + const borderRadiusPredicate = (style: string) => |
| 21 | + style.startsWith('border') && style.endsWith('Radius'); |
| 22 | + const [filteredStyles, marginStyles, paddingStyles, borderRadiusStyles] = |
| 23 | + splitStyles( |
| 24 | + styles, |
| 25 | + marginPredicate, |
| 26 | + paddingPredicate, |
| 27 | + borderRadiusPredicate |
| 28 | + ); |
| 29 | + |
| 30 | + expect(keysLength(filteredStyles)).toBeGreaterThan(0); |
| 31 | + expect(keysLength(filteredStyles)).toBeLessThan(keysLength(styles)); |
| 32 | + for (const style in filteredStyles) { |
| 33 | + expect(marginPredicate(style)).toBeFalsy(); |
| 34 | + expect(paddingPredicate(style)).toBeFalsy(); |
| 35 | + expect(borderRadiusPredicate(style)).toBeFalsy(); |
| 36 | + } |
| 37 | + |
| 38 | + expect(keysLength(marginStyles)).toBeGreaterThan(0); |
| 39 | + for (const style in marginStyles) { |
| 40 | + expect(marginPredicate(style)).toBeTruthy(); |
| 41 | + } |
| 42 | + |
| 43 | + expect(keysLength(paddingStyles)).toBeGreaterThan(0); |
| 44 | + for (const style in paddingStyles) { |
| 45 | + expect(paddingPredicate(style)).toBeTruthy(); |
| 46 | + } |
| 47 | + |
| 48 | + expect(keysLength(borderRadiusStyles)).toBeGreaterThan(0); |
| 49 | + for (const style in borderRadiusStyles) { |
| 50 | + expect(borderRadiusPredicate(style)).toBeTruthy(); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + it('filtered styles is an empty object if all styles matched some predicate', () => { |
| 55 | + const styles = { |
| 56 | + margin: 5, |
| 57 | + padding: 6, |
| 58 | + }; |
| 59 | + const [filteredStyles] = splitStyles( |
| 60 | + styles, |
| 61 | + (style) => style.startsWith('margin'), |
| 62 | + (style) => style.startsWith('padding') |
| 63 | + ); |
| 64 | + |
| 65 | + expect(keysLength(filteredStyles)).toBe(0); |
| 66 | + }); |
| 67 | + |
| 68 | + it('processes predicates in order', () => { |
| 69 | + const [, marginStyles, marginStyles2, marginStyles3] = splitStyles( |
| 70 | + styles, |
| 71 | + (style) => style.startsWith('margin'), |
| 72 | + (style) => style.startsWith('margin'), |
| 73 | + (style) => style.startsWith('margin') |
| 74 | + ); |
| 75 | + |
| 76 | + expect(keysLength(marginStyles)).toBeGreaterThan(0); |
| 77 | + expect(keysLength(marginStyles2)).toBe(0); |
| 78 | + expect(keysLength(marginStyles3)).toBe(0); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +function keysLength(object: object): number { |
| 83 | + return Object.keys(object).length; |
| 84 | +} |
0 commit comments