import { CollectionBlock, EvaluatedCollectionBlock } from './collectionBlock' import { BlockType, StructureType } from '../../authoring/types/enums' import type { RenderedBlock } from '../../components/types/structures.type' describe('collectionBlock component', () => { const mockEvaluatedBlock = (overrides?: Partial): EvaluatedCollectionBlock => ({ type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'collection-block', ...overrides, }) as EvaluatedCollectionBlock const mockRenderedBlock = (html: string): RenderedBlock => ({ block: { type: StructureType.BLOCK, blockType: BlockType.BASIC, variant: 'test', }, html, }) it('should render collection when it has items', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [ mockRenderedBlock('
Item 1
'), mockRenderedBlock('
Item 2
'), mockRenderedBlock('
Item 3
'), ], }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('
Item 1
Item 2
Item 3
') }) it('should render empty string when collection is empty', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [], }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('') }) it('should render with wrapper div when classes are provided', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [mockRenderedBlock('
Item 1
'), mockRenderedBlock('
Item 2
')], classes: 'collection-wrapper custom-class', }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('
Item 1
Item 2
') }) it('should render with wrapper div when attributes are provided', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [mockRenderedBlock('
Item 1
')], attributes: { 'data-collection': 'items', id: 'item-list', }, }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('
Item 1
') }) it('should render with wrapper div when both classes and attributes are provided', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [mockRenderedBlock('
Item 1
'), mockRenderedBlock('
Item 2
')], classes: 'styled-collection', attributes: { 'data-count': '2', role: 'list', }, }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe( '
Item 1
Item 2
', ) }) it('should render with specified tag instead of div', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [mockRenderedBlock('
  • Item 1
  • '), mockRenderedBlock('
  • Item 2
  • ')], tag: 'ul', classes: 'govuk-list', }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('') }) it('should render with tag even without classes or attributes', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [mockRenderedBlock('
  • Item 1
  • ')], tag: 'ol', }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('
    1. Item 1
    ') }) it('should render empty wrapper when classes provided and collection is empty', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [], classes: 'empty-collection', }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('
    ') }) it('should handle complex nested HTML in collection', async () => { // Arrange const html1 = '

    Title 1

    ' const html2 = '

    Title 2

    ' const block = mockEvaluatedBlock({ collection: [mockRenderedBlock(html1), mockRenderedBlock(html2)], }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe(`${html1}${html2}`) }) it('should have the correct variant', () => { // Assert expect(CollectionBlock.variant).toBe('collection-block') }) it('should handle undefined collection', async () => { // Arrange const block = mockEvaluatedBlock({ collection: undefined, }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('') }) it('should handle plain string items in collection', async () => { // Arrange const block = mockEvaluatedBlock({ collection: ['
  • Item 1
  • ', '
  • Item 2
  • ', '
  • Item 3
  • '] as unknown as any[], }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('
  • Item 1
  • Item 2
  • Item 3
  • ') }) it('should handle mixed rendered blocks and strings in collection', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [ mockRenderedBlock('
    Block 1
    '), '

    Plain string

    ', mockRenderedBlock('
    Block 2
    '), ] as unknown as any[], }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('
    Block 1

    Plain string

    Block 2
    ') }) it('should handle nested arrays in collection items', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [ ['
  • Nested 1
  • ', '
  • Nested 2
  • '], mockRenderedBlock('
  • Block item
  • '), ] as unknown as any[], }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('
  • Nested 1
  • Nested 2
  • Block item
  • ') }) it('should render fallback when collection is empty', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [], fallback: [mockRenderedBlock('

    No items found

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

    No items found

    ') }) it('should render fallback when collection is undefined', async () => { // Arrange const block = mockEvaluatedBlock({ collection: undefined, fallback: [mockRenderedBlock('

    No items available

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

    No items available

    ') }) it('should render collection items instead of fallback when collection has items', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [mockRenderedBlock('
    Item 1
    ')], fallback: [mockRenderedBlock('

    No items found

    ')], }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('
    Item 1
    ') }) it('should render fallback with wrapper div when classes are provided', async () => { // Arrange const block = mockEvaluatedBlock({ collection: [], fallback: [mockRenderedBlock('

    Empty state

    ')], classes: 'empty-collection', }) // Act const result = await CollectionBlock.render(block as any) // Assert expect(result).toBe('

    Empty state

    ') }) })