-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make sure to close the connection * make sure to close the connection * save * fix client code * cleanup * add some tests
- Loading branch information
1 parent
2a3a2ef
commit 983c00b
Showing
16 changed files
with
356 additions
and
156 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
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
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,5 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
const jestConfigBase = require("../../jest.config.base"); | ||
const babelConfig = require("./babel.config.js"); | ||
|
||
module.exports = jestConfigBase(babelConfig); |
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
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,13 @@ | ||
export const mockConnect = jest.fn(); | ||
export const mockSend = jest.fn(); | ||
export const mockRenewSession = jest.fn(); | ||
|
||
const mock = jest.fn().mockImplementation(() => { | ||
return { | ||
connect: mockConnect, | ||
send: mockSend, | ||
renewSession: mockRenewSession, | ||
}; | ||
}); | ||
|
||
export const WS = mock; |
100 changes: 100 additions & 0 deletions
100
packages/react-provider/src/hooks/__tests__/use-transport.spec.ts
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,100 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; // will attempt to auto-detect | ||
import { Transport } from "../../transports/base"; | ||
import useTransport from "../use-transport"; | ||
|
||
import { WS } from "../../ws"; | ||
|
||
const ws = new WS({}); | ||
|
||
const mockConnect = ws.connect as jest.Mock; | ||
const mockRenewSession = ws.renewSession as jest.Mock; | ||
|
||
jest.mock("../../ws"); | ||
describe("useTransport", () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("will throw an error if missing auth and clientkey", () => { | ||
expect.assertions(1); | ||
try { | ||
const { result } = renderHook(() => | ||
useTransport({ | ||
clientSourceId: "abc123", | ||
}) | ||
); | ||
|
||
if (result.error) { | ||
throw result.error; | ||
} | ||
} catch (ex) { | ||
expect(String(ex)).toBe("Error: Missing ClientKey or Authorization"); | ||
} | ||
}); | ||
|
||
test("will return the same transport passed in", () => { | ||
const transport = new Transport(); | ||
const { result } = renderHook(() => | ||
useTransport({ | ||
transport, | ||
}) | ||
); | ||
|
||
expect(result.current).toEqual(transport); | ||
}); | ||
|
||
test("will create a new transport if one is not provided", () => { | ||
const { result } = renderHook(() => | ||
useTransport({ | ||
clientSourceId: "mockClientSourceId", | ||
authorization: "mockAuth", | ||
}) | ||
); | ||
|
||
expect(result.current).toBeTruthy(); | ||
expect(mockConnect.mock.calls.length).toEqual(1); | ||
}); | ||
|
||
test("will call renewSession if a new token is provided", () => { | ||
let authorization = | ||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6InVzZXJfaWQ6NzBmNmE0ZjQtMjkwNy00NTE4LWI4ZjMtYjljZmFiMjI0NzY0IGluYm94OnJlYWQ6bWVzc2FnZXMiLCJ0ZW5hbnRfc2NvcGUiOiJwdWJsaXNoZWQvcHJvZHVjdGlvbiIsInRlbmFudF9pZCI6Ijc2ODI1MWNmLTNlYjgtNDI2YS05MmViLWZhYTBlNzY3ODc2OCIsImlhdCI6MTY3MjI1NzY1OSwianRpIjoiYmJlMDMyMmMtZWY4Mi00M2FkLWI3NGMtOGZlYWNiNTczYTY0In0.Xs_yd8IhdNORK8LyleS10FDLQbb4sXkCtGHPq7tUGa4"; | ||
|
||
const { result, rerender } = renderHook(() => | ||
useTransport({ | ||
clientSourceId: "mockClientSourceId", | ||
authorization, | ||
}) | ||
); | ||
|
||
expect(result.current).toBeTruthy(); | ||
expect(mockConnect.mock.calls.length).toEqual(1); | ||
|
||
authorization = | ||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6InVzZXJfaWQ6NzBmNmE0ZjQtMjkwNy00NTE4LWI4ZjMtYjljZmFiMjI0NzY0IGluYm94OnJlYWQ6bWVzc2FnZXMiLCJ0ZW5hbnRfc2NvcGUiOiJwdWJsaXNoZWQvcHJvZHVjdGlvbiIsInRlbmFudF9pZCI6Ijc2ODI1MWNmLTNlYjgtNDI2YS05MmViLWZhYTBlNzY3ODc2OCIsImlhdCI6MTY3Mjc4MDE5MSwianRpIjoiMzU1NmU1OTYtNjljZi00NjdiLTg1YjMtNDk5ZjZmYzk2YjVhIn0.peUty0F94bhulmD4HS-7H7N3-HI31mIvU8jLFBEpUgM"; | ||
|
||
rerender(); | ||
expect(mockRenewSession.mock.calls.length).toEqual(1); | ||
}); | ||
|
||
test("will NOT call renewSession if a new token is provided but the scope changes", () => { | ||
let authorization = | ||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6InVzZXJfaWQ6NzBmNmE0ZjQtMjkwNy00NTE4LWI4ZjMtYjljZmFiMjI0NzY0IGluYm94OnJlYWQ6bWVzc2FnZXMiLCJ0ZW5hbnRfc2NvcGUiOiJwdWJsaXNoZWQvcHJvZHVjdGlvbiIsInRlbmFudF9pZCI6Ijc2ODI1MWNmLTNlYjgtNDI2YS05MmViLWZhYTBlNzY3ODc2OCIsImlhdCI6MTY3MjI1NzY1OSwianRpIjoiYmJlMDMyMmMtZWY4Mi00M2FkLWI3NGMtOGZlYWNiNTczYTY0In0.Xs_yd8IhdNORK8LyleS10FDLQbb4sXkCtGHPq7tUGa4"; | ||
|
||
const { result, rerender } = renderHook(() => | ||
useTransport({ | ||
clientSourceId: "mockClientSourceId", | ||
authorization, | ||
}) | ||
); | ||
|
||
expect(result.current).toBeTruthy(); | ||
expect(mockConnect.mock.calls.length).toEqual(1); | ||
|
||
authorization = | ||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6InVzZXJfaWQ6YmxhaCBpbmJveDpyZWFkOm1lc3NhZ2VzIiwidGVuYW50X3Njb3BlIjoicHVibGlzaGVkL3Byb2R1Y3Rpb24iLCJ0ZW5hbnRfaWQiOiI3NjgyNTFjZi0zZWI4LTQyNmEtOTJlYi1mYWEwZTc2Nzg3NjgiLCJpYXQiOjE2NzI3ODIwNzYsImp0aSI6ImJjZjRiN2QzLWMyNDktNDQzNC04ZTQ0LWFjMTYxY2U0NTRiZCJ9.J7k0OQ1qfFR5MpdoP13mCusQWejpx7VB6Z6A194RxU8"; | ||
|
||
rerender(); | ||
expect(mockRenewSession.mock.calls.length).toEqual(0); | ||
expect(mockConnect.mock.calls.length).toEqual(2); | ||
}); | ||
}); |
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,29 +1,82 @@ | ||
import { useMemo } from "react"; | ||
import { Transport, CourierTransport } from "~/transports"; | ||
import { useMemo, useRef } from "react"; | ||
import { CourierTransport, Transport } from "~/transports"; | ||
import jwtDecode from "jwt-decode"; | ||
import { ITransportOptions } from "~/transports/courier/types"; | ||
interface DecodedAuth { | ||
scope: string; | ||
tenantId: string; | ||
} | ||
|
||
const useCourierTransport = ({ | ||
const useTransport = ({ | ||
authorization, | ||
clientSourceId, | ||
clientKey, | ||
transport, | ||
userSignature, | ||
wsOptions, | ||
}): Transport => { | ||
}: { | ||
authorization?: string; | ||
clientSourceId?: string; | ||
clientKey?: string; | ||
transport?: CourierTransport | Transport; | ||
userSignature?: string; | ||
wsOptions?: ITransportOptions["wsOptions"]; | ||
}): CourierTransport | Transport => { | ||
const transportRef = | ||
useRef<{ | ||
authorization: string; | ||
transport: CourierTransport; | ||
}>(); | ||
|
||
return useMemo(() => { | ||
if (transport) { | ||
return transport; | ||
} | ||
|
||
if ((clientKey || authorization) && !transport) { | ||
return new CourierTransport({ | ||
if ( | ||
authorization && | ||
transportRef?.current?.authorization && | ||
transportRef?.current.transport | ||
) { | ||
const oldDecodedAuth = jwtDecode( | ||
transportRef?.current?.authorization | ||
) as DecodedAuth; | ||
const newDecodedAuth = jwtDecode(authorization) as DecodedAuth; | ||
|
||
if ( | ||
oldDecodedAuth.scope === newDecodedAuth.scope && | ||
oldDecodedAuth.tenantId === newDecodedAuth.tenantId | ||
) { | ||
transportRef.current.transport.renewSession(authorization); | ||
return transportRef.current.transport; | ||
} | ||
} | ||
|
||
if (!clientKey && !authorization) { | ||
throw new Error("Missing ClientKey or Authorization"); | ||
} | ||
|
||
if (!clientSourceId) { | ||
throw new Error("Missing ClientSourceId"); | ||
} | ||
|
||
const newTransport = new CourierTransport({ | ||
authorization, | ||
clientSourceId, | ||
clientKey, | ||
userSignature, | ||
wsOptions, | ||
}); | ||
|
||
// keep track of the transport so we don't reconnect when we don't have to | ||
if (authorization) { | ||
transportRef.current = { | ||
authorization, | ||
clientSourceId, | ||
clientKey, | ||
userSignature, | ||
wsOptions, | ||
}); | ||
transport: newTransport, | ||
}; | ||
} | ||
return newTransport; | ||
}, [authorization, clientKey, transport, userSignature, wsOptions]); | ||
}; | ||
|
||
export default useCourierTransport; | ||
export default useTransport; |
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
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
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
Oops, something went wrong.