Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chasefleming committed Feb 5, 2025
1 parent 8d6d1d9 commit fed3097
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ describe("AccountManager", () => {
})
})

it("should get or create COA address", async () => {
const user = mockUser()
const accountManager = new AccountManager(user.mock)

// First call to `getAccounts` returns an empty array
jest.spyOn(accountManager, "getAccounts").mockResolvedValueOnce([])

jest.spyOn(accountManager, "createCOA").mockImplementation(async () => {
jest.spyOn(accountManager, "getAccounts").mockResolvedValueOnce(["0x123"])
return "0x123"
})

await expect(accountManager.getAndCreateAccounts()).resolves.toEqual([
"0x123",
])

expect(accountManager.createCOA).toHaveBeenCalledTimes(1)
})

it("should not update COA address if user has not changed", async () => {
const user = mockUser()
mockQuery.mockResolvedValue("0x123")
Expand Down
12 changes: 6 additions & 6 deletions packages/fcl-ethereum-provider/src/accounts/account-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ transaction() {
signer.capabilities.publish(cap, at: publicPath)
}
}
`;
`

export class AccountManager {
private $addressStore = new BehaviorSubject<{
Expand Down Expand Up @@ -153,18 +153,18 @@ export class AccountManager {
* Get the COA address and create it if it doesn't exist
*/
public async getAndCreateAccounts(): Promise<string[]> {
let coaAddress = await this.getCOAAddress()
let accounts = await this.getAccounts()

if (!coaAddress) {
if (accounts.length === 0) {
await this.createCOA()
coaAddress = await this.getCOAAddress()
accounts = await this.getAccounts()
}

if (!coaAddress) {
if (accounts.length === 0) {
throw new Error("COA address is still missing after creation.")
}

return [coaAddress]
return accounts
}

public async createCOA(): Promise<string> {
Expand Down

0 comments on commit fed3097

Please sign in to comment.