|
| 1 | +import type { Configuration } from '../domain/configuration' |
| 2 | +import type { Clock } from '../../test' |
| 3 | +import { createNewEvent, setPageVisibility, restorePageVisibility, registerCleanupTask, mockClock } from '../../test' |
| 4 | +import { createPageActivationObservable, REACTIVATE_DEBOUNCE } from './pageActivationObservable' |
| 5 | + |
| 6 | +describe('createPageActivationObservable', () => { |
| 7 | + let onActivateSpy: jasmine.Spy<() => void> |
| 8 | + let configuration: Configuration |
| 9 | + let clock: Clock |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + onActivateSpy = jasmine.createSpy() |
| 13 | + configuration = {} as Configuration |
| 14 | + clock = mockClock() |
| 15 | + registerCleanupTask(createPageActivationObservable(configuration).subscribe(onActivateSpy).unsubscribe) |
| 16 | + registerCleanupTask(() => { |
| 17 | + restorePageVisibility() |
| 18 | + clock.cleanup() |
| 19 | + }) |
| 20 | + }) |
| 21 | + |
| 22 | + it('notifies on focus after a blur', () => { |
| 23 | + window.dispatchEvent(createNewEvent('blur')) |
| 24 | + window.dispatchEvent(createNewEvent('focus')) |
| 25 | + |
| 26 | + expect(onActivateSpy).toHaveBeenCalledTimes(1) |
| 27 | + }) |
| 28 | + |
| 29 | + it('does NOT notify on focus without a prior blur or hide', () => { |
| 30 | + window.dispatchEvent(createNewEvent('focus')) |
| 31 | + |
| 32 | + expect(onActivateSpy).not.toHaveBeenCalled() |
| 33 | + }) |
| 34 | + |
| 35 | + it('notifies on visibilitychange visible after becoming hidden', () => { |
| 36 | + emulatePageVisibilityChange('hidden') |
| 37 | + emulatePageVisibilityChange('visible') |
| 38 | + |
| 39 | + expect(onActivateSpy).toHaveBeenCalledTimes(1) |
| 40 | + }) |
| 41 | + |
| 42 | + it('notifies on pageshow after being inactive', () => { |
| 43 | + window.dispatchEvent(createNewEvent('blur')) |
| 44 | + window.dispatchEvent(createNewEvent('pageshow')) |
| 45 | + |
| 46 | + expect(onActivateSpy).toHaveBeenCalledTimes(1) |
| 47 | + }) |
| 48 | + |
| 49 | + it('debounces two reactivations within the debounce window to one', () => { |
| 50 | + window.dispatchEvent(createNewEvent('blur')) |
| 51 | + window.dispatchEvent(createNewEvent('focus')) |
| 52 | + window.dispatchEvent(createNewEvent('blur')) |
| 53 | + window.dispatchEvent(createNewEvent('focus')) |
| 54 | + |
| 55 | + expect(onActivateSpy).toHaveBeenCalledTimes(1) |
| 56 | + }) |
| 57 | + |
| 58 | + it('notifies twice for two genuine cycles spaced beyond the debounce window', () => { |
| 59 | + window.dispatchEvent(createNewEvent('blur')) |
| 60 | + window.dispatchEvent(createNewEvent('focus')) |
| 61 | + clock.tick(REACTIVATE_DEBOUNCE + 1) |
| 62 | + window.dispatchEvent(createNewEvent('blur')) |
| 63 | + window.dispatchEvent(createNewEvent('focus')) |
| 64 | + |
| 65 | + expect(onActivateSpy).toHaveBeenCalledTimes(2) |
| 66 | + }) |
| 67 | + |
| 68 | + function emulatePageVisibilityChange(visibility: 'visible' | 'hidden') { |
| 69 | + setPageVisibility(visibility) |
| 70 | + document.dispatchEvent(createNewEvent('visibilitychange')) |
| 71 | + } |
| 72 | +}) |
0 commit comments