ObsiViewer/src/components/markdown-viewer/markdown-viewer.component.spec.ts

135 lines
4.1 KiB
TypeScript

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MarkdownViewerComponent } from './markdown-viewer.component';
import { MarkdownService } from '../../services/markdown.service';
import { DomSanitizer } from '@angular/platform-browser';
describe('MarkdownViewerComponent', () => {
let component: MarkdownViewerComponent;
let fixture: ComponentFixture<MarkdownViewerComponent>;
let markdownService: jasmine.SpyObj<MarkdownService>;
beforeEach(async () => {
const markdownServiceSpy = jasmine.createSpyObj('MarkdownService', ['render']);
await TestBed.configureTestingModule({
imports: [MarkdownViewerComponent],
providers: [
{ provide: MarkdownService, useValue: markdownServiceSpy }
]
}).compileComponents();
markdownService = TestBed.inject(MarkdownService) as jasmine.SpyObj<MarkdownService>;
fixture = TestBed.createComponent(MarkdownViewerComponent);
component = fixture.componentInstance;
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should render markdown content', () => {
const testMarkdown = '# Hello World';
const expectedHtml = '<h1>Hello World</h1>';
markdownService.render.and.returnValue(expectedHtml);
component.content = testMarkdown;
fixture.detectChanges();
expect(markdownService.render).toHaveBeenCalledWith(testMarkdown, [], undefined);
});
it('should detect excalidraw files', () => {
component.filePath = 'test.excalidraw.md';
fixture.detectChanges();
expect(component.isExcalidrawFile()).toBe(true);
});
it('should not detect regular markdown as excalidraw', () => {
component.filePath = 'test.md';
fixture.detectChanges();
expect(component.isExcalidrawFile()).toBe(false);
});
it('should toggle fullscreen mode', () => {
expect(component.isFullscreen()).toBe(false);
component.toggleFullscreen();
expect(component.isFullscreen()).toBe(true);
component.toggleFullscreen();
expect(component.isFullscreen()).toBe(false);
});
it('should handle render errors gracefully', () => {
const testMarkdown = '# Test';
markdownService.render.and.throwError('Render error');
component.content = testMarkdown;
fixture.detectChanges();
expect(component.error()).toContain('Erreur de rendu');
});
it('should clear error on content change', () => {
component.error.set('Previous error');
component.content = 'New content';
component.ngOnChanges({
content: {
currentValue: 'New content',
previousValue: 'Old content',
firstChange: false,
isFirstChange: () => false
}
});
expect(component.error()).toBeNull();
});
it('should show toolbar when showToolbar is true', () => {
component.showToolbar = true;
fixture.detectChanges();
const toolbar = fixture.nativeElement.querySelector('.markdown-viewer__toolbar');
expect(toolbar).toBeTruthy();
});
it('should hide toolbar when showToolbar is false', () => {
component.showToolbar = false;
fixture.detectChanges();
const toolbar = fixture.nativeElement.querySelector('.markdown-viewer__toolbar');
expect(toolbar).toBeFalsy();
});
it('should pass allNotes to markdown service', () => {
const testNotes = [
{ id: '1', title: 'Note 1', content: 'Content 1' },
{ id: '2', title: 'Note 2', content: 'Content 2' }
] as any[];
markdownService.render.and.returnValue('<p>Test</p>');
component.content = '# Test';
component.allNotes = testNotes;
fixture.detectChanges();
expect(markdownService.render).toHaveBeenCalledWith('# Test', testNotes, undefined);
});
it('should pass currentNote to markdown service', () => {
const currentNote = { id: '1', title: 'Current', content: 'Content' } as any;
markdownService.render.and.returnValue('<p>Test</p>');
component.content = '# Test';
component.currentNote = currentNote;
fixture.detectChanges();
expect(markdownService.render).toHaveBeenCalledWith('# Test', [], currentNote);
});
});