@@ -2,44 +2,94 @@ jest.mock('react-native');
2
2
import { scale , verticalScale , moderateScale } from '../lib/scalingUtils' ;
3
3
import ScaledSheet from '../lib/ScaledSheet' ;
4
4
5
- const input = {
6
- container : {
7
- width : '30@s' ,
8
- height : '50@vs' ,
9
- margin : {
10
- width : 12 ,
11
- height : '12@s' ,
12
- paddingBottom : - 1
5
+ const getRandomInt = ( min = 1 , max = 100 ) => Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ;
6
+
7
+ test ( 'Scale works' , ( ) => {
8
+ const number = getRandomInt ( ) ;
9
+ const input = { test : `${ number } @s` }
10
+ expect ( ScaledSheet . create ( input ) . test ) . toBe ( scale ( number ) ) ;
11
+ } ) ;
12
+
13
+ test ( 'verticalScale works' , ( ) => {
14
+ const number = getRandomInt ( ) ;
15
+ const input = { test : `${ number } @vs` }
16
+ expect ( ScaledSheet . create ( input ) . test ) . toBe ( verticalScale ( number ) ) ;
17
+ } ) ;
18
+
19
+ test ( 'moderateScale with default factor works' , ( ) => {
20
+ const number = getRandomInt ( ) ;
21
+ const input = { test : `${ number } @ms` }
22
+ expect ( ScaledSheet . create ( input ) . test ) . toBe ( moderateScale ( number ) ) ;
23
+ } ) ;
24
+
25
+ test ( 'moderateScale with custom factor works' , ( ) => {
26
+ const number = getRandomInt ( ) ;
27
+ const input = { test : `${ number } @ms0.7` }
28
+ expect ( ScaledSheet . create ( input ) . test ) . toBe ( moderateScale ( number , 0.7 ) ) ;
29
+ } ) ;
30
+
31
+ test ( 'Scale works with a negative value' , ( ) => {
32
+ const number = getRandomInt ( - 100 , - 1 ) ;
33
+ const input = { test : `${ number } @s` }
34
+ expect ( ScaledSheet . create ( input ) . test ) . toBe ( scale ( number ) ) ;
35
+ } ) ;
36
+
37
+ test ( 'moderateScale works with a negative value' , ( ) => {
38
+ const number = getRandomInt ( - 100 , - 1 ) ;
39
+ const input = { test : `${ number } @ms0.3` }
40
+ expect ( ScaledSheet . create ( input ) . test ) . toBe ( moderateScale ( number , 0.3 ) ) ;
41
+ } ) ;
42
+
43
+ test ( 'Scale works on a deeply nested object' , ( ) => {
44
+ const number = getRandomInt ( ) ;
45
+ const input = { test : { test : { test : `${ number } @s` } } }
46
+ expect ( ScaledSheet . create ( input ) . test . test . test ) . toBe ( scale ( number ) ) ;
47
+ } ) ;
48
+
49
+ test ( 'No special annotation should leave the number intact' , ( ) => {
50
+ const number = getRandomInt ( ) ;
51
+ const input = { test : number }
52
+ expect ( ScaledSheet . create ( input ) . test ) . toBe ( number ) ;
53
+ } ) ;
54
+
55
+ test ( 'ScaledSheet should map a complete StyleSheet with special annotations' , ( ) => {
56
+ const input = {
57
+ container : {
58
+ width : '30@s' ,
59
+ height : '50@vs' ,
60
+ margin : {
61
+ width : 12 ,
62
+ height : '12@s' ,
63
+ paddingBottom : - 1
64
+ }
65
+ } ,
66
+ row : {
67
+ padding : '10@ms0.3' ,
68
+ height : '34@ms' ,
69
+ marginRight : '0.5@ms0.9' ,
70
+ marginLeft : '-0.5@ms0.9' ,
71
+ marginTop : '-10@s' ,
13
72
}
14
- } ,
15
- row : {
16
- padding : '10@ms0.3' ,
17
- height : '34@ms' ,
18
- marginRight : '0.5@ms0.9' ,
19
- marginLeft : '-0.5@ms0.9' ,
20
- marginTop : '-10@s' ,
21
- }
22
- } ;
23
-
24
- const expectedOutput = {
25
- container : {
26
- width : scale ( 30 ) ,
27
- height : verticalScale ( 50 ) ,
28
- margin : {
29
- width : 12 ,
30
- height : scale ( 12 ) ,
31
- paddingBottom : - 1
73
+ } ;
74
+
75
+ const expectedOutput = {
76
+ container : {
77
+ width : scale ( 30 ) ,
78
+ height : verticalScale ( 50 ) ,
79
+ margin : {
80
+ width : 12 ,
81
+ height : scale ( 12 ) ,
82
+ paddingBottom : - 1
83
+ }
84
+ } ,
85
+ row : {
86
+ padding : moderateScale ( 10 , 0.3 ) ,
87
+ height : moderateScale ( 34 ) ,
88
+ marginRight : moderateScale ( 0.5 , 0.9 ) ,
89
+ marginLeft : moderateScale ( - 0.5 , 0.9 ) ,
90
+ marginTop : scale ( - 10 ) ,
32
91
}
33
- } ,
34
- row : {
35
- padding : moderateScale ( 10 , 0.3 ) ,
36
- height : moderateScale ( 34 ) ,
37
- marginRight : moderateScale ( 0.5 , 0.9 ) ,
38
- marginLeft : moderateScale ( - 0.5 , 0.9 ) ,
39
- marginTop : scale ( - 10 ) ,
40
- }
41
- } ;
42
-
43
- test ( 'ScaledSheet should map the special annotations as expected' , ( ) => {
92
+ } ;
93
+
44
94
expect ( JSON . stringify ( ScaledSheet . create ( input ) ) ) . toBe ( JSON . stringify ( expectedOutput ) ) ;
45
- } ) ;
95
+ } ) ;
0 commit comments