-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(navigation): Allow to listen for active navigation changes
This adds the native `EventTarget` class to the Navigation allowing to add listeners to it. Allowing to dispatch the `updateActive` event when the active navigation changed. The idea is to make it reactive in a native / framework agnostic way. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
- Loading branch information
Showing
4 changed files
with
92 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
import { describe, it, expect, vi } from 'vitest' | ||
import { Navigation } from '../lib/navigation/navigation' | ||
import { View } from '../lib/navigation/view' | ||
|
||
const mockView = (id = 'view', order = 1) => new View({ id, order, name: 'View', icon: '<svg></svg>', getContents: () => Promise.reject(new Error()) }) | ||
|
||
describe('Navigation', () => { | ||
it('Can register a view', async () => { | ||
const navigation = new Navigation() | ||
const view = mockView() | ||
navigation.register(view) | ||
|
||
expect(navigation.views).toEqual([view]) | ||
}) | ||
|
||
it('Throws when registering the same view twice', async () => { | ||
const navigation = new Navigation() | ||
const view = mockView() | ||
navigation.register(view) | ||
expect(() => navigation.register(view)).toThrow(/already registered/) | ||
expect(navigation.views).toEqual([view]) | ||
}) | ||
|
||
it('Can remove a view', async () => { | ||
const navigation = new Navigation() | ||
const view = mockView() | ||
navigation.register(view) | ||
expect(navigation.views).toEqual([view]) | ||
navigation.remove(view.id) | ||
expect(navigation.views).toEqual([]) | ||
}) | ||
|
||
it('Can set a view as active', async () => { | ||
const navigation = new Navigation() | ||
const view = mockView() | ||
navigation.register(view) | ||
|
||
expect(navigation.active).toBe(null) | ||
|
||
navigation.setActive(view) | ||
expect(navigation.active).toEqual(view) | ||
}) | ||
|
||
it('Emits event when setting a view as active', async () => { | ||
const navigation = new Navigation() | ||
const view = mockView() | ||
navigation.register(view) | ||
|
||
// add listener | ||
const listener = vi.fn() | ||
navigation.addEventListener('updateActive', listener) | ||
|
||
navigation.setActive(view) | ||
expect(listener).toHaveBeenCalledOnce() | ||
// So it was called, we then expect the first argument of the first call to be the event with the view as the detail | ||
expect(listener.mock.calls[0][0].detail).toBe(view) | ||
}) | ||
}) |
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.
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