-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
65a8a51
commit f541c34
Showing
9 changed files
with
323 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @emails oncall+react_native | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('DeviceInfo', () => { | ||
const DeviceInfo = require('DeviceInfo'); | ||
|
||
it('should give device info', () => { | ||
expect(DeviceInfo).toHaveProperty('Dimensions'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @emails oncall+react_native | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('PixelRatio', () => { | ||
const Dimensions = require('Dimensions'); | ||
const PixelRatio = require('PixelRatio'); | ||
|
||
beforeEach(() => { | ||
Dimensions.set({ | ||
windowPhysicalPixels: { | ||
width: 400, | ||
height: 800, | ||
scale: 2, | ||
fontScale: 3, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should give the pixel density', () => { | ||
expect(PixelRatio.get()).toEqual(2); | ||
}); | ||
|
||
it('should give the font scale when present', () => { | ||
expect(PixelRatio.getFontScale()).toEqual(3); | ||
}); | ||
|
||
it('should give the pixel density instead of the font scale when the front scale is not present', () => { | ||
Dimensions.set({ | ||
windowPhysicalPixels: { | ||
scale: 2, | ||
}, | ||
}); | ||
expect(PixelRatio.getFontScale()).toEqual(2); | ||
}); | ||
|
||
it('should convert a layout size to pixel size', () => { | ||
expect(PixelRatio.getPixelSizeForLayoutSize(400)).toEqual(800); | ||
}); | ||
|
||
it('should round a layout size to pixel size', () => { | ||
expect(PixelRatio.roundToNearestPixel(8.4)).toEqual(8.5); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @emails oncall+react_native | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const base64 = require('base64-js'); | ||
const {TextEncoder, TextDecoder} = require('util'); | ||
|
||
describe('binaryToBase64', () => { | ||
const binaryToBase64 = require('binaryToBase64'); | ||
|
||
it('should encode a Uint8Array', () => { | ||
const input = new TextEncoder().encode('Test string'); | ||
|
||
expect(base64ToString(binaryToBase64(input))).toEqual('Test string'); | ||
}); | ||
|
||
it('should encode an ArrayBuffer', () => { | ||
const input = new TextEncoder().encode('Test string').buffer; | ||
|
||
expect(base64ToString(binaryToBase64(input))).toEqual('Test string'); | ||
}); | ||
|
||
it('should encode a DataView', () => { | ||
const input = new DataView(new TextEncoder().encode('Test string').buffer); | ||
|
||
expect(base64ToString(binaryToBase64(input))).toEqual('Test string'); | ||
}); | ||
|
||
it('should not encode a non ArrayBuffer or non typed array', () => { | ||
const input = ['i', 'n', 'v', 'a', 'l', 'i', 'd']; | ||
|
||
expect(() => binaryToBase64(input)).toThrowError(); | ||
}); | ||
}); | ||
|
||
function base64ToString(base64String) { | ||
const byteArray = base64.toByteArray(base64String); | ||
|
||
return new TextDecoder().decode(byteArray); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @emails oncall+react_native | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('clamp', () => { | ||
const clamp = require('clamp'); | ||
|
||
it('should return the value if the value does not exceed boundaries', () => { | ||
expect(clamp(0, 5, 10)).toEqual(5); | ||
expect(clamp(5, 5, 10)).toEqual(5); | ||
expect(clamp(0, 5, 5)).toEqual(5); | ||
expect(clamp(5, 5, 5)).toEqual(5); | ||
}); | ||
|
||
it('should return the min value if the value is too low', () => { | ||
expect(clamp(10, 5, 20)).toEqual(10); | ||
expect(clamp(10, 9, 20)).toEqual(10); | ||
}); | ||
|
||
it('should return the max value if the value is too high', () => { | ||
expect(clamp(10, 25, 20)).toEqual(20); | ||
expect(clamp(10, 21, 20)).toEqual(20); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @emails oncall+react_native | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('infoLog', () => { | ||
const infoLog = require('infoLog'); | ||
|
||
it('logs messages to the console', () => { | ||
console.log = jest.fn(); | ||
|
||
infoLog('This is a log message'); | ||
|
||
expect(console.log).toHaveBeenCalledWith('This is a log message'); | ||
}); | ||
|
||
it('logs messages with multiple arguments to the console', () => { | ||
console.log = jest.fn(); | ||
|
||
const data = 'log'; | ||
infoLog('This is a', data, 'message'); | ||
|
||
expect(console.log).toHaveBeenCalledWith('This is a', 'log', 'message'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @emails oncall+react_native | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('logError', () => { | ||
const logError = require('logError'); | ||
|
||
it('logs error messages to the console', () => { | ||
console.error.apply = jest.fn(); | ||
|
||
logError('This is a log message'); | ||
|
||
expect(console.error.apply).toHaveBeenCalledWith(console, [ | ||
'This is a log message', | ||
]); | ||
}); | ||
|
||
it('logs error messages with multiple arguments to the console', () => { | ||
console.error.apply = jest.fn(); | ||
|
||
const data = 'log'; | ||
logError('This is a', data, 'message'); | ||
|
||
expect(console.error.apply).toHaveBeenCalledWith(console, [ | ||
'This is a', | ||
'log', | ||
'message', | ||
]); | ||
}); | ||
|
||
it('logs errors to the console', () => { | ||
console.error = jest.fn(); | ||
|
||
logError(new Error('The error message')); | ||
|
||
expect(console.error.mock.calls[0][0]).toContain( | ||
'Error: "The error message". Stack:', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @emails oncall+react_native | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('mergeIntoFast', () => { | ||
const mergeIntoFast = require('mergeIntoFast'); | ||
|
||
it('should merge two objects', () => { | ||
const a = {fontScale: 2, height: 1334}; | ||
const b = {scale: 2, width: 750}; | ||
|
||
mergeIntoFast(a, b); | ||
|
||
expect(a).toEqual({fontScale: 2, height: 1334, scale: 2, width: 750}); | ||
}); | ||
|
||
it('should use the values of the second object if there are duplicate keys', () => { | ||
const a = {fontScale: 2}; | ||
const b = {fontScale: 3}; | ||
|
||
mergeIntoFast(a, b); | ||
|
||
expect(a).toEqual({fontScale: 3}); | ||
}); | ||
|
||
it('should merge into an empty object', () => { | ||
const a = {}; | ||
const b = {scale: 2, width: 750}; | ||
|
||
mergeIntoFast(a, b); | ||
|
||
expect(a).toEqual({scale: 2, width: 750}); | ||
}); | ||
|
||
it('should merge from an empty object', () => { | ||
const a = {scale: 2, width: 750}; | ||
const b = {}; | ||
|
||
mergeIntoFast(a, b); | ||
|
||
expect(a).toEqual({scale: 2, width: 750}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @emails oncall+react_native | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('warnOnce', () => { | ||
const warnOnce = require('warnOnce'); | ||
|
||
it('logs warning messages to the console exactly once', () => { | ||
console.error = jest.fn(); | ||
|
||
warnOnce('test-message', 'This is a log message'); | ||
warnOnce('test-message', 'This is a second log message'); | ||
|
||
expect(console.error).toHaveBeenCalledWith( | ||
'Warning: This is a log message', | ||
); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |