migrate angular 21 tests
This commit is contained in:
75
src/app/modules/guest/guest-show-data.service.spec.ts
Normal file
75
src/app/modules/guest/guest-show-data.service.spec.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {of} from 'rxjs';
|
||||
import {DbService} from 'src/app/services/db.service';
|
||||
import {GuestShowDataService} from './guest-show-data.service';
|
||||
|
||||
describe('GuestShowDataService', () => {
|
||||
let service: GuestShowDataService;
|
||||
let docUpdateSpy: jasmine.Spy<() => Promise<void>>;
|
||||
let docDeleteSpy: jasmine.Spy<() => Promise<void>>;
|
||||
let docSpy: jasmine.Spy;
|
||||
let colAddSpy: jasmine.Spy<() => Promise<{id: string}>>;
|
||||
let colSpy: jasmine.Spy;
|
||||
let dbServiceSpy: jasmine.SpyObj<DbService>;
|
||||
|
||||
beforeEach(() => {
|
||||
docUpdateSpy = jasmine.createSpy('update').and.resolveTo();
|
||||
docDeleteSpy = jasmine.createSpy('delete').and.resolveTo();
|
||||
docSpy = jasmine.createSpy('doc').and.returnValue({
|
||||
update: docUpdateSpy,
|
||||
delete: docDeleteSpy,
|
||||
});
|
||||
colAddSpy = jasmine.createSpy('add').and.resolveTo({id: 'guest-2'});
|
||||
colSpy = jasmine.createSpy('col').and.returnValue({add: colAddSpy});
|
||||
dbServiceSpy = jasmine.createSpyObj<DbService>('DbService', ['col$', 'doc$', 'doc', 'col']);
|
||||
dbServiceSpy.col$.and.returnValue(of([{id: 'guest-1'}]) as never);
|
||||
dbServiceSpy.doc$.and.returnValue(of({id: 'guest-1'}) as never);
|
||||
dbServiceSpy.doc.and.callFake(docSpy);
|
||||
dbServiceSpy.col.and.callFake(colSpy);
|
||||
|
||||
void TestBed.configureTestingModule({
|
||||
providers: [{provide: DbService, useValue: dbServiceSpy}],
|
||||
});
|
||||
|
||||
service = TestBed.inject(GuestShowDataService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should read the guest collection for list$', () => {
|
||||
service.list$.subscribe();
|
||||
|
||||
expect(dbServiceSpy.col$).toHaveBeenCalledWith('guest');
|
||||
});
|
||||
|
||||
it('should read a single guest show by id', () => {
|
||||
service.read$('guest-7').subscribe();
|
||||
|
||||
expect(dbServiceSpy.doc$).toHaveBeenCalledWith('guest/guest-7');
|
||||
});
|
||||
|
||||
it('should update a guest show document', async () => {
|
||||
await service.update$('guest-7', {published: true} as never);
|
||||
|
||||
expect(docSpy).toHaveBeenCalledWith('guest/guest-7');
|
||||
const [updatePayload] = docUpdateSpy.calls.mostRecent().args as unknown as [Record<string, unknown>];
|
||||
expect(updatePayload).toEqual({published: true});
|
||||
});
|
||||
|
||||
it('should add a guest show and return the created id', async () => {
|
||||
await expectAsync(service.add({published: false} as never)).toBeResolvedTo('guest-2');
|
||||
|
||||
expect(colSpy).toHaveBeenCalledWith('guest');
|
||||
const [addPayload] = colAddSpy.calls.mostRecent().args as unknown as [Record<string, unknown>];
|
||||
expect(addPayload).toEqual({published: false});
|
||||
});
|
||||
|
||||
it('should delete a guest show document', async () => {
|
||||
await service.delete('guest-7');
|
||||
|
||||
expect(docSpy).toHaveBeenCalledWith('guest/guest-7');
|
||||
expect(docDeleteSpy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
62
src/app/modules/guest/guest-show.service.spec.ts
Normal file
62
src/app/modules/guest/guest-show.service.spec.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {GuestShowDataService} from './guest-show-data.service';
|
||||
import {GuestShowService} from './guest-show.service';
|
||||
import {ShowService} from '../shows/services/show.service';
|
||||
|
||||
describe('GuestShowService', () => {
|
||||
let service: GuestShowService;
|
||||
let guestShowDataServiceSpy: jasmine.SpyObj<GuestShowDataService>;
|
||||
let showServiceSpy: jasmine.SpyObj<ShowService>;
|
||||
|
||||
beforeEach(() => {
|
||||
guestShowDataServiceSpy = jasmine.createSpyObj<GuestShowDataService>('GuestShowDataService', ['add', 'update$']);
|
||||
showServiceSpy = jasmine.createSpyObj<ShowService>('ShowService', ['update$']);
|
||||
guestShowDataServiceSpy.add.and.resolveTo('share-1');
|
||||
guestShowDataServiceSpy.update$.and.resolveTo();
|
||||
showServiceSpy.update$.and.resolveTo();
|
||||
|
||||
void TestBed.configureTestingModule({
|
||||
providers: [
|
||||
{provide: GuestShowDataService, useValue: guestShowDataServiceSpy},
|
||||
{provide: ShowService, useValue: showServiceSpy},
|
||||
],
|
||||
});
|
||||
|
||||
service = TestBed.inject(GuestShowService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should create a new guest share, persist the generated shareId on the show and return the share url', async () => {
|
||||
const show = {id: 'show-1', showType: 'service-worship', date: new Date(), shareId: ''} as any;
|
||||
const songs = [{id: 'song-1'}] as any;
|
||||
const expectedUrl = window.location.protocol + '//' + window.location.host + '/guest/share-1';
|
||||
|
||||
await expectAsync(service.share(show, songs)).toBeResolvedTo(expectedUrl);
|
||||
|
||||
expect(guestShowDataServiceSpy.add).toHaveBeenCalledWith({
|
||||
showType: 'service-worship',
|
||||
date: show.date,
|
||||
songs,
|
||||
});
|
||||
expect(showServiceSpy.update$).toHaveBeenCalledWith('show-1', {shareId: 'share-1'});
|
||||
});
|
||||
|
||||
it('should update an existing share and reuse its id in the returned url', async () => {
|
||||
const show = {id: 'show-1', showType: 'service-worship', date: new Date(), shareId: 'share-9'} as any;
|
||||
const songs = [{id: 'song-1'}] as any;
|
||||
const expectedUrl = window.location.protocol + '//' + window.location.host + '/guest/share-9';
|
||||
|
||||
await expectAsync(service.share(show, songs)).toBeResolvedTo(expectedUrl);
|
||||
|
||||
expect(guestShowDataServiceSpy.update$).toHaveBeenCalledWith('share-9', {
|
||||
showType: 'service-worship',
|
||||
date: show.date,
|
||||
songs,
|
||||
});
|
||||
expect(guestShowDataServiceSpy.add).not.toHaveBeenCalled();
|
||||
expect(showServiceSpy.update$).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -17,14 +17,15 @@ export class GuestShowService {
|
||||
date: show.date,
|
||||
songs: songs,
|
||||
};
|
||||
let shareId = show.shareId;
|
||||
|
||||
if (!show.shareId) {
|
||||
const shareId = await this.guestShowDataService.add(data);
|
||||
shareId = await this.guestShowDataService.add(data);
|
||||
await this.showService.update$(show.id, {shareId});
|
||||
} else {
|
||||
await this.guestShowDataService.update$(show.shareId, data);
|
||||
}
|
||||
|
||||
return window.location.protocol + '//' + window.location.host + '/guest/' + show.shareId;
|
||||
return window.location.protocol + '//' + window.location.host + '/guest/' + shareId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user