From 802d7e010d243209b6367c7341d3bf41dfe102fc Mon Sep 17 00:00:00 2001 From: Lucas Azevedo Date: Wed, 6 Sep 2023 12:02:28 -0300 Subject: [PATCH] Add tests Signed-off-by: Lucas Azevedo --- __mocks__/css.js | 22 +++ .../actions/inlineSystemTagsAction.spec.ts | 143 ++++++++++++++++++ .../src/actions/inlineSystemTagsAction.ts | 6 +- .../src/css/fileEntryInlineSystemTags.scss | 21 +++ jest.config.ts | 1 + 5 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 __mocks__/css.js create mode 100644 apps/files/src/actions/inlineSystemTagsAction.spec.ts diff --git a/__mocks__/css.js b/__mocks__/css.js new file mode 100644 index 00000000000..f2c71be8c32 --- /dev/null +++ b/__mocks__/css.js @@ -0,0 +1,22 @@ +/** + * @copyright Copyright (c) 2023 Lucas Azevedo + * + * @author Lucas Azevedo + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +export default {} diff --git a/apps/files/src/actions/inlineSystemTagsAction.spec.ts b/apps/files/src/actions/inlineSystemTagsAction.spec.ts new file mode 100644 index 00000000000..2e81861a872 --- /dev/null +++ b/apps/files/src/actions/inlineSystemTagsAction.spec.ts @@ -0,0 +1,143 @@ +/** + * @copyright Copyright (c) 2023 Lucas Azevedo + * + * @author Lucas Azevedo + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +import { action } from './inlineSystemTagsAction' +import { expect } from '@jest/globals' +import { File, Permission, View, FileAction } from '@nextcloud/files' + +const view = { + id: 'files', + name: 'Files', +} as View + +describe('Inline system tags action conditions tests', () => { + test('Default values', () => { + const file = new File({ + id: 1, + source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + }) + + expect(action).toBeInstanceOf(FileAction) + expect(action.id).toBe('system-tags') + expect(action.displayName([file], view)).toBe('') + expect(action.iconSvgInline([], view)).toBe('') + expect(action.default).toBeUndefined() + expect(action.enabled).toBeUndefined() + expect(action.order).toBe(0) + }) +}) + +describe('Inline system tags action render tests', () => { + test('Render nothing when Node does not have system tags', async () => { + const file = new File({ + id: 1, + source: 'http://localhost/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + }) + + const result = await action.renderInline!(file, view) + expect(result).toBeNull() + }) + + test('Render a single system tag', async () => { + const file = new File({ + id: 1, + source: 'http://localhost/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + attributes: { + 'system-tags': { + 'system-tag': 'Confidential' + } + } + }) + + const result = await action.renderInline!(file, view) + expect(result).toBeInstanceOf(HTMLElement) + expect(result!.outerHTML).toBe( + '
    ' + + '
  • Confidential
  • ' + + '
' + ) + }) + + test('Render two system tags', async () => { + const file = new File({ + id: 1, + source: 'http://localhost/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + attributes: { + 'system-tags': { + 'system-tag': [ + 'Important', + 'Confidential' + ] + } + } + }) + + const result = await action.renderInline!(file, view) + expect(result).toBeInstanceOf(HTMLElement) + expect(result!.outerHTML).toBe( + '
    ' + + '
  • Important
  • ' + + '
  • +1
  • ' + + '
' + ) + }) + + test('Render multiple system tags', async () => { + const file = new File({ + id: 1, + source: 'http://localhost/remote.php/dav/files/admin/foobar.txt', + owner: 'admin', + mime: 'text/plain', + permissions: Permission.ALL, + attributes: { + 'system-tags': { + 'system-tag': [ + 'Important', + 'Confidential', + 'Secret', + 'Classified' + ] + } + } + }) + + const result = await action.renderInline!(file, view) + expect(result).toBeInstanceOf(HTMLElement) + expect(result!.outerHTML).toBe( + '
    ' + + '
  • Important
  • ' + + '
  • +3
  • ' + + '
' + ) + }) +}) diff --git a/apps/files/src/actions/inlineSystemTagsAction.ts b/apps/files/src/actions/inlineSystemTagsAction.ts index 395eba885d7..8aa68aea03d 100644 --- a/apps/files/src/actions/inlineSystemTagsAction.ts +++ b/apps/files/src/actions/inlineSystemTagsAction.ts @@ -37,7 +37,7 @@ const getNodeSystemTags = function (node: Node): string[] { const renderTag = function (tag: string, isMore: boolean = false): HTMLElement { const tagElement = document.createElement('li') tagElement.classList.add('files-list__system-tag') - tagElement.innerText = tag + tagElement.textContent = tag if (isMore) { tagElement.classList.add('files-list__system-tag--more') @@ -81,7 +81,9 @@ export const action = new FileAction({ } return systemTagsElement - } + }, + + order: 0 }) registerDavProperty('nc:system-tags') diff --git a/apps/files/src/css/fileEntryInlineSystemTags.scss b/apps/files/src/css/fileEntryInlineSystemTags.scss index e82e3da3c75..2159d3b01c7 100644 --- a/apps/files/src/css/fileEntryInlineSystemTags.scss +++ b/apps/files/src/css/fileEntryInlineSystemTags.scss @@ -1,3 +1,24 @@ +/** + * @copyright Copyright (c) 2023 Lucas Azevedo + * + * @author Lucas Azevedo + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ .files-list__system-tags { --min-size: 32px; display: flex; diff --git a/jest.config.ts b/jest.config.ts index 78b5912fee9..5bf104a4243 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -71,6 +71,7 @@ const config: Config = { // Allow mocking svg files moduleNameMapper: { '^.+\\.svg(\\?raw)?$': '/__mocks__/svg.js', + '\\.s?css$': '/__mocks__/css.js', }, modulePathIgnorePatterns: [ '/apps2/',