-
Notifications
You must be signed in to change notification settings - Fork 34
chore: refactoring react server component client #1186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+582
−619
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
61658d1
chore: refactoring react server component client
joker23 f2eee29
chore: now using react `cache` for managing server sessions
joker23 ef011c4
chore: pr comments
joker23 585e470
Merge branch 'main' into skz/sdk-2021/react-server
joker23 80eca87
chore: pr comments
joker23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
177 changes: 177 additions & 0 deletions
177
packages/sdk/react/__tests__/server/createLDServerSession.test.ts
This file contains hidden or 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,177 @@ | ||
| import { LDContext, LDFlagsStateOptions } from '@launchdarkly/js-server-sdk-common'; | ||
|
|
||
| import { createLDServerSession, isServer } from '../../src/server/index'; | ||
|
|
||
| const context: LDContext = { kind: 'user', key: 'test-user' }; | ||
|
|
||
| function makeMockBaseClient() { | ||
| return { | ||
| initialized: jest.fn(() => true), | ||
| boolVariation: jest.fn((_key: string, _ctx: LDContext, def: boolean) => Promise.resolve(def)), | ||
| numberVariation: jest.fn((_key: string, _ctx: LDContext, def: number) => Promise.resolve(def)), | ||
| stringVariation: jest.fn((_key: string, _ctx: LDContext, def: string) => Promise.resolve(def)), | ||
| jsonVariation: jest.fn((_key: string, _ctx: LDContext, def: unknown) => Promise.resolve(def)), | ||
| boolVariationDetail: jest.fn((_key: string, _ctx: LDContext, def: boolean) => | ||
| Promise.resolve({ value: def, variationIndex: null, reason: { kind: 'OFF' as const } }), | ||
| ), | ||
| numberVariationDetail: jest.fn((_key: string, _ctx: LDContext, def: number) => | ||
| Promise.resolve({ value: def, variationIndex: null, reason: { kind: 'OFF' as const } }), | ||
| ), | ||
| stringVariationDetail: jest.fn((_key: string, _ctx: LDContext, def: string) => | ||
| Promise.resolve({ value: def, variationIndex: null, reason: { kind: 'OFF' as const } }), | ||
| ), | ||
| jsonVariationDetail: jest.fn((_key: string, _ctx: LDContext, def: unknown) => | ||
| Promise.resolve({ value: def, variationIndex: null, reason: { kind: 'OFF' as const } }), | ||
| ), | ||
| // @ts-ignore — mock return shape matches LDFlagsState structurally | ||
| allFlagsState: jest.fn((_context: LDContext, _options?: LDFlagsStateOptions) => | ||
| Promise.resolve({ | ||
| valid: true, | ||
| getFlagValue: jest.fn(), | ||
| getFlagReason: jest.fn(), | ||
| allValues: jest.fn(() => ({})), | ||
| toJSON: jest.fn(() => ({ $flagsState: {}, $valid: true })), | ||
| }), | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| it('isServer() returns true in a Node test environment', () => { | ||
| expect(isServer()).toBe(true); | ||
| }); | ||
|
|
||
| it('getContext() returns the context passed at creation', () => { | ||
| const client = makeMockBaseClient(); | ||
| const session = createLDServerSession(client, context); | ||
| expect(session.getContext()).toEqual(context); | ||
| }); | ||
|
|
||
| it('initialized() delegates to the base client', () => { | ||
| const client = makeMockBaseClient(); | ||
| client.initialized.mockReturnValue(false); | ||
| const session = createLDServerSession(client, context); | ||
| expect(session.initialized()).toBe(false); | ||
| expect(client.initialized).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('boolVariation() calls base client with bound context', async () => { | ||
| const client = makeMockBaseClient(); | ||
| client.boolVariation.mockResolvedValue(true); | ||
| const session = createLDServerSession(client, context); | ||
| const result = await session.boolVariation('my-flag', false); | ||
| expect(result).toBe(true); | ||
| expect(client.boolVariation).toHaveBeenCalledWith('my-flag', context, false); | ||
| }); | ||
|
|
||
| it('numberVariation() calls base client with bound context', async () => { | ||
| const client = makeMockBaseClient(); | ||
| client.numberVariation.mockResolvedValue(42); | ||
| const session = createLDServerSession(client, context); | ||
| const result = await session.numberVariation('my-flag', 0); | ||
| expect(result).toBe(42); | ||
| expect(client.numberVariation).toHaveBeenCalledWith('my-flag', context, 0); | ||
| }); | ||
|
|
||
| it('stringVariation() calls base client with bound context', async () => { | ||
| const client = makeMockBaseClient(); | ||
| client.stringVariation.mockResolvedValue('hello'); | ||
| const session = createLDServerSession(client, context); | ||
| const result = await session.stringVariation('my-flag', 'default'); | ||
| expect(result).toBe('hello'); | ||
| expect(client.stringVariation).toHaveBeenCalledWith('my-flag', context, 'default'); | ||
| }); | ||
|
|
||
| it('jsonVariation() calls base client with bound context', async () => { | ||
| const client = makeMockBaseClient(); | ||
| const json = { key: 'value' }; | ||
| client.jsonVariation.mockResolvedValue(json); | ||
| const session = createLDServerSession(client, context); | ||
| const result = await session.jsonVariation('my-flag', {}); | ||
| expect(result).toEqual(json); | ||
| expect(client.jsonVariation).toHaveBeenCalledWith('my-flag', context, {}); | ||
| }); | ||
|
|
||
| it('boolVariationDetail() calls base client with bound context', async () => { | ||
| const client = makeMockBaseClient(); | ||
| const detail = { value: true, variationIndex: 1, reason: { kind: 'RULE_MATCH' as const } }; | ||
| // @ts-ignore — valid LDEvaluationDetailTyped<boolean> shape; mock type is too narrow | ||
| client.boolVariationDetail.mockResolvedValue(detail); | ||
| const session = createLDServerSession(client, context); | ||
| const result = await session.boolVariationDetail('my-flag', false); | ||
| expect(result).toEqual(detail); | ||
| expect(client.boolVariationDetail).toHaveBeenCalledWith('my-flag', context, false); | ||
| }); | ||
|
|
||
| it('numberVariationDetail() calls base client with bound context', async () => { | ||
| const client = makeMockBaseClient(); | ||
| const detail = { value: 42, variationIndex: 1, reason: { kind: 'RULE_MATCH' as const } }; | ||
| // @ts-ignore — valid LDEvaluationDetailTyped<number> shape; mock type is too narrow | ||
| client.numberVariationDetail.mockResolvedValue(detail); | ||
| const session = createLDServerSession(client, context); | ||
| const result = await session.numberVariationDetail('my-flag', 0); | ||
| expect(result).toEqual(detail); | ||
| expect(client.numberVariationDetail).toHaveBeenCalledWith('my-flag', context, 0); | ||
| }); | ||
|
|
||
| it('stringVariationDetail() calls base client with bound context', async () => { | ||
| const client = makeMockBaseClient(); | ||
| const detail = { value: 'hello', variationIndex: 1, reason: { kind: 'RULE_MATCH' as const } }; | ||
| // @ts-ignore — valid LDEvaluationDetailTyped<string> shape; mock type is too narrow | ||
| client.stringVariationDetail.mockResolvedValue(detail); | ||
| const session = createLDServerSession(client, context); | ||
| const result = await session.stringVariationDetail('my-flag', 'default'); | ||
| expect(result).toEqual(detail); | ||
| expect(client.stringVariationDetail).toHaveBeenCalledWith('my-flag', context, 'default'); | ||
| }); | ||
|
|
||
| it('jsonVariationDetail() calls base client with bound context', async () => { | ||
| const client = makeMockBaseClient(); | ||
| const detail = { | ||
| value: { key: 'value' }, | ||
| variationIndex: 1, | ||
| reason: { kind: 'RULE_MATCH' as const }, | ||
| }; | ||
| // @ts-ignore — valid LDEvaluationDetailTyped<unknown> shape; mock type is too narrow | ||
| client.jsonVariationDetail.mockResolvedValue(detail); | ||
| const session = createLDServerSession(client, context); | ||
| const result = await session.jsonVariationDetail('my-flag', {}); | ||
| expect(result).toEqual(detail); | ||
| expect(client.jsonVariationDetail).toHaveBeenCalledWith('my-flag', context, {}); | ||
| }); | ||
|
|
||
| it('allFlagsState() calls base client with bound context', async () => { | ||
| const client = makeMockBaseClient(); | ||
| const session = createLDServerSession(client, context); | ||
| await session.allFlagsState(); | ||
| expect(client.allFlagsState).toHaveBeenCalledWith(context, undefined); | ||
| }); | ||
|
|
||
| it('allFlagsState() forwards options to base client', async () => { | ||
| const client = makeMockBaseClient(); | ||
| const session = createLDServerSession(client, context); | ||
| const options = { clientSideOnly: true }; | ||
| await session.allFlagsState(options); | ||
| expect(client.allFlagsState).toHaveBeenCalledWith(context, options); | ||
| }); | ||
|
|
||
| describe('given a browser environment (window defined)', () => { | ||
| let originalWindow: typeof globalThis.window; | ||
|
|
||
| beforeEach(() => { | ||
| originalWindow = globalThis.window; | ||
| // @ts-ignore | ||
| globalThis.window = {}; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // @ts-ignore | ||
| globalThis.window = originalWindow; | ||
| }); | ||
|
|
||
| it('throws an error instead of returning a no-op session', () => { | ||
| const client = makeMockBaseClient(); | ||
| expect(() => createLDServerSession(client, context)).toThrow( | ||
| 'createLDServerWrapper must only be called on the server.', | ||
| ); | ||
| }); | ||
| }); |
62 changes: 62 additions & 0 deletions
62
packages/sdk/react/__tests__/server/useLDServerSession.test.ts
This file contains hidden or 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,62 @@ | ||
| import { LDContext } from '@launchdarkly/js-server-sdk-common'; | ||
|
|
||
| import { createLDServerSession, useLDServerSession } from '../../src/server/index'; | ||
| import type { LDServerSession } from '../../src/server/LDClient'; | ||
|
|
||
| // The `mock` prefix is required so ts-jest's hoist plugin allows this variable | ||
| // to be referenced inside the jest.mock() factory below. | ||
| let mockCacheStore: { session: LDServerSession | null } = { session: null }; | ||
|
|
||
| jest.mock('react', () => ({ | ||
| cache: (_fn: unknown) => () => mockCacheStore, | ||
| })); | ||
|
|
||
| beforeEach(() => { | ||
| mockCacheStore = { session: null }; | ||
| }); | ||
|
|
||
| it('useLDServerSession() returns null when no session has been stored', () => { | ||
| const result = useLDServerSession(); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('useLDServerSession() returns the session stored by createLDServerSession()', () => { | ||
| const context: LDContext = { kind: 'user', key: 'test-user' }; | ||
| const client = { | ||
| initialized: jest.fn(() => true), | ||
| boolVariation: jest.fn(), | ||
| numberVariation: jest.fn(), | ||
| stringVariation: jest.fn(), | ||
| jsonVariation: jest.fn(), | ||
| boolVariationDetail: jest.fn(), | ||
| numberVariationDetail: jest.fn(), | ||
| stringVariationDetail: jest.fn(), | ||
| jsonVariationDetail: jest.fn(), | ||
| allFlagsState: jest.fn(), | ||
| }; | ||
| // @ts-ignore — minimal mock satisfies LDServerBaseClient structurally | ||
| const session = createLDServerSession(client, context); | ||
| const result = useLDServerSession(); | ||
| expect(result).toBe(session); | ||
| }); | ||
|
|
||
| describe('given a browser environment (window defined)', () => { | ||
| let originalWindow: typeof globalThis.window; | ||
|
|
||
| beforeEach(() => { | ||
| originalWindow = globalThis.window; | ||
| // @ts-ignore | ||
| globalThis.window = {}; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| // @ts-ignore | ||
| globalThis.window = originalWindow; | ||
| }); | ||
|
|
||
| it('throws when called in a browser environment', () => { | ||
| expect(() => useLDServerSession()).toThrow( | ||
| 'useLDServerSession must only be called on the server', | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or 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,3 @@ | ||
| # Temporary ignore to keep PRs manageable. | ||
|
|
||
| server-only | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will commit the example application in a separate PR. Wanted to focus this one on the implementation changes