Skip to content

Commit 1d3ee36

Browse files
authored
test: add tests for normalize-text.js (#899)
* test: cherry-pick files from #598 * test: jest.isolateModules works * fix: device info * fix: pass tests * fix: consider as intervals
1 parent 99c0ded commit 1d3ee36

File tree

3 files changed

+205
-4
lines changed

3 files changed

+205
-4
lines changed

__tests__/data/screens.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// iPhones
2+
export const iPhone5 = { width: 320, height: 568 };
3+
export const iPhone6 = { width: 375, height: 667 };
4+
export const iPhone6Plus = { width: 414, height: 736 };
5+
export const iPhone7 = { width: 375, height: 667 };
6+
export const iPhone7Plus = { width: 414, height: 736 };
7+
8+
// iPods
9+
export const iPodTouch = { width: 320, height: 568 };
10+
11+
// iPads
12+
export const iPadPro = { width: 1024, height: 1366 };
13+
export const iPadGen3 = { witdh: 768, height: 1024 };
14+
export const iPadAir = { witdh: 768, height: 1024 };
15+
export const iPadMini = { witdh: 768, height: 1024 };
16+
17+
// Android Phones
18+
export const Nexus6P = { width: 411, height: 731 };
19+
export const Nexus5X = { width: 411, height: 731 };
20+
export const GooglePixel = { width: 411, height: 731 };
21+
export const GooglePixelXL = { width: 411, height: 731 };
22+
export const SamsungGalaxyNote5 = { width: 360, height: 640 };
23+
export const SamsungGalaxyS7 = { width: 360, height: 640 };
24+
export const SamsungGalaxyS7Edge = { width: 360, height: 640 };
25+
export const LGG5 = { width: 360, height: 640 };
26+
export const OnePlus3 = { width: 360, height: 640 };
27+
28+
// Android Tablets
29+
export const Nexus7 = { width: 600, height: 960 };
30+
export const Nexus9 = { width: 768, height: 1024 };
31+
export const SamsungGalaxyTab10 = { width: 800, height: 1280 };
32+
export const ChromebookPixel = { width: 1280, height: 850 };
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import * as screens from 'testData/screens';
2+
3+
const mockClassWithGetter = value => ({
4+
get: jest.fn().mockReturnValue(value),
5+
});
6+
7+
const mockRequiredClasses = mockValues => {
8+
jest.mock('Dimensions', () => mockClassWithGetter(mockValues.dimensions));
9+
jest.mock('PixelRatio', () => mockClassWithGetter(mockValues.pixelRatio));
10+
};
11+
12+
const expectSize = size => {
13+
jest.isolateModules(() => {
14+
const { normalize } = require('config');
15+
expect(normalize(1)).toEqual(size);
16+
});
17+
};
18+
19+
describe('Normalize Text', () => {
20+
// iOS Devices
21+
22+
it('should normalize correctly on iPhone 5', () => {
23+
mockRequiredClasses({ dimensions: screens.iPhone5, pixelRatio: 2 });
24+
25+
expectSize(0.95);
26+
});
27+
28+
it('should normalize correctly on iPhone 6', () => {
29+
mockRequiredClasses({ dimensions: screens.iPhone6, pixelRatio: 2 });
30+
31+
expectSize(1.15);
32+
});
33+
34+
it('should normalize correctly on iPhone 6 Plus', () => {
35+
mockRequiredClasses({ dimensions: screens.iPhone6Plus, pixelRatio: 3 });
36+
37+
expectSize(1.27);
38+
});
39+
40+
it('should normalize correctly on iPhone 7', () => {
41+
mockRequiredClasses({ dimensions: screens.iPhone7, pixelRatio: 2 });
42+
43+
expectSize(1.15);
44+
});
45+
46+
it('should normalize correctly on iPhone 7 Plus', () => {
47+
mockRequiredClasses({ dimensions: screens.iPhone7Plus, pixelRatio: 3 });
48+
49+
expectSize(1.27);
50+
});
51+
52+
it('should normalize correctly on iPod Touch', () => {
53+
mockRequiredClasses({ dimensions: screens.iPodTouch, pixelRatio: 2 });
54+
55+
expectSize(0.95);
56+
});
57+
58+
it('should normalize correctly on iPad Pro', () => {
59+
mockRequiredClasses({ dimensions: screens.iPadPro, pixelRatio: 2 });
60+
61+
expectSize(1.25);
62+
});
63+
64+
it('should normalize correctly on iPad Gen 3/4', () => {
65+
mockRequiredClasses({ dimensions: screens.iPadGen3, pixelRatio: 2 });
66+
67+
expectSize(1.25);
68+
});
69+
70+
it('should normalize correctly on iPad Air', () => {
71+
mockRequiredClasses({ dimensions: screens.iPadAir, pixelRatio: 2 });
72+
73+
expectSize(1.25);
74+
});
75+
76+
it('should normalize correctly on iPad Mini', () => {
77+
mockRequiredClasses({ dimensions: screens.iPadMini, pixelRatio: 1 });
78+
79+
expectSize(1);
80+
});
81+
82+
// Android Devices
83+
84+
it('should normalize correctly on Nexus 6P', () => {
85+
mockRequiredClasses({ dimensions: screens.Nexus6P, pixelRatio: 3.5 });
86+
87+
expectSize(1.25);
88+
});
89+
90+
it('should normalize correctly on Nexus 5X', () => {
91+
mockRequiredClasses({ dimensions: screens.Nexus5X, pixelRatio: 2.6 });
92+
93+
expectSize(1.15);
94+
});
95+
96+
it('should normalize correctly on Google Pixel', () => {
97+
mockRequiredClasses({ dimensions: screens.GooglePixel, pixelRatio: 2.6 });
98+
99+
expectSize(1.15);
100+
});
101+
102+
it('should normalize correctly on Google Pixel XL', () => {
103+
mockRequiredClasses({ dimensions: screens.GooglePixelXL, pixelRatio: 3.5 });
104+
105+
expectSize(1.25);
106+
});
107+
108+
it('should normalize correctly on Samsung Galaxy Note 5', () => {
109+
mockRequiredClasses({
110+
dimensions: screens.SamsungGalaxyNote5,
111+
pixelRatio: 4,
112+
});
113+
114+
expectSize(1);
115+
});
116+
117+
it('should normalize correctly on Samsung Galaxy S7', () => {
118+
mockRequiredClasses({ dimensions: screens.SamsungGalaxyS7, pixelRatio: 4 });
119+
120+
expectSize(1);
121+
});
122+
123+
it('should normalize correctly on Samsung Galaxy S7 Edge', () => {
124+
mockRequiredClasses({
125+
dimensions: screens.SamsungGalaxyS7Edge,
126+
pixelRatio: 4,
127+
});
128+
129+
expectSize(1);
130+
});
131+
132+
it('should normalize correctly on LG G5', () => {
133+
mockRequiredClasses({ dimensions: screens.LGG5, pixelRatio: 4 });
134+
135+
expectSize(1);
136+
});
137+
138+
it('should normalize correctly on OnePlus 3', () => {
139+
mockRequiredClasses({ dimensions: screens.OnePlus3, pixelRatio: 3 });
140+
141+
expectSize(1);
142+
});
143+
144+
it('should normalize correctly on Nexus 7', () => {
145+
mockRequiredClasses({ dimensions: screens.Nexus7, pixelRatio: 2 });
146+
147+
expectSize(1.25);
148+
});
149+
150+
it('should normalize correctly on Nexus 9', () => {
151+
mockRequiredClasses({ dimensions: screens.Nexus9, pixelRatio: 2 });
152+
153+
expectSize(1.25);
154+
});
155+
156+
it('should normalize correctly on Samsung Galaxy Tab 10', () => {
157+
mockRequiredClasses({
158+
dimensions: screens.SamsungGalaxyTab10,
159+
pixelRatio: 1,
160+
});
161+
162+
expectSize(1);
163+
});
164+
165+
it('should normalize correctly on ChromebookPixel', () => {
166+
mockRequiredClasses({ dimensions: screens.ChromebookPixel, pixelRatio: 2 });
167+
168+
expectSize(1.25);
169+
});
170+
});

src/config/normalize-text.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const deviceWidth = Dimensions.get('window').width;
2727
// console.log('normalizeText getPSFLS ->', layoutSize);
2828

2929
export const normalize = size => {
30-
if (pixelRatio === 2) {
30+
if (pixelRatio >= 2 && pixelRatio < 3) {
3131
// iphone 5s and older Androids
3232
if (deviceWidth < 360) {
3333
return size * 0.95;
@@ -45,7 +45,7 @@ export const normalize = size => {
4545
return size * 1.25;
4646
}
4747

48-
if (pixelRatio === 3) {
48+
if (pixelRatio >= 3 && pixelRatio < 3.5) {
4949
// catch Android font scaling on small machines
5050
// where pixel ratio / font scale ratio => 3:3
5151
if (deviceWidth <= 360) {
@@ -68,7 +68,7 @@ export const normalize = size => {
6868
return size * 1.27;
6969
}
7070

71-
if (pixelRatio === 3.5) {
71+
if (pixelRatio >= 3.5) {
7272
// catch Android font scaling on small machines
7373
// where pixel ratio / font scale ratio => 3:3
7474
if (deviceWidth <= 360) {
@@ -86,6 +86,5 @@ export const normalize = size => {
8686
return size * 1.4;
8787
}
8888

89-
// if older device ie pixelRatio !== 2 || 3 || 3.5
9089
return size;
9190
};

0 commit comments

Comments
 (0)