I am writing a test for my app's login service and would like to mock one of service calls.
This is my code for the test:
describe('LoginService', () => {
let service: LoginService;
let mockSupabaseService: SupabaseService;
beforeEach(() => {
TestBed.configureTestingModule({});
mockSupabaseService = TestBed.inject(SupabaseService);
service = TestBed.inject(LoginService);
spyOn(mockSupabaseService, 'session').and.returnValue(of(null));
});
describe('isLoggedIn()', () => {
it('should set login form', () => {
const isLoggedIn = service.isLoggedIn();
expect(isLoggedIn).toEqual(true);
});
});
});
This is the code for the login service I am testing:
export class LoginService {
constructor(private supabaseService: SupabaseService) {}
isLoggedIn(): boolean {
return !!this.supabaseService.session?.user;
}
}
This is the service I am trying to mock:
export class SupabaseService {
supabase: SupabaseClient;
token: string | undefined | null;
constructor() {
this.supabase = createClient(environment.supabaseConfig.url, environment.supabaseConfig.key);
}
get session(): Session | undefined | null {
return this.supabase.auth.session();
}
}
For some reason spyOn(mockSupabaseService, 'session').and.returnValue(of(null)) is flagging as unrecognised (it throws an error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.). But I don't think I am mocking the get call correctly. How do I do that?