import {MenuItemComponent} from "../src/components/fec-menu/menu-item.component"; import {MenuItem} from "../src/components/fec-menu/menu-item"; describe('MenuItemComponent', () => { var menuItemComponent: MenuItemComponent; beforeEach(function() { menuItemComponent = new MenuItemComponent(); menuItemComponent.menuItem = {label: 'placeholder', menuId: 'placeholderId', expanded: false}; }); describe('getToggledIcon', () => { it('should return "fec-icon-right menu-open" when menuItem.expanded is true', () => { menuItemComponent.menuItem.expanded = true; expect(menuItemComponent.getToggledIcon()).toBe("fec-icon-right menu-open"); }); it('should return "fec-icon-right" when menuItem.expanded is false', () => { menuItemComponent.menuItem.expanded = false; expect(menuItemComponent.getToggledIcon()).toBe("fec-icon-right"); }); }); describe('onClick', () => { it('should call goToUrl when menuItem.url is set', () => { menuItemComponent.menuItem.url = "example.com"; spyOn(menuItemComponent, 'goToUrl'); menuItemComponent.onClick(); expect(menuItemComponent.goToUrl).toHaveBeenCalledWith("example.com"); }); it('should emit a click event when menuItem.url is undefined and menuItem.nestedItems is undefined', (done: any) => { menuItemComponent.menuItemClickEvent.subscribe( (event: any) => { expect(event).toEqual([{value: menuItemComponent.menuItem.menuId}]); done(); }); menuItemComponent.onClick(); }); it('should call expandAction(true) when menuItem.url is undefined, menuItem.nestedItems are not undefined, and menuItem.expanded is false', () => { menuItemComponent.menuItem.nestedItems = [ {label: 'placeholder', menuId: 'placeholderId', expanded: false}]; menuItemComponent.menuItem.expanded = false; spyOn(menuItemComponent, 'expandAction'); menuItemComponent.onClick(); expect(menuItemComponent.expandAction).toHaveBeenCalledWith(true); }); }); describe('getRightIcon', () => { it('should return "example-icon" when menuItem.rightIcon is set to "example-icon"', () => { menuItemComponent.menuItem.rightIcon = "example-icon"; expect(menuItemComponent.getRightIcon()).toBe("example-icon"); }); it('should set shouldToggleIcon to true and return "fec-icon-right" when menuItem.rightIcon is undefined and menuItem.nestedItems are not undefined', () => { menuItemComponent.menuItem.nestedItems = [ {label: 'placeholder', menuId: 'placeholderId', expanded: false}]; expect(menuItemComponent.getRightIcon()).toBe("fec-icon-right"); expect(menuItemComponent.shouldToggleIcon).toBe(true); }); it('should return nothing if menuItem.rightIcon and menuItem.nestedItems are undefined', () => { expect(menuItemComponent.getRightIcon()).toBe(undefined); }); }); describe('expandAction', () => { it('should set active to true when called with true', () => { menuItemComponent.menuItem.nestedItems = [ {label: 'placeholder', menuId: 'placeholderId', expanded: false}]; menuItemComponent.expandAction(true); expect(menuItemComponent.active).toBe(true); }); it('should set active to false when called with false', () => { menuItemComponent.menuItem.nestedItems = [ {label: 'placeholder', menuId: 'placeholderId', expanded: false}]; menuItemComponent.expandAction(false); expect(menuItemComponent.active).toBe(false); }); it('should should set rightIcon when shouldToggleIcon is true', () => { menuItemComponent.shouldToggleIcon = true; spyOn(menuItemComponent, 'getToggledIcon').and.returnValue('expected'); menuItemComponent.expandAction(true); expect(menuItemComponent.rightIcon).toBe('expected'); }); }); describe('checkDropdownToggle', () => { it('should set active to true and call expandAction(true) when menuItem.nestedItems has a menuId equal to changes.activeMenuId.currentValue', () => { menuItemComponent.menuItem.nestedItems = [ {label: 'placeholder', menuId: 'placeholderId', expanded: false}]; spyOn(menuItemComponent, 'expandAction').and.callThrough(); menuItemComponent.checkDropdownToggle({activeMenuId: {currentValue: 'placeholderId'}}); expect(menuItemComponent.expandAction).toHaveBeenCalledWith(true); expect(menuItemComponent.active).toBe(true); }); it('calls expandAction(false) when this.menutItems.nestedItems exists but does not contain menuId equal to changes.activeMenuId.currentValue', () => { menuItemComponent.menuItem.nestedItems = [ {label: 'placeholder', menuId: 'placeholderId', expanded: false}]; spyOn(menuItemComponent, 'expandAction').and.callThrough(); menuItemComponent.checkDropdownToggle({activeMenuId: {currentValue: 'notPlaceholderId'}}); expect(menuItemComponent.expandAction).toHaveBeenCalledWith(false); }); }); describe('ngOnChanges', () => { it('calls checkDropdownToggle when menuItem.nestedItems exists', () => { menuItemComponent.menuItem.nestedItems = [ {label: 'placeholder', menuId: 'placeholderId', expanded: false}]; spyOn(menuItemComponent, 'checkDropdownToggle'); menuItemComponent.ngOnChanges({activeMenuId: {currentValue: 'placeholderId'}}); expect(menuItemComponent.checkDropdownToggle).toHaveBeenCalled(); }); it('toggles active to !active if menuItem.nestedItems is undefined and menuItem.menuId is changes.activeMenuId.currentValue', () => { menuItemComponent.active = false; menuItemComponent.menuItem.nestedItems = null; menuItemComponent.ngOnChanges({activeMenuId: {currentValue: 'placeholderId'}}); expect(menuItemComponent.active).toBe(true); }); it('sets active to false if menuItem.nestedItems is undefined and menuItem.menuId is not changes.activeMenuId.currentValue', () => { menuItemComponent.active = true; menuItemComponent.menuItem.nestedItems = null; menuItemComponent.ngOnChanges({activeMenuId: {currentValue: 'notPlaceholderId'}}); expect(menuItemComponent.active).toBe(false); }); }); });