testing/src/session.mock.ts
Mock class of the StarkSessionService interface.
SpyObj
Properties |
|
Public
constructor(devAuthenticationHeaders?: Map
|
||||||||
Defined in testing/src/session.mock.ts:53
|
||||||||
Creates a new mock instance.
Parameters :
|
Public devAuthenticationHeaders |
Defined in testing/src/session.mock.ts:13
|
See StarkSessionService devAuthenticationHeaders property |
Public getCurrentLanguage |
Type : Spy<>
|
Default value : createSpy("getCurrentLanguage")
|
Defined in testing/src/session.mock.ts:23
|
See StarkSessionService getCurrentLanguage() method |
Public getCurrentUser |
Type : Spy<>
|
Default value : createSpy("getCurrentUser")
|
Defined in testing/src/session.mock.ts:18
|
See StarkSessionService getCurrentUser() method |
Public login |
Type : Spy<>
|
Default value : createSpy("login")
|
Defined in testing/src/session.mock.ts:33
|
See StarkSessionService login() method |
Public logout |
Type : Spy<>
|
Default value : createSpy("logout")
|
Defined in testing/src/session.mock.ts:38
|
See StarkSessionService logout() method |
Public pauseUserActivityTracking |
Type : Spy<>
|
Default value : createSpy("pauseUserActivityTracking")
|
Defined in testing/src/session.mock.ts:43
|
Public resumeUserActivityTracking |
Type : Spy<>
|
Default value : createSpy("resumeUserActivityTracking")
|
Defined in testing/src/session.mock.ts:48
|
Public setCurrentLanguage |
Type : Spy<>
|
Default value : createSpy("setCurrentLanguage")
|
Defined in testing/src/session.mock.ts:28
|
See StarkSessionService setCurrentLanguage() method |
Public setDevAuthenticationHeaders |
Type : Spy<>
|
Default value : createSpy("setDevAuthenticationHeaders")
|
Defined in testing/src/session.mock.ts:53
|
The mock class MockStarkSessionService
can be imported as follows:
import { MockStarkSessionService } from "@nationalbankbelgium/stark-core/testing";
Since the mock class implements the base interface of the service it mocks, you just need to provide the mock in your TestingModule
:
TestBed.configureTestingModule({
imports: [...],
declarations: [...],
providers: [
...
{ provide: STARK_SESSION_SERVICE, useValue: new MockStarkSessionService() },
...
]
});
The instantiated mock will have this default property assigned:
devAuthenticationHeaders = new Map<string, string>()
In case you need specific values for those headers, then you need to pass a custom devAuthenticationHeaders
to the constructor of the mock class:
const customAuthenticationHeaders = new Map<string, string>();
// add the headers you need to the map
customAuthenticationHeaders.set(someHeaderName, someHeaderValue);
customAuthenticationHeaders.set(anotherHeaderName, anotherHeaderValue);
// pass the map to the constructor
new MockStarkSessionService(customAuthenticationHeaders);
Then you can just inject the Stark service via the TestBed using its corresponding InjectionToken
:
// this will inject the instantiated mock class
mockSessionService = TestBed.inject(STARK_SESSION_SERVICE);
In fact, every method of the base interface is simply mocked with a Jasmine Spy which can then be used in the unit tests to:
For example:
Example :// reading a value
const devAuthenticationHeaders = mockSessionService.devAuthenticationHeaders;
// overriding a method with a custom function
mockSessionService.getCurrentUser.and.callFake(() => {
// some custom logic to return a specific value
});
// asserting that a method was indeed called
expect(mockSessionService.login).toHaveBeenCalledTimes(1);
expect(mockSessionService.login).toHaveBeenCalledWith(someUser);
import { StarkSessionService } from "@nationalbankbelgium/stark-core";
import Spy = jasmine.Spy;
import SpyObj = jasmine.SpyObj;
import createSpy = jasmine.createSpy;
/**
* Mock class of the {@link StarkSessionService} interface.
*/
export class MockStarkSessionService implements SpyObj<StarkSessionService> {
/**
* See [StarkSessionService devAuthenticationHeaders]{@link StarkSessionService#devAuthenticationHeaders} property
*/
public devAuthenticationHeaders: StarkSessionService["devAuthenticationHeaders"];
/**
* See [StarkSessionService getCurrentUser()]{@link StarkSessionService#getCurrentUser} method
*/
public getCurrentUser: Spy<StarkSessionService["getCurrentUser"]> = createSpy("getCurrentUser");
/**
* See [StarkSessionService getCurrentLanguage()]{@link StarkSessionService#getCurrentLanguage} method
*/
public getCurrentLanguage: Spy<StarkSessionService["getCurrentLanguage"]> = createSpy("getCurrentLanguage");
/**
* See [StarkSessionService setCurrentLanguage()]{@link StarkSessionService#setCurrentLanguage} method
*/
public setCurrentLanguage: Spy<StarkSessionService["setCurrentLanguage"]> = createSpy("setCurrentLanguage");
/**
* See [StarkSessionService login()]{@link StarkSessionService#login} method
*/
public login: Spy<StarkSessionService["login"]> = createSpy("login");
/**
* See [StarkSessionService logout()]{@link StarkSessionService#logout} method
*/
public logout: Spy<StarkSessionService["logout"]> = createSpy("logout");
/**
* See [StarkSessionService pauseUserActivityTracking()]{@link StarkSessionService#pauseUserActivityTracking} method
*/
public pauseUserActivityTracking: Spy<StarkSessionService["pauseUserActivityTracking"]> = createSpy("pauseUserActivityTracking");
/**
* See [StarkSessionService resumeUserActivityTracking()]{@link StarkSessionService#resumeUserActivityTracking} method
*/
public resumeUserActivityTracking: Spy<StarkSessionService["resumeUserActivityTracking"]> = createSpy("resumeUserActivityTracking");
/**
* See [StarkSessionService setDevAuthenticationHeaders()]{@link StarkSessionService#setDevAuthenticationHeaders} method
*/
public setDevAuthenticationHeaders: Spy<StarkSessionService["setDevAuthenticationHeaders"]> = createSpy("setDevAuthenticationHeaders");
/**
* Creates a new mock instance.
* @param devAuthenticationHeaders - Development authentication headers to set to this instance
*/
public constructor(devAuthenticationHeaders?: Map<string, string | string[]>) {
if (!devAuthenticationHeaders) {
this.devAuthenticationHeaders = new Map<string, string>();
} else {
this.devAuthenticationHeaders = devAuthenticationHeaders;
}
}
}