Skip to content

Commit

Permalink
feat(data): add getGroupMembers to SemaphoreSubgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
zkfriendly committed Feb 26, 2024
1 parent 7af787a commit 8f73965
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/data/src/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ export default class SemaphoreSubgraph {
return groups[0]
}

/**
* Returns a list of group members.
* @param groupId Group id.
* @returns Group members.
*/
async getGroupMembers(groupId: string): Promise<string[]> {
const group = await this.getGroup(groupId, { members: true }) // parameters are checked inside getGroup
return group.members ?? []
}

/**
* Returns true if a member is part of group, and false otherwise.
* @param groupId Group id
Expand Down
40 changes: 40 additions & 0 deletions packages/data/tests/subgraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,46 @@ describe("SemaphoreSubgraph", () => {
})
})

describe("# getGroupMembers", () => {
it("Should return a list of group members", async () => {
requestMocked.mockImplementationOnce(() =>
Promise.resolve({
groups: [
{
id: "1",
merkleTree: {
depth: 20,
size: 2,
root: "2"
},
admin: "0x7bcd6f009471e9974a77086a69289d16eadba286",
members: [
{
identityCommitment: "20"
},
{
identityCommitment: "17"
}
]
}
]
})
)

const expectedValue = await semaphore.getGroupMembers("1")

expect(expectedValue).toBeDefined()
expect(Array.isArray(expectedValue)).toBeTruthy()
expect(expectedValue[0]).toBe("20")
expect(expectedValue[1]).toBe("17")
})

it("Should throw an error if the groupId parameter type is wrong", async () => {
const fun = () => semaphore.getGroupMembers(1 as any)
await expect(fun).rejects.toThrow("Parameter 'groupId' is not a string")
})
})

describe("# isGroupMember", () => {
it("Should throw an error if the member parameter type is wrong", async () => {
const fun = () => semaphore.isGroupMember("1", 1 as any)
Expand Down

0 comments on commit 8f73965

Please sign in to comment.