-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
266 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { EventEmitter } from 'events' | ||
|
||
const FOCUS_EVENT = 'focus' | ||
const VISIBILITYCHANGE_EVENT = 'visibilitychange' | ||
|
||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace NodeJS { | ||
interface EventEmitter { | ||
addEventListener: typeof EventEmitter.prototype.on | ||
removeEventListener: typeof EventEmitter.prototype.off | ||
} | ||
} | ||
} | ||
|
||
EventEmitter.prototype['addEventListener'] = EventEmitter.prototype.on | ||
EventEmitter.prototype['removeEventListener'] = EventEmitter.prototype.off | ||
|
||
const eventEmitter = new EventEmitter() | ||
|
||
describe('Init Revalidate On Focus', () => { | ||
type TWindow = typeof global.window | ||
type TDocument = typeof global.document | ||
|
||
const globalSpy = { | ||
window: jest.spyOn(global, 'window', 'get'), | ||
document: jest.spyOn(global, 'document', 'get') | ||
} | ||
|
||
beforeEach(() => { | ||
globalSpy.window.mockImplementation( | ||
() => eventEmitter as unknown as TWindow | ||
) | ||
globalSpy.document.mockImplementation( | ||
() => eventEmitter as unknown as TDocument | ||
) | ||
|
||
jest.resetModules() | ||
}) | ||
|
||
afterEach(() => { | ||
globalSpy.window.mockClear() | ||
globalSpy.document.mockClear() | ||
}) | ||
|
||
it('Should trigger focus event', () => { | ||
const mockFn = jest.fn() | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { initRevalidateOnFocus } = require('../src/middleware/persist') | ||
|
||
const revalidateOnFocus = initRevalidateOnFocus(mockFn) | ||
|
||
// Trigger focus event | ||
eventEmitter.emit(FOCUS_EVENT) | ||
|
||
// Remove focus event from window | ||
revalidateOnFocus() | ||
|
||
eventEmitter.emit(FOCUS_EVENT) | ||
|
||
expect(mockFn).toBeCalledTimes(1) | ||
}) | ||
|
||
it('Should trigger visibilitychange event', () => { | ||
const mockFn = jest.fn() | ||
|
||
globalSpy.window.mockImplementation( | ||
() => eventEmitter as unknown as TWindow | ||
) | ||
globalSpy.document.mockImplementation( | ||
() => eventEmitter as unknown as TDocument | ||
) | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { initRevalidateOnFocus } = require('../src/middleware/persist') | ||
|
||
const revalidateOnFocus = initRevalidateOnFocus(mockFn) | ||
|
||
// Trigger visibilitychange event | ||
eventEmitter.emit(VISIBILITYCHANGE_EVENT) | ||
|
||
// Remove visibilitychange event from document | ||
revalidateOnFocus() | ||
|
||
eventEmitter.emit(VISIBILITYCHANGE_EVENT) | ||
|
||
expect(mockFn).toBeCalledTimes(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.