import type { EvaluatedBlock } from '../../components/types/structures.type' import { TemplateWrapper } from './templateWrapper' import { BlockType, StructureType } from '../../authoring/types/enums' describe('templateWrapper component', () => { const mockBlock = (overrides?: Partial>): EvaluatedBlock => ({ type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'templateWrapper', template: '
{{slot:content}}
', ...overrides, }) as EvaluatedBlock describe('render()', () => { it('should render template with slot content', async () => { // Arrange const block = mockBlock({ template: '
{{slot:content}}
', slots: { content: [ { block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'html' }, html: '

Hello

' }, { block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'html' }, html: '

World

' }, ], }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('

Hello

World

') }) it('should render template with multiple named slots', async () => { // Arrange const block = mockBlock({ template: '
{{slot:header}}
{{slot:content}}
{{slot:footer}}
', slots: { header: [ { block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'html' }, html: '

Title

', }, ], content: [ { block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'html' }, html: '

Body

' }, ], footer: [ { block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'html' }, html: '
End
', }, ], }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('

Title

Body

End
') }) it('should render template with value substitutions', async () => { // Arrange const block = mockBlock({ template: '

{{title}}

{{description}}

', values: { title: 'Journey Config', description: 'Learn about journey configuration.', }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('

Journey Config

Learn about journey configuration.

') }) it('should render template with both slots and values', async () => { // Arrange const block = mockBlock({ template: '

{{title}}

{{slot:content}}

{{footer}}

', values: { title: 'My Section', footer: 'See also...', }, slots: { content: [ { block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'html' }, html: '

Content here

', }, ], }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('

My Section

Content here

See also...

') }) it('should remove unreplaced slot markers', async () => { // Arrange const block = mockBlock({ template: '
{{slot:content}}{{slot:missing}}
', slots: { content: [ { block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'html' }, html: '

Present

', }, ], }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('

Present

') }) it('should remove unreplaced value markers', async () => { // Arrange const block = mockBlock({ template: '
{{title}}{{missing}}
', values: { title: 'Hello', }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('
Hello
') }) it('should render with wrapper div when classes are provided', async () => { // Arrange const block = mockBlock({ template: '

Content

', classes: 'govuk-section', }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('

Content

') }) it('should render with wrapper div when attributes are provided', async () => { // Arrange const block = mockBlock({ template: '

Content

', attributes: { 'data-module': 'section', id: 'my-section', }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('

Content

') }) it('should render with wrapper div when both classes and attributes are provided', async () => { // Arrange const block = mockBlock({ template: '

Content

', classes: 'custom-class', attributes: { 'data-test': 'value', }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('

Content

') }) it('should render with custom tag when tag is provided', async () => { // Arrange const block = mockBlock({ template: '

Content

', tag: 'section', }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('

Content

') }) it('should render with custom tag and classes', async () => { // Arrange const block = mockBlock({ template: '

Content

', tag: 'aside', classes: 'app-sidebar', }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('') }) it('should render with custom tag, classes and attributes', async () => { // Arrange const block = mockBlock({ template: '

Content

', tag: 'nav', classes: 'app-nav', attributes: { 'aria-label': 'Main navigation', }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('') }) it('should render template without modification when no slots or values provided', async () => { // Arrange const block = mockBlock({ template: '

Plain template

', }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('

Plain template

') }) it('should handle empty slots array', async () => { // Arrange const block = mockBlock({ template: '
{{slot:content}}
', slots: { content: [], }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('
') }) it('should handle slot markers appearing multiple times', async () => { // Arrange const block = mockBlock({ template: '
{{slot:content}}
{{slot:content}}
', slots: { content: [ { block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'html' }, html: 'Repeated', }, ], }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('
Repeated
Repeated
') }) it('should handle value markers appearing multiple times', async () => { // Arrange const block = mockBlock({ template: '{{name}} and {{name}}', values: { name: 'Alice', }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('Alice and Alice') }) it('should join array values with empty string', async () => { // Arrange const block = mockBlock({ template: '
    {{items}}
', values: { items: ['
  • One
  • ', '
  • Two
  • ', '
  • Three
  • '] as unknown as string, }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('
    • One
    • Two
    • Three
    ') }) it('should extract html from rendered block values', async () => { // Arrange const block = mockBlock({ template: '
    {{content}}
    ', values: { content: { block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'html' }, html: '

    Rendered block content

    ', } as unknown as string, }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('

    Rendered block content

    ') }) it('should extract html from array of rendered blocks in values', async () => { // Arrange const block = mockBlock({ template: '
      {{items}}
    ', values: { items: [ { block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'html' }, html: '
  • First
  • ', }, { block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'html' }, html: '
  • Second
  • ', }, ] as unknown as string, }, }) // Act const result = await TemplateWrapper.render(block) // Assert expect(result).toBe('
    • First
    • Second
    ') }) }) it('should have the correct variant', () => { expect(TemplateWrapper.variant).toBe('templateWrapper') }) })