From 4a3e573e18da864bd5c3756bdf115e92b7749b21 Mon Sep 17 00:00:00 2001 From: Chris Price <6090660+crprice@users.noreply.github.com> Date: Wed, 22 Jan 2025 12:35:46 -0800 Subject: [PATCH] Add unit test to goff-api.spec.ts for support endpoint paths in data collector goff-api.ts goff-api.spec.ts Signed-off-by: Chris Price <6090660+crprice@users.noreply.github.com> --- .../src/lib/controller/goff-api.spec.ts | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/libs/providers/go-feature-flag-web/src/lib/controller/goff-api.spec.ts b/libs/providers/go-feature-flag-web/src/lib/controller/goff-api.spec.ts index 2e6d0a779..e66143a54 100644 --- a/libs/providers/go-feature-flag-web/src/lib/controller/goff-api.spec.ts +++ b/libs/providers/go-feature-flag-web/src/lib/controller/goff-api.spec.ts @@ -103,6 +103,52 @@ describe('Collect Data API', () => { ); }); + it('should call the API to collect data with endpoint path', async () => { + fetchMock.post('https://gofeatureflag.org/examplepath/v1/data/collector', 200); + const options: GoFeatureFlagWebProviderOptions = { + endpoint: 'https://gofeatureflag.org/examplepath', + apiTimeout: 1000, + }; + const goff = new GoffApiController(options); + await goff.collectData( + [ + { + key: 'flagKey', + contextKind: 'user', + creationDate: 1733138237486, + default: false, + kind: 'feature', + userKey: 'toto', + value: true, + variation: 'varA', + }, + ], + { provider: 'open-feature-js-sdk' }, + ); + expect(fetchMock.lastUrl()).toBe('https://gofeatureflag.org/examplepath/v1/data/collector'); + expect(fetchMock.lastOptions()?.headers).toEqual({ + 'Content-Type': 'application/json', + Accept: 'application/json', + }); + expect(fetchMock.lastOptions()?.body).toEqual( + JSON.stringify({ + events: [ + { + key: 'flagKey', + contextKind: 'user', + creationDate: 1733138237486, + default: false, + kind: 'feature', + userKey: 'toto', + value: true, + variation: 'varA', + }, + ], + meta: { provider: 'open-feature-js-sdk' }, + }), + ); + }); + it('should not call the API to collect data if no event provided', async () => { fetchMock.post('https://gofeatureflag.org/v1/data/collector', 200); const options: GoFeatureFlagWebProviderOptions = {