-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enhance: bind removeEventListener #1596
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9ff35ae
fix: bind removeEventListener
huozhi 1e76c12
mock web preset
huozhi 13e12b6
dynamic add check condition
huozhi 2574373
Merge branch 'master' into jc/8wd2
huozhi 8a80ab8
Merge branch 'master' into jc/8wd2
huozhi fdbdc62
Merge branch 'master' into jc/8wd2
huozhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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,98 @@ | ||
import { EventEmitter } from 'events' | ||
|
||
const FOCUS_EVENT = 'focus' | ||
const VISIBILITYCHANGE_EVENT = 'visibilitychange' | ||
|
||
function createEventTarget() { | ||
EventEmitter.prototype['addEventListener'] = EventEmitter.prototype.on | ||
EventEmitter.prototype['removeEventListener'] = EventEmitter.prototype.off | ||
const target = new EventEmitter() | ||
|
||
return target | ||
} | ||
|
||
function runTests(propertyName) { | ||
let webPreset | ||
let initFocus | ||
const eventName = | ||
propertyName === 'window' ? FOCUS_EVENT : VISIBILITYCHANGE_EVENT | ||
|
||
describe(`Web Preset ${propertyName}`, () => { | ||
const globalSpy = { | ||
window: undefined, | ||
document: undefined | ||
} | ||
|
||
beforeEach(() => { | ||
globalSpy.window = jest.spyOn(global, 'window', 'get') | ||
globalSpy.document = jest.spyOn(global, 'document', 'get') | ||
|
||
jest.resetModules() | ||
}) | ||
|
||
afterEach(() => { | ||
globalSpy.window.mockClear() | ||
globalSpy.document.mockClear() | ||
}) | ||
|
||
it(`should trigger listener when ${propertyName} has browser APIs`, async () => { | ||
const target = createEventTarget() | ||
if (propertyName === 'window') { | ||
globalSpy.window.mockImplementation(() => target) | ||
globalSpy.document.mockImplementation(() => undefined) | ||
} else if (propertyName === 'document') { | ||
globalSpy.window.mockImplementation(() => undefined) | ||
globalSpy.document.mockImplementation(() => target) | ||
} | ||
|
||
webPreset = require('../../src/utils/web-preset') | ||
initFocus = webPreset.defaultConfigOptions.initFocus | ||
|
||
const fn = jest.fn() | ||
const release = initFocus(fn) as () => void | ||
|
||
target.emit(eventName) | ||
expect(fn).toBeCalledTimes(1) | ||
|
||
release() | ||
target.emit(eventName) | ||
expect(fn).toBeCalledTimes(1) | ||
}) | ||
|
||
it(`should not trigger listener when ${propertyName} is falsy`, async () => { | ||
if (propertyName === 'window') { | ||
// window exists but without event APIs | ||
globalSpy.window.mockImplementation(() => ({ | ||
emit: createEventTarget().emit | ||
})) | ||
globalSpy.document.mockImplementation(() => undefined) | ||
} else if (propertyName === 'document') { | ||
globalSpy.window.mockImplementation(() => undefined) | ||
globalSpy.document.mockImplementation(() => undefined) | ||
} | ||
|
||
webPreset = require('../../src/utils/web-preset') | ||
initFocus = webPreset.defaultConfigOptions.initFocus | ||
|
||
const fn = jest.fn() | ||
const release = initFocus(fn) as () => void | ||
const target = global[propertyName] | ||
|
||
// TODO: target?.emit?() breaks prettier, fix prettier format | ||
if (target && target.emit) { | ||
target.emit(eventName) | ||
} | ||
|
||
expect(fn).toBeCalledTimes(0) | ||
|
||
release() | ||
if (target && target.emit) { | ||
target.emit(eventName) | ||
} | ||
expect(fn).toBeCalledTimes(0) | ||
}) | ||
}) | ||
} | ||
|
||
runTests('window') | ||
runTests('document') |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@huozhi Could you tell me why do we need to use a function? I'm curious to learn.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's for both source code and tests, if we make it a function, then in the test case we could switch environment between node and jsdom so that the evaluation is different when runtime changes.
I didn't find a good way to keep it a pre-evaluated variable while switching runtime. If you have any good idea to keep it simple that would be great 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to add a comment in the source code ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@huozhi Thank you 🙏 I think this is the simplest way to achieve it 👍