Skip to content

Commit f541c34

Browse files
casperboonefacebook-github-bot
authored andcommitted
Add tests for utilities (#23989)
Summary: This PR add tests for several utilities in `Libraries/Utilities`, as a follow-up of #23903. The following utilities are now tested: * `clamp.js` * `binareToBase64.js` * `DeviceInfo.js` * `mergeIntoFast.js` * `PixelRatio.js` * `infoLog.js` * `logError.js` * `warnOnce.js` * `mapWithSeparator` (added a missing test) Not applicable, since it only adds tests. Pull Request resolved: #23989 Differential Revision: D14502806 Pulled By: cpojer fbshipit-source-id: e2c3b3a35f4f765d5336b998ab92dba14eeac7bc
1 parent 65a8a51 commit f541c34

9 files changed

+323
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @emails oncall+react_native
9+
*/
10+
11+
'use strict';
12+
13+
describe('DeviceInfo', () => {
14+
const DeviceInfo = require('DeviceInfo');
15+
16+
it('should give device info', () => {
17+
expect(DeviceInfo).toHaveProperty('Dimensions');
18+
});
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @emails oncall+react_native
9+
*/
10+
11+
'use strict';
12+
13+
describe('PixelRatio', () => {
14+
const Dimensions = require('Dimensions');
15+
const PixelRatio = require('PixelRatio');
16+
17+
beforeEach(() => {
18+
Dimensions.set({
19+
windowPhysicalPixels: {
20+
width: 400,
21+
height: 800,
22+
scale: 2,
23+
fontScale: 3,
24+
},
25+
});
26+
});
27+
28+
it('should give the pixel density', () => {
29+
expect(PixelRatio.get()).toEqual(2);
30+
});
31+
32+
it('should give the font scale when present', () => {
33+
expect(PixelRatio.getFontScale()).toEqual(3);
34+
});
35+
36+
it('should give the pixel density instead of the font scale when the front scale is not present', () => {
37+
Dimensions.set({
38+
windowPhysicalPixels: {
39+
scale: 2,
40+
},
41+
});
42+
expect(PixelRatio.getFontScale()).toEqual(2);
43+
});
44+
45+
it('should convert a layout size to pixel size', () => {
46+
expect(PixelRatio.getPixelSizeForLayoutSize(400)).toEqual(800);
47+
});
48+
49+
it('should round a layout size to pixel size', () => {
50+
expect(PixelRatio.roundToNearestPixel(8.4)).toEqual(8.5);
51+
});
52+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @emails oncall+react_native
9+
*/
10+
11+
'use strict';
12+
13+
const base64 = require('base64-js');
14+
const {TextEncoder, TextDecoder} = require('util');
15+
16+
describe('binaryToBase64', () => {
17+
const binaryToBase64 = require('binaryToBase64');
18+
19+
it('should encode a Uint8Array', () => {
20+
const input = new TextEncoder().encode('Test string');
21+
22+
expect(base64ToString(binaryToBase64(input))).toEqual('Test string');
23+
});
24+
25+
it('should encode an ArrayBuffer', () => {
26+
const input = new TextEncoder().encode('Test string').buffer;
27+
28+
expect(base64ToString(binaryToBase64(input))).toEqual('Test string');
29+
});
30+
31+
it('should encode a DataView', () => {
32+
const input = new DataView(new TextEncoder().encode('Test string').buffer);
33+
34+
expect(base64ToString(binaryToBase64(input))).toEqual('Test string');
35+
});
36+
37+
it('should not encode a non ArrayBuffer or non typed array', () => {
38+
const input = ['i', 'n', 'v', 'a', 'l', 'i', 'd'];
39+
40+
expect(() => binaryToBase64(input)).toThrowError();
41+
});
42+
});
43+
44+
function base64ToString(base64String) {
45+
const byteArray = base64.toByteArray(base64String);
46+
47+
return new TextDecoder().decode(byteArray);
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @emails oncall+react_native
9+
*/
10+
11+
'use strict';
12+
13+
describe('clamp', () => {
14+
const clamp = require('clamp');
15+
16+
it('should return the value if the value does not exceed boundaries', () => {
17+
expect(clamp(0, 5, 10)).toEqual(5);
18+
expect(clamp(5, 5, 10)).toEqual(5);
19+
expect(clamp(0, 5, 5)).toEqual(5);
20+
expect(clamp(5, 5, 5)).toEqual(5);
21+
});
22+
23+
it('should return the min value if the value is too low', () => {
24+
expect(clamp(10, 5, 20)).toEqual(10);
25+
expect(clamp(10, 9, 20)).toEqual(10);
26+
});
27+
28+
it('should return the max value if the value is too high', () => {
29+
expect(clamp(10, 25, 20)).toEqual(20);
30+
expect(clamp(10, 21, 20)).toEqual(20);
31+
});
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @emails oncall+react_native
9+
*/
10+
11+
'use strict';
12+
13+
describe('infoLog', () => {
14+
const infoLog = require('infoLog');
15+
16+
it('logs messages to the console', () => {
17+
console.log = jest.fn();
18+
19+
infoLog('This is a log message');
20+
21+
expect(console.log).toHaveBeenCalledWith('This is a log message');
22+
});
23+
24+
it('logs messages with multiple arguments to the console', () => {
25+
console.log = jest.fn();
26+
27+
const data = 'log';
28+
infoLog('This is a', data, 'message');
29+
30+
expect(console.log).toHaveBeenCalledWith('This is a', 'log', 'message');
31+
});
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @emails oncall+react_native
9+
*/
10+
11+
'use strict';
12+
13+
describe('logError', () => {
14+
const logError = require('logError');
15+
16+
it('logs error messages to the console', () => {
17+
console.error.apply = jest.fn();
18+
19+
logError('This is a log message');
20+
21+
expect(console.error.apply).toHaveBeenCalledWith(console, [
22+
'This is a log message',
23+
]);
24+
});
25+
26+
it('logs error messages with multiple arguments to the console', () => {
27+
console.error.apply = jest.fn();
28+
29+
const data = 'log';
30+
logError('This is a', data, 'message');
31+
32+
expect(console.error.apply).toHaveBeenCalledWith(console, [
33+
'This is a',
34+
'log',
35+
'message',
36+
]);
37+
});
38+
39+
it('logs errors to the console', () => {
40+
console.error = jest.fn();
41+
42+
logError(new Error('The error message'));
43+
44+
expect(console.error.mock.calls[0][0]).toContain(
45+
'Error: "The error message". Stack:',
46+
);
47+
});
48+
});

Libraries/Utilities/__tests__/mapWithSeparator-test.js

+14
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,18 @@ describe('mapWithSeparator', () => {
5454
);
5555
expect(result).toEqual([3, 0, 2, 1, 1]);
5656
});
57+
58+
it('mapWithSeparator returns empty array when empty array is given as input', () => {
59+
const array = [];
60+
const result = mapWithSeparator(
61+
array,
62+
function(value) {
63+
return value * 2;
64+
},
65+
function() {
66+
return 0;
67+
},
68+
);
69+
expect(result).toEqual([]);
70+
});
5771
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @emails oncall+react_native
9+
*/
10+
11+
'use strict';
12+
13+
describe('mergeIntoFast', () => {
14+
const mergeIntoFast = require('mergeIntoFast');
15+
16+
it('should merge two objects', () => {
17+
const a = {fontScale: 2, height: 1334};
18+
const b = {scale: 2, width: 750};
19+
20+
mergeIntoFast(a, b);
21+
22+
expect(a).toEqual({fontScale: 2, height: 1334, scale: 2, width: 750});
23+
});
24+
25+
it('should use the values of the second object if there are duplicate keys', () => {
26+
const a = {fontScale: 2};
27+
const b = {fontScale: 3};
28+
29+
mergeIntoFast(a, b);
30+
31+
expect(a).toEqual({fontScale: 3});
32+
});
33+
34+
it('should merge into an empty object', () => {
35+
const a = {};
36+
const b = {scale: 2, width: 750};
37+
38+
mergeIntoFast(a, b);
39+
40+
expect(a).toEqual({scale: 2, width: 750});
41+
});
42+
43+
it('should merge from an empty object', () => {
44+
const a = {scale: 2, width: 750};
45+
const b = {};
46+
47+
mergeIntoFast(a, b);
48+
49+
expect(a).toEqual({scale: 2, width: 750});
50+
});
51+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @emails oncall+react_native
9+
*/
10+
11+
'use strict';
12+
13+
describe('warnOnce', () => {
14+
const warnOnce = require('warnOnce');
15+
16+
it('logs warning messages to the console exactly once', () => {
17+
console.error = jest.fn();
18+
19+
warnOnce('test-message', 'This is a log message');
20+
warnOnce('test-message', 'This is a second log message');
21+
22+
expect(console.error).toHaveBeenCalledWith(
23+
'Warning: This is a log message',
24+
);
25+
expect(console.error).toHaveBeenCalledTimes(1);
26+
});
27+
});

0 commit comments

Comments
 (0)