Skip to content

Commit

Permalink
Merge pull request #483 from PermanentOrg/fix-creationsharetoken-tests
Browse files Browse the repository at this point in the history
Fix Glam Onboarding With Shares tests
  • Loading branch information
meisekimiu authored Oct 29, 2024
2 parents 6f8adc6 + e9659e2 commit 08edd95
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 82 deletions.
15 changes: 14 additions & 1 deletion src/app/models/invite-vo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
/* @format */
import { BaseVO } from '@models/base-vo';
import { ArchiveVO, AccountVO, RecordVO, FolderVO, ShareVO } from '.';
import { AccountVOData } from './account-vo';
import {
ArchiveVO,
AccountVO,
RecordVO,
FolderVO,
ShareVO,
RecordVOData,
FolderVOData,
} from '.';

type InviteStatusType =
| 'status.invite.pending'
Expand Down Expand Up @@ -59,4 +68,8 @@ export interface InviteVOData {

byArchiveId?: number;
accessRole?: string;

AccountVO?: AccountVOData;
RecordVO?: RecordVOData;
FolderVO?: FolderVOData;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,106 +2,105 @@
import { Shallow } from 'shallow-render';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ApiService } from '@shared/services/api/api.service';
import { InviteVO, InviteVOData } from '@models/invite-vo';
import { InviteResponse } from '@shared/services/api/invite.repo';
import { OnboardingModule } from '../../onboarding.module';
import { ArchiveCreationWithShareComponent } from './archive-creation-with-share.component';

class MockInviteApiResponse extends InviteResponse {
public inviteVo: InviteVO | undefined;

constructor(invite: InviteVOData) {
super({});
this.inviteVo = new InviteVO(invite);
}

public getInviteVO(): InviteVO {
return this.inviteVo;
}
}

class MockInviteRepo {
public inviteVo: InviteVOData = {};
protected token: string = null;

public resetToken(): void {
this.token = null;
}

public getToken(): string {
return this.token;
}

public async getFullShareInvite(token: string): Promise<InviteResponse> {
this.token = token;
return new MockInviteApiResponse(this.inviteVo);
}
}

describe('ArchiveCreationWithShareToken', () => {
let shallow: Shallow<ArchiveCreationWithShareComponent>;
let mockInvite: MockInviteRepo;

function setLocalStorage(token: string) {
spyOn(localStorage, 'getItem').and.returnValue(token);
}

beforeEach(() => {
shallow = new Shallow(
ArchiveCreationWithShareComponent,
OnboardingModule,
).import(HttpClientTestingModule);
mockInvite = new MockInviteRepo();

shallow = new Shallow(ArchiveCreationWithShareComponent, OnboardingModule)
.provideMock({
provide: ApiService,
useValue: { invite: mockInvite },
})
.import(HttpClientTestingModule);
});

it('should create', async () => {
setLocalStorage(null);
const { instance } = await shallow.render();

expect(instance).toBeTruthy();
});

it('should fetch invite data and set sharer and shared item names when shareToken is present', async () => {
const mockApi = {
invite: {
getFullShareInvite: jasmine.createSpy().and.returnValue(
Promise.resolve({
getInviteVO: () => ({
AccountVO: { fullName: 'Sharer Name' },
RecordVO: { displayName: 'Shared Item Name' },
}),
}),
),
},
mockInvite.inviteVo = {
AccountVO: { fullName: 'Sharer Name' },
RecordVO: { displayName: 'Shared Item Name' },
};
setLocalStorage('shareToken');

const { instance, fixture } = await shallow
.mock(ApiService, mockApi)
.render();

spyOn(localStorage, 'getItem').and.returnValue('shareToken');
const { instance, fixture } = await shallow.render();

instance.ngOnInit();
await fixture.whenStable();

expect(mockApi.invite.getFullShareInvite).toHaveBeenCalledWith(
'shareToken',
);

expect(mockInvite.getToken()).toBe('shareToken');
expect(instance.sharerName).toBe('Sharer Name');
expect(instance.sharedItemName).toBe('Shared Item Name');
});

it('should not fetch invite data if no shareToken is present', async () => {
const mockApi = {
invite: {
getFullShareInvite: jasmine.createSpy(),
},
};

const { instance, fixture } = await shallow
.mock(ApiService, mockApi)
.render();

spyOn(localStorage, 'getItem').and.returnValue(null);
setLocalStorage(null);
const { instance, fixture } = await shallow.render();

instance.ngOnInit();
await fixture.whenStable();

expect(mockApi.invite.getFullShareInvite).not.toHaveBeenCalled();
expect(instance.sharerName).toBeUndefined();
expect(instance.sharedItemName).toBeUndefined();
});

it('should not set sharerName or sharedItemName if no token is present', async () => {
const { instance } = await shallow.render();
spyOn(localStorage, 'getItem').and.returnValue(null);

instance.ngOnInit();

expect(mockInvite.getToken()).toBeNull();
expect(instance.sharerName).toBeUndefined();
expect(instance.sharedItemName).toBeUndefined();
});

it('should display the record icon if the shared item is a record', async () => {
const mockApi = {
invite: {
getFullShareInvite: jasmine.createSpy().and.returnValue(
Promise.resolve({
getInviteVO: () => ({
AccountVO: { fullName: 'Sharer Name' },
RecordVO: { displayName: 'Shared Item Name' },
}),
}),
),
},
mockInvite.inviteVo = {
AccountVO: { fullName: 'Sharer Name' },
RecordVO: { displayName: 'Shared Item Name' },
};
setLocalStorage('shareToken');

const { instance, fixture } = await shallow
.mock(ApiService, mockApi)
.render();

spyOn(localStorage, 'getItem').and.returnValue('shareToken');
const { instance, fixture } = await shallow.render();

instance.ngOnInit();
await fixture.whenStable();
Expand All @@ -110,28 +109,17 @@ describe('ArchiveCreationWithShareToken', () => {
});

it('should display the folder icon if the shared item is a folder', async () => {
const mockApi = {
invite: {
getFullShareInvite: jasmine.createSpy().and.returnValue(
Promise.resolve({
getInviteVO: () => ({
AccountVO: { fullName: 'Sharer Name' },
RecordVO: null,
}),
}),
),
},
mockInvite.inviteVo = {
AccountVO: { fullName: 'Sharer Name' },
FolderVO: { displayName: 'potato' },
};
setLocalStorage('shareToken');

const { instance, fixture } = await shallow
.mock(ApiService, mockApi)
.render();

spyOn(localStorage, 'getItem').and.returnValue('shareToken');
const { instance, fixture } = await shallow.render();

instance.ngOnInit();
await fixture.whenStable();

expect(instance.isFolder).toBe(false);
expect(instance.isFolder).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ describe('ArchiveCreationStartScreenComponent', () => {
});

it('should not set hasShareToken if shareToken does not exist in localStorage', async () => {
const { instance, fixture } = await shallow.render();

spyOn(localStorage, 'getItem').and.returnValue(null);
const { instance, fixture } = await shallow.render();

instance.ngOnInit();
fixture.detectChanges();
Expand Down

0 comments on commit 08edd95

Please sign in to comment.