diff --git a/Libraries/Utilities/__tests__/DeviceInfo-test.js b/Libraries/Utilities/__tests__/DeviceInfo-test.js new file mode 100644 index 00000000000000..dc246c0434d008 --- /dev/null +++ b/Libraries/Utilities/__tests__/DeviceInfo-test.js @@ -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'); + }); +}); diff --git a/Libraries/Utilities/__tests__/PixelRatio-test.js b/Libraries/Utilities/__tests__/PixelRatio-test.js new file mode 100644 index 00000000000000..ff3735ac484ac5 --- /dev/null +++ b/Libraries/Utilities/__tests__/PixelRatio-test.js @@ -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); + }); +}); diff --git a/Libraries/Utilities/__tests__/binaryToBase64-test.js b/Libraries/Utilities/__tests__/binaryToBase64-test.js new file mode 100644 index 00000000000000..f8c2a58f3cd1a9 --- /dev/null +++ b/Libraries/Utilities/__tests__/binaryToBase64-test.js @@ -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); +} diff --git a/Libraries/Utilities/__tests__/clamp-test.js b/Libraries/Utilities/__tests__/clamp-test.js new file mode 100644 index 00000000000000..8591166730dcad --- /dev/null +++ b/Libraries/Utilities/__tests__/clamp-test.js @@ -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); + }); +}); diff --git a/Libraries/Utilities/__tests__/infoLog-test.js b/Libraries/Utilities/__tests__/infoLog-test.js new file mode 100644 index 00000000000000..70c776b302f785 --- /dev/null +++ b/Libraries/Utilities/__tests__/infoLog-test.js @@ -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'); + }); +}); diff --git a/Libraries/Utilities/__tests__/logError-test.js b/Libraries/Utilities/__tests__/logError-test.js new file mode 100644 index 00000000000000..e56c4541ce53b5 --- /dev/null +++ b/Libraries/Utilities/__tests__/logError-test.js @@ -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:', + ); + }); +}); diff --git a/Libraries/Utilities/__tests__/mapWithSeparator-test.js b/Libraries/Utilities/__tests__/mapWithSeparator-test.js index 0bbe0dc7defc0e..b54989019a48d6 100644 --- a/Libraries/Utilities/__tests__/mapWithSeparator-test.js +++ b/Libraries/Utilities/__tests__/mapWithSeparator-test.js @@ -54,4 +54,18 @@ describe('mapWithSeparator', () => { ); expect(result).toEqual([3, 0, 2, 1, 1]); }); + + it('mapWithSeparator returns empty array when empty array is given as input', () => { + const array = []; + const result = mapWithSeparator( + array, + function(value) { + return value * 2; + }, + function() { + return 0; + }, + ); + expect(result).toEqual([]); + }); }); diff --git a/Libraries/Utilities/__tests__/mergeIntoFast-test.js b/Libraries/Utilities/__tests__/mergeIntoFast-test.js new file mode 100644 index 00000000000000..51e21af1e2c10a --- /dev/null +++ b/Libraries/Utilities/__tests__/mergeIntoFast-test.js @@ -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}); + }); +}); diff --git a/Libraries/Utilities/__tests__/warnOnce-test.js b/Libraries/Utilities/__tests__/warnOnce-test.js new file mode 100644 index 00000000000000..78a985faa0f70a --- /dev/null +++ b/Libraries/Utilities/__tests__/warnOnce-test.js @@ -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); + }); +});