From bf90e6f8582871f858aed8f55dbe5be8f578f267 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sat, 13 Jan 2024 01:04:54 -0800 Subject: [PATCH] Update tests --- .../document-detail.component.spec.ts | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts index d37fa852a..099823c25 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts @@ -254,9 +254,6 @@ describe('DocumentDetailComponent', () => { router = TestBed.inject(Router) activatedRoute = TestBed.inject(ActivatedRoute) - jest - .spyOn(activatedRoute, 'paramMap', 'get') - .mockReturnValue(of(convertToParamMap({ id: 3 }))) openDocumentsService = TestBed.inject(OpenDocumentsService) documentService = TestBed.inject(DocumentService) modalService = TestBed.inject(NgbModal) @@ -294,6 +291,17 @@ describe('DocumentDetailComponent', () => { expect(navigateSpy).toHaveBeenCalledWith(['documents', 3, 'notes']) }) + it('should forward id without section to details', () => { + const navigateSpy = jest.spyOn(router, 'navigate') + jest + .spyOn(activatedRoute, 'paramMap', 'get') + .mockReturnValue(of(convertToParamMap({ id: 3 }))) + fixture.detectChanges() + expect(navigateSpy).toHaveBeenCalledWith(['documents', 3, 'details'], { + replaceUrl: true, + }) + }) + it('should update title after debounce', fakeAsync(() => { initNormally() component.titleInput.value = 'Foo Bar' @@ -319,6 +327,7 @@ describe('DocumentDetailComponent', () => { }) it('should load already-opened document via param', () => { + initNormally() jest.spyOn(documentService, 'get').mockReturnValueOnce(of(doc)) jest.spyOn(openDocumentsService, 'getOpenDocument').mockReturnValue(doc) jest.spyOn(customFieldsService, 'listAll').mockReturnValue( @@ -399,8 +408,11 @@ describe('DocumentDetailComponent', () => { }) it('should 404 on invalid id', () => { - jest.spyOn(documentService, 'get').mockReturnValueOnce(of(null)) const navigateSpy = jest.spyOn(router, 'navigate') + jest + .spyOn(activatedRoute, 'paramMap', 'get') + .mockReturnValue(of(convertToParamMap({ id: 999, section: 'details' }))) + jest.spyOn(documentService, 'get').mockReturnValueOnce(of(null)) fixture.detectChanges() expect(navigateSpy).toHaveBeenCalledWith(['404'], { replaceUrl: true }) }) @@ -935,7 +947,29 @@ describe('DocumentDetailComponent', () => { expect(refreshSpy).toHaveBeenCalled() }) + it('should get suggestions', () => { + const suggestionsSpy = jest.spyOn(documentService, 'getSuggestions') + suggestionsSpy.mockReturnValue(of({ tags: [1, 2] })) + initNormally() + expect(suggestionsSpy).toHaveBeenCalled() + expect(component.suggestions).toEqual({ tags: [1, 2] }) + }) + + it('should show error if needed for get suggestions', () => { + const suggestionsSpy = jest.spyOn(documentService, 'getSuggestions') + const errorSpy = jest.spyOn(toastService, 'showError') + suggestionsSpy.mockImplementationOnce(() => + throwError(() => new Error('failed to get suggestions')) + ) + initNormally() + expect(suggestionsSpy).toHaveBeenCalled() + expect(errorSpy).toHaveBeenCalled() + }) + function initNormally() { + jest + .spyOn(activatedRoute, 'paramMap', 'get') + .mockReturnValue(of(convertToParamMap({ id: 3, section: 'details' }))) jest .spyOn(documentService, 'get') .mockReturnValueOnce(of(Object.assign({}, doc)))