Skip to content

Commit d8c88ef

Browse files
Add more segment tests
1 parent cf1e3c4 commit d8c88ef

File tree

6 files changed

+103
-28
lines changed

6 files changed

+103
-28
lines changed

bug_bash/src/App.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import React from "react";
2-
import AppLayout from "./layout/layout";
3-
import { createBrowserRouter, RouterProvider } from "react-router-dom";
4-
import FetchQualifiedSegments from "./pages/fetch-qualified-segments/fetch-qualified-segments";
1+
import React from 'react';
2+
import AppLayout from './layout/layout';
3+
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
4+
import FetchQualifiedSegments from './pages/fetch-qualified-segments/fetch-qualified-segments';
5+
import ValidateUserContext from './pages/validate-user-context/validate-user-context';
56

67
const router = createBrowserRouter([
78
{
8-
path: "/",
9+
path: '/fetch-qualified-segments',
910
element: <FetchQualifiedSegments />,
1011
},
12+
{
13+
path: '/',
14+
element: <ValidateUserContext />,
15+
},
1116
]);
1217

1318
const App = () => {

bug_bash/src/layout/layout.css.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export const Grid = {
2-
display: "grid",
3-
gap: "50px 100px",
2+
display: 'flex',
3+
justifyContent: 'space-around',
44
};
55

66
export const Content = {
7-
margin: "16px",
7+
margin: '16px',
88
};

bug_bash/src/layout/layout.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import React, { PropsWithChildren } from "react";
2-
import { Content, Grid } from "./layout.css";
1+
import React, { PropsWithChildren } from 'react';
2+
import { Content, Grid } from './layout.css';
33

44
const AppLayout: React.FC<PropsWithChildren> = (props: PropsWithChildren) => {
55
const { children } = props;
66

77
return (
88
<div style={Content}>
99
<div style={Grid}>
10-
<button>Fetch Qualified Segments</button>
10+
<div>
11+
<a href="/">Validate User Context</a>
12+
</div>
13+
<div>
14+
<a href="/fetch-qualified-segments">Fetch Qualified Segments</a>
15+
</div>
1116
</div>
1217
{children}
1318
</div>

bug_bash/src/pages/fetch-qualified-segments/fetch-qualified-segments.tsx

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
import React, { useEffect, useState } from "react";
2-
import {
3-
createInstance,
4-
OptimizelyProvider,
5-
useDecision,
6-
} from "@optimizely/react-sdk";
7-
import { OptimizelyReturnType } from "./fetch-qualified-segments.types";
1+
import React, { useEffect, useState } from 'react';
2+
import { createInstance, OptimizelyProvider, useDecision } from '@optimizely/react-sdk';
3+
import { OptimizelyReturnType } from './fetch-qualified-segments.types';
4+
import { OptimizelySegmentOption } from '@optimizely/optimizely-sdk';
85

96
const optimizelyClient = createInstance({
107
sdkKey: process.env.REACT_APP_OPTIMIZELY_SDK_KEY,
118
});
129

1310
export const FetchQualifiedSegments: React.FC = () => {
14-
const [isSegmentsFetched, setIsSegmentsFetched] = useState<boolean | null>(
15-
null
16-
);
11+
const [isSegmentsFetched, setIsSegmentsFetched] = useState<boolean | null>(null);
1712
const [readyResult, setReadyResult] = useState<OptimizelyReturnType>();
1813

19-
const [userId] = useState<string>("matjaz-user-1");
14+
const [userId] = useState<string>('matjaz-user-1');
2015
// const [userId] = useState<string>("matjaz-user-2");
2116
// const [userId] = useState<string>("matjaz-user-3");
17+
// const [userId] = useState<null>(null);
2218

2319
const prepareClient = () => {
24-
console.log("optimizelyClient");
20+
console.log('optimizelyClient');
2521
optimizelyClient.onReady().then(async (res: any) => {
2622
setReadyResult(res);
2723
setIsSegmentsFetched(true);
2824
// console.log(optimizelyClient.isReady());
25+
await optimizelyClient.fetchQualifiedSegments();
26+
await optimizelyClient.fetchQualifiedSegments([OptimizelySegmentOption.IGNORE_CACHE]);
2927
});
3028
};
3129

@@ -40,11 +38,7 @@ export const FetchQualifiedSegments: React.FC = () => {
4038
user={{ id: userId }}
4139
>
4240
{readyResult?.success && (
43-
<div>
44-
{`Is segments fetched for user ${userId}: ${
45-
isSegmentsFetched ? "Yes" : "No"
46-
} `}
47-
</div>
41+
<div>{`Is segments fetched for user ${userId}: ${isSegmentsFetched ? 'Yes' : 'No'} `}</div>
4842
)}
4943
{readyResult && !readyResult.success && (
5044
<div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React, { useEffect } from 'react';
2+
import { ReactSDKClient, withOptimizely } from '@optimizely/react-sdk';
3+
4+
function TestComponent(props: any) {
5+
console.log(props);
6+
const { optimizely } = props;
7+
const client: ReactSDKClient = optimizely;
8+
useEffect(() => {
9+
console.log(client.user.id);
10+
}, []);
11+
12+
return <div>TestComponent</div>;
13+
}
14+
15+
export default withOptimizely(TestComponent);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React, { useEffect, useState } from 'react';
2+
import { createInstance, OptimizelyProvider, useDecision, withOptimizely } from '@optimizely/react-sdk';
3+
import { OptimizelyReturnType } from '../fetch-qualified-segments/fetch-qualified-segments.types';
4+
import TestComponent from './test-component';
5+
6+
const optimizelyClient = createInstance({
7+
sdkKey: process.env.REACT_APP_OPTIMIZELY_SDK_KEY,
8+
});
9+
10+
export const ValidateUserContext = () => {
11+
// This should create a user with id matjaz-user-1, and create an vuid in the local storage
12+
const [userId] = useState<string>('matjaz-user-1');
13+
14+
// This should create a user with id matjaz-user-1, and create an vuid in the local storage
15+
// const [userId] = useState<null>(null);
16+
17+
// inspect network tab, you should see that the vuid_xxxxx is not sent in the request, instead 'vuid_overridden' is sent as the vuid and there shouldnt be a fs_userid sent
18+
// const [userId] = useState<string>('vuid_overridden');
19+
20+
const [readyResult, setReadyResult] = useState<OptimizelyReturnType>();
21+
22+
const prepareClient = () => {
23+
optimizelyClient.onReady().then(async (res: any) => {
24+
setReadyResult(res);
25+
});
26+
};
27+
28+
useEffect(prepareClient, []);
29+
30+
return (
31+
<OptimizelyProvider
32+
optimizely={optimizelyClient}
33+
// Generally React SDK runs for one client at a time i.e for one user throughout the lifecycle.
34+
// You can provide the user Id here once and the SDK will memoize and reuse it throughout the application lifecycle.
35+
// For this example, we are simulating 10 different users so we will ignore this and pass override User IDs to the useDecision hook for demonstration purpose.
36+
user={{ id: userId }}
37+
>
38+
{readyResult && (
39+
<>
40+
<div> UserID: {optimizelyClient.user.id === null ? 'null' : optimizelyClient.user.id}</div>
41+
<div> Vuid: {localStorage.getItem('optimizely-vuid')}</div>
42+
</>
43+
)}
44+
{/* {readyResult && !readyResult.success && (
45+
<div>
46+
Client failed to intialized. Check console for more details.
47+
<div>Reason: {readyResult.reason}</div>
48+
<div>Message: {readyResult.message}</div>
49+
</div>
50+
)} */}
51+
<TestComponent />
52+
</OptimizelyProvider>
53+
);
54+
};
55+
56+
export default ValidateUserContext;

0 commit comments

Comments
 (0)