rimashah25 commented on code in PR #7514: URL: https://github.com/apache/trafficcontrol/pull/7514#discussion_r1201507855
########## experimental/traffic-portal/src/app/core/users/roles/detail/role-detail.component.spec.ts: ########## @@ -0,0 +1,143 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { ComponentFixture, TestBed } from "@angular/core/testing"; +import { MatDialog, MatDialogModule, MatDialogRef } from "@angular/material/dialog"; +import { ActivatedRoute } from "@angular/router"; +import { RouterTestingModule } from "@angular/router/testing"; +import { of, ReplaySubject } from "rxjs"; + +import { UserService } from "src/app/api"; +import { APITestingModule } from "src/app/api/testing"; +import { RoleDetailComponent } from "src/app/core/users/roles/detail/role-detail.component"; +import { NavigationService } from "src/app/shared/navigation/navigation.service"; + +describe("RoleDetailComponent", () => { + let component: RoleDetailComponent; + let fixture: ComponentFixture<RoleDetailComponent>; + let route: ActivatedRoute; + let paramMap: jasmine.Spy; + // const name = "test"; + + const headerSvc = jasmine.createSpyObj([],{headerHidden: new ReplaySubject<boolean>(), headerTitle: new ReplaySubject<string>()}); + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ RoleDetailComponent ], + imports: [ + APITestingModule, + RouterTestingModule.withRoutes([ + {component: RoleDetailComponent, path: "core/roles/:name"}, + // This route is never actually loaded, but the tests + // complain that it can't be routed to, so it doesn't matter + // that it's loading the wrong component, only that it has a + // route definition. + {component: RoleDetailComponent, path: "core/roles"} + ]), + MatDialogModule + ], + providers: [ { provide: NavigationService, useValue: headerSvc } ] + }) + .compileComponents(); + + route = TestBed.inject(ActivatedRoute); + paramMap = spyOn(route.snapshot.paramMap, "get"); + // paramMap.and.returnValue(name); + fixture = TestBed.createComponent(RoleDetailComponent); + component = fixture.componentInstance; + component.role = {...await TestBed.inject(UserService).createRole(component.role)}; + fixture.detectChanges(); + }); + + it("should create", () => { + expect(component).toBeTruthy(); + // expect(paramMap).toHaveBeenCalled(); + }); + + it("new role", async () => { + paramMap.and.returnValue(null); + + fixture = TestBed.createComponent(RoleDetailComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + await fixture.whenStable(); + expect(paramMap).toHaveBeenCalled(); + expect(component.role).not.toBeNull(); + expect(component.role.name).toBe(""); + expect(component.new).toBeTrue(); + }); + + it("existing role", async () => { + paramMap.and.returnValue("admin"); + + fixture = TestBed.createComponent(RoleDetailComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + await fixture.whenStable(); + expect(paramMap).toHaveBeenCalled(); + expect(component.role).not.toBeNull(); + expect(component.role.name).toBe("admin"); + expect(component.new).toBeFalse(); + }); + + it("opens a dialog for role deletion", async () => { + const api = TestBed.inject(UserService); + const spy = spyOn(api, "deleteRole").and.callThrough(); + expect(spy).not.toHaveBeenCalled(); + + const dialogService = TestBed.inject(MatDialog); + const openSpy = spyOn(dialogService, "open").and.returnValue({ + afterClosed: () => of(true) + } as MatDialogRef<unknown>); Review Comment: It is erroring otherwise. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
