File

testing/src/app-sidebar.mock.ts

Description

Mock class of the StarkAppSidebarService interface.

Implements

SpyObj

Index

Properties

Properties

Public close
Type : Spy<>
Default value : createSpy("close")
Public closeSidebar$
Type : Subject<void>
Default value : new Subject<void>()
Public openLeft
Type : Spy<>
Default value : createSpy("openLeft")
Public openMenu
Type : Spy<>
Default value : createSpy("openMenu")
Public openRight
Type : Spy<>
Default value : createSpy("openRight")
Public openSidebar$
Type : Subject<StarkAppSidebarOpenEvent>
Default value : new Subject<StarkAppSidebarOpenEvent>()
Public toggleMenu
Type : Spy<>
Default value : createSpy("toggleMenu")
Public toggleSidebar$
Type : Subject<StarkAppSidebarOpenEvent>
Default value : new Subject<StarkAppSidebarOpenEvent>()

Usage

The mock class MockStarkAppSidebarService can be imported as follows:

Example :
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:

Example :
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:

Example :
// 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:

  • 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 :
// 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");
}

results matching ""

    No results matching ""