I am new to Jest and the mocks, and am having a hard time trying to mock some Angular 4 code for testing. I am missing that 'AH HA' moment.
I am trying to mock a provider so I can test without calling the actual services.
Provider - login-services.ts
import { Injectable } from '@angular/core';
import { LoginService } from '../../interfaces/login-service';
@Injectable()
export class LoginServicesProvider {
Services: LoginService[];
constructor() {
console.log('Hello LoginServicesProvider Provider');
this.LoadLoginServices()
}
LoadLoginServices() {
this.Services = [
{ Name: 'Google', Enabled: true },
{ Name: 'Facebook', Enabled: true},
{ Name: 'Twitter', Enabled: false}
];
}
LogInUsingService(Service: LoginService):boolean {
let LoggedIn = false;
... // Calls to login setting LoggedIn on success
return LoggedIn;
}
}
Then in my login-service.spec.ts I am testing the base setup:
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { LoginService } from '../../interfaces/login-service';
import { LoginServicesProvider } from './login-services';
describe('LoginServicesProvider', () => {
let TestObject: LoginServicesProvider;
describe('Provider', () => {
TestObject = new LoginServicesProvider();
test('should be an instance of LoginServicesProvider', () => {
expect(TestObject).toBeInstanceOf(LoginServicesProvider);
});
});
describe('Data', () => {
beforeEach(() => {
TestObject = new LoginServicesProvider();
});
test('should have providers defined', () => {
expect(TestObject.Services).toBeDefined();
expect(TestObject.Services).toBeInstanceOf(Array);
expect(TestObject.Services.length).toBeGreaterThan(0);
});
});
describe('Functions', () => {
beforeEach(() => {
TestObject = new LoginServicesProvider();
})
test('should have LogInUsingService()', () => {
expect(TestObject.LogInUsingService).toBeDefined();
});
});
});
These tests all pass. I can confirm there are Login services defined, the function is there etc. Now, I want to mock the method LogInUsingService() with some data (maybe the Google entry for instance) to ensure that the rest of the code works correctly when the LogInUsingService returns true or false. Such things as Local Storage is written, user data updated etc...
I do not know how to mock the call to test it. I am just not getting the mock syntax to have my code work.
describe('LogInUsingService', () => {
let TestObject:LoginServicesProvider;
beforeEach(() => {
TestObject = new LoginServicesProvider();
})
test('should have called LogInUsingService()', () => {
let Parameters:LoginService;
Parameters = { Name: 'Google', Enabled: true};
const mockService = jest.fn( ().LogInUsingService => true);
expect(mockService.LogInUsingService(Parameters)).toBe(true);
expect(mockLogInUsingService.mock.calls.length).toBe(1);
});
Now the above code does not compile and obviously does not work. I do not know how to set this up to mock the method in LoginServicesProviders.
Aucun commentaire:
Enregistrer un commentaire