about summary refs log tree commit diff
path: root/src/alf/util/__tests__/colors.test.ts
blob: 350b6ff4a4ef07f50776c2e1dd9ea7516e0cb8b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {jest} from '@jest/globals'

import {logger} from '#/logger'
import {transparentifyColor} from '../colorGeneration'

jest.mock('#/logger', () => ({
  logger: {warn: jest.fn()},
}))

describe('transparentifyColor', () => {
  beforeEach(() => {
    jest.clearAllMocks()
  })

  it('converts hsl() to hsla()', () => {
    const result = transparentifyColor('hsl(120 100% 50%)', 0.5)
    expect(result).toBe('hsla(120 100% 50%, 0.5)')
  })

  it('converts hsl() to hsla() - fully transparent', () => {
    const result = transparentifyColor('hsl(120 100% 50%)', 0)
    expect(result).toBe('hsla(120 100% 50%, 0)')
  })

  it('converts rgb() to rgba()', () => {
    const result = transparentifyColor('rgb(255 0 0)', 0.75)
    expect(result).toBe('rgba(255 0 0, 0.75)')
  })

  it('expands 3-digit hex and appends alpha channel', () => {
    const result = transparentifyColor('#abc', 0.4)
    expect(result).toBe('#aabbcc66')
  })

  it('appends alpha to 6-digit hex', () => {
    const result = transparentifyColor('#aabbcc', 0.4)
    expect(result).toBe('#aabbcc66')
  })

  it('returns the original string and warns for unsupported formats', () => {
    const unsupported = 'blue'
    const result = transparentifyColor(unsupported, 0.5)
    expect(result).toBe(unsupported)
    expect(logger.warn).toHaveBeenCalledWith(
      `Could not make '${unsupported}' transparent`,
    )
  })
})