From 4e6d79b1b2cc998d7356c79793d1bc0eb5d435b6 Mon Sep 17 00:00:00 2001 From: Tiago Barbosa Date: Wed, 15 Nov 2023 22:48:25 +0000 Subject: [PATCH] feat(oncall user): :recycle: remove duplicates from oncall user list remove duplicates from oncall user list --- dev/mockPagerDutyApi.ts | 6 +- src/components/Escalation/Escalation.test.tsx | 55 ++++++++++++++++++- .../Escalation/EscalationPolicy.tsx | 11 ++++ 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/dev/mockPagerDutyApi.ts b/dev/mockPagerDutyApi.ts index 47bb232..270c490 100644 --- a/dev/mockPagerDutyApi.ts +++ b/dev/mockPagerDutyApi.ts @@ -124,10 +124,10 @@ export const mockPagerDutyApi: PagerDutyApi = { }, async getOnCallByPolicyId() { - const oncall = (name: string, escalation: number) => { + const oncall = (id: string, name: string, escalation: number) => { return { user: { - id: '123', + id: id, name: name, html_url: 'http://assignee', summary: 'summary', @@ -139,7 +139,7 @@ export const mockPagerDutyApi: PagerDutyApi = { }; return { - oncalls: [oncall('Jane Doe', 1), oncall('John Doe', 2), oncall('James Doe', 1)], + oncalls: [oncall('1', 'Jane Doe', 1), oncall('2', 'John Doe', 2), oncall('3', 'James Doe', 1)], }; }, diff --git a/src/components/Escalation/Escalation.test.tsx b/src/components/Escalation/Escalation.test.tsx index 85b3eda..7524c44 100644 --- a/src/components/Escalation/Escalation.test.tsx +++ b/src/components/Escalation/Escalation.test.tsx @@ -61,7 +61,6 @@ describe('Escalation', () => { } as PagerDutyUser, escalation_level: 1, }, - ], })); @@ -79,6 +78,60 @@ describe('Escalation', () => { expect(mockPagerDutyApi.getOnCallByPolicyId).toHaveBeenCalledWith('abc'); }); + it("Renders a list of users without duplicates", async () => { + mockPagerDutyApi.getOnCallByPolicyId = jest + .fn() + .mockImplementationOnce(async () => ({ + oncalls: [ + { + user: { + name: "person1", + id: "p1", + summary: "person1", + email: "person1@example.com", + html_url: "http://a.com/id1", + } as PagerDutyUser, + escalation_level: 1, + }, + { + user: { + name: "person2", + id: "p2", + summary: "person2", + email: "person2@example.com", + html_url: "http://a.com/id2", + } as PagerDutyUser, + escalation_level: 1, + }, + { + user: { + name: "person2", + id: "p2", + summary: "person2", + email: "person2@example.com", + html_url: "http://a.com/id2", + } as PagerDutyUser, + escalation_level: 1, + }, + ], + })); + + const { getByText, queryByTestId, queryAllByText } = render( + wrapInTestApp( + + + + ) + ); + await waitFor(() => !queryByTestId("progress")); + + expect(getByText("person1")).toBeInTheDocument(); + expect(getByText("person1@example.com")).toBeInTheDocument(); + expect(queryAllByText("person2").length).toBe(1); + expect(queryAllByText("person2@example.com").length).toBe(1); + expect(mockPagerDutyApi.getOnCallByPolicyId).toHaveBeenCalledWith("abc"); + }); + it("Renders only user(s) in escalation level 1", async () => { mockPagerDutyApi.getOnCallByPolicyId = jest .fn() diff --git a/src/components/Escalation/EscalationPolicy.tsx b/src/components/Escalation/EscalationPolicy.tsx index 01f9a78..21e41ca 100644 --- a/src/components/Escalation/EscalationPolicy.tsx +++ b/src/components/Escalation/EscalationPolicy.tsx @@ -43,6 +43,17 @@ export const EscalationPolicy = ({ policyId }: Props) => { .sort((a, b) => a.user.name > b.user.name ? 1 : -1) .map((oncall) => oncall.user); + // remove duplicates from usersItem + const uniqueUsers = new Map(); + usersItem.forEach((user) => { + uniqueUsers.set(user.id, user); + }); + + usersItem.length = 0; + uniqueUsers.forEach((user) => { + usersItem.push(user); + }); + return usersItem; });