Skip to content

Commit

Permalink
Merge pull request #3140 from tahowallet/abilities-tests
Browse files Browse the repository at this point in the history
Add  basic tests for abilities
  • Loading branch information
0xDaedalus authored Mar 17, 2023
2 parents f2ebb62 + 933507f commit d42e4ae
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 1 deletion.
2 changes: 1 addition & 1 deletion background/services/abilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const normalizeDaylightRequirements = (
}
}

const normalizeDaylightAbilities = (
export const normalizeDaylightAbilities = (
daylightAbilities: DaylightAbility[],
address: string
): Ability[] => {
Expand Down
141 changes: 141 additions & 0 deletions background/services/abilities/tests/index.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import sinon from "sinon"
import AbilitiesService, { normalizeDaylightAbilities } from ".."
import {
createAbilitiesService,
createChainService,
createDaylightAbility,
} from "../../../tests/factories"
import ChainService from "../../chain"
import * as daylight from "../../../lib/daylight"

const TEST_ADDRESS = "0x208e94d5661a73360d9387d3ca169e5c130090cd"

describe("AbilitiesService", () => {
const sandbox = sinon.createSandbox()
let abilitiesService: AbilitiesService
let chainService: ChainService

beforeEach(async () => {
sandbox.restore()
chainService = await createChainService()
abilitiesService = await createAbilitiesService({
chainService: Promise.resolve(chainService),
})
await abilitiesService.startService()
})

afterEach(async () => {
await abilitiesService.stopService()
})

describe("refreshAbilities", () => {
beforeEach(async () => {
// Sun Jan 01 2023 10:00:00
Date.now = jest.fn(() => +new Date(2023, 0, 1, 10))
})

afterEach(async () => {
jest.clearAllMocks()
})

it("should load accounts to refresh abilities when the last refresh was over an hour ago", async () => {
// Sun Jan 01 2023 08:00:00
const lastFetchTime = +new Date(2023, 0, 1, 8)
localStorage.setItem("LAST_ABILITY_FETCH_TIME", lastFetchTime.toString())
const stub = sandbox
.stub(chainService, "getAccountsToTrack")
.callsFake(async () => [])

await abilitiesService.refreshAbilities()
expect(stub.called).toBe(true)
})
it("should not load accounts to refresh abilities when the last refresh was less than an hour ago", async () => {
// Sun Jan 01 2023 09:30:00
const lastFetchTime = +new Date(2023, 0, 1, 9, 30)
localStorage.setItem("LAST_ABILITY_FETCH_TIME", lastFetchTime.toString())
const stub = sandbox
.stub(chainService, "getAccountsToTrack")
.callsFake(async () => [])

await abilitiesService.refreshAbilities()
expect(stub.called).toBe(false)
})
})

describe("pollForAbilities", () => {
beforeEach(async () => {
jest.spyOn(abilitiesService.emitter, "emit")
})

afterEach(async () => {
jest.clearAllMocks()
})

it("should not emit newAbilities if there are no new abilities", async () => {
const stub = sandbox
.stub(daylight, "getDaylightAbilities")
.callsFake(async () => [])

await abilitiesService.pollForAbilities(TEST_ADDRESS)
expect(stub.called).toBe(true)
expect(abilitiesService.emitter.emit).toBeCalledTimes(0)
})

it("should emit newAbilities if there is a new ability", async () => {
const daylightAbilities = [createDaylightAbility()]
const stubAddNewAbility = sandbox
// eslint-disable-next-line @typescript-eslint/dot-notation
.stub(abilitiesService["db"], "addNewAbility")
.callsFake(async () => true)
const stubGetAbilities = sandbox
.stub(daylight, "getDaylightAbilities")
.callsFake(async () => daylightAbilities)

await abilitiesService.pollForAbilities(TEST_ADDRESS)

const normalizedAbilities = normalizeDaylightAbilities(
daylightAbilities,
TEST_ADDRESS
)

expect(stubGetAbilities.called).toBe(true)
expect(stubAddNewAbility.called).toBe(true)
expect(abilitiesService.emitter.emit).toBeCalledTimes(1)
expect(abilitiesService.emitter.emit).toBeCalledWith(
"newAbilities",
normalizedAbilities
)
})

it("should emit newAbilities when only one ability is new", async () => {
const slug = "new-test-daylight"
const daylightAbilities = [
createDaylightAbility(),
createDaylightAbility({ slug }),
]
const stubAddNewAbility = sandbox
// eslint-disable-next-line @typescript-eslint/dot-notation
.stub(abilitiesService["db"], "addNewAbility")
.callsFake(async (ability) => {
return ability.slug === slug
})
const stubGetAbilities = sandbox
.stub(daylight, "getDaylightAbilities")
.callsFake(async () => daylightAbilities)

await abilitiesService.pollForAbilities(TEST_ADDRESS)

const normalizedAbilities = normalizeDaylightAbilities(
daylightAbilities,
TEST_ADDRESS
)

expect(stubGetAbilities.called).toBe(true)
expect(stubAddNewAbility.called).toBe(true)
expect(abilitiesService.emitter.emit).toBeCalledTimes(1)
expect(abilitiesService.emitter.emit).toBeCalledWith("newAbilities", [
normalizedAbilities[1],
])
})
})
})
46 changes: 46 additions & 0 deletions background/tests/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
POLYGON,
USD,
} from "../constants"
import { DaylightAbility } from "../lib/daylight"
import {
AnyEVMTransaction,
LegacyEVMTransactionRequest,
Expand All @@ -45,6 +46,7 @@ import {
ProviderBridgeService,
SigningService,
} from "../services"
import AbilitiesService from "../services/abilities"
import {
PriorityQueuedTxToRetrieve,
QueuedTxToRetrieve,
Expand Down Expand Up @@ -115,6 +117,11 @@ type CreateSigningServiceOverrides = {
chainService?: Promise<ChainService>
}

type CreateAbilitiesServiceOverrides = {
ledgerService?: Promise<LedgerService>
chainService?: Promise<ChainService>
}

type CreateProviderBridgeServiceOverrides = {
internalEthereumProviderService?: Promise<InternalEthereumProviderService>
preferenceService?: Promise<PreferenceService>
Expand Down Expand Up @@ -147,6 +154,15 @@ export const createSigningService = async (
)
}

export const createAbilitiesService = async (
overrides: CreateAbilitiesServiceOverrides = {}
): Promise<AbilitiesService> => {
return AbilitiesService.create(
overrides.chainService ?? createChainService(),
overrides.ledgerService ?? createLedgerService()
)
}

export const createInternalEthereumProviderService = async (
overrides: CreateInternalEthereumProviderServiceOverrides = {}
): Promise<InternalEthereumProviderService> => {
Expand Down Expand Up @@ -424,6 +440,36 @@ export const createAssetAmount = (
}
}

export const createDaylightAbility = (
overrides: Partial<DaylightAbility> = {}
): DaylightAbility => ({
type: "mint",
title: "Test ability!",
description: "Test description",
imageUrl: "./images/test.png",
openAt: null,
closeAt: null,
isClosed: false,
createdAt: "2023-02-20T17:24:25.000Z",
chain: "ethereum",
sourceId: "",
uid: getRandomStr(5),
slug: getRandomStr(5),
requirements: [
{
type: "onAllowlist",
chain: "ethereum",
addresses: ["0x208e94d5661a73360d9387d3ca169e5c130090cd"],
},
],
action: {
linkUrl: "",
completedBy: [],
},
walletCompleted: false,
...overrides,
})

/**
* @param asset Any type of asset
* @param price Price, e.g. 1.5 => 1.5$
Expand Down

0 comments on commit d42e4ae

Please sign in to comment.