testing/src/toast-notification.mock.ts
Mock class of the StarkToastNotificationService interface.
SpyObj
Properties |
Public hide |
Type : Spy<>
|
Default value : createSpy("hide")
|
Defined in testing/src/toast-notification.mock.ts:18
|
See StarkToastNotificationService hide() method |
Public show |
Type : Spy<>
|
Default value : createSpy("show")
|
Defined in testing/src/toast-notification.mock.ts:13
|
See StarkToastNotificationService show() method |
The mock class MockStarkToastNotificationService
can be imported as follows:
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
:
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
:
// 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:
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");
}