Skip to content

Commit

Permalink
chore: third party tests (#126)
Browse files Browse the repository at this point in the history
* wip

* wip

* fix

* fix

* chore: emotes (#122)

* chore: add emotes just duplicated all logic from wearables

* chore: abstraction in definitions-fetcher

* chore: abstract wearablesFetcher and emotesFetcher in itemsFetcher

* chore: fix tests

* fix: tests

* chore: item-fetcher queries record

* chore: 3rd party

* chore: cleanup code

* test: try new approach

* test: cover third party wearables endpoint

* minor fix

---------

Co-authored-by: Hugo Arregui <[email protected]>
Co-authored-by: Alejo Thomas Ortega <[email protected]>
  • Loading branch information
3 people authored Mar 17, 2023
1 parent dd14300 commit cb0867d
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 6 deletions.
20 changes: 14 additions & 6 deletions test/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { createEmoteFetcherComponent, createWearableFetcherComponent } from '../
import { createTheGraphComponentMock } from './mocks/the-graph-mock'
import { createContentComponentMock } from './mocks/content-mock'
import { createDefinitionsFetcherComponent } from '../src/adapters/definitions-fetcher'
import { IFetchComponent } from '@well-known-components/http-server'
import { TheGraphComponent } from '../src/ports/the-graph'

/**
* Behaves like Jest "describe" function, used to describe a test for a
Expand All @@ -27,11 +29,19 @@ export const test = createRunner<TestComponents>({
initComponents
})

async function initComponents(): Promise<TestComponents> {
export function testWithComponents(preConfigureComponents: () => { fetchComponent?: IFetchComponent, theGraphComponent?: TheGraphComponent }) {
const preConfiguredComponents = preConfigureComponents()
return createRunner<TestComponents>({
main,
initComponents: () => initComponents(preConfiguredComponents.fetchComponent, preConfiguredComponents.theGraphComponent)
})
}

async function initComponents(fetchComponent?: IFetchComponent, theGraphComponent?: TheGraphComponent): Promise<TestComponents> {
const defaultFetchConfig = defaultServerConfig()
const config = await createDotEnvConfigComponent({}, { COMMIT_HASH: 'commit_hash', ...defaultFetchConfig })
const fetch = await createLocalFetchCompoment(config)
const theGraphMock = createTheGraphComponentMock()
const fetch = fetchComponent ? fetchComponent : await createLocalFetchCompoment(config)
const theGraphMock = theGraphComponent ? theGraphComponent : createTheGraphComponentMock()

const components = await originalInitComponents(fetch, theGraphMock)

Expand All @@ -41,9 +51,7 @@ async function initComponents(): Promise<TestComponents> {
const wearablesFetcher = await createWearableFetcherComponent({ config, theGraph: theGraphMock, logs })
const emotesFetcher = await createEmoteFetcherComponent({ config, theGraph: theGraphMock, logs })
const definitionsFetcher = await createDefinitionsFetcherComponent({ config, content: contentMock, logs })

jest.spyOn(theGraphMock.thirdPartyRegistrySubgraph, 'query').mockResolvedValueOnce({ thirdParties: [] })


return {
...components,
config: config,
Expand Down
34 changes: 34 additions & 0 deletions test/data/wearables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,37 @@ export function generateDefinitions(urns: string[]) {
}
}))
}

export function generateThirdPartyWearables(quantity: number) {
const generatedThirdPartyWearables = []
for (let i = 0; i < quantity; i++) {
generatedThirdPartyWearables.push({
id: 'id-' + i,
amount: 1,
urn: {
decentraland: 'urn-' + i
}
})
}

return generatedThirdPartyWearables
}

export function getThirdPartyProviders() {
return {
thirdParties: [
{
id: "urn:decentraland:matic:collections-thirdparty:baby-doge-coin",
resolver: "https://decentraland-api.babydoge.com/v1"
},
{
id: "urn:decentraland:matic:collections-thirdparty:cryptoavatars",
resolver: "https://api.cryptoavatars.io/"
},
{
id: "urn:decentraland:matic:collections-thirdparty:dolcegabbana-disco-drip",
resolver: "https://wearables-api.unxd.com"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { testWithComponents } from '../../components'
import { generateThirdPartyWearables, getThirdPartyProviders } from '../../data/wearables'
import Wallet from 'ethereumjs-wallet'
import { createTheGraphComponentMock } from '../../mocks/the-graph-mock'

testWithComponents(() => {
const theGraphMock = createTheGraphComponentMock()
const resolverResponse = getThirdPartyProviders()

theGraphMock.thirdPartyRegistrySubgraph.query = jest.fn().mockResolvedValue(resolverResponse)
return {
theGraphComponent: theGraphMock
}
})('third-party-wearables-handler: GET /users/:address/third-party-wearables with multiple providers should', function ({ components }) {
it('return wearables when found on a single provider', async () => {
const { localFetch, fetch } = components
const wearables = generateThirdPartyWearables(2)

fetch.fetch = jest.fn().mockImplementation((url) => {
if (url.includes('babydoge')) {
return({ok: true, json: () => ({
assets: wearables
})})
} else {
return({ ok: true, json: () => ( { assets: [] } ) })
}
})

const r = await localFetch.fetch(`/users/${Wallet.generate().getAddressString()}/third-party-wearables`)

expect(r.status).toBe(200)
expect(await r.json()).toEqual({
elements: convertToDataModel(wearables),
totalAmount: 2,
pageNum: 1,
pageSize: 100
})
})

it('return wearables when found on multiple providers', async () => {
const { localFetch, fetch } = components
const wearables = generateThirdPartyWearables(6)

fetch.fetch = jest.fn().mockImplementation((url) => {
if (url.includes('babydoge')) {
return({ok: true, json: () => ({
assets: wearables.slice(0, 2)
})})
}

if (url.includes('cryptoavatars')) {
return({ok: true, json: () => ({
assets: wearables.slice(2, 4)
})})
}

if (url.includes('unxd')) {
return({ok: true, json: () => ({
assets: wearables.slice(4, 6)
})})
}
})

const r = await localFetch.fetch(`/users/${Wallet.generate().getAddressString()}/third-party-wearables`)

expect(r.status).toBe(200)
expect(await r.json()).toEqual({
elements: convertToDataModel(wearables),
totalAmount: 6,
pageNum: 1,
pageSize: 100
})
})
})

function convertToDataModel(wearables, definitions = undefined) {
return wearables.map(wearable => {
const definition = definitions?.find(def => def.id === wearable.urn.decentraland)
const definitionData = definition?.metadata?.data

return {
amount: wearable.amount,
individualData: [
{
id: wearable.id
}
],
urn: wearable.urn.decentraland,
...(definitions ? {
definition: definitionData && {
id: wearable.urn.decentraland,
data: {
...definitionData,
representations: [{ contents: [{ key: definitionData.representations[0]?.contents[0] }] }]
}
}
} : {})
}
})
}
Loading

0 comments on commit cb0867d

Please sign in to comment.