Skip to content

Commit

Permalink
feat(oncall user): ♻️ remove duplicates from oncall user list
Browse files Browse the repository at this point in the history
remove duplicates from oncall user list
  • Loading branch information
t1agob committed Nov 15, 2023
1 parent e5d5bbd commit 4e6d79b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
6 changes: 3 additions & 3 deletions dev/mockPagerDutyApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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)],
};
},

Expand Down
55 changes: 54 additions & 1 deletion src/components/Escalation/Escalation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ describe('Escalation', () => {
} as PagerDutyUser,
escalation_level: 1,
},

],
}));

Expand All @@ -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(
<ApiProvider apis={apis}>
<EscalationPolicy policyId="abc" />
</ApiProvider>
)
);
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()
Expand Down
11 changes: 11 additions & 0 deletions src/components/Escalation/EscalationPolicy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down

0 comments on commit 4e6d79b

Please sign in to comment.