Skip to content

Commit a40c7e6

Browse files
authored
chore: Improve context merge test (#158)
Signed-off-by: Todd Baert <[email protected]>
1 parent 3cf5ff6 commit a40c7e6

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

test/hooks.spec.ts

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -203,30 +203,57 @@ describe('Hooks', () => {
203203
});
204204

205205
describe('Requirement 4.3.4', () => {
206-
it('"before" evaluationContext must be merged with invocation evaluationContext, which before taking precedence', async () => {
207-
await client.getBooleanValue(
208-
FLAG_KEY,
209-
false,
210-
// add a prop to the context, and some duplicates to overwrite
211-
{ invocationProp: 'abc', propToOverwrite: 'xxx' },
212-
{
213-
hooks: [
214-
{
215-
before: () => {
216-
return { hookProp: 'def', propToOverwrite: 'ghi' };
217-
},
206+
it('When before hooks have finished executing, any resulting evaluation context MUST be merged with the existing evaluation context in the following order: before-hook (highest precedence), invocation, client, api (lowest precedence).', async () => {
207+
const globalProp434 = 'globalProp';
208+
const globalPropToOverwrite434 = 'globalPropToOverwrite';
209+
const clientProp434 = 'clientProp';
210+
const clientPropToOverwrite434 = 'clientPropToOverwrite';
211+
const invocationProp434 = 'invocationProp';
212+
const invocationPropToOverwrite434 = 'invocationPropToOverwrite';
213+
const hookProp434 = 'hookProp';
214+
215+
OpenFeature.context = {
216+
[globalProp434]: true,
217+
[globalPropToOverwrite434]: false,
218+
};
219+
const clientContext = {
220+
[clientProp434]: true,
221+
[clientPropToOverwrite434]: false,
222+
[globalPropToOverwrite434]: true,
223+
};
224+
const invocationContext = {
225+
[invocationProp434]: true,
226+
[invocationPropToOverwrite434]: false,
227+
[clientPropToOverwrite434]: true,
228+
};
229+
const hookContext = {
230+
[invocationPropToOverwrite434]: true,
231+
[hookProp434]: true,
232+
};
233+
234+
const localClient = OpenFeature.getClient('merge-test', 'test', clientContext);
235+
236+
await localClient.getBooleanValue(FLAG_KEY, false, invocationContext, {
237+
hooks: [
238+
{
239+
before: () => {
240+
return hookContext;
218241
},
219-
],
220-
}
221-
);
242+
},
243+
],
244+
});
222245
expect(MOCK_PROVIDER.resolveBooleanEvaluation).toHaveBeenCalledWith(
223246
expect.anything(),
224247
expect.anything(),
225248
// ensure correct properties were maintained/overwritten
226249
expect.objectContaining({
227-
invocationProp: 'abc', // some come from invocation
228-
hookProp: 'def', // should come from before hook
229-
propToOverwrite: 'ghi', // should be overwritten by before hook
250+
[globalProp434]: true,
251+
[globalPropToOverwrite434]: true,
252+
[clientProp434]: true,
253+
[clientPropToOverwrite434]: true,
254+
[invocationProp434]: true,
255+
[invocationPropToOverwrite434]: true,
256+
[hookProp434]: true,
230257
}),
231258
expect.anything()
232259
);

0 commit comments

Comments
 (0)