Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for utilities #23989

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions Libraries/Utilities/__tests__/DeviceInfo-test.js
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');
});
});
52 changes: 52 additions & 0 deletions Libraries/Utilities/__tests__/PixelRatio-test.js
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);
});
});
48 changes: 48 additions & 0 deletions Libraries/Utilities/__tests__/binaryToBase64-test.js
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);
}
32 changes: 32 additions & 0 deletions Libraries/Utilities/__tests__/clamp-test.js
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);
});
});
32 changes: 32 additions & 0 deletions Libraries/Utilities/__tests__/infoLog-test.js
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');
});
});
48 changes: 48 additions & 0 deletions Libraries/Utilities/__tests__/logError-test.js
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:',
);
});
});
14 changes: 14 additions & 0 deletions Libraries/Utilities/__tests__/mapWithSeparator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
});
});
51 changes: 51 additions & 0 deletions Libraries/Utilities/__tests__/mergeIntoFast-test.js
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});
});
});
27 changes: 27 additions & 0 deletions Libraries/Utilities/__tests__/warnOnce-test.js
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);
});
});