use MockStore to provide test for menuKeySelector function

This commit is contained in:
Giuseppe Digilio
2020-01-13 11:58:12 +01:00
parent d47a8fc572
commit 24015b2f95
2 changed files with 46 additions and 45 deletions

View File

@@ -1,41 +1,43 @@
import * as ngrx from '@ngrx/store';
import { Store } from '@ngrx/store';
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 * 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 {
ActivateMenuSectionAction,
AddMenuSectionAction,
CollapseMenuAction, CollapseMenuPreviewAction, DeactivateMenuSectionAction,
ExpandMenuAction, ExpandMenuPreviewAction, HideMenuAction,
RemoveMenuSectionAction, ShowMenuAction, ToggleActiveMenuSectionAction, ToggleMenuAction
CollapseMenuAction,
CollapseMenuPreviewAction,
DeactivateMenuSectionAction,
ExpandMenuAction,
ExpandMenuPreviewAction,
HideMenuAction,
RemoveMenuSectionAction,
ShowMenuAction,
ToggleActiveMenuSectionAction,
ToggleMenuAction
} from './menu.actions';
import { MenuSection } from './menu.reducer';
import { MenuSection, menusReducer } from './menu.reducer';
describe('MenuService', () => {
let service: MenuService;
let selectSpy;
let store;
let store: any;
let fakeMenu;
let visibleSection1;
let visibleSection2;
let hiddenSection3;
let subSection4;
let topSections;
let initialState;
function init() {
store = Object.assign(observableOf({}), {
dispatch: () => {/***/
}
}) as any;
fakeMenu = {
id: MenuID.ADMIN,
collapsed: true,
visible: false,
previewCollapsed: true
} as any;
visibleSection1 = {
id: 'section',
visible: true,
@@ -61,35 +63,44 @@ describe('MenuService', () => {
section_3: hiddenSection3,
section_4: subSection4
};
fakeMenu = {
id: MenuID.ADMIN,
collapsed: true,
visible: false,
sections: topSections,
previewCollapsed: true
} as any;
initialState = {
menus: {
'admin-sidebar' : fakeMenu
}
};
}
beforeEach(async(() => {
init();
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({ menus: menusReducer })
],
providers: [
{ provide: Store, useValue: store },
provideMockStore({ initialState }),
{ provide: MenuService, useValue: service }
]
}).compileComponents();
}));
beforeEach(() => {
store = TestBed.get(Store);
service = new MenuService(store);
selectSpy = spyOnProperty(ngrx, 'select');
selectSpy = spyOnProperty(ngrx, 'select').and.callThrough();
spyOn(store, 'dispatch');
});
describe('getMenu', () => {
beforeEach(() => {
selectSpy.and.callFake(() => {
return () => {
return () => hot('a', {
a: fakeMenu
}
);
};
});
});
it('should return the menu', () => {
const result = service.getMenu(MenuID.ADMIN);
@@ -226,19 +237,9 @@ describe('MenuService', () => {
});
describe('getMenuSection', () => {
beforeEach(() => {
selectSpy.and.callFake(() => {
return () => {
return () => hot('a', {
a: hiddenSection3
}
);
};
});
});
it('should return false', () => {
it('should return menu section', () => {
const result = service.getMenuSection(MenuID.ADMIN, 'fakeId');
const result = service.getMenuSection(MenuID.ADMIN, 'section_3');
const expected = cold('b', {
b: hiddenSection3
});

View File

@@ -52,7 +52,7 @@ const getSubSectionsFromSectionSelector = (id: string): MemoizedSelector<MenuSta
@Injectable()
export class MenuService {
constructor(private store: Store<any>) {
constructor(private store: Store<AppState>) {
}
/**