-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
141 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
})*/ | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters