Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/__tests__/pages/api/__tests__/[version]/tokens.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { GET } from '../../../../../pages/api/[version]/tokens'

const mockApiIndex = {
versions: ['v5', 'v6'],
sections: {},
pages: {},
tabs: {},
}

jest.mock('../../../../../utils/tokens', () => ({
getTokenCategories: jest.fn(() => [
'c',
'chart',
'global',
'hidden',
'l',
't',
]),
}))

it('returns sorted token categories for valid version', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockApiIndex),
} as Response),
)

const response = await GET({
params: { version: 'v6' },
url: new URL('http://localhost:4321/api/v6/tokens'),
} as any)
const body = await response.json()

expect(response.status).toBe(200)
expect(response.headers.get('Content-Type')).toBe(
'application/json; charset=utf-8',
)
expect(Array.isArray(body)).toBe(true)
expect(body).toEqual(['c', 'chart', 'global', 'hidden', 'l', 't'])

jest.restoreAllMocks()
})

it('returns categories alphabetically sorted', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockApiIndex),
} as Response),
)

const response = await GET({
params: { version: 'v6' },
url: new URL('http://localhost:4321/api/v6/tokens'),
} as any)
const body = await response.json()

const sorted = [...body].sort()
expect(body).toEqual(sorted)

jest.restoreAllMocks()
})

it('returns 404 error for nonexistent version', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockApiIndex),
} as Response),
)

const response = await GET({
params: { version: 'v99' },
url: new URL('http://localhost:4321/api/v99/tokens'),
} as any)
const body = await response.json()

expect(response.status).toBe(404)
expect(body).toHaveProperty('error')
expect(body.error).toContain('v99')
expect(body.error).toContain('not found')

jest.restoreAllMocks()
})

it('returns 400 error when version parameter is missing', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockApiIndex),
} as Response),
)

const response = await GET({
params: {},
url: new URL('http://localhost:4321/api/tokens'),
} as any)
const body = await response.json()

expect(response.status).toBe(400)
expect(body).toHaveProperty('error')
expect(body.error).toContain('Version parameter is required')

jest.restoreAllMocks()
})
201 changes: 201 additions & 0 deletions src/__tests__/pages/api/__tests__/[version]/tokens/[category].test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import { GET } from '../../../../../../pages/api/[version]/tokens/[category]'

const mockApiIndex = {
versions: ['v5', 'v6'],
sections: {},
pages: {},
tabs: {},
}

const mockTokens = {
c: [
{
name: '--pf-v6-c-alert--Color',
value: '#000',
var: 'var(--pf-v6-c-alert--Color)',
},
{
name: '--pf-v6-c-button--Color',
value: '#fff',
var: 'var(--pf-v6-c-button--Color)',
},
],
t: [
{
name: '--pf-v6-t-global--Color',
value: '#333',
var: 'var(--pf-v6-t-global--Color)',
},
],
}

jest.mock('../../../../../../utils/tokens', () => ({
getTokenCategories: jest.fn(() => ['c', 't']),
getTokensForCategory: jest.fn(
(category: string) => mockTokens[category as keyof typeof mockTokens],
),
filterTokens: jest.fn((tokens, filter) =>
tokens.filter((token: any) =>
token.name.toLowerCase().includes(filter.toLowerCase()),
),
),
}))

it('returns tokens for valid category', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockApiIndex),
} as Response),
)

const response = await GET({
params: { version: 'v6', category: 'c' },
url: new URL('http://localhost:4321/api/v6/tokens/c'),
} as any)
const body = await response.json()

expect(response.status).toBe(200)
expect(response.headers.get('Content-Type')).toBe(
'application/json; charset=utf-8',
)
expect(Array.isArray(body)).toBe(true)
expect(body).toHaveLength(2)
expect(body[0]).toHaveProperty('name')
expect(body[0]).toHaveProperty('value')
expect(body[0]).toHaveProperty('var')

jest.restoreAllMocks()
})

it('filters tokens when filter parameter is provided', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockApiIndex),
} as Response),
)

const response = await GET({
params: { version: 'v6', category: 'c' },
url: new URL('http://localhost:4321/api/v6/tokens/c?filter=alert'),
} as any)
const body = await response.json()

expect(response.status).toBe(200)
expect(Array.isArray(body)).toBe(true)
expect(body).toHaveLength(1)
expect(body[0].name).toContain('alert')

jest.restoreAllMocks()
})

it('returns empty array when filter yields no matches', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockApiIndex),
} as Response),
)

const response = await GET({
params: { version: 'v6', category: 'c' },
url: new URL('http://localhost:4321/api/v6/tokens/c?filter=nonexistent'),
} as any)
const body = await response.json()

expect(response.status).toBe(200)
expect(Array.isArray(body)).toBe(true)
expect(body).toHaveLength(0)

jest.restoreAllMocks()
})

it('returns 404 error for invalid category with valid categories list', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockApiIndex),
} as Response),
)

const response = await GET({
params: { version: 'v6', category: 'invalid' },
url: new URL('http://localhost:4321/api/v6/tokens/invalid'),
} as any)
const body = await response.json()

expect(response.status).toBe(404)
expect(body).toHaveProperty('error')
expect(body.error).toContain('invalid')
expect(body.error).toContain('not found')
expect(body).toHaveProperty('validCategories')
expect(Array.isArray(body.validCategories)).toBe(true)
expect(body.validCategories).toContain('c')
expect(body.validCategories).toContain('t')

jest.restoreAllMocks()
})

it('returns 404 error for nonexistent version', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockApiIndex),
} as Response),
)

const response = await GET({
params: { version: 'v99', category: 'c' },
url: new URL('http://localhost:4321/api/v99/tokens/c'),
} as any)
const body = await response.json()

expect(response.status).toBe(404)
expect(body).toHaveProperty('error')
expect(body.error).toContain('v99')

jest.restoreAllMocks()
})

it('returns 400 error when parameters are missing', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockApiIndex),
} as Response),
)

const response = await GET({
params: {},
url: new URL('http://localhost:4321/api/tokens/'),
} as any)
const body = await response.json()

expect(response.status).toBe(400)
expect(body).toHaveProperty('error')
expect(body.error).toContain('required')

jest.restoreAllMocks()
})

it('filter is case-insensitive', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve(mockApiIndex),
} as Response),
)

const response = await GET({
params: { version: 'v6', category: 'c' },
url: new URL('http://localhost:4321/api/v6/tokens/c?filter=ALERT'),
} as any)
const body = await response.json()

expect(response.status).toBe(200)
expect(Array.isArray(body)).toBe(true)
expect(body).toHaveLength(1)

jest.restoreAllMocks()
})
Loading