Skip to content

Commit

Permalink
Add tests for utilities (#23989)
Browse files Browse the repository at this point in the history
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
casperboone authored and facebook-github-bot committed Mar 18, 2019
1 parent 65a8a51 commit f541c34
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 0 deletions.
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);
});
});

0 comments on commit f541c34

Please sign in to comment.