|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import type { Page } from '@playwright/test' |
| 7 | +import { test, expect } from '../../support/fixtures/files-page.ts' |
| 8 | +import { mkdir, rm, uploadContent } from '../../support/utils/dav.ts' |
| 9 | + |
| 10 | +/** |
| 11 | + * Run an action that toggles a favorite and wait for the server to store it. |
| 12 | + * Toggling hits POST /apps/files/api/v1/files/<path>; the listener is registered |
| 13 | + * before the action and awaited after, so later assertions see the stored state. |
| 14 | + */ |
| 15 | +async function toggleFavorite(page: Page, path: string, action: () => Promise<void>): Promise<void> { |
| 16 | + const encoded = path.split('/').map(encodeURIComponent).join('/') |
| 17 | + const response = page.waitForResponse( |
| 18 | + (r) => r.url().includes(`/apps/files/api/v1/files/${encoded}`) |
| 19 | + && r.request().method() === 'POST', |
| 20 | + ) |
| 21 | + await action() |
| 22 | + await response |
| 23 | +} |
| 24 | + |
| 25 | +test.describe('Files: Favorites', () => { |
| 26 | + test.beforeEach(async ({ page, user, filesListPage }) => { |
| 27 | + // New users get welcome.txt — remove it so the list contains only our test files |
| 28 | + await rm(page.request, user, '/welcome.txt') |
| 29 | + await uploadContent(page.request, user, Buffer.alloc(0), 'text/plain', '/file.txt') |
| 30 | + await mkdir(page.request, user, '/new folder') |
| 31 | + await filesListPage.open() |
| 32 | + }) |
| 33 | + |
| 34 | + test('marks a file as favorite from the row actions', async ({ page, filesListPage }) => { |
| 35 | + await expect(filesListPage.getRowForFile('file.txt')).toBeVisible() |
| 36 | + |
| 37 | + const menu = await filesListPage.openActionsMenuForFile('file.txt') |
| 38 | + const favoriteAction = filesListPage.getActionButtonInMenu(menu, 'favorite') |
| 39 | + await expect(favoriteAction).toContainText('Add to favorites') |
| 40 | + |
| 41 | + await toggleFavorite(page, 'file.txt', () => favoriteAction.click()) |
| 42 | + |
| 43 | + await expect(filesListPage.getFavoriteIconForFile('file.txt')).toBeVisible() |
| 44 | + }) |
| 45 | + |
| 46 | + test('un-marks a file as favorite from the row actions', async ({ page, filesListPage }) => { |
| 47 | + await expect(filesListPage.getRowForFile('file.txt')).toBeVisible() |
| 48 | + |
| 49 | + // Favorite it first |
| 50 | + await toggleFavorite(page, 'file.txt', () => filesListPage.triggerActionForFile('file.txt', 'favorite')) |
| 51 | + await expect(filesListPage.getFavoriteIconForFile('file.txt')).toBeVisible() |
| 52 | + |
| 53 | + // Re-open the menu — the action now offers to remove the favorite |
| 54 | + const menu = await filesListPage.openActionsMenuForFile('file.txt') |
| 55 | + const favoriteAction = filesListPage.getActionButtonInMenu(menu, 'favorite') |
| 56 | + await expect(favoriteAction).toContainText('Remove from favorites') |
| 57 | + |
| 58 | + await toggleFavorite(page, 'file.txt', () => favoriteAction.click()) |
| 59 | + |
| 60 | + await expect(filesListPage.getFavoriteIconForFile('file.txt')).toHaveCount(0) |
| 61 | + }) |
| 62 | + |
| 63 | + test('shows favorite folders in the navigation', async ({ page, filesListPage, filesNavigation }) => { |
| 64 | + const favoritesNav = filesNavigation.getNavigationItem('favorites') |
| 65 | + const favoriteEntry = favoritesNav.getByRole('link', { name: 'new folder' }) |
| 66 | + |
| 67 | + await expect(favoritesNav).toBeVisible() |
| 68 | + await expect(favoriteEntry).toHaveCount(0) |
| 69 | + |
| 70 | + // Favorite the folder — it appears as a (collapsed) child of the favorites view |
| 71 | + await toggleFavorite(page, 'new folder', () => filesListPage.triggerActionForFile('new folder', 'favorite')) |
| 72 | + await filesNavigation.expandNavigationItem('favorites') |
| 73 | + await expect(favoriteEntry).toBeVisible() |
| 74 | + |
| 75 | + // Un-favorite — it disappears again |
| 76 | + await toggleFavorite(page, 'new folder', () => filesListPage.triggerActionForFile('new folder', 'favorite')) |
| 77 | + await expect(favoriteEntry).toHaveCount(0) |
| 78 | + }) |
| 79 | + |
| 80 | + test('marks a folder as favorite from the sidebar', async ({ page, filesListPage, filesNavigation, filesSidebar }) => { |
| 81 | + await expect(filesListPage.getRowForFile('new folder')).toBeVisible() |
| 82 | + |
| 83 | + const favoriteEntry = filesNavigation.getNavigationItem('favorites').getByRole('link', { name: 'new folder' }) |
| 84 | + await expect(favoriteEntry).toHaveCount(0) |
| 85 | + |
| 86 | + // Open the sidebar for the folder |
| 87 | + await filesListPage.triggerActionForFile('new folder', 'details') |
| 88 | + await expect(filesSidebar.sidebar()).toBeVisible() |
| 89 | + |
| 90 | + await toggleFavorite(page, 'new folder', () => filesSidebar.triggerAction('Favorite')) |
| 91 | + |
| 92 | + await filesSidebar.close() |
| 93 | + await expect(filesSidebar.sidebar()).not.toBeVisible() |
| 94 | + await expect(filesListPage.getFavoriteIconForFile('new folder')).toBeVisible() |
| 95 | + |
| 96 | + // Favorite survives a reload |
| 97 | + await page.reload() |
| 98 | + await expect(filesListPage.getRowForFile('new folder')).toBeVisible() |
| 99 | + await expect(filesListPage.getFavoriteIconForFile('new folder')).toBeVisible() |
| 100 | + |
| 101 | + // Un-favorite again from the sidebar |
| 102 | + await filesListPage.triggerActionForFile('new folder', 'details') |
| 103 | + await expect(filesSidebar.sidebar()).toBeVisible() |
| 104 | + |
| 105 | + await toggleFavorite(page, 'new folder', () => filesSidebar.triggerAction('Unfavorite')) |
| 106 | + |
| 107 | + await filesSidebar.close() |
| 108 | + await expect(filesSidebar.sidebar()).not.toBeVisible() |
| 109 | + await expect(filesListPage.getFavoriteIconForFile('new folder')).toHaveCount(0) |
| 110 | + }) |
| 111 | +}) |
0 commit comments