Skip to content

Commit

Permalink
idiomatic wkc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
menduz committed Aug 11, 2022
1 parent 8968588 commit 9af6407
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 337 deletions.
2 changes: 1 addition & 1 deletion .env.default
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ PG_COMPONENT_PSQL_CONNECTION_STRING=
RENTALS_SUBGRAPH_URL=https://api.thegraph.com/subgraphs/name/decentraland/rentals-ethereum-goerli
LOG_LEVEL=INFO
CHAIN_NAME=Goerli
MAX_CONCURRENT_RENTAL_UPDATES=5
MAX_CONCURRENT_RENTAL_UPDATES=5
77 changes: 8 additions & 69 deletions test/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ import {
createLocalFetchCompoment as createLocalFetchComponent,
} from "@well-known-components/test-helpers"
import { ILoggerComponent } from "@well-known-components/interfaces"
import { createSubgraphComponent, ISubgraphComponent } from "@well-known-components/thegraph-component"
import { createPgComponent, IPgComponent } from "@well-known-components/pg-component"
import { createDotEnvConfigComponent } from "@well-known-components/env-config-provider"
import { createServerComponent, createStatusCheckComponent, IFetchComponent } from "@well-known-components/http-server"
import { createMetricsComponent } from "@well-known-components/metrics"
import { createSchemaValidatorComponent } from "../src/ports/schema-validator"
import { ISubgraphComponent } from "@well-known-components/thegraph-component"
import { IPgComponent } from "@well-known-components/pg-component"
import { initComponents as originalInitComponents } from "../src/components"
import { main } from "../src/service"
import { metricDeclarations } from "../src/metrics"
import { GlobalContext, TestComponents } from "../src/types"
import { createRentalsComponent, IRentalsComponent } from "../src/ports/rentals"
import { TestComponents } from "../src/types"
import { IRentalsComponent } from "../src/ports/rentals"

let lastUsedPort = 19000 + parseInt(process.env.JEST_WORKER_ID || "1") * 1000
function getFreePort() {
Expand All @@ -35,72 +31,15 @@ export const test = createRunner<TestComponents>({
})

export async function initComponents(): Promise<TestComponents> {
const currentPort = getFreePort()
const defaultConfig = {
HTTP_SERVER_PORT: (currentPort + 1).toString(),
HTTP_SERVER_HOST: "localhost",
MARKETPLACE_SUBGRAPH_URL: "https://some-url.com",
RENTALS_SUBGRAPH_URL: "https://some-url.com",
}
const components = await originalInitComponents()

const config = await createDotEnvConfigComponent({}, defaultConfig)
const cors = {
origin: await config.getString("CORS_ORIGIN"),
method: await config.getString("CORS_METHOD"),
}
const { config, database } = components

const logs = createTestConsoleLogComponent()
const server = await createServerComponent<GlobalContext>({ config, logs }, { cors, compression: {} })
const fetcher = await createTestFetchComponent()
const metrics = await createMetricsComponent(metricDeclarations, {
server,
config,
})
const marketplaceSubgraph = await createSubgraphComponent(
{ config, logs, fetch: fetcher, metrics },
await config.requireString("MARKETPLACE_SUBGRAPH_URL")
)
const rentalsSubgraph = await createSubgraphComponent(
{ config, logs, fetch: fetcher, metrics },
await config.requireString("RENTALS_SUBGRAPH_URL")
)
const database = await createPgComponent({ logs, config, metrics })
const rentals = await createRentalsComponent({
logs,
database,
marketplaceSubgraph,
rentalsSubgraph,
config,
})
const schemaValidator = await createSchemaValidatorComponent()
const statusChecks = await createStatusCheckComponent({ server, config })
// Mock the start function to avoid connecting to a local database
jest.spyOn(database, "start").mockResolvedValue()

const updateMetadataJob = createTestJobComponent()
const updateRentalsListingsJob = createTestJobComponent()

return {
config,
logs,
server,
statusChecks,
fetch: fetcher,
metrics,
database,
marketplaceSubgraph,
rentalsSubgraph,
schemaValidator,
rentals,
...components,
localFetch: await createLocalFetchComponent(config),
updateMetadataJob,
updateRentalsListingsJob,
}
}

export function createTestFetchComponent({ fetch = jest.fn() } = { fetch: jest.fn() }): IFetchComponent {
return {
fetch,
}
}

Expand Down
Loading

0 comments on commit 9af6407

Please sign in to comment.