testing/src/app-sidebar.mock.ts
Mock class of the StarkAppSidebarService interface.
SpyObj
Properties |
|
Public close |
Type : Spy<>
|
Default value : createSpy("close")
|
Defined in testing/src/app-sidebar.mock.ts:29
|
See StarkAppSidebarService close() method |
Public closeSidebar$ |
Type : Subject<void>
|
Default value : new Subject<void>()
|
Defined in testing/src/app-sidebar.mock.ts:14
|
See StarkAppSidebarService closeSidebar$ property |
Public openLeft |
Type : Spy<>
|
Default value : createSpy("openLeft")
|
Defined in testing/src/app-sidebar.mock.ts:39
|
See StarkAppSidebarService openLeft() method |
Public openMenu |
Type : Spy<>
|
Default value : createSpy("openMenu")
|
Defined in testing/src/app-sidebar.mock.ts:34
|
See StarkAppSidebarService openMenu() method |
Public openRight |
Type : Spy<>
|
Default value : createSpy("openRight")
|
Defined in testing/src/app-sidebar.mock.ts:44
|
See StarkAppSidebarService openRight() method |
Public openSidebar$ |
Type : Subject<StarkAppSidebarOpenEvent>
|
Default value : new Subject<StarkAppSidebarOpenEvent>()
|
Defined in testing/src/app-sidebar.mock.ts:19
|
See StarkAppSidebarService openSidebar$ property |
Public toggleMenu |
Type : Spy<>
|
Default value : createSpy("toggleMenu")
|
Defined in testing/src/app-sidebar.mock.ts:49
|
See StarkAppSidebarService toggleMenu() method |
Public toggleSidebar$ |
Type : Subject<StarkAppSidebarOpenEvent>
|
Default value : new Subject<StarkAppSidebarOpenEvent>()
|
Defined in testing/src/app-sidebar.mock.ts:24
|
See StarkAppSidebarService toggleSidebar$ property |
The mock class MockStarkAppSidebarService
can be imported as follows:
import { MockStarkAppSidebarService } 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_APP_SIDEBAR_SERVICE, useValue: new MockStarkAppSidebarService() },
...
]
});
Then you can just inject the Stark service via the TestBed using its corresponding InjectionToken
:
// this will inject the instantiated mock class
mockStarkAppSidebarService = TestBed.inject(STARK_APP_SIDEBAR_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 sidebarOpen$ = mockStarkAppSidebarService.openSidebar$;
// overriding a method with a custom function
mockStarkAppSidebarService.openMenu.and.callFake(() => {
// some custom logic
});
// asserting that a method was indeed called
expect(mockStarkAppSidebarService.close).toHaveBeenCalledTimes(1);
import { StarkAppSidebarOpenEvent, StarkAppSidebarService } from "@nationalbankbelgium/stark-ui";
import { Subject } from "rxjs";
import Spy = jasmine.Spy;
import SpyObj = jasmine.SpyObj;
import createSpy = jasmine.createSpy;
/**
* Mock class of the {@link StarkAppSidebarService} interface.
*/
export class MockStarkAppSidebarService implements SpyObj<StarkAppSidebarService> {
/**
* See [StarkAppSidebarService closeSidebar$]{@link StarkAppSidebarService#closeSidebar$} property
*/
public closeSidebar$: Subject<void> = new Subject<void>();
/**
* See [StarkAppSidebarService openSidebar$]{@link StarkAppSidebarService#openSidebar$} property
*/
public openSidebar$: Subject<StarkAppSidebarOpenEvent> = new Subject<StarkAppSidebarOpenEvent>();
/**
* See [StarkAppSidebarService toggleSidebar$]{@link StarkAppSidebarService#toggleSidebar$} property
*/
public toggleSidebar$: Subject<StarkAppSidebarOpenEvent> = new Subject<StarkAppSidebarOpenEvent>();
/**
* See [StarkAppSidebarService close()]{@link StarkAppSidebarService#close} method
*/
public close: Spy<StarkAppSidebarService["close"]> = createSpy("close");
/**
* See [StarkAppSidebarService openMenu()]{@link StarkAppSidebarService#openMenu} method
*/
public openMenu: Spy<StarkAppSidebarService["openMenu"]> = createSpy("openMenu");
/**
* See [StarkAppSidebarService openLeft()]{@link StarkAppSidebarService#openLeft} method
*/
public openLeft: Spy<StarkAppSidebarService["openLeft"]> = createSpy("openLeft");
/**
* See [StarkAppSidebarService openRight()]{@link StarkAppSidebarService#openRight} method
*/
public openRight: Spy<StarkAppSidebarService["openRight"]> = createSpy("openRight");
/**
* See [StarkAppSidebarService toggleMenu()]{@link StarkAppSidebarService#toggleMenu} method
*/
public toggleMenu: Spy<StarkAppSidebarService["toggleMenu"]> = createSpy("toggleMenu");
}