Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/core/src/flags/__tests__/FlagsClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ jest.spyOn(NativeModules.DdFlags, 'setEvaluationContext').mockResolvedValue({
variationType: '',
variationValue: '',
extraLogging: {}
},
'test-flag-with-alloc-key': {
key: 'test-flag-with-alloc-key',
value: true,
allocationKey: 'alloc-abc',
variationKey: 'true',
reason: 'TARGETED',
doLog: true,
variationType: '',
variationValue: '',
extraLogging: {}
}
});

Expand Down Expand Up @@ -206,6 +217,21 @@ describe('FlagsClient', () => {
});
});

it('should populate allocationKey in details', async () => {
const flagsClient = DdFlags.getClient();
await flagsClient.setEvaluationContext({
targetingKey: 'test-user-1',
attributes: { country: 'US' }
});

const details = flagsClient.getBooleanDetails(
'test-flag-with-alloc-key',
false
);

expect(details.allocationKey).toBe('alloc-abc');
});

it('should return TYPE_MISMATCH when using wrong typed accessor method', async () => {
// Flag values are mocked in the __mocks__/react-native.ts file.
const flagsClient = DdFlags.getClient();
Expand Down
16 changes: 16 additions & 0 deletions packages/react-native-openfeature/__mocks__/react-native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

// eslint-disable-next-line @typescript-eslint/no-var-requires
const actualRN = require('react-native');

actualRN.NativeModules.DdFlags = {
enable: jest.fn(() => Promise.resolve()),
setEvaluationContext: jest.fn(() => Promise.resolve({})),
trackEvaluation: jest.fn(() => Promise.resolve())
};

module.exports = actualRN;
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

import { DdFlags } from '@datadog/mobile-react-native';
import { NativeModules } from 'react-native';

import { DatadogOpenFeatureProvider } from '../provider';

jest.spyOn(NativeModules.DdFlags, 'setEvaluationContext').mockResolvedValue({
'bool-flag': {
key: 'bool-flag',
value: true,
allocationKey: 'alloc-xyz',
variationKey: 'true',
reason: 'TARGETED',
doLog: true,
variationType: '',
variationValue: '',
extraLogging: {}
},
'flag-no-alloc': {
key: 'flag-no-alloc',
value: 'hello',
allocationKey: '',
variationKey: 'hello',
reason: 'STATIC',
doLog: true,
variationType: '',
variationValue: '',
extraLogging: {}
}
});

describe('DatadogOpenFeatureProvider', () => {
let provider: DatadogOpenFeatureProvider;

beforeEach(async () => {
jest.clearAllMocks();

Object.assign(DdFlags, {
isFeatureEnabled: false,
clients: {}
});

await DdFlags.enable();

provider = new DatadogOpenFeatureProvider();
await provider.initialize({ targetingKey: 'user-1' });
});

describe('toFlagResolution / flagMetadata', () => {
it('should include allocationKey in flagMetadata when present', () => {
const result = provider.resolveBooleanEvaluation(
'bool-flag',
false,
{},
{} as any
);

expect(result.flagMetadata).toEqual({
allocationKey: 'alloc-xyz'
});
});
Comment on lines +63 to +66

it('should return undefined flagMetadata when allocationKey is absent', () => {
const result = provider.resolveStringEvaluation(
'flag-no-alloc',
'default',
{},
{} as any
);

expect(result.flagMetadata).toBeUndefined();
});
});
});
4 changes: 2 additions & 2 deletions packages/react-native-openfeature/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { DdFlags } from '@datadog/mobile-react-native';
import type {
FlagDetails,
FlagsClient,
EvaluationContext as DdEvaluationContext
EvaluationContext as DdEvaluationContext,
PrimitiveValue
} from '@datadog/mobile-react-native';
Comment on lines 8 to 13
import { OpenFeatureEventEmitter, ErrorCode } from '@openfeature/web-sdk';
import type {
Expand All @@ -19,7 +20,6 @@ import type {
Provider,
ProviderMetadata,
ResolutionDetails,
PrimitiveValue,
ProviderEventEmitter,
ProviderEvents
} from '@openfeature/web-sdk';
Expand Down