mengw15 commented on code in PR #6390: URL: https://github.com/apache/texera/pull/6390#discussion_r3568487882
########## frontend/src/app/hub/service/hub.service.spec.ts: ########## @@ -0,0 +1,240 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 { TestBed } from "@angular/core/testing"; +import { HttpClientTestingModule, HttpTestingController } from "@angular/common/http/testing"; +import { + AccessResponse, + ActionType, + CountResponse, + EntityType, + HubService, + LikedStatus, + WORKFLOW_BASE_URL, +} from "./hub.service"; + +describe("HubService", () => { + let service: HubService; + let httpMock: HttpTestingController; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [HubService], + }); + service = TestBed.inject(HubService); + httpMock = TestBed.inject(HttpTestingController); + }); + + afterEach(() => { + httpMock.verify(); + }); + + it("should be created", () => { + expect(service).toBeTruthy(); + }); + + it("getCount GETs /count with the entityType param and emits the count", () => { + let result: number | undefined; + service.getCount(EntityType.Workflow).subscribe(n => (result = n)); + + const req = httpMock.expectOne(r => r.url === `${service.BASE_URL}/count`); + expect(req.request.method).toBe("GET"); + expect(req.request.params.get("entityType")).toBe("workflow"); + + req.flush(5); + expect(result).toBe(5); + }); + + it("cloneWorkflow POSTs to /workflow/clone/:wid with a null body and emits the new wid", () => { + let result: number | undefined; + service.cloneWorkflow(42).subscribe(n => (result = n)); + + const req = httpMock.expectOne(`${WORKFLOW_BASE_URL}/clone/42`); + expect(req.request.method).toBe("POST"); + expect(req.request.body).toBeNull(); + + req.flush(99); + expect(result).toBe(99); + }); + + it("isLiked GETs /isLiked appending every id and type, and emits the statuses", () => { + const statuses: LikedStatus[] = [ + { entityId: 1, entityType: EntityType.Workflow, isLiked: true }, + { entityId: 2, entityType: EntityType.Dataset, isLiked: false }, + ]; + let result: LikedStatus[] | undefined; + service.isLiked([1, 2], [EntityType.Workflow, EntityType.Dataset]).subscribe(s => (result = s)); + + const req = httpMock.expectOne(r => r.url === `${service.BASE_URL}/isLiked`); + expect(req.request.method).toBe("GET"); + expect(req.request.params.getAll("entityId")).toEqual(["1", "2"]); + expect(req.request.params.getAll("entityType")).toEqual(["workflow", "dataset"]); + + req.flush(statuses); + expect(result).toEqual(statuses); + }); + + it("postLike POSTs /like with a json body and emits the boolean result", () => { + let result: boolean | undefined; + service.postLike(1, EntityType.Workflow).subscribe(b => (result = b)); + + const req = httpMock.expectOne(`${service.BASE_URL}/like`); + expect(req.request.method).toBe("POST"); + expect(req.request.body).toEqual({ entityId: 1, entityType: EntityType.Workflow }); + expect(req.request.headers.get("Content-Type")).toBe("application/json"); + + req.flush(true); + expect(result).toBe(true); + }); + + it("postUnlike POSTs /unlike with a json body and emits the boolean result", () => { + let result: boolean | undefined; + service.postUnlike(1, EntityType.Workflow).subscribe(b => (result = b)); + + const req = httpMock.expectOne(`${service.BASE_URL}/unlike`); + expect(req.request.method).toBe("POST"); + expect(req.request.body).toEqual({ entityId: 1, entityType: EntityType.Workflow }); + + req.flush(false); + expect(result).toBe(false); + }); + + it("toggleLike likes (not currently liked) then re-fetches the like count", () => { + let result: { liked: boolean; likeCount: number } | undefined; + service.toggleLike(1, EntityType.Workflow, false).subscribe(r => (result = r)); + + // First request: the like POST. + const likeReq = httpMock.expectOne(r => r.url === `${service.BASE_URL}/like`); + expect(likeReq.request.method).toBe("POST"); + likeReq.flush(true); + + // Second request: the counts GET fired by switchMap. + const countsReq = httpMock.expectOne(r => r.url === `${service.BASE_URL}/counts`); + expect(countsReq.request.method).toBe("GET"); + expect(countsReq.request.params.getAll("actionType")).toEqual(["like"]); + countsReq.flush([{ entityId: 1, entityType: EntityType.Workflow, counts: { like: 7 } }] as CountResponse[]); Review Comment: Addressed — the like path now asserts the POST body `{ entityId, entityType }` and the counts GET's `entityType` / `entityId` / `actionType` params, so a wrong id/type routed to either endpoint now fails the test (verified by mutating the id to a wrong value → red). ########## frontend/src/app/hub/service/hub.service.spec.ts: ########## @@ -0,0 +1,240 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 { TestBed } from "@angular/core/testing"; +import { HttpClientTestingModule, HttpTestingController } from "@angular/common/http/testing"; +import { + AccessResponse, + ActionType, + CountResponse, + EntityType, + HubService, + LikedStatus, + WORKFLOW_BASE_URL, +} from "./hub.service"; + +describe("HubService", () => { + let service: HubService; + let httpMock: HttpTestingController; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [HubService], + }); + service = TestBed.inject(HubService); + httpMock = TestBed.inject(HttpTestingController); + }); + + afterEach(() => { + httpMock.verify(); + }); + + it("should be created", () => { + expect(service).toBeTruthy(); + }); + + it("getCount GETs /count with the entityType param and emits the count", () => { + let result: number | undefined; + service.getCount(EntityType.Workflow).subscribe(n => (result = n)); + + const req = httpMock.expectOne(r => r.url === `${service.BASE_URL}/count`); + expect(req.request.method).toBe("GET"); + expect(req.request.params.get("entityType")).toBe("workflow"); + + req.flush(5); + expect(result).toBe(5); + }); + + it("cloneWorkflow POSTs to /workflow/clone/:wid with a null body and emits the new wid", () => { + let result: number | undefined; + service.cloneWorkflow(42).subscribe(n => (result = n)); + + const req = httpMock.expectOne(`${WORKFLOW_BASE_URL}/clone/42`); + expect(req.request.method).toBe("POST"); + expect(req.request.body).toBeNull(); + + req.flush(99); + expect(result).toBe(99); + }); + + it("isLiked GETs /isLiked appending every id and type, and emits the statuses", () => { + const statuses: LikedStatus[] = [ + { entityId: 1, entityType: EntityType.Workflow, isLiked: true }, + { entityId: 2, entityType: EntityType.Dataset, isLiked: false }, + ]; + let result: LikedStatus[] | undefined; + service.isLiked([1, 2], [EntityType.Workflow, EntityType.Dataset]).subscribe(s => (result = s)); + + const req = httpMock.expectOne(r => r.url === `${service.BASE_URL}/isLiked`); + expect(req.request.method).toBe("GET"); + expect(req.request.params.getAll("entityId")).toEqual(["1", "2"]); + expect(req.request.params.getAll("entityType")).toEqual(["workflow", "dataset"]); + + req.flush(statuses); + expect(result).toEqual(statuses); + }); + + it("postLike POSTs /like with a json body and emits the boolean result", () => { + let result: boolean | undefined; + service.postLike(1, EntityType.Workflow).subscribe(b => (result = b)); + + const req = httpMock.expectOne(`${service.BASE_URL}/like`); + expect(req.request.method).toBe("POST"); + expect(req.request.body).toEqual({ entityId: 1, entityType: EntityType.Workflow }); + expect(req.request.headers.get("Content-Type")).toBe("application/json"); + + req.flush(true); + expect(result).toBe(true); + }); + + it("postUnlike POSTs /unlike with a json body and emits the boolean result", () => { + let result: boolean | undefined; + service.postUnlike(1, EntityType.Workflow).subscribe(b => (result = b)); + + const req = httpMock.expectOne(`${service.BASE_URL}/unlike`); + expect(req.request.method).toBe("POST"); + expect(req.request.body).toEqual({ entityId: 1, entityType: EntityType.Workflow }); + + req.flush(false); + expect(result).toBe(false); + }); + + it("toggleLike likes (not currently liked) then re-fetches the like count", () => { + let result: { liked: boolean; likeCount: number } | undefined; + service.toggleLike(1, EntityType.Workflow, false).subscribe(r => (result = r)); + + // First request: the like POST. + const likeReq = httpMock.expectOne(r => r.url === `${service.BASE_URL}/like`); + expect(likeReq.request.method).toBe("POST"); + likeReq.flush(true); + + // Second request: the counts GET fired by switchMap. + const countsReq = httpMock.expectOne(r => r.url === `${service.BASE_URL}/counts`); + expect(countsReq.request.method).toBe("GET"); + expect(countsReq.request.params.getAll("actionType")).toEqual(["like"]); + countsReq.flush([{ entityId: 1, entityType: EntityType.Workflow, counts: { like: 7 } }] as CountResponse[]); + + expect(result).toEqual({ liked: true, likeCount: 7 }); + }); + + it("toggleLike unlikes (currently liked) then re-fetches the like count", () => { + let result: { liked: boolean; likeCount: number } | undefined; + service.toggleLike(1, EntityType.Workflow, true).subscribe(r => (result = r)); + + const unlikeReq = httpMock.expectOne(r => r.url === `${service.BASE_URL}/unlike`); + expect(unlikeReq.request.method).toBe("POST"); + unlikeReq.flush(true); + + const countsReq = httpMock.expectOne(r => r.url === `${service.BASE_URL}/counts`); + countsReq.flush([{ entityId: 1, entityType: EntityType.Workflow, counts: { like: 3 } }] as CountResponse[]); Review Comment: Addressed — the unlike test now asserts the unlike POST body and the counts GET method plus its `entityType` / `entityId` / `actionType` params. ########## frontend/src/app/hub/service/hub.service.spec.ts: ########## @@ -0,0 +1,240 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 { TestBed } from "@angular/core/testing"; +import { HttpClientTestingModule, HttpTestingController } from "@angular/common/http/testing"; +import { + AccessResponse, + ActionType, + CountResponse, + EntityType, + HubService, + LikedStatus, + WORKFLOW_BASE_URL, +} from "./hub.service"; + +describe("HubService", () => { + let service: HubService; + let httpMock: HttpTestingController; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [HubService], + }); + service = TestBed.inject(HubService); + httpMock = TestBed.inject(HttpTestingController); + }); + + afterEach(() => { + httpMock.verify(); + }); + + it("should be created", () => { + expect(service).toBeTruthy(); + }); + + it("getCount GETs /count with the entityType param and emits the count", () => { + let result: number | undefined; + service.getCount(EntityType.Workflow).subscribe(n => (result = n)); + + const req = httpMock.expectOne(r => r.url === `${service.BASE_URL}/count`); + expect(req.request.method).toBe("GET"); + expect(req.request.params.get("entityType")).toBe("workflow"); + + req.flush(5); + expect(result).toBe(5); + }); + + it("cloneWorkflow POSTs to /workflow/clone/:wid with a null body and emits the new wid", () => { + let result: number | undefined; + service.cloneWorkflow(42).subscribe(n => (result = n)); + + const req = httpMock.expectOne(`${WORKFLOW_BASE_URL}/clone/42`); + expect(req.request.method).toBe("POST"); + expect(req.request.body).toBeNull(); + + req.flush(99); + expect(result).toBe(99); + }); + + it("isLiked GETs /isLiked appending every id and type, and emits the statuses", () => { + const statuses: LikedStatus[] = [ + { entityId: 1, entityType: EntityType.Workflow, isLiked: true }, + { entityId: 2, entityType: EntityType.Dataset, isLiked: false }, + ]; + let result: LikedStatus[] | undefined; + service.isLiked([1, 2], [EntityType.Workflow, EntityType.Dataset]).subscribe(s => (result = s)); + + const req = httpMock.expectOne(r => r.url === `${service.BASE_URL}/isLiked`); + expect(req.request.method).toBe("GET"); + expect(req.request.params.getAll("entityId")).toEqual(["1", "2"]); + expect(req.request.params.getAll("entityType")).toEqual(["workflow", "dataset"]); + + req.flush(statuses); + expect(result).toEqual(statuses); + }); + + it("postLike POSTs /like with a json body and emits the boolean result", () => { + let result: boolean | undefined; + service.postLike(1, EntityType.Workflow).subscribe(b => (result = b)); + + const req = httpMock.expectOne(`${service.BASE_URL}/like`); + expect(req.request.method).toBe("POST"); + expect(req.request.body).toEqual({ entityId: 1, entityType: EntityType.Workflow }); + expect(req.request.headers.get("Content-Type")).toBe("application/json"); + + req.flush(true); + expect(result).toBe(true); + }); + + it("postUnlike POSTs /unlike with a json body and emits the boolean result", () => { + let result: boolean | undefined; + service.postUnlike(1, EntityType.Workflow).subscribe(b => (result = b)); + + const req = httpMock.expectOne(`${service.BASE_URL}/unlike`); + expect(req.request.method).toBe("POST"); + expect(req.request.body).toEqual({ entityId: 1, entityType: EntityType.Workflow }); + + req.flush(false); + expect(result).toBe(false); + }); + + it("toggleLike likes (not currently liked) then re-fetches the like count", () => { + let result: { liked: boolean; likeCount: number } | undefined; + service.toggleLike(1, EntityType.Workflow, false).subscribe(r => (result = r)); + + // First request: the like POST. + const likeReq = httpMock.expectOne(r => r.url === `${service.BASE_URL}/like`); + expect(likeReq.request.method).toBe("POST"); + likeReq.flush(true); + + // Second request: the counts GET fired by switchMap. + const countsReq = httpMock.expectOne(r => r.url === `${service.BASE_URL}/counts`); + expect(countsReq.request.method).toBe("GET"); + expect(countsReq.request.params.getAll("actionType")).toEqual(["like"]); + countsReq.flush([{ entityId: 1, entityType: EntityType.Workflow, counts: { like: 7 } }] as CountResponse[]); + + expect(result).toEqual({ liked: true, likeCount: 7 }); + }); + + it("toggleLike unlikes (currently liked) then re-fetches the like count", () => { + let result: { liked: boolean; likeCount: number } | undefined; + service.toggleLike(1, EntityType.Workflow, true).subscribe(r => (result = r)); + + const unlikeReq = httpMock.expectOne(r => r.url === `${service.BASE_URL}/unlike`); + expect(unlikeReq.request.method).toBe("POST"); + unlikeReq.flush(true); + + const countsReq = httpMock.expectOne(r => r.url === `${service.BASE_URL}/counts`); + countsReq.flush([{ entityId: 1, entityType: EntityType.Workflow, counts: { like: 3 } }] as CountResponse[]); + + expect(result).toEqual({ liked: false, likeCount: 3 }); + }); + + it("toggleLike keeps the current state when the action fails and defaults the count to 0", () => { + let result: { liked: boolean; likeCount: number } | undefined; + service.toggleLike(1, EntityType.Workflow, false).subscribe(r => (result = r)); + + httpMock.expectOne(r => r.url === `${service.BASE_URL}/like`).flush(false); + httpMock.expectOne(r => r.url === `${service.BASE_URL}/counts`).flush([] as CountResponse[]); Review Comment: Addressed — the failure-path test now asserts the like POST method/body and the counts GET method + `entityId` before flushing, instead of flushing immediately. -- 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]
