import { BlockType, StructureType } from '@ministryofjustice/hmpps-forge/core/authoring' import type { RenderedBlock } from '@ministryofjustice/hmpps-forge/core/components' import { MojComponentTestHelper } from '../../test-utils/MojComponentTestHelper' import { setupComponentTest } from '../../test-utils/setupComponentTest' import { MOJBanner } from './mojBanner' vi.mock('nunjucks') describe('mojBanner', () => { setupComponentTest() const helper = new MojComponentTestHelper(MOJBanner) const renderedBlock = (html: string): RenderedBlock => ({ block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'html', }, html, }) describe('Data transformation', () => { it('should pass through text content', async () => { // Arrange & Act const params = await helper.getParams({ text: 'Your application has been submitted.', }) // Assert expect(params.text).toBe('Your application has been submitted.') }) it('should pass through HTML content', async () => { // Arrange & Act const params = await helper.getParams({ html: '
Your application has been submitted.
', }) // Assert expect(params.html).toBe('Your application has been submitted.
') }) it('should leave text undefined when not provided', async () => { // Arrange & Act const params = await helper.getParams({ html: 'HTML only
', }) // Assert expect(params.text).toBeUndefined() }) it('should handle both text and html when provided together', async () => { // Arrange & Act const params = await helper.getParams({ text: 'Plain text', html: 'HTML content
', }) // Assert expect(params.text).toBe('Plain text') expect(params.html).toBe('HTML content
') }) it('should use blocks over text and html when provided', async () => { // Arrange & Act const params = await helper.getParams({ 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
') }) }) describe('Banner types', () => { it('should pass through success type', async () => { // Arrange & Act const params = await helper.getParams({ bannerType: 'success', text: 'Success message', }) // Assert expect(params.type).toBe('success') }) it('should pass through warning type', async () => { // Arrange & Act const params = await helper.getParams({ bannerType: 'warning', text: 'Warning message', }) // Assert expect(params.type).toBe('warning') }) it('should pass through information type', async () => { // Arrange & Act const params = await helper.getParams({ bannerType: 'information', text: 'Information message', }) // Assert expect(params.type).toBe('information') }) it('should leave type undefined when bannerType not provided', async () => { // Arrange & Act const params = await helper.getParams({ text: 'Default banner', }) // Assert expect(params.type).toBeUndefined() }) }) describe('Icon fallback text', () => { it('should pass through iconFallbackText for accessibility', async () => { // Arrange & Act const params = await helper.getParams({ bannerType: 'success', text: 'Success message', iconFallbackText: 'Success', }) // Assert expect(params.iconFallbackText).toBe('Success') }) it('should leave iconFallbackText undefined when not provided', async () => { // Arrange & Act const params = await helper.getParams({ bannerType: 'success', text: 'Success message', }) // Assert expect(params.iconFallbackText).toBeUndefined() }) it('should handle custom iconFallbackText', async () => { // Arrange & Act const params = await helper.getParams({ bannerType: 'warning', text: 'Warning message', iconFallbackText: 'Important warning', }) // Assert expect(params.iconFallbackText).toBe('Important warning') }) }) describe('Optional attributes', () => { it('should pass through classes', async () => { // Arrange & Act const params = await helper.getParams({ text: 'Banner message', classes: 'app-banner--custom', }) // Assert expect(params.classes).toBe('app-banner--custom') }) it('should pass through attributes', async () => { // Arrange const attributes = { 'data-module': 'custom-banner', 'data-testid': 'banner-component', } // Act const params = await helper.getParams({ text: 'Banner message', attributes, }) // Assert expect(params.attributes).toEqual(attributes) }) it('should leave classes undefined when not provided', async () => { // Arrange & Act const params = await helper.getParams({ text: 'Banner message', }) // Assert expect(params.classes).toBeUndefined() }) it('should leave attributes undefined when not provided', async () => { // Arrange & Act const params = await helper.getParams({ text: 'Banner message', }) // Assert expect(params.attributes).toBeUndefined() }) }) describe('Template and context', () => { it('should call nunjucks with correct template path', async () => { // Arrange & Act const { template } = await helper.executeComponent({ text: 'Banner message', }) // Assert expect(template).toBe('moj/components/banner/template.njk') }) it('should wrap params in context object', async () => { // Arrange & Act const { context } = await helper.executeComponent({ text: 'Banner message', }) // Assert expect(context).toHaveProperty('params') expect((context as { params: RecordSuccess HTML
', iconFallbackText: 'Success', classes: 'custom-class', attributes: { 'data-test': 'value' }, }) // Assert const params = (context as { params: RecordSuccess HTML
') expect(params).toHaveProperty('iconFallbackText', 'Success') expect(params).toHaveProperty('classes', 'custom-class') expect(params).toHaveProperty('attributes') }) }) })