Search code examples
angularunit-testingjestjsangular-services

Unit testing for Angular service method with imported third party dependancy


I have an Angular service which imports a third party dependancy. I call the dependancy to give me the browser fingerprint which is then stored in the service.

I am not sure how to mock this dependency in the test so I can assert it has been called and mock a return value.

This is the service:

import { Inject, Injectable } from '@angular/core';
import * as Fingerprint2 from 'fingerprintjs2';

@Injectable()
export class ClientInfoService {
    public fingerprint: string | null = null;

    constructor() {
    }

    createFingerprint(): any {
        return new Fingerprint2();
    }

    setFingerprint(): void {
        let fprint = this.createFingerprint();
        setTimeout(() => fprint.get(hash => this.fingerprint = hash), 500);
    }

    getFingerprint(): string | null {
        return this.fingerprint;
    }

}

This is the current test code:

import { TestBed } from '@angular/core/testing';
import { ClientInfoService } from './client-info.service';

describe('Client Info Service', () => {
    const hash = 'a6e5b498951af7c3033d0c7580ec5fc6';
    let service: ClientInfoService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [ClientInfoService],
        });
        service = TestBed.get(ClientInfoService);
    });

    test('should be defined', () => {
        expect(service).toBeDefined();
    });


    describe('get the fingerprint', () => {

        test('it should be null', () => {
            let fprint = service.getFingerprint();
            expect(fprint).toBeNull();
        });

        test('it should be the hash value', () => {
            service.fingerprint = hash;
            let fprint = service.getFingerprint();
            expect(fprint).toEqual(hash);
        });

    test('it should get the hash value after setting', () => {
        jest.useFakeTimers();
        service.createFingerprint = jest.fn().mockReturnValue(() => {
            return {
                get: function (cb) {
                    return cb(hash);
                }
            };
        });
        spyOn(service, 'createFingerprint');
        service.setFingerprint();
        jest.runAllTimers();
        expect(service.createFingerprint).toHaveBeenCalled();
        expect(service.fingerprint).toEqual(hash);
    });

    });

});

Solution

  • I managed to achieve this with the below spec. I used spies and returned values to mock the fingerprint creation.

    import { TestBed } from '@angular/core/testing';
    import { ClientInfoService } from './client-info.service';
    
    describe('Client Info Service', () => {
        const hash = 'a6e5b498951af7c3033d0c7580ec5fc6';
        let service: ClientInfoService;
    
        beforeEach(() => {
            TestBed.configureTestingModule({
                providers: [ClientInfoService],
            });
            service = TestBed.get(ClientInfoService);
        });
    
        test('should be defined', () => {
            expect(service).toBeDefined();
        });
    
        test('it should set the fingerprint', () => {
            jest.useFakeTimers()
            let cb = (h) => {return h;};
            spyOn(service, 'createFingerprint').and.returnValue({
                get: (cb) => {
                    return cb(hash);
                },
            });
            service.setFingerprint();
            jest.runAllTimers();
            expect(service.createFingerprint).toHaveBeenCalled();
            expect(service.fingerprint).toEqual(hash);
        });
    
        test('it should get the fingerprint', () => {
            let fprint = service.getFingerprint();
            expect(fprint).toEqual(service.fingerprint);
        });
    
    });