Skip to content

Commit

Permalink
test(libs-credentials): add more tests to improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
waddaboo committed Jul 4, 2024
1 parent 8e1b837 commit c0282c8
Showing 1 changed file with 121 additions and 1 deletion.
122 changes: 121 additions & 1 deletion libs/credentials/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import {
validateCredentials,
validateManyCredentials
} from "./validateCredentials"
import { testUtils } from "."
import { githubFollowers, testUtils } from "."
import { evaluate, tokenize } from "./evaluateExpression"
import checkCriteria from "./checkCriteria"
import getJsonRpcProvider from "./getJsonRpcProvider"
import queryGraph from "./queryGraph"

describe("Credentials library", () => {
describe("# addProvider", () => {
Expand Down Expand Up @@ -78,11 +81,115 @@ describe("Credentials library", () => {
})
})

describe("# getJsonRpcProvider", () => {
it("Should return a JSON-RPC Provider", () => {
const jsonRpcProvider = getJsonRpcProvider(
"https://rpc.sepolia.org"
)

expect(jsonRpcProvider).toBeDefined()
})
})

describe("# checkCriteria", () => {
it("Should return undefined for a valid criteria", () => {
const criteria = {
minFollowers: 12
}
const check = checkCriteria(criteria, githubFollowers.criteriaABI)

expect(check).toBeUndefined()
})

it("Should return undefined for a valid criteria with multiple parameters", () => {
const criteria = {
minTransactions: 12,
network: "sepolia"
}
const check = checkCriteria(
criteria,
blockchainTransactions.criteriaABI
)

expect(check).toBeUndefined()
})

it("Should throw an error if the criteria parameter is undefined", () => {
const criteria = {}
const fun = () =>
checkCriteria(criteria, githubFollowers.criteriaABI)

expect(fun).toThrow(`Parameter 'minFollowers' has not been defined`)
})

it("Should throw an error if the criteria parameter is not a part of the criteria", () => {
const criteria = {
minFollowers: 12,
minBalance: "10"
}
const fun = () =>
checkCriteria(criteria, githubFollowers.criteriaABI)

expect(fun).toThrow(
`Parameter 'minBalance' should not be part of the criteria`
)
})

it("Should throw an error if the criteria parameter has the wrong data type", () => {
const criteria = {
minFollowers: "12"
}
const fun = () =>
checkCriteria(criteria, githubFollowers.criteriaABI)

expect(fun).toThrow(`Parameter 'minFollowers' is not a number`)
})
})

describe("# queryGraph", () => {
it("Should return a function that can be used to query graphs data using GraphQL", () => {
const query = queryGraph(
"https://easscan.org/graphql",
`
query {
attestations {
recipient
attester
revocable
revoked
schemaId
isOffchain
}
}
`
)

expect(query).toBeUndefined()
})
})

describe("# validateCredentials", () => {
const jsonRpcProviderMocked = {
getBalance: jest.fn(),
getTransactionCount: jest.fn()
}
it("Should throw an error if the credentials cannot be validated", async () => {
const fun = validateCredentials(
{
id: githubPersonalStars.id,
criteria: {
minStars: 100
}
},
{
address: "0x",
jsonRpcProvider: null
}
)

await expect(fun).rejects.toThrow("Credentials cannot be validated")
})

it("Should return true if an account has a balance greater than or equal to 10", async () => {
jsonRpcProviderMocked.getBalance.mockReturnValue(
BigNumber.from("12000000000000000000")
Expand Down Expand Up @@ -279,6 +386,19 @@ describe("Credentials library", () => {
const result = evaluate(expression)
expect(result).toBeTruthy()
})
it("Should sucessfully evaluate a logical expression with the and or not and xor operators in parentheses", () => {
const expression = [
"true",
"and",
"false",
"or",
"(",
"not",
")"
]
const result = evaluate(expression)
expect(result).toBeTruthy()
})
})
})
})

0 comments on commit c0282c8

Please sign in to comment.