mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 18:14:17 +00:00
use MockStore to provide test for menuKeySelector function
This commit is contained in:
@@ -1,41 +1,43 @@
|
|||||||
import * as ngrx from '@ngrx/store';
|
|
||||||
import { Store } from '@ngrx/store';
|
|
||||||
import { async, TestBed } from '@angular/core/testing';
|
import { async, TestBed } from '@angular/core/testing';
|
||||||
import { MenuService } from './menu.service';
|
|
||||||
import { cold, hot } from 'jasmine-marbles';
|
|
||||||
import { MenuID } from './initial-menus-state';
|
|
||||||
import { of as observableOf } from 'rxjs';
|
import { of as observableOf } from 'rxjs';
|
||||||
|
import * as ngrx from '@ngrx/store';
|
||||||
|
import { Store, StoreModule } from '@ngrx/store';
|
||||||
|
import { provideMockStore } from '@ngrx/store/testing';
|
||||||
|
import { cold, hot } from 'jasmine-marbles';
|
||||||
|
|
||||||
|
import { MenuService } from './menu.service';
|
||||||
|
import { MenuID } from './initial-menus-state';
|
||||||
import {
|
import {
|
||||||
ActivateMenuSectionAction,
|
ActivateMenuSectionAction,
|
||||||
AddMenuSectionAction,
|
AddMenuSectionAction,
|
||||||
CollapseMenuAction, CollapseMenuPreviewAction, DeactivateMenuSectionAction,
|
CollapseMenuAction,
|
||||||
ExpandMenuAction, ExpandMenuPreviewAction, HideMenuAction,
|
CollapseMenuPreviewAction,
|
||||||
RemoveMenuSectionAction, ShowMenuAction, ToggleActiveMenuSectionAction, ToggleMenuAction
|
DeactivateMenuSectionAction,
|
||||||
|
ExpandMenuAction,
|
||||||
|
ExpandMenuPreviewAction,
|
||||||
|
HideMenuAction,
|
||||||
|
RemoveMenuSectionAction,
|
||||||
|
ShowMenuAction,
|
||||||
|
ToggleActiveMenuSectionAction,
|
||||||
|
ToggleMenuAction
|
||||||
} from './menu.actions';
|
} from './menu.actions';
|
||||||
import { MenuSection } from './menu.reducer';
|
import { MenuSection, menusReducer } from './menu.reducer';
|
||||||
|
|
||||||
describe('MenuService', () => {
|
describe('MenuService', () => {
|
||||||
let service: MenuService;
|
let service: MenuService;
|
||||||
let selectSpy;
|
let selectSpy;
|
||||||
let store;
|
let store: any;
|
||||||
let fakeMenu;
|
let fakeMenu;
|
||||||
let visibleSection1;
|
let visibleSection1;
|
||||||
let visibleSection2;
|
let visibleSection2;
|
||||||
let hiddenSection3;
|
let hiddenSection3;
|
||||||
let subSection4;
|
let subSection4;
|
||||||
let topSections;
|
let topSections;
|
||||||
|
let initialState;
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
store = Object.assign(observableOf({}), {
|
|
||||||
dispatch: () => {/***/
|
|
||||||
}
|
|
||||||
}) as any;
|
|
||||||
fakeMenu = {
|
|
||||||
id: MenuID.ADMIN,
|
|
||||||
collapsed: true,
|
|
||||||
visible: false,
|
|
||||||
previewCollapsed: true
|
|
||||||
} as any;
|
|
||||||
visibleSection1 = {
|
visibleSection1 = {
|
||||||
id: 'section',
|
id: 'section',
|
||||||
visible: true,
|
visible: true,
|
||||||
@@ -61,35 +63,44 @@ describe('MenuService', () => {
|
|||||||
section_3: hiddenSection3,
|
section_3: hiddenSection3,
|
||||||
section_4: subSection4
|
section_4: subSection4
|
||||||
};
|
};
|
||||||
|
|
||||||
|
fakeMenu = {
|
||||||
|
id: MenuID.ADMIN,
|
||||||
|
collapsed: true,
|
||||||
|
visible: false,
|
||||||
|
sections: topSections,
|
||||||
|
previewCollapsed: true
|
||||||
|
} as any;
|
||||||
|
|
||||||
|
initialState = {
|
||||||
|
menus: {
|
||||||
|
'admin-sidebar' : fakeMenu
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeEach(async(() => {
|
beforeEach(async(() => {
|
||||||
init();
|
init();
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
|
imports: [
|
||||||
|
StoreModule.forRoot({ menus: menusReducer })
|
||||||
|
],
|
||||||
providers: [
|
providers: [
|
||||||
{ provide: Store, useValue: store },
|
provideMockStore({ initialState }),
|
||||||
{ provide: MenuService, useValue: service }
|
{ provide: MenuService, useValue: service }
|
||||||
]
|
]
|
||||||
}).compileComponents();
|
}).compileComponents();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
store = TestBed.get(Store);
|
||||||
service = new MenuService(store);
|
service = new MenuService(store);
|
||||||
selectSpy = spyOnProperty(ngrx, 'select');
|
selectSpy = spyOnProperty(ngrx, 'select').and.callThrough();
|
||||||
spyOn(store, 'dispatch');
|
spyOn(store, 'dispatch');
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getMenu', () => {
|
describe('getMenu', () => {
|
||||||
beforeEach(() => {
|
|
||||||
selectSpy.and.callFake(() => {
|
|
||||||
return () => {
|
|
||||||
return () => hot('a', {
|
|
||||||
a: fakeMenu
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it('should return the menu', () => {
|
it('should return the menu', () => {
|
||||||
|
|
||||||
const result = service.getMenu(MenuID.ADMIN);
|
const result = service.getMenu(MenuID.ADMIN);
|
||||||
@@ -226,19 +237,9 @@ describe('MenuService', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('getMenuSection', () => {
|
describe('getMenuSection', () => {
|
||||||
beforeEach(() => {
|
it('should return menu section', () => {
|
||||||
selectSpy.and.callFake(() => {
|
|
||||||
return () => {
|
|
||||||
return () => hot('a', {
|
|
||||||
a: hiddenSection3
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it('should return false', () => {
|
|
||||||
|
|
||||||
const result = service.getMenuSection(MenuID.ADMIN, 'fakeId');
|
const result = service.getMenuSection(MenuID.ADMIN, 'section_3');
|
||||||
const expected = cold('b', {
|
const expected = cold('b', {
|
||||||
b: hiddenSection3
|
b: hiddenSection3
|
||||||
});
|
});
|
||||||
|
@@ -52,7 +52,7 @@ const getSubSectionsFromSectionSelector = (id: string): MemoizedSelector<MenuSta
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class MenuService {
|
export class MenuService {
|
||||||
|
|
||||||
constructor(private store: Store<any>) {
|
constructor(private store: Store<AppState>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user