From 0fb3f9460da0f25d8d30e3c726ccc1bec10b753b Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Fri, 5 Apr 2024 09:52:33 +0200 Subject: [PATCH] feat: modify clean context function to return removed properties --- .../features/playground/clean-context.test.ts | 40 ++++++++++++++----- src/lib/features/playground/clean-context.ts | 22 +++++++--- .../features/playground/playground-service.ts | 3 +- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/lib/features/playground/clean-context.test.ts b/src/lib/features/playground/clean-context.test.ts index bf32cacf98..74f672739f 100644 --- a/src/lib/features/playground/clean-context.test.ts +++ b/src/lib/features/playground/clean-context.test.ts @@ -1,15 +1,15 @@ import { cleanContext } from './clean-context'; -test('strips invalid context properties from the context', async () => { - const invalidJsonTypes = { - object: {}, - array: [], - true: true, - false: false, - number: 123, - null: null, - }; +const invalidJsonTypes = { + object: {}, + array: [], + true: true, + false: false, + number: 123, + null: null, +}; +test('strips invalid context properties from the context', async () => { const validValues = { appName: 'test', }; @@ -19,7 +19,7 @@ test('strips invalid context properties from the context', async () => { ...validValues, }; - const cleanedContext = cleanContext(inputContext); + const { context: cleanedContext } = cleanContext(inputContext); expect(cleanedContext).toStrictEqual(validValues); }); @@ -29,7 +29,25 @@ test("doesn't add non-existing properties", async () => { appName: 'test', }; - const output = cleanContext(input); + const { context: output } = cleanContext(input); expect(output).toStrictEqual(input); }); + +test('it returns the names of all the properties it removed', async () => { + const { removedProperties } = cleanContext({ + appName: 'test', + ...invalidJsonTypes, + }); + + const invalidProperties = Object.keys(invalidJsonTypes); + + // verify that the two lists contain all the same elements + expect(removedProperties).toEqual( + expect.arrayContaining(invalidProperties), + ); + + expect(invalidProperties).toEqual( + expect.arrayContaining(removedProperties), + ); +}); diff --git a/src/lib/features/playground/clean-context.ts b/src/lib/features/playground/clean-context.ts index 238c0b0960..e9055fd470 100644 --- a/src/lib/features/playground/clean-context.ts +++ b/src/lib/features/playground/clean-context.ts @@ -1,16 +1,26 @@ import type { SdkContextSchema } from '../../openapi'; -export const cleanContext = (context: SdkContextSchema): SdkContextSchema => { +export const cleanContext = ( + context: SdkContextSchema, +): { context: SdkContextSchema; removedProperties: string[] } => { const { appName, ...otherContextFields } = context; + const removedProperties: string[] = []; const cleanedContextFields = Object.fromEntries( - Object.entries(otherContextFields).filter( - ([key, value]) => key === 'properties' || typeof value === 'string', - ), + Object.entries(otherContextFields).filter(([key, value]) => { + if (key === 'properties' || typeof value === 'string') { + return true; + } + removedProperties.push(key); + return false; + }), ); return { - ...cleanedContextFields, - appName, + context: { + ...cleanedContextFields, + appName, + }, + removedProperties, }; }; diff --git a/src/lib/features/playground/playground-service.ts b/src/lib/features/playground/playground-service.ts index a04b05828a..320090686c 100644 --- a/src/lib/features/playground/playground-service.ts +++ b/src/lib/features/playground/playground-service.ts @@ -126,7 +126,8 @@ export class PlaygroundService { ), ); - const contexts = generateObjectCombinations(cleanContext(context)); + const { context: cleanedContext } = cleanContext(context); + const contexts = generateObjectCombinations(cleanedContext); validateQueryComplexity( environments.length,