lucid-ui
Version:
A UI component library from AppNexus.
262 lines (241 loc) • 10.7 kB
JavaScript
import _isFunction from "lodash/isFunction";
import _has from "lodash/has";
import _isBoolean from "lodash/isBoolean";
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { isDevMode, isNode, logger, checkIsDev } from './logger';
describe('logger', function () {
describe('isDevMode', function () {
it('should be a boolean', function () {
expect(_isBoolean(isDevMode));
});
});
describe('isNode', function () {
it('should be a boolean', function () {
expect(_isBoolean(isNode));
});
});
describe('checkIsDev', function () {
it('should return a boolean', function () {
expect(_isBoolean(checkIsDev()));
});
});
describe('logger', function () {
/* eslint-disable no-console */
var originalError;
var originalLog;
var originalWarn;
var mockError;
var mockLog;
var mockWarn;
beforeAll(function () {
mockError = jest.fn();
mockLog = jest.fn();
mockWarn = jest.fn();
console.error = mockError;
console.log = mockLog;
console.warn = mockWarn;
originalError = console.error;
originalLog = console.log;
originalWarn = console.warn;
});
beforeEach(function () {
mockError.mockReset();
mockLog.mockReset();
mockWarn.mockReset();
});
afterAll(function () {
console.error = originalError;
console.log = originalLog;
console.warn = originalWarn;
});
it('should do stuff', function () {
expect(_has(logger, 'log'));
expect(_isFunction(logger.log));
expect(_has(logger, 'logOnce'));
expect(_isFunction(logger.logOnce));
expect(_has(logger, 'warn'));
expect(_isFunction(logger.warn));
expect(_has(logger, 'warnOnce'));
expect(_isFunction(logger.warnOnce));
expect(_has(logger, 'error'));
expect(_isFunction(logger.error));
expect(_has(logger, 'errorOnce'));
expect(_isFunction(logger.errorOnce));
});
describe('log', function () {
var log = logger.log;
it('should log a message', function () {
log('Message Sent.');
expect(mockLog).toHaveBeenCalledTimes(1);
expect(mockLog).toHaveBeenCalledWith('Message Sent.');
});
it('should log multiple messages', function () {
log('First Message.');
log('Second Message.');
expect(mockLog).toHaveBeenCalledTimes(2);
var _mockLog$mock$calls = _slicedToArray(mockLog.mock.calls, 2),
firstCall = _mockLog$mock$calls[0],
secondCall = _mockLog$mock$calls[1];
expect(firstCall[0]).toEqual('First Message.');
expect(secondCall[0]).toEqual('Second Message.');
});
});
describe('logOnce', function () {
var logOnce = logger.logOnce,
resetOnce = logger.resetOnce;
var onceID1 = 'id-111';
var onceID2 = 'id-222';
afterEach(function () {
resetOnce(onceID1);
resetOnce(onceID2);
});
it('should log a message', function () {
logOnce(onceID1, 'Message Sent.');
expect(mockLog).toHaveBeenCalledTimes(1);
expect(mockLog).toHaveBeenCalledWith('Message Sent.');
});
it('should log multiple messages with different keys', function () {
logOnce(onceID1, 'First Message.');
logOnce(onceID2, 'Second Message.');
expect(mockLog).toHaveBeenCalledTimes(2);
var _mockLog$mock$calls2 = _slicedToArray(mockLog.mock.calls, 2),
firstCall = _mockLog$mock$calls2[0],
secondCall = _mockLog$mock$calls2[1];
expect(firstCall[0]).toEqual('First Message.');
expect(secondCall[0]).toEqual('Second Message.');
});
it('should not log multiple messages with the same key', function () {
logOnce(onceID1, 'First Message.');
logOnce(onceID1, 'Second Message.');
expect(mockLog).toHaveBeenCalledTimes(1);
expect(mockLog).toHaveBeenCalledWith('First Message.');
});
});
describe('warn', function () {
var warn = logger.warn;
it('should log a warn message', function () {
warn('Message Sent.');
expect(mockWarn).toHaveBeenCalledTimes(1);
expect(mockWarn).toHaveBeenCalledWith('Message Sent.');
});
it('should log multiple warn messages', function () {
warn('First Message.');
warn('Second Message.');
expect(mockWarn).toHaveBeenCalledTimes(2);
var _mockWarn$mock$calls = _slicedToArray(mockWarn.mock.calls, 2),
firstCall = _mockWarn$mock$calls[0],
secondCall = _mockWarn$mock$calls[1];
expect(firstCall[0]).toEqual('First Message.');
expect(secondCall[0]).toEqual('Second Message.');
});
});
describe('warnOnce', function () {
var warnOnce = logger.warnOnce,
resetOnce = logger.resetOnce;
var onceID1 = 'id-111';
var onceID2 = 'id-222';
afterEach(function () {
resetOnce(onceID1);
resetOnce(onceID2);
});
it('should log a message', function () {
warnOnce(onceID1, 'Message Sent.');
expect(mockWarn).toHaveBeenCalledTimes(1);
expect(mockWarn).toHaveBeenCalledWith('Message Sent.');
});
it('should log multiple warn messages with different keys', function () {
warnOnce(onceID1, 'First Message.');
warnOnce(onceID2, 'Second Message.');
expect(mockWarn).toHaveBeenCalledTimes(2);
var _mockWarn$mock$calls2 = _slicedToArray(mockWarn.mock.calls, 2),
firstCall = _mockWarn$mock$calls2[0],
secondCall = _mockWarn$mock$calls2[1];
expect(firstCall[0]).toEqual('First Message.');
expect(secondCall[0]).toEqual('Second Message.');
});
it('should not log multiple warn messages with the same key', function () {
warnOnce(onceID1, 'First Message.');
warnOnce(onceID1, 'Second Message.');
expect(mockWarn).toHaveBeenCalledTimes(1);
expect(mockWarn).toHaveBeenCalledWith('First Message.');
});
});
describe('error', function () {
var error = logger.error;
it('should log an error message', function () {
error('Message Sent.');
expect(mockError).toHaveBeenCalledTimes(1);
expect(mockError).toHaveBeenCalledWith('Message Sent.');
});
it('should log multiple error messages', function () {
error('First Message.');
error('Second Message.');
var _mockError$mock$calls = _slicedToArray(mockError.mock.calls, 2),
firstCall = _mockError$mock$calls[0],
secondCall = _mockError$mock$calls[1];
expect(firstCall[0]).toEqual('First Message.');
expect(secondCall[0]).toEqual('Second Message.');
});
});
describe('errorOnce', function () {
var errorOnce = logger.errorOnce,
resetOnce = logger.resetOnce;
var onceID1 = 'id-111';
var onceID2 = 'id-222';
afterEach(function () {
resetOnce(onceID1);
resetOnce(onceID2);
});
it('should log an error message', function () {
errorOnce(onceID1, 'Message Sent.');
expect(mockError).toHaveBeenCalledTimes(1);
expect(mockError).toHaveBeenCalledWith('Message Sent.');
});
it('should log multiple warn messages with different keys', function () {
errorOnce(onceID1, 'First Message.');
errorOnce(onceID2, 'Second Message.');
var _mockError$mock$calls2 = _slicedToArray(mockError.mock.calls, 2),
firstCall = _mockError$mock$calls2[0],
secondCall = _mockError$mock$calls2[1];
expect(firstCall[0]).toEqual('First Message.');
expect(secondCall[0]).toEqual('Second Message.');
});
it('should not log multiple warn messages with the same key', function () {
errorOnce(onceID1, 'First Message.');
errorOnce(onceID1, 'Second Message.');
expect(mockError).toHaveBeenCalledTimes(1);
expect(mockError).toHaveBeenCalledWith('First Message.');
});
});
describe('resetOnce', function () {
var logOnce = logger.logOnce,
resetOnce = logger.resetOnce;
var onceID1 = 'id-111';
afterEach(function () {
resetOnce(onceID1);
});
it('should not log multiple messages with the same key', function () {
logOnce(onceID1, 'First Message.');
logOnce(onceID1, 'Second Message.');
expect(mockLog).toHaveBeenCalledTimes(1);
expect(mockLog).toHaveBeenCalledWith('First Message.');
});
it('should allow logging multiple messages with the same key if resetOnce is called', function () {
logOnce(onceID1, 'First Message.');
resetOnce(onceID1);
logOnce(onceID1, 'Second Message.');
expect(mockLog).toHaveBeenCalledTimes(2);
var _mockLog$mock$calls3 = _slicedToArray(mockLog.mock.calls, 2),
firstCall = _mockLog$mock$calls3[0],
secondCall = _mockLog$mock$calls3[1];
expect(firstCall[0]).toEqual('First Message.');
expect(secondCall[0]).toEqual('Second Message.');
});
});
});
});