From 28477225fc3a8c3b449ecd5f64a7c98f5541069f Mon Sep 17 00:00:00 2001 From: yuhao900914 Date: Mon, 17 Oct 2022 17:21:03 -0700 Subject: [PATCH] feat: flag configs clear up --- __tests__/client.test.ts | 13 +++++++++++++ src/experimentClient.ts | 8 ++++++++ src/stubClient.ts | 2 ++ 3 files changed, 23 insertions(+) diff --git a/__tests__/client.test.ts b/__tests__/client.test.ts index 00d1e37..8708170 100644 --- a/__tests__/client.test.ts +++ b/__tests__/client.test.ts @@ -362,3 +362,16 @@ test('configure httpClient, success', async () => { const v = client.variant('flag'); expect(v).toEqual({ value: 'key' }); }); + +test('ExperimentClient.clear, clear all variants in the cache and storage', async () => { + const client = new ExperimentClient(API_KEY, { + httpClient: new TestHttpClient(), + }); + await client.fetch(testUser); + const variant = client.variant(serverKey); + expect(variant).toEqual(serverVariant); + + client.clear(); + var clearedVariants = client.all(); + expect(clearedVariants).toEqual({}); +}); diff --git a/src/experimentClient.ts b/src/experimentClient.ts index f06955a..fe542fb 100644 --- a/src/experimentClient.ts +++ b/src/experimentClient.ts @@ -172,6 +172,14 @@ export class ExperimentClient implements Client { return { ...this.secondaryVariants(), ...this.sourceVariants() }; } + /** + * Clear all variants in the cache and storage. + */ + public clear() { + this.storage.clear(); + this.storage.save(); + } + /** * Get a copy of the internal {@link ExperimentUser} object if it is set. * diff --git a/src/stubClient.ts b/src/stubClient.ts index e830a70..2b6df12 100644 --- a/src/stubClient.ts +++ b/src/stubClient.ts @@ -35,5 +35,7 @@ export class StubExperimentClient implements Client { return {}; } + public clear() {} + public exposure(_key: string): void {} }