Skip to content

Commit

Permalink
Merge pull request #587 from jaykayudo/refactor/refactor-test
Browse files Browse the repository at this point in the history
Remove global apiSdk variable in test and make it a local constant
  • Loading branch information
vplasencia authored Oct 26, 2024
2 parents a9e6146 + 2581e0e commit 3323d18
Showing 1 changed file with 35 additions and 40 deletions.
75 changes: 35 additions & 40 deletions libs/api-sdk/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jest.mock("@bandada/utils", () => {
const requestMocked = request as jest.MockedFunction<typeof request>

describe("Bandada API SDK", () => {
let apiSdk: ApiSdk
describe("ApiSdk constructor", () => {
it("Should create new ApiSdk instance without url and config", () => {
const config = {
Expand All @@ -32,7 +31,7 @@ describe("Bandada API SDK", () => {
},
baseURL: SupportedUrl.PROD
}
apiSdk = new ApiSdk()
const apiSdk: ApiSdk = new ApiSdk()
expect(apiSdk.url).toBe(SupportedUrl.PROD)
expect(JSON.stringify(apiSdk.config)).toBe(JSON.stringify(config))
})
Expand All @@ -44,7 +43,7 @@ describe("Bandada API SDK", () => {
},
baseURL: url
}
apiSdk = new ApiSdk(url)
const apiSdk: ApiSdk = new ApiSdk(url)
expect(apiSdk.url).toBe(url)
expect(JSON.stringify(apiSdk.config)).toBe(JSON.stringify(config))
})
Expand All @@ -55,7 +54,7 @@ describe("Bandada API SDK", () => {
},
baseURL: SupportedUrl.PROD
}
apiSdk = new ApiSdk(SupportedUrl.PROD, config)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.PROD, config)
expect(apiSdk.url).toBe(SupportedUrl.PROD)
expect(apiSdk.config).toBe(config)
})
Expand All @@ -66,16 +65,12 @@ describe("Bandada API SDK", () => {
},
baseURL: SupportedUrl.PROD
}
const fun = () => {
apiSdk = new ApiSdk(SupportedUrl.DEV, config)
}
const fun = () => new ApiSdk(SupportedUrl.DEV, config)
expect(fun).toThrow("The url and baseURL should be the same")
})
it("Should throw an error when the url has the wrong type", () => {
const url = 123
const fun = () => {
apiSdk = new ApiSdk(url as any)
}
const fun = () => new ApiSdk(url as any)
expect(fun).toThrow("Parameter 'url' is not a string")
})
it("Should add the baseURL to config", () => {
Expand All @@ -84,7 +79,7 @@ describe("Bandada API SDK", () => {
"Content-Type": "text/html"
}
}
apiSdk = new ApiSdk(SupportedUrl.DEV, config)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV, config)
expect(apiSdk.url).toBe(SupportedUrl.DEV)
})
})
Expand Down Expand Up @@ -115,7 +110,7 @@ describe("Bandada API SDK", () => {
])
)

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const group: Group = await apiSdk.createGroup(
expectedGroup,
apiKey
Expand Down Expand Up @@ -164,7 +159,7 @@ describe("Bandada API SDK", () => {
])
)

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const group: Group = await apiSdk.createGroup(
expectedGroup,
apiKey
Expand Down Expand Up @@ -227,7 +222,7 @@ describe("Bandada API SDK", () => {
])
)

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const group: Group = await apiSdk.createGroup(
expectedGroup,
apiKey
Expand Down Expand Up @@ -290,7 +285,7 @@ describe("Bandada API SDK", () => {
])
)

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const groups: Array<Group> = await apiSdk.createGroups(
[expectedGroups[0], expectedGroups[1]],
apiKey
Expand Down Expand Up @@ -320,7 +315,7 @@ describe("Bandada API SDK", () => {
}
]
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"
apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const fun = apiSdk.createGroups([expectedGroups[0]], apiKey)

await expect(fun).rejects.toThrow(
Expand All @@ -335,7 +330,7 @@ describe("Bandada API SDK", () => {

requestMocked.mockImplementationOnce(() => Promise.resolve())

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const res = await apiSdk.removeGroup(groupId, apiKey)
expect(res).toBeUndefined()
})
Expand All @@ -350,7 +345,7 @@ describe("Bandada API SDK", () => {

requestMocked.mockImplementationOnce(() => Promise.resolve())

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const res = await apiSdk.removeGroups(groupIds, apiKey)
expect(res).toBeUndefined()
})
Expand Down Expand Up @@ -379,7 +374,7 @@ describe("Bandada API SDK", () => {
})
)

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const group: Group = await apiSdk.updateGroup(
groupId,
updatedGroup,
Expand Down Expand Up @@ -440,7 +435,7 @@ describe("Bandada API SDK", () => {
])
)

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const groups: Array<Group> = await apiSdk.updateGroups(
groupIds,
updatedGroups,
Expand Down Expand Up @@ -473,7 +468,7 @@ describe("Bandada API SDK", () => {
}
])
)
apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const groups: Group[] = await apiSdk.getGroups()
expect(groups).toHaveLength(1)
})
Expand All @@ -493,7 +488,7 @@ describe("Bandada API SDK", () => {
}
])
)
apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const groups: Group[] = await apiSdk.getGroups()
expect(groups).toHaveLength(1)
expect(groups[0].credentials).toBeNull()
Expand All @@ -520,7 +515,7 @@ describe("Bandada API SDK", () => {
const adminId =
"0xdf558148e66850ac48dbe2c8119b0eefa7d08bfd19c997c90a142eb97916b847"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const groups: Group[] = await apiSdk.getGroupsByAdminId(adminId)
expect(groups).toHaveLength(1)
groups.forEach((group: Group) => {
Expand All @@ -547,7 +542,7 @@ describe("Bandada API SDK", () => {
const adminId =
"0xdf558148e66850ac48dbe2c8119b0eefa7d08bfd19c997c90a142eb97916b847"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const groups: Group[] = await apiSdk.getGroupsByAdminId(adminId)
expect(groups).toHaveLength(1)
groups.forEach((group: Group) => {
Expand Down Expand Up @@ -576,7 +571,7 @@ describe("Bandada API SDK", () => {

const memberId = "1"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const groups: Group[] = await apiSdk.getGroupsByMemberId(
memberId
)
Expand All @@ -601,7 +596,7 @@ describe("Bandada API SDK", () => {

const memberId = "1"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const groups: Group[] = await apiSdk.getGroupsByMemberId(
memberId
)
Expand All @@ -626,7 +621,7 @@ describe("Bandada API SDK", () => {
)
const groupId = "10402173435763029700781503965100"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const group: Group = await apiSdk.getGroup(groupId)
expect(group.id).toBe(groupId)
})
Expand All @@ -653,7 +648,7 @@ describe("Bandada API SDK", () => {
)
const groupId = "10402173435763029700781503965100"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const group: Group = await apiSdk.getGroup(groupId)
expect(group.id).toBe(groupId)
expect(group.credentials).toStrictEqual(credentials)
Expand All @@ -674,7 +669,7 @@ describe("Bandada API SDK", () => {
)
const groupId = "10402173435763029700781503965100"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const group: Group = await apiSdk.getGroup(groupId)
expect(group.id).toBe(groupId)
expect(group.credentials).toBeNull()
Expand All @@ -689,7 +684,7 @@ describe("Bandada API SDK", () => {
const groupId = "10402173435763029700781503965100"
const memberId = "1"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const isMember: boolean = await apiSdk.isGroupMember(
groupId,
memberId
Expand All @@ -704,7 +699,7 @@ describe("Bandada API SDK", () => {
const groupId = "10402173435763029700781503965100"
const memberId = "2"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const isMember: boolean = await apiSdk.isGroupMember(
groupId,
memberId
Expand Down Expand Up @@ -744,7 +739,7 @@ describe("Bandada API SDK", () => {
const groupId = "10402173435763029700781503965100"
const memberId = "1"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const proof: string = await apiSdk.generateMerkleProof(
groupId,
memberId
Expand All @@ -760,7 +755,7 @@ describe("Bandada API SDK", () => {
const memberId = "1"
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const res = await apiSdk.addMemberByApiKey(
groupId,
memberId,
Expand All @@ -775,7 +770,7 @@ describe("Bandada API SDK", () => {
const memberId = "1"
const inviteCode = "MQYS4UR5"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const res = await apiSdk.addMemberByInviteCode(
groupId,
memberId,
Expand All @@ -792,7 +787,7 @@ describe("Bandada API SDK", () => {
const memberIds = ["1", "2", "3"]
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const res = await apiSdk.addMembersByApiKey(
groupId,
memberIds,
Expand All @@ -810,7 +805,7 @@ describe("Bandada API SDK", () => {
const memberId = "1"
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const res = await apiSdk.removeMemberByApiKey(
groupId,
memberId,
Expand All @@ -830,7 +825,7 @@ describe("Bandada API SDK", () => {
const memberIds = ["1", "2", "3"]
const apiKey = "70f07d0d-6aa2-4fe1-b4b9-06c271a641dc"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const res = await apiSdk.removeMembersByApiKey(
groupId,
memberIds,
Expand All @@ -848,7 +843,7 @@ describe("Bandada API SDK", () => {
const providerName = "github"
const redirectUri = "http://localhost:3003"

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const res = apiSdk.getCredentialGroupJoinUrl(
dashboardUrl,
groupId,
Expand Down Expand Up @@ -895,7 +890,7 @@ describe("Bandada API SDK", () => {
})
)

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const invite: Invite = await apiSdk.createInvite(groupId, apiKey)

expect(invite.code).toBe(inviteCode)
Expand Down Expand Up @@ -934,7 +929,7 @@ describe("Bandada API SDK", () => {
})
)

apiSdk = new ApiSdk(SupportedUrl.DEV)
const apiSdk: ApiSdk = new ApiSdk(SupportedUrl.DEV)
const invite: Invite = await apiSdk.getInvite(inviteCode)

expect(invite.code).toBe(inviteCode)
Expand Down

0 comments on commit 3323d18

Please sign in to comment.