diff --git a/src/app/share-preview/components/share-preview/share-preview.component.html b/src/app/share-preview/components/share-preview/share-preview.component.html index 3c71b8c15..91fc25705 100644 --- a/src/app/share-preview/components/share-preview/share-preview.component.html +++ b/src/app/share-preview/components/share-preview/share-preview.component.html @@ -327,7 +327,7 @@ diff --git a/src/app/share-preview/components/share-preview/share-preview.component.spec.ts b/src/app/share-preview/components/share-preview/share-preview.component.spec.ts index c5a4e0623..2d0af6e97 100644 --- a/src/app/share-preview/components/share-preview/share-preview.component.spec.ts +++ b/src/app/share-preview/components/share-preview/share-preview.component.spec.ts @@ -20,6 +20,10 @@ import { DialogCdkService } from '@root/app/dialog-cdk/dialog-cdk.service'; import { AccountVO, ArchiveVO, RecordVO } from '@root/app/models'; import { AuthResponse } from '@shared/services/api/auth.repo'; import { Subject } from 'rxjs'; +import { ShareLinksService } from '@root/app/share-links/services/share-links.service'; +import { ApiService } from '@shared/services/api/api.service'; +import { GoogleAnalyticsService } from '@shared/services/google-analytics/google-analytics.service'; +import { ShareResponse } from '@shared/services/api/share.repo'; import { CreateAccountDialogComponent } from '../create-account-dialog/create-account-dialog.component'; import { SharePreviewComponent } from './share-preview.component'; @@ -40,6 +44,10 @@ export const mockAccountService = jasmine.createSpyObj('AccountService', [ const defaultAccount = new AccountVO({ primaryEmail: 'test@example.com' }); const defaultArchive = new ArchiveVO({ archiveId: 123 }); +const mockGoogleAnalyticsService = { + sendEvent: jasmine.createSpy(), +}; + mockAccountService.getAccount.and.returnValue(defaultAccount); mockAccountService.getArchive.and.returnValue(defaultArchive); mockAccountService.isLoggedIn.and.returnValue(true); @@ -59,11 +67,17 @@ mockAccountService.setRedirect.and.stub(); mockAccountService.archiveChange = new Subject(); mockAccountService.accountChange = new Subject(); +const mockShareLinksService = { + currentShareToken: null, + isUnlistedShare: () => true, +}; + describe('SharePreviewComponent', () => { let component: SharePreviewComponent; let fixture: ComponentFixture; let dialog: DialogCdkService; let router: Router; + let apiService: ApiService; beforeEach(async () => { const config: TestModuleMetadata = cloneDeep(Testing.BASE_TEST_CONFIG); @@ -98,10 +112,21 @@ describe('SharePreviewComponent', () => { useValue: mockRoute, }); + config.providers.push({ + provide: ShareLinksService, + useValue: mockShareLinksService, + }); + + config.providers.push({ + provide: GoogleAnalyticsService, + useValue: mockGoogleAnalyticsService, + }); + await TestBed.configureTestingModule(config).compileComponents(); dialog = TestBed.inject(DialogCdkService); router = TestBed.inject(Router); + apiService = TestBed.inject(ApiService); spyOn(router, 'navigate'); fixture = TestBed.createComponent(SharePreviewComponent); @@ -113,11 +138,22 @@ describe('SharePreviewComponent', () => { expect(component).toBeTruthy(); }); - it('should open dialog shortly after loading if user is logged out', fakeAsync(() => { + it('should mark it as unlisted share if restrictions are none', fakeAsync(() => { + spyOn(mockShareLinksService, 'isUnlistedShare').and.returnValue(true); + component.ngOnInit(); + + expect(mockShareLinksService.isUnlistedShare).toHaveBeenCalled(); + tick(1005); + + expect(component.isUnlistedShare).toEqual(true); + })); + + it('should open dialog shortly after loading if user is logged out and it is not an unlisted share', fakeAsync(() => { const dialogRefSpy = jasmine.createSpyObj('DialogRef', ['close']); const dialogSpy = spyOn(dialog, 'open').and.returnValue(dialogRefSpy); component.isLoggedIn = false; + component.isUnlistedShare = false; component.showCreateAccountDialog(); tick(1005); @@ -126,23 +162,48 @@ describe('SharePreviewComponent', () => { }); })); - it('should not open dialog if already open', () => { - const dialogSpy = spyOn(dialog, 'open'); - component.createAccountDialogIsOpen = true; + it('should not open dialog if user is logged out, but it is an unlisted share', fakeAsync(() => { + const dialogRefSpy = jasmine.createSpyObj('DialogRef', ['close']); + const dialogSpy = spyOn(dialog, 'open').and.returnValue(dialogRefSpy); - component.showCreateAccountDialog(); + component.isLoggedIn = false; + component.isUnlistedShare = true; + component.ngOnInit(); + tick(1005); expect(dialogSpy).not.toHaveBeenCalled(); - }); + })); + + it('should not open dialog if user is logged in and it is not an unlisted share', fakeAsync(() => { + const dialogRefSpy = jasmine.createSpyObj('DialogRef', ['close']); + const dialogSpy = spyOn(dialog, 'open').and.returnValue(dialogRefSpy); + + component.isLoggedIn = true; + component.isUnlistedShare = false; + component.ngOnInit(); + tick(1005); + + expect(dialogSpy).not.toHaveBeenCalled(); + })); - it('should not open dialog shortly after loading if user is logged in', fakeAsync(() => { + it('should not open dialog shortly after loading if user is logged in and share is unlisted', fakeAsync(() => { const dialogSpy = spyOn(dialog, 'open'); component.isLoggedIn = true; + component.isUnlistedShare = true; tick(1005); expect(dialogSpy).not.toHaveBeenCalled(); })); + it('should not open dialog if already open', () => { + const dialogSpy = spyOn(dialog, 'open'); + component.createAccountDialogIsOpen = true; + + component.showCreateAccountDialog(); + + expect(dialogSpy).not.toHaveBeenCalled(); + }); + it('should open dialog when a thumbnail is clicked', fakeAsync(() => { const dialogRefSpy = jasmine.createSpyObj('DialogRef', ['close']); const dialogSpy = spyOn(dialog, 'open').and.returnValue(dialogRefSpy); @@ -212,4 +273,61 @@ describe('SharePreviewComponent', () => { expect(router.navigate).toHaveBeenCalledWith(['/app', 'auth', 'signup']); }); + + it('should reload share preview data for link share', fakeAsync(() => { + component.isLinkShare = true; + component.isRelationshipShare = false; + + const mockVO = { ShareVO: { status: 'ok', accessRole: 'editor' } }; + spyOn(apiService.share, 'checkShareLink').and.returnValue( + Promise.resolve({ + isSuccessful: true, + getShareByUrlVO: () => mockVO, + } as unknown as ShareResponse), + ); + + spyOn(component, 'checkAccess'); + + component.reloadSharePreviewData(); + tick(1005); + + expect(apiService.share.checkShareLink).toHaveBeenCalled(); + expect(component.sharePreviewVO).toEqual(mockVO); + expect(component.checkAccess).toHaveBeenCalled(); + })); + + it('should reload share preview data for relationship share', fakeAsync(() => { + const mockVO = { ShareVO: { status: 'ok', accessRole: 'owner' } }; + spyOn(apiService.share, 'getShareForPreview').and.returnValue( + Promise.resolve({ + getShareVO: () => mockVO, + } as unknown as ShareResponse), + ); + component.isLinkShare = false; + component.isRelationshipShare = true; + + spyOn(component, 'checkAccess'); + + component.reloadSharePreviewData(); + tick(1005); + + expect(apiService.share.getShareForPreview).toHaveBeenCalled(); + expect(component.sharePreviewVO).toEqual(mockVO); + expect(component.checkAccess).toHaveBeenCalled(); + })); + + it('should request access and not show cover', fakeAsync(() => { + component.archiveConfirmed = false; + component.chooseArchiveText = 'Choose archive'; + component.shareToken = 'mock-token'; + component.shareAccount = { + fullName: 'Sharer Name', + } as unknown as AccountVO; + + component.onRequestAccessClick(); + tick(2005); + + expect(component.hasRequested).toBeTrue(); + expect(component.showCover).toBeFalse(); + })); }); diff --git a/src/app/share-preview/components/share-preview/share-preview.component.ts b/src/app/share-preview/components/share-preview/share-preview.component.ts index 343ad38d5..623c7630a 100644 --- a/src/app/share-preview/components/share-preview/share-preview.component.ts +++ b/src/app/share-preview/components/share-preview/share-preview.component.ts @@ -196,7 +196,7 @@ export class SharePreviewComponent implements OnInit, OnDestroy { this.sendGaEvent('previewed'); } - if (!this.isLoggedIn) { + if (!this.isLoggedIn && !this.isUnlistedShare) { setTimeout(() => { this.showCreateAccountDialog(); }, 1000);