diff --git a/package.json b/package.json index 9fbcef7..ca6629b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@absmartly/react-sdk", - "version": "1.4.0", + "version": "1.4.1", "homepage": "https://github.com/absmartly/react-sdk#README.md", "bugs": "https://github.com/absmartly/react-sdk/issues", "keywords": [ @@ -51,7 +51,7 @@ "react-dom": ">=16.8.0" }, "dependencies": { - "@absmartly/javascript-sdk": "^1.13.0", + "@absmartly/javascript-sdk": "^1.13.4", "core-js": "^3.26.0" }, "publishConfig": { diff --git a/src/components/SDKProvider/SDKProvider.tsx b/src/components/SDKProvider/SDKProvider.tsx index 55ff8ea..3f6f0d8 100644 --- a/src/components/SDKProvider/SDKProvider.tsx +++ b/src/components/SDKProvider/SDKProvider.tsx @@ -11,6 +11,13 @@ import { SDKOptionsType, } from "../../types"; +const isReactNative = + typeof navigator !== "undefined" && navigator.product === "ReactNative"; + +const SDK_AGENT = isReactNative + ? "absmartly-react-native-sdk" + : "absmartly-react-sdk"; + type SDKProviderNoContext = { sdkOptions: SDKOptionsType; context?: never; @@ -35,7 +42,12 @@ export const SDKProvider: FC = ({ }) => { const sdk = context ? context["_sdk"] - : new absmartly.SDK({ retries: 5, timeout: 3000, ...sdkOptions }); + : new absmartly.SDK({ + agent: SDK_AGENT, + retries: 5, + timeout: 3000, + ...sdkOptions, + }); const [providedContext, setProvidedContext] = useState( context ? context : sdk.createContext(contextOptions), diff --git a/src/types.ts b/src/types.ts index c23492b..1cc7a5c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -7,6 +7,7 @@ export type SDKOptionsType = { apiKey: string; environment: string; application: string; + agent?: string; retries?: number; timeout?: number; eventLogger?: (context: Context, eventName: EventNameType, data: any) => void; diff --git a/tests/SDKProvider.test.tsx b/tests/SDKProvider.test.tsx index 698c144..4c6f1dc 100644 --- a/tests/SDKProvider.test.tsx +++ b/tests/SDKProvider.test.tsx @@ -78,8 +78,8 @@ describe("SDKProvider", () => { apiKey: "salkjdhclkjsdbca", application: "www", environment: "Environment 5", - retries: 5, - timeout: 3000, + retries: 10, + timeout: 5000, overrides: overrides, attributes: attrs, data: mockContextData, @@ -101,12 +101,40 @@ describe("SDKProvider", () => { ); expect(SDK).toHaveBeenCalledTimes(1); - expect(SDK).toHaveBeenLastCalledWith(sdkOptions); + expect(SDK).toHaveBeenLastCalledWith({ + agent: "absmartly-react-sdk", + retries: 10, + timeout: 5000, + ...sdkOptions, + }); expect(mockCreateContext).toHaveBeenCalledTimes(1); expect(mockCreateContext).toHaveBeenLastCalledWith(contextOptions); }); + it("should allow the user to override the agent", async () => { + const customSdkOptions = { + ...sdkOptions, + agent: "custom-agent", + }; + + render( + + + , + ); + + expect(SDK).toHaveBeenCalledTimes(1); + expect(SDK).toHaveBeenLastCalledWith( + expect.objectContaining({ + agent: "custom-agent", + }), + ); + }); + it("Whether it will create an SDK instance with a context that has prefetched context data", async () => { render( @@ -177,3 +205,47 @@ describe("SDKProvider", () => { ); }); }); + +describe("SDKProvider React Native agent", () => { + const originalProduct = navigator.product; + + afterEach(() => { + Object.defineProperty(navigator, "product", { + value: originalProduct, + configurable: true, + }); + }); + + it("should use absmartly-react-native-sdk agent when navigator.product is ReactNative", async () => { + Object.defineProperty(navigator, "product", { + value: "ReactNative", + configurable: true, + }); + + vi.resetModules(); + + const { SDKProvider: RNSDKProvider } = await import( + "../src/components/SDKProvider" + ); + + render( + +
+ , + ); + + expect(SDK).toHaveBeenLastCalledWith( + expect.objectContaining({ + agent: "absmartly-react-native-sdk", + }), + ); + }); +}); diff --git a/yarn.lock b/yarn.lock index d14e4c8..2fbee69 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,10 +2,10 @@ # yarn lockfile v1 -"@absmartly/javascript-sdk@^1.13.0": - version "1.13.1" - resolved "https://registry.yarnpkg.com/@absmartly/javascript-sdk/-/javascript-sdk-1.13.1.tgz#1ab36024b7c3b95a3a3314784e3d47dbebfcae2c" - integrity sha512-u6Ga8QFZ2tTElj7Picrm8ZAONog9E8X0fW2Mdl+ep9O/y2x2KmAKioVikvAx9NQH4vTTkFAetcvidWJgxhu9nA== +"@absmartly/javascript-sdk@^1.13.4": + version "1.13.4" + resolved "https://registry.yarnpkg.com/@absmartly/javascript-sdk/-/javascript-sdk-1.13.4.tgz#83847f54bb2bf4d414e0530b2b47c1ffb62c2960" + integrity sha512-uNENphAJQ2iq8J1U8Eanf/rMeQ9p7DvnEWlHPr4doWWLRoaLiWRo4Ke9j9oO0WTsVt+Lz3r/HCEkyqVP6Q9BHQ== dependencies: core-js "^3.20.0" node-fetch "^2.6.7"