File

testing/src/session.mock.ts

Description

Mock class of the StarkSessionService interface.

Implements

SpyObj

Index

Properties

Constructor

Public constructor(devAuthenticationHeaders?: Map)

Creates a new mock instance.

Parameters :
Name Type Optional Description
devAuthenticationHeaders Map<string | string | []> Yes
  • Development authentication headers to set to this instance

Properties

Public devAuthenticationHeaders
Public getCurrentLanguage
Type : Spy<>
Default value : createSpy("getCurrentLanguage")
Public getCurrentUser
Type : Spy<>
Default value : createSpy("getCurrentUser")
Public login
Type : Spy<>
Default value : createSpy("login")
Public logout
Type : Spy<>
Default value : createSpy("logout")
Public pauseUserActivityTracking
Type : Spy<>
Default value : createSpy("pauseUserActivityTracking")
Public resumeUserActivityTracking
Type : Spy<>
Default value : createSpy("resumeUserActivityTracking")
Public setCurrentLanguage
Type : Spy<>
Default value : createSpy("setCurrentLanguage")
Public setDevAuthenticationHeaders
Type : Spy<>
Default value : createSpy("setDevAuthenticationHeaders")

Usage

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 = TetBed.get(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:

  • return custom values
  • override a method with a custom function
  • asserting that they are actually called
  • do any other operation than can be performed with an Spy.

For 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;
		}
	}
}

result-matching ""

    No results matching ""