Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ§ͺ Tests for new workspace view

Alicia Sykesb47093bac665
diff --git a/tests/components/item.test.js b/tests/components/item.test.js
index 03decfed..c966e3de 100644
--- a/tests/components/item.test.js
+++ b/tests/components/item.test.js
@@ -613,7 +613,7 @@ describe('Methods: itemClicked', () => {
},
});
wrapper.vm.itemClicked(event());
- expect(router.push).toHaveBeenCalledWith({ name: 'workspace', query: { url: 'https://x.com' } });
+ expect(router.push).toHaveBeenCalledWith({ path: '/workspace', query: { url: 'https://x.com' } });
});
it('target clipboard: calls copyToClipboard', () => {
@@ -660,7 +660,7 @@ describe('Methods: launchItem', () => {
it('workspace calls router.push', () => {
const { wrapper } = mountItem();
wrapper.vm.launchItem('workspace', 'https://test.com');
- expect(router.push).toHaveBeenCalledWith({ name: 'workspace', query: { url: 'https://test.com' } });
+ expect(router.push).toHaveBeenCalledWith({ path: '/workspace', query: { url: 'https://test.com' } });
});
it('clipboard calls copyToClipboard', () => {
diff --git a/tests/components/sidebar-item.test.js b/tests/components/sidebar-item.test.js
index 6381ecd0..48b095fd 100644
--- a/tests/components/sidebar-item.test.js
+++ b/tests/components/sidebar-item.test.js
@@ -4,9 +4,12 @@ import {
import { shallowMount } from '@vue/test-utils';
import SideBarItem from '@/components/Workspace/SideBarItem.vue';
-function mountItem(props = {}) {
+vi.mock('@/utils/request', () => ({ default: { get: vi.fn() } }));
+vi.mock('@/router', () => ({ default: { push: vi.fn() } }));
+
+function mountItem(item, props = {}) {
return shallowMount(SideBarItem, {
- props,
+ props: { item, ...props },
global: {
directives: {
tooltip: {},
@@ -32,58 +35,45 @@ describe('SideBarItem', () => {
vi.restoreAllMocks();
});
- it('renders icon when icon prop is provided', () => {
- const wrapper = mountItem({ icon: 'fas fa-server', title: 'Server' });
- expect(wrapper.classes()).toContain('w-icon');
+ it('renders a link with icon and title when item has a url', () => {
+ const wrapper = mountItem({ icon: 'fas fa-server', title: 'Server', url: 'https://example.com' });
+ expect(wrapper.element.tagName).toBe('A');
+ expect(wrapper.attributes('href')).toBe('https://example.com');
expect(wrapper.findComponent({ name: 'Icon' }).exists()).toBe(true);
- expect(wrapper.find('p.small-title').exists()).toBe(false);
+ expect(wrapper.find('.title').text()).toBe('Server');
});
- it('renders title as small-title when icon is empty', () => {
+ it('renders a button with initials when there is no url or icon', () => {
const wrapper = mountItem({ title: 'No Icon App' });
- expect(wrapper.classes()).toContain('text-only');
- expect(wrapper.find('p.small-title').exists()).toBe(true);
- expect(wrapper.find('p.small-title').text()).toBe('No Icon App');
- });
-
- it('emits launch-app on normal click', async () => {
- const wrapper = mountItem({ title: 'App', url: 'https://example.com', target: 'workspace' });
- await wrapper.trigger('click');
- expect(wrapper.emitted('launch-app')).toBeTruthy();
- expect(wrapper.emitted('launch-app')[0]).toEqual([{ url: 'https://example.com', target: 'workspace' }]);
- expect(openSpy).not.toHaveBeenCalled();
- });
-
- it('launches the workspace app when the icon is clicked', async () => {
- const wrapper = mountItem({ icon: 'fas fa-server', title: 'App', url: 'https://example.com', target: 'workspace' });
- await wrapper.findComponent({ name: 'Icon' }).trigger('click');
- expect(wrapper.emitted('launch-app')).toEqual([[{ url: 'https://example.com', target: 'workspace' }]]);
- expect(openSpy).not.toHaveBeenCalled();
+ expect(wrapper.element.tagName).toBe('BUTTON');
+ expect(wrapper.find('.initials').text()).toBe('NI');
});
- it('launches the workspace app when the title is clicked', async () => {
+ it('emits launch-app on normal click, without navigating', () => {
const wrapper = mountItem({ title: 'App', url: 'https://example.com', target: 'workspace' });
- await wrapper.find('.small-title').trigger('click');
- expect(wrapper.emitted('launch-app')).toEqual([[{ url: 'https://example.com', target: 'workspace' }]]);
+ const event = new MouseEvent('click', { bubbles: true, cancelable: true });
+ wrapper.element.dispatchEvent(event);
+ expect(event.defaultPrevented).toBe(true);
+ expect(wrapper.emitted('launch-app')).toEqual([['https://example.com']]);
expect(openSpy).not.toHaveBeenCalled();
});
- it('opens in new tab when clicked with ctrlKey or metaKey', async () => {
- const wrapper = mountItem({ title: 'App', url: 'https://example.com', target: 'workspace' });
+ it('leaves ctrl/meta clicks to the browser', async () => {
+ const wrapper = mountItem({ title: 'App', url: 'https://example.com' });
await wrapper.trigger('click', { ctrlKey: true });
- expect(openSpy).toHaveBeenCalledWith('https://example.com', '_blank', 'noopener,noreferrer');
+ await wrapper.trigger('click', { metaKey: true });
expect(wrapper.emitted('launch-app')).toBeFalsy();
+ });
- openSpy.mockClear();
-
- await wrapper.trigger('click', { metaKey: true });
- expect(openSpy).toHaveBeenCalledWith('https://example.com', '_blank', 'noopener,noreferrer');
+ it('opens newtab targets outside the workspace', async () => {
+ const wrapper = mountItem({ title: 'App', url: 'https://example.com', target: 'newtab' });
+ await wrapper.trigger('click');
+ expect(openSpy).toHaveBeenCalledWith('https://example.com', '_blank');
expect(wrapper.emitted('launch-app')).toBeFalsy();
});
- it('opens in new tab when middle clicked (auxclick button 1)', async () => {
- const wrapper = mountItem({ title: 'App', url: 'https://example.com', target: 'workspace' });
- await wrapper.trigger('auxclick', { button: 1 });
- expect(openSpy).toHaveBeenCalledWith('https://example.com', '_blank', 'noopener,noreferrer');
+ it('marks the active item', () => {
+ const wrapper = mountItem({ title: 'App', url: 'https://example.com' }, { active: true });
+ expect(wrapper.classes()).toContain('active');
});
});
diff --git a/tests/components/workspace.test.js b/tests/components/workspace.test.js
new file mode 100644
index 00000000..ba865de7
--- /dev/null
+++ b/tests/components/workspace.test.js
@@ -0,0 +1,164 @@
+import {
+ describe, it, expect, vi, afterEach,
+} from 'vitest';
+import { reactive, nextTick } from 'vue';
+import { mount, shallowMount } from '@vue/test-utils';
+import { createStore } from 'vuex';
+import Workspace from '@/views/Workspace.vue';
+import SideBar from '@/components/Workspace/SideBar.vue';
+import WidgetView from '@/components/Workspace/WidgetView.vue';
+
+vi.mock('@/utils/request', () => ({ default: { get: vi.fn() } }));
+vi.mock('@/router', () => ({ default: { push: vi.fn() } }));
+vi.mock('@/utils/auth/Auth', () => ({ getCurrentUser: () => null, isLoggedInAsGuest: () => false }));
+
+const widgets = [{ type: 'clock' }];
+const sections = [
+ {
+ name: 'Apps',
+ items: [
+ { title: 'One', url: 'https://one.com/' },
+ { title: 'Hidden', url: 'https://hidden.com/', displayData: { hideFromWorkspace: true } },
+ ],
+ },
+ { name: 'Group', items: [{ title: 'Group', subItems: [{ title: 'Two', url: 'https://two.com/' }] }] },
+ { name: 'Widgets', widgets },
+ { name: 'Secret', displayData: { hideFromWorkspace: true }, items: [{ title: 'S', url: 'https://s.com/' }] },
+];
+
+function mountWith(component, {
+ query = {}, appConfig = {}, props = {}, shallow = true,
+} = {}) {
+ const store = createStore({
+ state: { currentConfigInfo: {}, editMode: false },
+ getters: {
+ sections: () => sections,
+ appConfig: () => appConfig,
+ visibleComponents: () => ({ searchBar: true }),
+ },
+ });
+ const route = reactive({ path: '/workspace', params: {}, query });
+ const router = { replace: vi.fn((location) => { route.query = location.query; }) };
+ const wrapper = (shallow ? shallowMount : mount)(component, {
+ props,
+ global: {
+ plugins: [store],
+ directives: { tooltip: {} },
+ mocks: { $route: route, $router: router, $t: (key) => key },
+ stubs: {
+ WebContent: false, IconWorkspace: true, RouterLink: true, Icon: true,
+ },
+ },
+ });
+ return { wrapper, router, route };
+}
+
+describe('Workspace view', () => {
+ const frameSrcs = (wrapper) => wrapper.findAll('iframe').map((f) => f.attributes('src'));
+ const sideBarEmit = async (wrapper, event, value) => {
+ wrapper.findComponent(SideBar).vm.$emit(event, value);
+ await nextTick();
+ };
+
+ it('shows the app from the url param, and nothing for unsafe urls', () => {
+ expect(frameSrcs(mountWith(Workspace, { query: { url: 'https://one.com/' } }).wrapper))
+ .toEqual(['https://one.com/']);
+ const { wrapper } = mountWith(Workspace, { query: { url: 'javascript:alert(1)' } });
+ expect(frameSrcs(wrapper)).toEqual([]);
+ expect(wrapper.find('.empty-state').exists()).toBe(true);
+ });
+
+ it('shows one app at a time, unless multi-tasking', async () => {
+ const { wrapper } = mountWith(Workspace, { query: { url: 'https://one.com/' } });
+ await sideBarEmit(wrapper, 'launch-app', 'https://two.com/');
+ expect(frameSrcs(wrapper)).toEqual(['https://two.com/']);
+ });
+
+ it('keeps apps mounted when multi-tasking, and switches to the last one on close', async () => {
+ const { wrapper, router } = mountWith(Workspace, {
+ query: { url: 'https://one.com/' }, appConfig: { enableMultiTasking: true },
+ });
+ await sideBarEmit(wrapper, 'launch-app', 'https://two.com/');
+ expect(frameSrcs(wrapper)).toEqual(['https://one.com/', 'https://two.com/']);
+ await sideBarEmit(wrapper, 'close-app', 'https://two.com/');
+ expect(router.replace).toHaveBeenLastCalledWith({ query: { url: 'https://one.com/' } });
+ expect(frameSrcs(wrapper)).toEqual(['https://one.com/']);
+ });
+
+ it('switches to an open app, rather than reopening it, for a variant of its url', async () => {
+ const { wrapper, router } = mountWith(Workspace, { query: { url: 'https://one.com/' } });
+ await sideBarEmit(wrapper, 'launch-app', 'https://ONE.com');
+ expect(router.replace).toHaveBeenLastCalledWith({ query: { url: 'https://one.com/' } });
+ expect(wrapper.findComponent(SideBar).props('openUrls')).toEqual(['https://one.com/']);
+ });
+
+ it('shows widgets in place of the app, until another app is launched', async () => {
+ const { wrapper, router } = mountWith(Workspace, { query: { url: 'https://one.com/' } });
+ await sideBarEmit(wrapper, 'launch-widget', widgets);
+ expect(router.replace).toHaveBeenLastCalledWith({ query: {} });
+ await sideBarEmit(wrapper, 'close-app', 'https://one.com/');
+ expect(wrapper.findComponent(WidgetView).exists()).toBe(true);
+ await sideBarEmit(wrapper, 'launch-app', 'https://two.com/');
+ expect(wrapper.findComponent(WidgetView).exists()).toBe(false);
+ });
+
+ it('clears widgets when switching to another page', async () => {
+ const { wrapper, route } = mountWith(Workspace);
+ await sideBarEmit(wrapper, 'launch-widget', widgets);
+ route.path = '/workspace/other';
+ await nextTick();
+ expect(wrapper.findComponent(WidgetView).exists()).toBe(false);
+ });
+
+ it('opens the landing url only when no app is in the url', () => {
+ const appConfig = { workspaceLandingUrl: 'https://two.com/' };
+ expect(mountWith(Workspace, { appConfig }).router.replace)
+ .toHaveBeenCalledWith({ query: { url: 'https://two.com/' } });
+ expect(mountWith(Workspace, { appConfig, query: { url: 'https://one.com/' } }).router.replace)
+ .not.toHaveBeenCalled();
+ });
+});
+
+describe('Workspace SideBar', () => {
+ afterEach(() => localStorage.clear());
+
+ const mountSideBar = (props = {}) => mountWith(SideBar, {
+ props: { sections, ...props }, shallow: false,
+ }).wrapper;
+ const titlesIn = (wrapper, selector) => wrapper.findAll(`${selector} .title`).map((title) => title.text());
+
+ it('hides sections and items marked hideFromWorkspace', () => {
+ const wrapper = mountSideBar({ activeUrl: 'https://one.com/' });
+ expect(titlesIn(wrapper, '.side-bar-item-container')).toEqual(['Apps', 'Group', 'Widgets']);
+ expect(titlesIn(wrapper, '.sub-side-bar')).toEqual(['One']);
+ });
+
+ it('opens the section holding the active app, including sub-items', () => {
+ expect(titlesIn(mountSideBar({ activeUrl: 'https://two.com' }), '.sub-side-bar')).toEqual(['Two']);
+ });
+
+ it('filters items by search, opening every section with a match', async () => {
+ const wrapper = mountSideBar();
+ await wrapper.find('.side-bar-toggle').trigger('click');
+ await wrapper.find('.side-bar-search').setValue('two');
+ expect(titlesIn(wrapper, '.side-bar-item-container')).toEqual(['Group']);
+ expect(titlesIn(wrapper, '.sub-side-bar')).toEqual(['Two']);
+ });
+
+ it('shows widgets when their section is opened, but not when closed', async () => {
+ const wrapper = mountSideBar();
+ const header = wrapper.find('.side-bar-item-container [aria-label="Widgets"]');
+ await header.trigger('click');
+ await header.trigger('click');
+ expect(wrapper.emitted('launch-widget')).toEqual([[widgets]]);
+ });
+
+ it('lists open apps from the first, by item title or host, and closes them', async () => {
+ const wrapper = mountSideBar({ openUrls: ['https://two.com/'] });
+ expect(titlesIn(wrapper, '.open-apps')).toEqual(['Two']);
+ await wrapper.setProps({ openUrls: ['https://two.com/', 'https://other.com/'] });
+ expect(titlesIn(wrapper, '.open-apps')).toEqual(['Two', 'other.com']);
+ await wrapper.findAll('.close-app')[1].trigger('click');
+ expect(wrapper.emitted('close-app')).toEqual([['https://other.com/']]);
+ });
+});
diff --git a/tests/unit/config-helpers.test.js b/tests/unit/config-helpers.test.js
index 33fadf39..5bdf49a6 100644
--- a/tests/unit/config-helpers.test.js
+++ b/tests/unit/config-helpers.test.js
@@ -3,6 +3,7 @@ import {
makePageName,
makePageSlug,
makeRoutePath,
+ makeWorkspaceRoute,
viewFromPath,
resolveRouteIntent,
PAGE_STATUS,
@@ -267,6 +268,19 @@ describe('ConfigHelpers - makeRoutePath (view capability)', () => {
});
});
+describe('ConfigHelpers - makeWorkspaceRoute', () => {
+ it('opens the url in the workspace for the current sub-page', () => {
+ const store = { state: { currentConfigInfo: { confId: 'kiosk' } } };
+ expect(makeWorkspaceRoute('https://a.com', store))
+ .toEqual({ path: '/workspace/kiosk', query: { url: 'https://a.com' } });
+ });
+
+ it('uses the root workspace when no sub-page is loaded', () => {
+ const store = { state: { currentConfigInfo: {} } };
+ expect(makeWorkspaceRoute('https://a.com', store).path).toBe('/workspace');
+ });
+});
+
describe('Integration: view switching preserves page + section', () => {
const fakeStore = ({ pages = [], rootSections = [] } = {}) => ({
getters: { pages },
diff --git a/tests/unit/misc-helpers.test.js b/tests/unit/misc-helpers.test.js
new file mode 100644
index 00000000..b740989e
--- /dev/null
+++ b/tests/unit/misc-helpers.test.js
@@ -0,0 +1,18 @@
+import { describe, it, expect } from 'vitest';
+import { sameUrl } from '@/utils/MiscHelpers';
+
+describe('MiscHelpers - sameUrl', () => {
+ it('matches urls that differ only by host case, default port or trailing slash', () => {
+ expect(sameUrl('https://App.com', 'https://app.com/')).toBe(true);
+ expect(sameUrl('https://app.com:443/docs/', 'https://app.com/docs')).toBe(true);
+ });
+
+ it('keeps paths and queries case-sensitive', () => {
+ expect(sameUrl('https://app.com/Doc?id=A', 'https://app.com/doc?id=a')).toBe(false);
+ });
+
+ it('never matches a missing url', () => {
+ expect(sameUrl(undefined, undefined)).toBe(false);
+ expect(sameUrl('', 'https://app.com')).toBe(false);
+ });
+});