|
| 1 | +import { |
| 2 | + Component, |
| 3 | + DebugElement, |
| 4 | +} from '@angular/core'; |
| 5 | +import { |
| 6 | + ComponentFixture, |
| 7 | + TestBed, |
| 8 | + waitForAsync, |
| 9 | +} from '@angular/core/testing'; |
| 10 | +import { By } from '@angular/platform-browser'; |
| 11 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 12 | + |
| 13 | +import { DaffioApiPackageComponent } from './api-package.component'; |
| 14 | +import { DaffioApiReference } from '../../models/api-reference'; |
| 15 | +import { DaffioApiListSectionComponent } from '../api-list-section/api-list-section.component'; |
| 16 | + |
| 17 | +@Component({ |
| 18 | + template: ` |
| 19 | + <daffio-api-package [doc]="apiListValue"></daffio-api-package> |
| 20 | + `, |
| 21 | + standalone: true, |
| 22 | + imports: [ |
| 23 | + DaffioApiPackageComponent, |
| 24 | + ], |
| 25 | +}) |
| 26 | +class WrapperComponent { |
| 27 | + apiListValue: DaffioApiReference; |
| 28 | +} |
| 29 | + |
| 30 | +describe('DaffioApiPackageComponent', () => { |
| 31 | + let wrapper: WrapperComponent; |
| 32 | + let fixture: ComponentFixture<WrapperComponent>; |
| 33 | + let component: DaffioApiPackageComponent; |
| 34 | + let packageLinks: Array<DebugElement>; |
| 35 | + |
| 36 | + beforeEach(waitForAsync(() => { |
| 37 | + TestBed.configureTestingModule({ |
| 38 | + imports: [ |
| 39 | + WrapperComponent, |
| 40 | + RouterTestingModule, |
| 41 | + ], |
| 42 | + }) |
| 43 | + .compileComponents(); |
| 44 | + })); |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + fixture = TestBed.createComponent(WrapperComponent); |
| 48 | + wrapper = fixture.componentInstance; |
| 49 | + wrapper.apiListValue = { |
| 50 | + id: 'Root', |
| 51 | + path: 'path', |
| 52 | + docType: 'package', |
| 53 | + docTypeShorthand: 'pk', |
| 54 | + title: 'title', |
| 55 | + description: 'description', |
| 56 | + children: [ |
| 57 | + { |
| 58 | + id: 'name1Component', |
| 59 | + title: 'title1Component', |
| 60 | + path: 'path1', |
| 61 | + docType: 'docType1', |
| 62 | + docTypeShorthand: 'dt', |
| 63 | + children: [], |
| 64 | + }, |
| 65 | + { |
| 66 | + id: 'name2Module', |
| 67 | + title: 'title2Module', |
| 68 | + path: 'path2', |
| 69 | + docType: 'package', |
| 70 | + docTypeShorthand: 'pk', |
| 71 | + children: [ |
| 72 | + { |
| 73 | + id: 'name1Component', |
| 74 | + title: 'title1Component', |
| 75 | + path: 'path1', |
| 76 | + docType: 'docType1', |
| 77 | + docTypeShorthand: 'dt', |
| 78 | + children: [], |
| 79 | + }, |
| 80 | + { |
| 81 | + id: 'name2Module', |
| 82 | + title: 'title2Module', |
| 83 | + path: 'path2', |
| 84 | + docType: 'package', |
| 85 | + docTypeShorthand: 'pk', |
| 86 | + children: [], |
| 87 | + }, |
| 88 | + ], |
| 89 | + }, |
| 90 | + ], |
| 91 | + }; |
| 92 | + fixture.detectChanges(); |
| 93 | + |
| 94 | + component = fixture.debugElement.query(By.css('daffio-api-package')).componentInstance; |
| 95 | + packageLinks = fixture.debugElement.queryAll(By.css('a.daffio-api-package__package-name')); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should create', () => { |
| 99 | + expect(component).toBeTruthy(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should be able to take doc as input', () => { |
| 103 | + expect(component.doc).toEqual(wrapper.apiListValue); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('for every subpackage in children', () => { |
| 107 | + let subpackages: Array<DaffioApiReference>; |
| 108 | + |
| 109 | + beforeEach(() => { |
| 110 | + subpackages = wrapper.apiListValue.children.filter((d) => d.docType === 'package'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should render a link with the title', () => { |
| 114 | + packageLinks.forEach((de, i) => { |
| 115 | + expect(de.nativeElement.innerText).toEqual(subpackages[i].title); |
| 116 | + expect(de.attributes['ng-reflect-router-link']).toEqual(subpackages[i].path); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should render an API section with the subpackage children excluding packages', () => { |
| 121 | + packageLinks.forEach((de, i) => { |
| 122 | + const apiSection: DaffioApiListSectionComponent = fixture.debugElement.query(By.css(`daffio-api-list-section[data-section-for-subpackage="${subpackages[i].id}"]`)).componentInstance; |
| 123 | + expect(apiSection.children).toEqual(subpackages[i].children.filter((c) => c.docType !== 'package')); |
| 124 | + }); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | +}); |
0 commit comments