-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13772 from nextcloud/backport/13736/stable30
- Loading branch information
Showing
4 changed files
with
330 additions
and
2 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
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,283 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
import { EventBus } from '../EventBus.ts' | ||
|
||
describe('EventBus', () => { | ||
const customEvent1 = jest.fn() | ||
const customEvent2 = jest.fn() | ||
const customEvent3 = jest.fn() | ||
const customEventOnce1 = jest.fn() | ||
const customEventOnce2 = jest.fn() | ||
const customEventOnce3 = jest.fn() | ||
|
||
const testEventBus = (type, handlers, onceHandlers) => { | ||
expect(EventBus.all.get(type).length).toBe(handlers) | ||
if (!onceHandlers) { | ||
expect(EventBus._onceHandlers.get(type)).toBeUndefined() | ||
} else { | ||
expect(EventBus._onceHandlers.get(type).size).toBe(onceHandlers) | ||
} | ||
} | ||
|
||
afterEach(() => { | ||
EventBus.all.clear() | ||
jest.clearAllMocks() | ||
}) | ||
|
||
describe('on and off', () => { | ||
it('should emit and listen to custom events', () => { | ||
// Arrange | ||
EventBus.on('custom-event', customEvent1) | ||
EventBus.on('custom-event', customEvent2) | ||
|
||
// Act | ||
EventBus.emit('custom-event') | ||
|
||
// Assert | ||
testEventBus('custom-event', 2) | ||
expect(customEvent1).toHaveBeenCalledTimes(1) | ||
expect(customEvent2).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should emit and listen to custom events with wildcard * ', () => { | ||
// Arrange | ||
EventBus.on('*', customEvent1) | ||
EventBus.on('*', customEvent2) | ||
|
||
// Act | ||
EventBus.emit('custom-event-1') | ||
|
||
// Assert | ||
testEventBus('*', 2) | ||
expect(customEvent1).toHaveBeenCalledTimes(1) | ||
expect(customEvent2).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should remove listeners by given type and handler', () => { | ||
// Arrange | ||
EventBus.on('custom-event', customEvent1) | ||
EventBus.on('custom-event', customEvent2) | ||
EventBus.emit('custom-event') | ||
testEventBus('custom-event', 2) | ||
|
||
// Act | ||
EventBus.off('custom-event', customEvent1) | ||
EventBus.emit('custom-event') | ||
|
||
// Assert | ||
testEventBus('custom-event', 1) | ||
expect(customEvent1).toHaveBeenCalledTimes(1) | ||
expect(customEvent2).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('should remove listeners by wildcard * and handler', () => { | ||
// Arrange | ||
EventBus.on('custom-event-1', customEvent1) | ||
EventBus.on('custom-event-2', customEvent2) | ||
EventBus.on('*', customEvent3) | ||
EventBus.emit('custom-event-1') | ||
EventBus.emit('custom-event-2') | ||
testEventBus('custom-event-1', 1) | ||
testEventBus('custom-event-2', 1) | ||
testEventBus('*', 1) | ||
expect(customEvent3).toHaveBeenCalledTimes(2) | ||
|
||
// Act | ||
EventBus.off('*', customEvent3) | ||
EventBus.emit('custom-event-1') | ||
EventBus.emit('custom-event-2') | ||
|
||
// Assert | ||
testEventBus('custom-event-1', 1) | ||
testEventBus('custom-event-2', 1) | ||
testEventBus('*', 0) | ||
expect(customEvent1).toHaveBeenCalledTimes(2) | ||
expect(customEvent2).toHaveBeenCalledTimes(2) | ||
expect(customEvent3).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('should remove listeners by given type only', () => { | ||
// Arrange | ||
EventBus.on('custom-event', customEvent1) | ||
EventBus.on('custom-event', customEvent2) | ||
EventBus.emit('custom-event') | ||
testEventBus('custom-event', 2) | ||
|
||
// Act | ||
EventBus.off('custom-event') | ||
EventBus.emit('custom-event') | ||
|
||
// Assert | ||
testEventBus('custom-event', 0) | ||
expect(customEvent1).toHaveBeenCalledTimes(1) | ||
expect(customEvent2).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should remove listeners by wildcard * only', () => { | ||
// Arrange | ||
EventBus.on('custom-event-1', customEvent1) | ||
EventBus.on('custom-event-2', customEvent2) | ||
EventBus.on('*', customEvent3) | ||
EventBus.emit('custom-event-1') | ||
EventBus.emit('custom-event-2') | ||
testEventBus('custom-event-1', 1) | ||
testEventBus('custom-event-2', 1) | ||
testEventBus('*', 1) | ||
expect(customEvent3).toHaveBeenCalledTimes(2) | ||
|
||
// Act | ||
EventBus.off('*') | ||
EventBus.emit('custom-event-1') | ||
EventBus.emit('custom-event-2') | ||
|
||
// Assert | ||
testEventBus('custom-event-1', 1) | ||
testEventBus('custom-event-2', 1) | ||
testEventBus('*', 0) | ||
expect(customEvent1).toHaveBeenCalledTimes(2) | ||
expect(customEvent2).toHaveBeenCalledTimes(2) | ||
expect(customEvent3).toHaveBeenCalledTimes(2) | ||
}) | ||
}) | ||
|
||
describe('once and off', () => { | ||
it('should emit and listen to custom events', () => { | ||
// Arrange | ||
EventBus.on('custom-event', customEvent1) | ||
EventBus.once('custom-event', customEventOnce1) | ||
EventBus.once('custom-event', customEventOnce2) | ||
testEventBus('custom-event', 3, 2) | ||
|
||
// Act | ||
EventBus.emit('custom-event') | ||
EventBus.emit('custom-event') | ||
|
||
// Assert | ||
expect(customEvent1).toHaveBeenCalledTimes(2) | ||
expect(customEventOnce1).toHaveBeenCalledTimes(1) | ||
expect(customEventOnce2).toHaveBeenCalledTimes(1) | ||
testEventBus('custom-event', 1, 0) | ||
}) | ||
|
||
it('should emit and listen to custom events with wildcard * ', () => { | ||
// Arrange | ||
EventBus.on('*', customEvent1) | ||
EventBus.once('*', customEventOnce1) | ||
EventBus.once('*', customEventOnce2) | ||
testEventBus('*', 3, 2) | ||
|
||
// Act | ||
EventBus.emit('custom-event-1') | ||
EventBus.emit('custom-event-2') | ||
|
||
// Assert | ||
expect(customEvent1).toHaveBeenCalledTimes(2) | ||
expect(customEventOnce1).toHaveBeenCalledTimes(1) | ||
expect(customEventOnce2).toHaveBeenCalledTimes(1) | ||
testEventBus('*', 1, 0) | ||
}) | ||
|
||
it('should remove listeners by given type and handler', () => { | ||
// Arrange | ||
EventBus.on('custom-event', customEvent1) | ||
EventBus.once('custom-event', customEventOnce1) | ||
EventBus.once('custom-event', customEventOnce2) | ||
testEventBus('custom-event', 3, 2) | ||
|
||
// Act | ||
EventBus.off('custom-event', customEventOnce1) | ||
testEventBus('custom-event', 2, 1) | ||
EventBus.emit('custom-event') | ||
EventBus.emit('custom-event') | ||
|
||
// Assert | ||
expect(customEvent1).toHaveBeenCalledTimes(2) | ||
expect(customEventOnce1).toHaveBeenCalledTimes(0) | ||
expect(customEventOnce2).toHaveBeenCalledTimes(1) | ||
testEventBus('custom-event', 1, 0) | ||
}) | ||
|
||
it('should remove listeners by wildcard * and handler', () => { | ||
// Arrange | ||
EventBus.once('custom-event-1', customEventOnce1) | ||
EventBus.on('custom-event-2', customEvent2) | ||
EventBus.once('custom-event-2', customEventOnce2) | ||
EventBus.on('*', customEvent3) | ||
EventBus.once('*', customEventOnce3) | ||
testEventBus('custom-event-1', 1, 1) | ||
testEventBus('custom-event-2', 2, 1) | ||
testEventBus('*', 2, 1) | ||
|
||
// Act | ||
EventBus.off('*', customEventOnce3) | ||
testEventBus('custom-event-1', 1, 1) | ||
testEventBus('custom-event-2', 2, 1) | ||
testEventBus('*', 1, 0) | ||
EventBus.emit('custom-event-1') | ||
EventBus.emit('custom-event-2') | ||
EventBus.emit('custom-event-3') | ||
|
||
// Assert | ||
expect(customEventOnce1).toHaveBeenCalledTimes(1) | ||
expect(customEvent2).toHaveBeenCalledTimes(1) | ||
expect(customEventOnce2).toHaveBeenCalledTimes(1) | ||
expect(customEvent3).toHaveBeenCalledTimes(3) | ||
expect(customEventOnce3).toHaveBeenCalledTimes(0) | ||
testEventBus('custom-event-1', 0, 0) | ||
testEventBus('custom-event-2', 1, 0) | ||
testEventBus('*', 1, 0) | ||
}) | ||
|
||
it('should remove listeners by given type only', () => { | ||
// Arrange | ||
EventBus.on('custom-event', customEvent1) | ||
EventBus.once('custom-event', customEventOnce1) | ||
EventBus.once('custom-event', customEventOnce2) | ||
testEventBus('custom-event', 3, 2) | ||
|
||
// Act | ||
EventBus.off('custom-event') | ||
testEventBus('custom-event', 0, 0) | ||
EventBus.emit('custom-event') | ||
EventBus.emit('custom-event') | ||
|
||
// Assert | ||
expect(customEvent1).toHaveBeenCalledTimes(0) | ||
expect(customEventOnce1).toHaveBeenCalledTimes(0) | ||
expect(customEventOnce2).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
it('should remove listeners by wildcard * only', () => { | ||
// Arrange | ||
EventBus.once('custom-event-1', customEventOnce1) | ||
EventBus.on('custom-event-2', customEvent2) | ||
EventBus.once('custom-event-2', customEventOnce2) | ||
EventBus.on('*', customEvent3) | ||
EventBus.once('*', customEventOnce3) | ||
testEventBus('custom-event-1', 1, 1) | ||
testEventBus('custom-event-2', 2, 1) | ||
testEventBus('*', 2, 1) | ||
|
||
// Act | ||
EventBus.off('*') | ||
testEventBus('custom-event-1', 1, 1) | ||
testEventBus('custom-event-2', 2, 1) | ||
testEventBus('*', 0, 0) | ||
EventBus.emit('custom-event-1') | ||
EventBus.emit('custom-event-2') | ||
EventBus.emit('custom-event-3') | ||
|
||
// Assert | ||
expect(customEventOnce1).toHaveBeenCalledTimes(1) | ||
expect(customEvent2).toHaveBeenCalledTimes(1) | ||
expect(customEventOnce2).toHaveBeenCalledTimes(1) | ||
expect(customEvent3).toHaveBeenCalledTimes(0) | ||
expect(customEventOnce3).toHaveBeenCalledTimes(0) | ||
testEventBus('custom-event-1', 0, 0) | ||
testEventBus('custom-event-2', 1, 0) | ||
testEventBus('*', 0, 0) | ||
}) | ||
}) | ||
}) |