Skip to content

Commit

Permalink
chore(systemtags): add inline tag color tests
Browse files Browse the repository at this point in the history
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Dec 4, 2024
1 parent b9da6f6 commit c6bd30d
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
107 changes: 106 additions & 1 deletion apps/systemtags/src/files_actions/inlineSystemTagsAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { emit, subscribe } from '@nextcloud/event-bus'
import { File, Permission, View, FileAction } from '@nextcloud/files'
import { setNodeSystemTags } from '../utils'
import * as serviceTagApi from '../services/api'
import { set } from 'lodash'

const view = {
id: 'files',
Expand Down Expand Up @@ -192,3 +191,109 @@ describe('Inline system tags action render tests', () => {
)
})
})

describe('Inline system tags action colors', () => {

const tag = {
id: 1,
displayName: 'Confidential',
color: '000000',
etag: '123',
userVisible: true,
userAssignable: true,
canAssign: true,
}

beforeEach(() => {
document.body.innerHTML = ''
vi.spyOn(serviceTagApi, 'fetchTags').mockImplementation(async () => {
return [tag]
})
})

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).toMatchInlineSnapshot(
'"<ul class="files-list__system-tags" aria-label="Assigned collaborative tags" data-systemtags-fileid="1"><li class="files-list__system-tag" data-systemtag-name="Confidential" style="--systemtag-color: #000000;" data-systemtag-color="true">Confidential</li></ul>"',
)
})

test('Render a single system tag with invalid WCAG color', 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',
},
},
})

document.body.setAttribute('data-themes', 'theme-dark')

const result = await action.renderInline!(file, view)
expect(result).toBeInstanceOf(HTMLElement)
expect(result!.outerHTML).toMatchInlineSnapshot(
'"<ul class="files-list__system-tags" aria-label="Assigned collaborative tags" data-systemtags-fileid="1"><li class="files-list__system-tag" data-systemtag-name="Confidential" style="--systemtag-color: #646464;" data-systemtag-color="true">Confidential</li></ul>"',
)

document.body.removeAttribute('data-themes')
})

test('Rendered color gets updated on system tag change', 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) as HTMLElement
document.body.appendChild(result)
expect(result).toBeInstanceOf(HTMLElement)
expect(document.body.innerHTML).toMatchInlineSnapshot(
'"<ul class="files-list__system-tags" aria-label="Assigned collaborative tags" data-systemtags-fileid="1"><li class="files-list__system-tag" data-systemtag-name="Confidential" style="--systemtag-color: #000000;" data-systemtag-color="true">Confidential</li></ul>"',
)

// Subscribe to the event
const eventPromise = new Promise((resolve) => {
subscribe('systemtags:tag:updated', () => {
setTimeout(resolve, 100)
})
})

// Change tag color
tag.color = '456789'
emit('systemtags:tag:updated', tag)

// Wait for the event to be processed
await eventPromise

expect(document.body.innerHTML).toMatchInlineSnapshot(
'"<ul class="files-list__system-tags" aria-label="Assigned collaborative tags" data-systemtags-fileid="1"><li class="files-list__system-tag" data-systemtag-name="Confidential" style="--systemtag-color: #456789;" data-systemtag-color="true">Confidential</li></ul>"',
)
})
})
4 changes: 2 additions & 2 deletions apps/systemtags/src/utils/colorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type hexColor = `#${string & (
* Is the current theme dark?
*/
export function isDarkModeEnabled() {
const darkModePreference = window.matchMedia('(prefers-color-scheme: dark)').matches
const darkModePreference = window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches
const darkModeSetting = document.body.getAttribute('data-themes')?.includes('dark')
return darkModeSetting || darkModePreference || false
}
Expand All @@ -22,7 +22,7 @@ export function isDarkModeEnabled() {
* Is the current theme high contrast?
*/
export function isHighContrastModeEnabled() {
const highContrastPreference = window.matchMedia('(forced-colors: active)').matches
const highContrastPreference = window?.matchMedia?.('(forced-colors: active)')?.matches
const highContrastSetting = document.body.getAttribute('data-themes')?.includes('highcontrast')
return highContrastSetting || highContrastPreference || false
}
Expand Down

0 comments on commit c6bd30d

Please sign in to comment.