import { BlockType, StructureType } from '@ministryofjustice/hmpps-forge/core/authoring'
import type { RenderedBlock } from '@ministryofjustice/hmpps-forge/core/components'
import { GovukComponentTestHelper } from '../../test-utils/GovukComponentTestHelper'
import { setupComponentTest } from '../../test-utils/setupComponentTest'
import { GovUKPanel } from './govukPanel'
vi.mock('nunjucks')
describe('GOV.UK Panel Component', () => {
setupComponentTest()
const helper = new GovukComponentTestHelper(GovUKPanel)
const renderedBlock = (html: string): RenderedBlock => ({
block: {
type: StructureType.BLOCK,
blockType: BlockType.BASIC,
variant: 'html',
},
html,
})
describe('Title transformation', () => {
it('sets titleText correctly', async () => {
// Arrange & Act
const params = await helper.getParams({
titleText: 'Application complete',
text: 'Your reference number is HDJ2123F',
})
// Assert
expect(params.titleText).toBe('Application complete')
expect(params.titleHtml).toBeUndefined()
})
it('uses titleHtml over titleText', async () => {
// Arrange & Act
const params = await helper.getParams({
titleText: 'This is ignored',
titleHtml: 'Success: Application complete',
text: 'Your reference number is HDJ2123F',
})
// Assert
expect(params.titleText).toBeUndefined()
expect(params.titleHtml).toBe('Success: Application complete')
})
it('passes through headingLevel', async () => {
// Arrange & Act
const params = await helper.getParams({
titleText: 'Application complete',
headingLevel: 2,
text: 'Your reference number is HDJ2123F',
})
// Assert
expect(params.headingLevel).toBe(2)
})
it('handles title without body content', async () => {
// Arrange & Act
const params = await helper.getParams({
titleText: 'Application complete',
})
// Assert
expect(params.titleText).toBe('Application complete')
expect(params.text).toBeUndefined()
expect(params.html).toBeUndefined()
})
})
describe('Body content', () => {
it('sets text content correctly', async () => {
// Arrange & Act
const params = await helper.getParams({
titleText: 'Application complete',
text: 'Your reference number is HDJ2123F',
})
// Assert
expect(params.text).toBe('Your reference number is HDJ2123F')
expect(params.html).toBeUndefined()
})
it('uses html over text when both provided', async () => {
// Arrange & Act
const params = await helper.getParams({
titleText: 'Application complete',
text: 'This is ignored',
html: 'Your reference number
HDJ2123F',
})
// Assert
expect(params.text).toBeUndefined()
expect(params.html).toBe('Your reference number
HDJ2123F')
})
it('uses blocks over text and html when provided', async () => {
// Arrange & Act
const params = await helper.getParams({
titleText: 'Application complete',
text: 'This is ignored',
html: '
This is also ignored
', blocks: [renderedBlock('First block
'), renderedBlock('Second block
')], }) // Assert expect(params.text).toBeUndefined() expect(params.html).toBe('First block
Second block
') }) it('handles panel with only title', async () => { // Arrange & Act const params = await helper.getParams({ titleText: 'Application complete', }) // Assert expect(params.titleText).toBe('Application complete') expect(params.text).toBeUndefined() expect(params.html).toBeUndefined() }) }) describe('Optional attributes', () => { it('passes through classes', async () => { // Arrange & Act const params = await helper.getParams({ titleText: 'Application complete', text: 'Your reference number is HDJ2123F', classes: 'app-panel--custom', }) // Assert expect(params.classes).toBe('app-panel--custom') }) it('passes through attributes', async () => { // Arrange & Act const params = await helper.getParams({ titleText: 'Application complete', text: 'Your reference number is HDJ2123F', attributes: { 'data-module': 'custom-panel', 'data-track': 'panel-shown', }, }) // Assert expect(params.attributes).toEqual({ 'data-module': 'custom-panel', 'data-track': 'panel-shown', }) }) it('handles all options together', async () => { // Arrange & Act const params = await helper.getParams({ titleText: 'Application complete', headingLevel: 1, html: 'Your reference number