File

testing/src/toast-notification.mock.ts

Description

Mock class of the StarkToastNotificationService interface.

Implements

SpyObj

Index

Properties

Properties

Public hide
Type : Spy<>
Default value : createSpy("hide")
Public show
Type : Spy<>
Default value : createSpy("show")

Usage

The mock class MockStarkToastNotificationService can be imported as follows:

Example :
import { MockStarkToastNotificationService } from "@nationalbankbelgium/stark-ui/testing";

Since the mock class implements the base interface of the service it mocks, you just need to provide the mock in your TestingModule:

Example :
TestBed.configureTestingModule({
    imports: [...],
    declarations: [...],
    providers: [
        ...
        { provide: STARK_TOAST_NOTIFICATION_SERVICE, useValue: new MockStarkToastNotificationService() },
        ...
    ]
});

Then you can just inject the Stark service via the TestBed using its corresponding InjectionToken:

Example :
// this will inject the instantiated mock class
mockStarkToastNotificationService = TestBed.inject(STARK_TOAST_NOTIFICATION_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:

Example :
// returning custom value
mockStarkToastNotificationService.show.and.returnValue(someCustomObservable);

// overriding a method with a custom function
mockStarkToastNotificationService.show.and.callFake((message: StarkToastMessage) => {
  // some custom logic
});

// asserting that a method was indeed called
expect(mockStarkToastNotificationService.hide).toHaveBeenCalledTimes(1);

import { StarkToastNotificationService } from "@nationalbankbelgium/stark-ui";
import Spy = jasmine.Spy;
import SpyObj = jasmine.SpyObj;
import createSpy = jasmine.createSpy;

/**
 * Mock class of the {@link StarkToastNotificationService} interface.
 */
export class MockStarkToastNotificationService implements SpyObj<StarkToastNotificationService> {
	/**
	 * See [StarkToastNotificationService show()]{@link StarkToastNotificationService#show} method
	 */
	public show: Spy<StarkToastNotificationService["show"]> = createSpy("show");

	/**
	 * See [StarkToastNotificationService hide()]{@link StarkToastNotificationService#hide} method
	 */
	public hide: Spy<StarkToastNotificationService["hide"]> = createSpy("hide");
}

results matching ""

    No results matching ""