Skip to content

Commit

Permalink
Merge pull request #3527 from mhenkens/feature/ui-stark-app-menu
Browse files Browse the repository at this point in the history
feat(stark-ui): keep highlight the menu when go to child state
  • Loading branch information
SuperITMan authored Nov 25, 2022
2 parents a5f24ca + 52cd434 commit a6191cc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ describe("AppMenuItemComponent", () => {
* Synchronous beforeEach
*/
beforeEach(() => {
mockRoutingService.addTransitionHook.calls.reset();
hostFixture = TestBed.createComponent(TestHostComponent);
hostComponent = hostFixture.componentInstance;
hostComponent.level = 1;
hostFixture.detectChanges();
component = hostComponent.starkAppMenuItem;
mockRoutingService.navigateTo.calls.reset();
mockRoutingService.isCurrentUiStateIncludedIn.calls.reset();
mockRoutingService.isCurrentUiState.calls.reset();
});

describe("simple menu item", () => {
Expand Down Expand Up @@ -135,10 +138,20 @@ describe("AppMenuItemComponent", () => {
it("should dispatch 'deactivated' event when 'isActive' is false", () => {
spyOn(component.deactivated, "emit");
component.isActive = false;
hostFixture.detectChanges();

expect(component.deactivated.emit).toHaveBeenCalledTimes(1);
});

it("should activate when child state is selected and do not have child menu", () => {
hostFixture.detectChanges();
// `stateChange` is attached to `routingTransitionSuccessCallback` in `StarkAppMenuItemComponent`
const stateChange = <() => void>mockRoutingService.addTransitionHook.calls.first().args[2];
mockRoutingService.isCurrentUiStateIncludedIn.withArgs("test").and.returnValue(true);
stateChange();
expect(component.isActive).toBeTrue();
expect(mockRoutingService.isCurrentUiStateIncludedIn).toHaveBeenCalledWith("test");
expect(mockRoutingService.isCurrentUiState).not.toHaveBeenCalledWith("test");
});
});

describe("menu item with children", () => {
Expand Down Expand Up @@ -166,5 +179,16 @@ describe("AppMenuItemComponent", () => {
const expansionPanel: HTMLElement = hostFixture.nativeElement.querySelector(".mat-expansion-panel");
expect(expansionPanel).toBeDefined();
});

it("should not activate when child state is selected and do not have child menu", () => {
hostFixture.detectChanges();
// `stateChange` is attached to `routingTransitionSuccessCallback` in `StarkAppMenuItemComponent`
const stateChange = <() => void>mockRoutingService.addTransitionHook.calls.first().args[2];
mockRoutingService.isCurrentUiState.withArgs("test").and.returnValue(false);
stateChange();
expect(component.isActive).toBeFalse();
expect(mockRoutingService.isCurrentUiStateIncludedIn).not.toHaveBeenCalledWith("test");
expect(mockRoutingService.isCurrentUiState).toHaveBeenCalledWith("test");
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ export class StarkAppMenuItemComponent extends AbstractStarkUiComponent implemen
* Utility function to check if the component targetState is the current state
*/
public isCurrentState(): boolean {
if(!this.menuGroup.entries) {
return this.menuGroup.targetState ? this.routingService.isCurrentUiStateIncludedIn(this.menuGroup.targetState): false;
}
return this.menuGroup.targetState ? this.routingService.isCurrentUiState(this.menuGroup.targetState) : false;
}

Expand Down

0 comments on commit a6191cc

Please sign in to comment.