Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Nov 27, 2024
1 parent 6aa7318 commit a2caec8
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 4 deletions.
135 changes: 135 additions & 0 deletions packages/sdk/src/transport/get-transport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import {SdkTransport} from "@onflow/typedefs"
import {getTransport} from "./get-transport"
import {httpTransport} from "@onflow/transport-http"
import {config} from "@onflow/config"

jest.mock("@onflow/transport-http", () => ({
httpTransport: {
send: jest.fn(),
subscribe: jest.fn(),
} as jest.Mocked<SdkTransport.Transport>,
}))

describe("getTransport", () => {
beforeEach(() => {
jest.resetAllMocks()
})

test("fallback to http transport", async () => {
const transport = await getTransport()
expect(transport).toBe(httpTransport)
})

test("override with custom transport", async () => {
const customTransport = {
send: jest.fn(),
subscribe: jest.fn(),
} as jest.Mocked<SdkTransport.Transport>

const transport = await getTransport({transport: customTransport})
expect(transport).toBe(customTransport)
})

test("override with custom send function", async () => {
const customSend = jest.fn()

const transport = await getTransport({send: customSend})
expect(transport).toEqual({
send: customSend,
subscribe: expect.any(Function),
})
})

test("override with both custom transport and send function", async () => {
await expect(
getTransport({
send: jest.fn(),
transport: {
send: jest.fn(),
subscribe: jest.fn(),
},
})
).rejects.toThrow(
/Cannot provide both "transport" and legacy "send" options/
)
})

test("transport from global config - sdk.transport", async () => {
const customTransport = {
send: jest.fn(),
subscribe: jest.fn(),
} as jest.Mocked<SdkTransport.Transport>

const tranpsort = await config().overload(
{
"sdk.transport": customTransport,
},
async () => {
return await getTransport()
}
)

expect(tranpsort).toBe(customTransport)
})

test("send function from global config - sdk.transport", async () => {
const customSend = jest.fn()

const transport = await config().overload(
{
"sdk.transport": customSend,
},
async () => {
return await getTransport()
}
)
expect(transport).toEqual({
send: customSend,
subscribe: expect.any(Function),
})
})

test("send function from global config - sdk.send", async () => {
const customSend = jest.fn()

const transport = await config().overload(
{
"sdk.send": customSend,
},
async () => {
return await getTransport()
}
)

expect(transport).toEqual({
send: customSend,
subscribe: expect.any(Function),
})
})

/**
* TODO: (jribbink) Figure out what to do with this logic.
*
* Currently, and previously, this logic is the reverse where the global config has priority over the custom transport.
* I disagree with this logic and believe that the custom transport should have priority over the global config.
* However, it would be a breaking change to change this logic.
*
*/
/*test("custom transport has priority over global config", async () => {
const customTransport = {
send: jest.fn(),
subscribe: jest.fn(),
} as jest.Mocked<SdkTransport.Transport>
const transport = await config().overload(
{
"sdk.transport": httpTransport,
},
async () => {
return await getTransport({transport: customTransport})
}
)
expect(transport).toBe(customTransport)
})*/
})
10 changes: 6 additions & 4 deletions packages/sdk/src/transport/get-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {invariant} from "@onflow/util-invariant"
* @param overrides - Override default configuration with custom transport or send function.
* @returns The SDK transport object.
*/
export async function getTransport(override: {
send?: SdkTransport.SendFn
transport?: SdkTransport.Transport
}): Promise<SdkTransport.Transport> {
export async function getTransport(
override: {
send?: SdkTransport.SendFn
transport?: SdkTransport.Transport
} = {}
): Promise<SdkTransport.Transport> {
invariant(
override.send == null || override.transport == null,
`SDK Transport Error: Cannot provide both "transport" and legacy "send" options.`
Expand Down

0 comments on commit a2caec8

Please sign in to comment.