lundi 3 juin 2019

how to write testcases for code written inside pipe function in angular with jasmine?

I have written one function in my service file in angular. But the problem that I am facing is that at the time of writing testcase I do not know how to test the function in which we have used pipe with effects also

fetch(createdBy: string) {
        const millisInAWeek = 7 * 24 * 60 * 60 * 1000;
        const previousWeek = Date.now() - millisInAWeek;
        const query = ref => ref
            .where('createdBy', '==', createdBy)
            .where('updatedAt', '>', previousWeek)
            .orderBy('updatedAt', 'desc')
            .limit(25);
        return this.afsDb.collection('searches', query)
            .get()
            .pipe(
                map(snapshot => snapshot.docs.map(documentSnapshot => documentSnapshot.data() as SearchRecord)),
                switchMap(searchRecords => of(new ActionLoadSearchRecords({ searchRecords })))
            );
    }

Testcase Written by me

it('should fetch top 25 searchRecord in descending Order', () => {
        const createdBy = 'User1';
        const pipeSpy = jasmine.createSpy('pipeSpy');
        const getSpy = jasmine.createSpy('getSpy');

        const collectionSpy = spyOn((service as any).afsDb, 'collection').and.callFake(() => ({
            get: getSpy.and.callFake(() => ({
                pipe: pipeSpy.and.returnValue(of(new ActionLoadSearchRecords({ searchRecords: [searchRecord] })))
            }))
        }));
        service.fetchSearches(createdBy).subscribe(() => {
            expect(pipeSpy).toHaveBeenCalled();
            expect(getSpy).toHaveBeenCalled();
            expect(collectionSpy).toHaveBeenCalled();
        });
    });

But the line in the function

.pipe(
     map(snapshot => snapshot.docs.map(documentSnapshot => documentSnapshot.data() as SearchRecord)),
                switchMap(searchRecords => of(new ActionLoadSearchRecords({ searchRecords })))
            );
``
is not covered in code-coverage? How can I solve this ?

Aucun commentaire:

Enregistrer un commentaire