|
| 1 | +import { TestBed, ComponentFixture } from '@angular/core/testing'; |
| 2 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 3 | +import { By } from '@angular/platform-browser'; |
| 4 | +import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; |
| 5 | + |
| 6 | +import { of } from 'rxjs'; |
| 7 | + |
| 8 | +import { BonGatewayTestModule } from 'src/test/javascript/spec/test.module'; |
| 9 | +import { SireDetailsComponent } from 'app/farm/cow/sire-details/sire-details.component'; |
| 10 | +import { |
| 11 | + GetArticleGQL, |
| 12 | + GetCowGQL, |
| 13 | + FindCowPicturesGQL, |
| 14 | + PictureVo, |
| 15 | + PictureSourceVo, |
| 16 | + Visibility, |
| 17 | + ArticleVo, |
| 18 | + Category, |
| 19 | + SectionVo, |
| 20 | + CowVo, |
| 21 | +} from 'app/bonpublicgraphql/bonpublicgraphql'; |
| 22 | +import { ActivatedRoute } from '@angular/router'; |
| 23 | +import { JhiTranslateDirective } from 'ng-jhipster'; |
| 24 | +import { ApolloModule } from 'apollo-angular'; |
| 25 | +import { BonVisibilityClassDirective } from 'app/shared/bon/bon-visibility-class.directive'; |
| 26 | +import { CowPictureDirective } from 'app/farm/cow/cow-pictures.directive'; |
| 27 | + |
| 28 | +describe('Component Tests', () => { |
| 29 | + describe('SireDetailsComponent', () => { |
| 30 | + let comp: SireDetailsComponent; |
| 31 | + let fixture: ComponentFixture<SireDetailsComponent>; |
| 32 | + let getArticleGQL: GetArticleGQL; |
| 33 | + let getCowGQL: GetCowGQL; |
| 34 | + let findCowPicturesGQL: FindCowPicturesGQL; |
| 35 | + |
| 36 | + const TEST_COW = { |
| 37 | + id: 100, |
| 38 | + earTagId: 200, |
| 39 | + name: 'cowname200', |
| 40 | + visibility: Visibility.RoleAnonymous, |
| 41 | + storyHandle: 'cattlestory200', |
| 42 | + matriId: 10, |
| 43 | + patriId: 20, |
| 44 | + } as CowVo; |
| 45 | + |
| 46 | + const TEST_PICTURE_SOURCE = { |
| 47 | + name: 'cow1_1.png', |
| 48 | + contentType: 'image/png', |
| 49 | + width: 192, |
| 50 | + height: 192, |
| 51 | + url: '/api/public/cows/200/pictures/1/cow1_1.png', |
| 52 | + } as PictureSourceVo; |
| 53 | + |
| 54 | + const TEST_PICTURE = { |
| 55 | + id: 1, |
| 56 | + taken: Date.now().toLocaleString(), |
| 57 | + visibility: Visibility.RoleAnonymous, |
| 58 | + caption: 'cap', |
| 59 | + sources: [TEST_PICTURE_SOURCE], |
| 60 | + } as PictureVo; |
| 61 | + const API_PICTURES_RESPONSE$ = of({ |
| 62 | + data: { |
| 63 | + apiPublicCowsPictures: [TEST_PICTURE], |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + const TEST_ARTICLE = { |
| 68 | + id: 100, |
| 69 | + category: Category.Matrilineality, |
| 70 | + name: 'lineagehandle1', |
| 71 | + visibility: Visibility.RoleAnonymous, |
| 72 | + sections: [ |
| 73 | + { |
| 74 | + id: 101, |
| 75 | + title: 'Muuu', |
| 76 | + } as SectionVo, |
| 77 | + ], |
| 78 | + } as ArticleVo; |
| 79 | + const API_ARTICLE_RESPONSE$ = of({ |
| 80 | + data: { |
| 81 | + articleVO: TEST_ARTICLE, |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + const TEST_COW_PARENT = { |
| 86 | + id: 10, |
| 87 | + earTagId: 100, |
| 88 | + name: 'cowparentname200', |
| 89 | + visibility: Visibility.RoleAnonymous, |
| 90 | + } as CowVo; |
| 91 | + const API_COWPARENT_RESPONSE$ = of({ |
| 92 | + data: { |
| 93 | + cowVO: TEST_COW_PARENT, |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + beforeEach(() => { |
| 98 | + TestBed.configureTestingModule({ |
| 99 | + imports: [BonGatewayTestModule, RouterTestingModule, ApolloModule], |
| 100 | + declarations: [SireDetailsComponent, JhiTranslateDirective, BonVisibilityClassDirective, CowPictureDirective], |
| 101 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 102 | + providers: [ |
| 103 | + { |
| 104 | + provide: ActivatedRoute, |
| 105 | + useValue: ({ |
| 106 | + data: of({ cowVo: TEST_COW }), |
| 107 | + } as any) as ActivatedRoute, |
| 108 | + }, |
| 109 | + ], |
| 110 | + }).compileComponents(); |
| 111 | + |
| 112 | + fixture = TestBed.createComponent(SireDetailsComponent); |
| 113 | + comp = fixture.componentInstance; |
| 114 | + getArticleGQL = fixture.debugElement.injector.get(GetArticleGQL); |
| 115 | + getCowGQL = fixture.debugElement.injector.get(GetCowGQL); |
| 116 | + findCowPicturesGQL = fixture.debugElement.injector.get(FindCowPicturesGQL); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should find sire on load', () => { |
| 120 | + // GIVEN |
| 121 | + spyOn(findCowPicturesGQL, 'fetch').and.returnValue(API_PICTURES_RESPONSE$); |
| 122 | + spyOn(getArticleGQL, 'fetch').and.returnValue(API_ARTICLE_RESPONSE$); |
| 123 | + spyOn(getCowGQL, 'fetch').and.returnValue(API_COWPARENT_RESPONSE$); |
| 124 | + |
| 125 | + // WHEN |
| 126 | + comp.ngOnInit(); |
| 127 | + fixture.detectChanges(); |
| 128 | + |
| 129 | + // THEN |
| 130 | + const sire = comp.sire!; |
| 131 | + expect(sire.earTagId).toEqual(TEST_COW.earTagId); |
| 132 | + expect(sire.visibility).toEqual(TEST_COW.visibility); |
| 133 | + |
| 134 | + const liElements = fixture.debugElement.queryAll(By.css('.sire-item')); |
| 135 | + expect(liElements.length).toBe(1); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should find sire photos on load', done => { |
| 139 | + // GIVEN |
| 140 | + spyOn(findCowPicturesGQL, 'fetch').and.returnValue(API_PICTURES_RESPONSE$); |
| 141 | + spyOn(getArticleGQL, 'fetch').and.returnValue(API_ARTICLE_RESPONSE$); |
| 142 | + spyOn(getCowGQL, 'fetch').and.returnValue(API_COWPARENT_RESPONSE$); |
| 143 | + |
| 144 | + // WHEN |
| 145 | + comp.ngOnInit(); |
| 146 | + fixture.detectChanges(); |
| 147 | + |
| 148 | + // THEN |
| 149 | + expect(findCowPicturesGQL.fetch).toHaveBeenCalled(); |
| 150 | + comp.pictures$!.subscribe(pics => { |
| 151 | + expect(pics).not.toBeNull(); |
| 152 | + expect(pics?.length).toEqual(1); |
| 153 | + const pic = pics ? pics[0] : null; |
| 154 | + expect(pic).not.toBeNull(); |
| 155 | + expect(pic?.sources?.length).toEqual(1); |
| 156 | + const ps = pic?.sources ? pic?.sources[0] : null; |
| 157 | + expect(ps?.contentType).toEqual(TEST_PICTURE_SOURCE.contentType); |
| 158 | + done(); |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should not find sire photos so use fallback image on load', done => { |
| 163 | + // GIVEN |
| 164 | + spyOn(findCowPicturesGQL, 'fetch').and.returnValue( |
| 165 | + of({ |
| 166 | + data: { |
| 167 | + apiPublicCowsPictures: [], |
| 168 | + }, |
| 169 | + }) |
| 170 | + ); |
| 171 | + spyOn(getArticleGQL, 'fetch').and.returnValue(API_ARTICLE_RESPONSE$); |
| 172 | + spyOn(getCowGQL, 'fetch').and.returnValue(API_COWPARENT_RESPONSE$); |
| 173 | + |
| 174 | + // WHEN |
| 175 | + comp.ngOnInit(); |
| 176 | + fixture.detectChanges(); |
| 177 | + |
| 178 | + // THEN |
| 179 | + expect(findCowPicturesGQL.fetch).toHaveBeenCalled(); |
| 180 | + comp.pictures$!.subscribe(pics => { |
| 181 | + expect(pics).not.toBeNull(); |
| 182 | + expect(pics?.length).toEqual(1); |
| 183 | + const pic = pics ? pics[0] : null; |
| 184 | + expect(pic).not.toBeNull(); |
| 185 | + done(); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should find sire mother on load', done => { |
| 190 | + // GIVEN |
| 191 | + spyOn(findCowPicturesGQL, 'fetch').and.returnValue(API_PICTURES_RESPONSE$); |
| 192 | + spyOn(getArticleGQL, 'fetch').and.returnValue(API_ARTICLE_RESPONSE$); |
| 193 | + spyOn(getCowGQL, 'fetch').and.returnValue(API_COWPARENT_RESPONSE$); |
| 194 | + |
| 195 | + // WHEN |
| 196 | + comp.ngOnInit(); |
| 197 | + fixture.detectChanges(); |
| 198 | + |
| 199 | + // THEN |
| 200 | + expect(getCowGQL.fetch).toHaveBeenCalled(); |
| 201 | + comp.matri$!.subscribe(matri => { |
| 202 | + expect(matri?.name).toEqual(TEST_COW_PARENT.name); |
| 203 | + done(); |
| 204 | + }); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should find sire father on load', done => { |
| 208 | + // GIVEN |
| 209 | + spyOn(findCowPicturesGQL, 'fetch').and.returnValue(API_PICTURES_RESPONSE$); |
| 210 | + spyOn(getArticleGQL, 'fetch').and.returnValue(API_ARTICLE_RESPONSE$); |
| 211 | + spyOn(getCowGQL, 'fetch').and.returnValue(API_COWPARENT_RESPONSE$); |
| 212 | + |
| 213 | + // WHEN |
| 214 | + comp.ngOnInit(); |
| 215 | + fixture.detectChanges(); |
| 216 | + |
| 217 | + // THEN |
| 218 | + expect(getCowGQL.fetch).toHaveBeenCalled(); |
| 219 | + comp.patri$!.subscribe(patri => { |
| 220 | + expect(patri?.name).toEqual(TEST_COW_PARENT.name); |
| 221 | + done(); |
| 222 | + }); |
| 223 | + }); |
| 224 | + }); |
| 225 | + |
| 226 | + describe('DamDetailsComponent with less data', () => { |
| 227 | + let comp: SireDetailsComponent; |
| 228 | + let fixture: ComponentFixture<SireDetailsComponent>; |
| 229 | + let getArticleGQL: GetArticleGQL; |
| 230 | + let getCowGQL: GetCowGQL; |
| 231 | + let findCowPicturesGQL: FindCowPicturesGQL; |
| 232 | + |
| 233 | + const TEST_COW = { |
| 234 | + id: 100, |
| 235 | + earTagId: 200, |
| 236 | + name: 'cowname200', |
| 237 | + visibility: Visibility.RoleAnonymous, |
| 238 | + storyHandle: null, |
| 239 | + matriId: null, |
| 240 | + patriId: null, |
| 241 | + } as CowVo; |
| 242 | + |
| 243 | + const API_PICTURES_RESPONSE$ = of({ |
| 244 | + data: { |
| 245 | + apiPublicCowsPictures: [], |
| 246 | + }, |
| 247 | + }); |
| 248 | + |
| 249 | + const API_ARTICLE_RESPONSE$ = of({ |
| 250 | + data: { |
| 251 | + articleVO: null, |
| 252 | + }, |
| 253 | + }); |
| 254 | + |
| 255 | + const API_COWPARENT_RESPONSE$ = of({ |
| 256 | + data: { |
| 257 | + cowVO: null, |
| 258 | + }, |
| 259 | + }); |
| 260 | + |
| 261 | + beforeEach(() => { |
| 262 | + TestBed.configureTestingModule({ |
| 263 | + imports: [BonGatewayTestModule, RouterTestingModule, ApolloModule], |
| 264 | + declarations: [SireDetailsComponent, JhiTranslateDirective, BonVisibilityClassDirective, CowPictureDirective], |
| 265 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 266 | + providers: [ |
| 267 | + { |
| 268 | + provide: ActivatedRoute, |
| 269 | + useValue: ({ |
| 270 | + data: of({ cowVo: TEST_COW }), |
| 271 | + } as any) as ActivatedRoute, |
| 272 | + }, |
| 273 | + ], |
| 274 | + }).compileComponents(); |
| 275 | + |
| 276 | + fixture = TestBed.createComponent(SireDetailsComponent); |
| 277 | + comp = fixture.componentInstance; |
| 278 | + getArticleGQL = fixture.debugElement.injector.get(GetArticleGQL); |
| 279 | + getCowGQL = fixture.debugElement.injector.get(GetCowGQL); |
| 280 | + findCowPicturesGQL = fixture.debugElement.injector.get(FindCowPicturesGQL); |
| 281 | + }); |
| 282 | + |
| 283 | + it('should not query article on load', () => { |
| 284 | + // GIVEN |
| 285 | + spyOn(findCowPicturesGQL, 'fetch').and.returnValue(API_PICTURES_RESPONSE$); |
| 286 | + spyOn(getArticleGQL, 'fetch').and.returnValue(API_ARTICLE_RESPONSE$); |
| 287 | + spyOn(getCowGQL, 'fetch').and.returnValue(API_COWPARENT_RESPONSE$); |
| 288 | + |
| 289 | + // WHEN |
| 290 | + comp.ngOnInit(); |
| 291 | + fixture.detectChanges(); |
| 292 | + |
| 293 | + // THEN |
| 294 | + expect(getCowGQL.fetch).not.toHaveBeenCalled(); |
| 295 | + }); |
| 296 | + |
| 297 | + it('should not query mother or father on load', () => { |
| 298 | + // GIVEN |
| 299 | + spyOn(findCowPicturesGQL, 'fetch').and.returnValue(API_PICTURES_RESPONSE$); |
| 300 | + spyOn(getArticleGQL, 'fetch').and.returnValue(API_ARTICLE_RESPONSE$); |
| 301 | + spyOn(getCowGQL, 'fetch').and.returnValue(API_COWPARENT_RESPONSE$); |
| 302 | + |
| 303 | + // WHEN |
| 304 | + comp.ngOnInit(); |
| 305 | + fixture.detectChanges(); |
| 306 | + |
| 307 | + // THEN |
| 308 | + expect(getCowGQL.fetch).not.toHaveBeenCalled(); |
| 309 | + }); |
| 310 | + }); |
| 311 | +}); |
0 commit comments