|
| 1 | +// libs/chat/src/lib/markdown/views/markdown-list-item.component.spec.ts |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 4 | +import { TestBed } from '@angular/core/testing'; |
| 5 | +import { Component, signal } from '@angular/core'; |
| 6 | +import { views } from '@ngaf/render'; |
| 7 | +import type { MarkdownListItemNode } from '@cacheplane/partial-markdown'; |
| 8 | +import { MarkdownListItemComponent } from './markdown-list-item.component'; |
| 9 | +import { MarkdownTextComponent } from './markdown-text.component'; |
| 10 | +import { MARKDOWN_VIEW_REGISTRY } from '../markdown-view-registry'; |
| 11 | + |
| 12 | +function makeItemNode(task?: { checked: boolean }): MarkdownListItemNode { |
| 13 | + return { |
| 14 | + id: 10, type: 'list-item', status: 'complete', |
| 15 | + parent: null, index: null, |
| 16 | + task, |
| 17 | + children: [], |
| 18 | + } as MarkdownListItemNode; |
| 19 | +} |
| 20 | + |
| 21 | +@Component({ |
| 22 | + standalone: true, |
| 23 | + imports: [MarkdownListItemComponent], |
| 24 | + template: `<chat-md-list-item [node]="node()" />`, |
| 25 | +}) |
| 26 | +class HostComponent { |
| 27 | + node = signal<MarkdownListItemNode>(makeItemNode()); |
| 28 | +} |
| 29 | + |
| 30 | +describe('MarkdownListItemComponent', () => { |
| 31 | + beforeEach(() => { |
| 32 | + TestBed.configureTestingModule({ |
| 33 | + imports: [HostComponent], |
| 34 | + providers: [{ |
| 35 | + provide: MARKDOWN_VIEW_REGISTRY, |
| 36 | + useValue: views({ 'text': MarkdownTextComponent }), |
| 37 | + }], |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('renders a <li> element', () => { |
| 42 | + const fixture = TestBed.createComponent(HostComponent); |
| 43 | + fixture.detectChanges(); |
| 44 | + expect(fixture.nativeElement.querySelector('li')).toBeTruthy(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('does not render a checkbox for plain items', () => { |
| 48 | + const fixture = TestBed.createComponent(HostComponent); |
| 49 | + fixture.detectChanges(); |
| 50 | + expect(fixture.nativeElement.querySelector('input[type="checkbox"]')).toBeFalsy(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('does not apply task class for plain items', () => { |
| 54 | + const fixture = TestBed.createComponent(HostComponent); |
| 55 | + fixture.detectChanges(); |
| 56 | + const li = fixture.nativeElement.querySelector('li'); |
| 57 | + expect(li.classList.contains('chat-md-list-item--task')).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + it('renders a disabled unchecked checkbox for task items (unchecked)', () => { |
| 61 | + const fixture = TestBed.createComponent(HostComponent); |
| 62 | + fixture.componentInstance.node.set(makeItemNode({ checked: false })); |
| 63 | + fixture.detectChanges(); |
| 64 | + const checkbox = fixture.nativeElement.querySelector('input[type="checkbox"]'); |
| 65 | + expect(checkbox).toBeTruthy(); |
| 66 | + expect(checkbox.disabled).toBe(true); |
| 67 | + expect(checkbox.checked).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + it('renders a disabled checked checkbox for task items (checked)', () => { |
| 71 | + const fixture = TestBed.createComponent(HostComponent); |
| 72 | + fixture.componentInstance.node.set(makeItemNode({ checked: true })); |
| 73 | + fixture.detectChanges(); |
| 74 | + const checkbox = fixture.nativeElement.querySelector('input[type="checkbox"]'); |
| 75 | + expect(checkbox).toBeTruthy(); |
| 76 | + expect(checkbox.disabled).toBe(true); |
| 77 | + expect(checkbox.checked).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + it('applies task class when task is defined', () => { |
| 81 | + const fixture = TestBed.createComponent(HostComponent); |
| 82 | + fixture.componentInstance.node.set(makeItemNode({ checked: false })); |
| 83 | + fixture.detectChanges(); |
| 84 | + const li = fixture.nativeElement.querySelector('li'); |
| 85 | + expect(li.classList.contains('chat-md-list-item--task')).toBe(true); |
| 86 | + }); |
| 87 | +}); |
0 commit comments