-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from PermanentOrg/featured-archives-api
Fetch featured archives from API
- Loading branch information
Showing
19 changed files
with
398 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 12 additions & 7 deletions
19
src/app/gallery/components/featured-archive/featured-archive.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/app/gallery/components/featured-archive/featured-archive.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* @format */ | ||
import { Shallow } from 'shallow-render'; | ||
import { ArchiveType } from '@models/archive-vo'; | ||
import { FeaturedArchive } from '../../types/featured-archive'; | ||
import { GalleryModule } from '../../gallery.module'; | ||
import { FeaturedArchiveComponent } from './featured-archive.component'; | ||
|
||
const testArchive: FeaturedArchive = { | ||
archiveNbr: '0000-0000', | ||
name: 'Unit Testing', | ||
type: 'type.archive.person', | ||
profileImage: 'thumbUrl', | ||
bannerImage: 'bannerUrl', | ||
}; | ||
|
||
describe('FeaturedArchiveComponent', () => { | ||
let shallow: Shallow<FeaturedArchiveComponent>; | ||
|
||
const defaultRender = async () => | ||
await shallow.render( | ||
'<pr-featured-archive [archive]="archive"></pr-featured-archive>', | ||
{ | ||
bind: { | ||
archive: testArchive, | ||
}, | ||
} | ||
); | ||
|
||
beforeEach(() => { | ||
shallow = new Shallow(FeaturedArchiveComponent, GalleryModule); | ||
}); | ||
|
||
it('should exist', async () => { | ||
const { instance } = await defaultRender(); | ||
|
||
expect(instance).toBeTruthy(); | ||
}); | ||
|
||
it('should include all archive information', async () => { | ||
const { find, element } = await defaultRender(); | ||
|
||
expect(find('.profile-pic img').attributes.src).toBe('thumbUrl'); | ||
expect(element.nativeElement.innerText).toContain( | ||
'The Unit Testing Archive' | ||
); | ||
}); | ||
|
||
it('should be able to get proper classnames', async () => { | ||
const { instance } = await defaultRender(); | ||
function expectClassnameForArchiveType( | ||
archiveType: ArchiveType, | ||
expectedClassname: string | ||
) { | ||
instance.archive.type = archiveType; | ||
instance.ngOnInit(); | ||
|
||
expect(instance.classNames).toContain(expectedClassname); | ||
} | ||
|
||
expectClassnameForArchiveType('type.archive.person', 'personal'); | ||
expectClassnameForArchiveType('type.archive.family', 'group'); | ||
expectClassnameForArchiveType('type.archive.organization', 'organization'); | ||
expectClassnameForArchiveType('type.archive.nonprofit', 'organization'); | ||
}); | ||
|
||
describe('Accessibility', () => { | ||
it('has alt text for all img tags', async () => { | ||
const { find } = await defaultRender(); | ||
const images = find('img'); | ||
images.forEach(() => { | ||
expect(images.attributes.alt).not.toBeUndefined(); | ||
}); | ||
}); | ||
|
||
it('has specific label text for each link', async () => { | ||
const { find } = await defaultRender(); | ||
const link = find('a'); | ||
|
||
expect(link.attributes['aria-label']).toContain('Unit Testing'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/app/gallery/components/gallery/gallery.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* @format */ | ||
import { Shallow } from 'shallow-render'; | ||
import { AccountService } from '@shared/services/account/account.service'; | ||
import { FeaturedArchive } from '../../types/featured-archive'; | ||
import { | ||
FEATURED_ARCHIVE_API, | ||
FeaturedArchiveApi, | ||
} from '../../types/featured-archive-api'; | ||
import { GalleryModule } from '../../gallery.module'; | ||
import { GalleryComponent } from './gallery.component'; | ||
|
||
class DummyFeaturedArchiveAPI implements FeaturedArchiveApi { | ||
public static failRequest = false; | ||
public static FeaturedArchives: FeaturedArchive[] = []; | ||
public static reset(): void { | ||
DummyFeaturedArchiveAPI.FeaturedArchives = []; | ||
DummyFeaturedArchiveAPI.failRequest = false; | ||
} | ||
|
||
public fetchedFromApi: boolean = false; | ||
|
||
public async getFeaturedArchiveList(): Promise<FeaturedArchive[]> { | ||
if (DummyFeaturedArchiveAPI.failRequest) { | ||
throw new Error('Forced unit test error'); | ||
} | ||
this.fetchedFromApi = true; | ||
return DummyFeaturedArchiveAPI.FeaturedArchives; | ||
} | ||
} | ||
|
||
class DummyAccountService { | ||
public static loggedIn: boolean = false; | ||
|
||
public isLoggedIn(): boolean { | ||
return DummyAccountService.loggedIn; | ||
} | ||
} | ||
|
||
const testArchive: FeaturedArchive = { | ||
archiveNbr: '0000-0000', | ||
name: 'Unit Testing', | ||
type: 'type.archive.person', | ||
profileImage: 'thumbUrl', | ||
bannerImage: 'bannerUrl', | ||
} as const; | ||
|
||
describe('GalleryComponent', () => { | ||
let shallow: Shallow<GalleryComponent>; | ||
let dummyApi: DummyFeaturedArchiveAPI; | ||
let dummyAccount: DummyAccountService; | ||
|
||
beforeEach(async () => { | ||
DummyFeaturedArchiveAPI.reset(); | ||
DummyAccountService.loggedIn = false; | ||
dummyApi = new DummyFeaturedArchiveAPI(); | ||
dummyAccount = new DummyAccountService(); | ||
shallow = new Shallow(GalleryComponent, GalleryModule); | ||
shallow | ||
.provide({ | ||
provide: FEATURED_ARCHIVE_API, | ||
useValue: dummyApi, | ||
}) | ||
.provide({ | ||
provide: AccountService, | ||
useValue: dummyAccount, | ||
}); | ||
shallow.dontMock(FEATURED_ARCHIVE_API, AccountService); | ||
}); | ||
|
||
it('should fetch featured archives from the API', async () => { | ||
await shallow.render(); | ||
|
||
expect(dummyApi.fetchedFromApi).toBeTrue(); | ||
}); | ||
|
||
it('displays the list of featured archives', async () => { | ||
DummyFeaturedArchiveAPI.FeaturedArchives = [testArchive]; | ||
const { fixture, find } = await shallow.render(); | ||
await fixture.whenStable(); | ||
|
||
expect(find('pr-featured-archive').length).toBe(1); | ||
}); | ||
|
||
it('does not display the error message while loading the archives', async () => { | ||
DummyFeaturedArchiveAPI.FeaturedArchives = [testArchive]; | ||
const { find, instance } = await shallow.render(); | ||
instance.loading = true; | ||
|
||
expect(find('.null-message').length).toBe(0); | ||
}); | ||
|
||
it('displays an error message if no featured archives exist', async () => { | ||
const { find } = await shallow.render(); | ||
|
||
expect(find('pr-featured-archive').length).toBe(0); | ||
expect(find('.null-message').length).toBe(1); | ||
}); | ||
|
||
it('displays an error message if the fetch failed', async () => { | ||
DummyFeaturedArchiveAPI.FeaturedArchives = [testArchive]; | ||
DummyFeaturedArchiveAPI.failRequest = true; | ||
const { find } = await shallow.render(); | ||
|
||
expect(find('.null-message').length).toBe(1); | ||
}); | ||
|
||
it("does not display the user's public archives list if logged out", async () => { | ||
const { find } = await shallow.render(); | ||
|
||
expect(find('pr-public-archives-list').length).toBe(0); | ||
}); | ||
|
||
it("displays the user's public archives list if logged in", async () => { | ||
DummyAccountService.loggedIn = true; | ||
const { find } = await shallow.render(); | ||
|
||
expect(find('pr-public-archives-list').length).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,36 @@ | ||
/* @format */ | ||
import { Component, Inject, OnInit } from '@angular/core'; | ||
import { AccountService } from '@shared/services/account/account.service'; | ||
import { Component, OnInit } from '@angular/core'; | ||
import { environment } from '@root/environments/environment'; | ||
import { SecretsService } from '@shared/services/secrets/secrets.service'; | ||
import { FeaturedArchive } from '../../types/featured-archive'; | ||
import { featuredArchives } from '../../data/featured'; | ||
import { | ||
FEATURED_ARCHIVE_API, | ||
FeaturedArchiveApi, | ||
} from '../../types/featured-archive-api'; | ||
|
||
@Component({ | ||
selector: 'pr-gallery', | ||
templateUrl: './gallery.component.html', | ||
styleUrls: ['./gallery.component.scss'], | ||
}) | ||
export class GalleryComponent implements OnInit { | ||
public environment: string = ''; | ||
public archives: FeaturedArchive[] = this.getFeaturedArchives(); | ||
public isLoggedIn: boolean; | ||
constructor(private accountService:AccountService) { | ||
public archives: FeaturedArchive[] = []; | ||
public loading = true; | ||
|
||
constructor( | ||
@Inject(FEATURED_ARCHIVE_API) private api: FeaturedArchiveApi, | ||
private accountService: AccountService | ||
) { | ||
this.isLoggedIn = this.accountService.isLoggedIn(); | ||
} | ||
|
||
ngOnInit(): void {} | ||
|
||
protected getFeaturedArchives(): FeaturedArchive[] { | ||
this.environment = environment.environment; | ||
if ( | ||
this.environment !== 'prod' && | ||
SecretsService.hasStatic('FEATURED_ARCHIVES') | ||
) { | ||
const envFeatured = SecretsService.getStatic('FEATURED_ARCHIVES'); | ||
if (envFeatured) { | ||
const archives = JSON.parse(envFeatured) as FeaturedArchive[]; | ||
if (archives) { | ||
return archives; | ||
} | ||
} | ||
async ngOnInit() { | ||
try { | ||
this.archives = await this.api.getFeaturedArchiveList(); | ||
} catch { | ||
// do nothing | ||
} finally { | ||
this.loading = false; | ||
} | ||
return featuredArchives; | ||
} | ||
} |
Oops, something went wrong.