UNPKG

@enact/i18n

Version:

Internationalization support for Enact using iLib

76 lines (75 loc) 2.49 kB
"use strict"; var _windowFocus = require("../windowFocus"); describe('windowFocus', function () { var dispatchFocus = function dispatchFocus() { return window.dispatchEvent(new window.FocusEvent('focus')); }; var dispatchBlur = function dispatchBlur() { return window.dispatchEvent(new window.FocusEvent('blur')); }; describe('at launch', function () { test('should invoke fn immediately', function () { var fn = jest.fn(); (0, _windowFocus.onWindowFocus)(fn); var expected = 1; var actual = fn.mock.calls.length; expect(actual).toEqual(expected); }); }); describe('when window focused', function () { beforeEach(dispatchFocus); afterEach(dispatchBlur); test('should invoke fn immediately', function () { var fn = jest.fn(); (0, _windowFocus.onWindowFocus)(fn); var expected = 1; var actual = fn.mock.calls.length; expect(actual).toEqual(expected); }); }); describe('when window blurred', function () { beforeEach(dispatchBlur); afterEach(dispatchFocus); test('should not invoke fn', function () { var fn = jest.fn(); (0, _windowFocus.onWindowFocus)(fn); var expected = 0; var actual = fn.mock.calls.length; expect(actual).toEqual(expected); }); test('should invoke fn after focus', function () { var fn = jest.fn(); (0, _windowFocus.onWindowFocus)(fn); var expected = 0; var actual = fn.mock.calls.length; expect(actual).toEqual(expected); dispatchFocus(); expected = 1; actual = fn.mock.calls.length; expect(actual).toEqual(expected); }); test('should invoke fn only once after focus when queued multiple times', function () { var fn = jest.fn(); (0, _windowFocus.onWindowFocus)(fn); (0, _windowFocus.onWindowFocus)(fn); (0, _windowFocus.onWindowFocus)(fn); (0, _windowFocus.onWindowFocus)(fn); dispatchFocus(); var expected = 1; var actual = fn.mock.calls.length; expect(actual).toEqual(expected); }); test('should continue if a handler throws', function () { var fails = function fails() { throw new Error('Failed'); }; var fn = jest.fn(); (0, _windowFocus.onWindowFocus)(fails); (0, _windowFocus.onWindowFocus)(fn); dispatchFocus(); var expected = 1; var actual = fn.mock.calls.length; expect(actual).toEqual(expected); }); }); });