From da49993e3e0f79eb3f12d57e95f2ce7a4482ca49 Mon Sep 17 00:00:00 2001 From: Marcos Passos Date: Wed, 2 Oct 2024 14:56:08 -0600 Subject: [PATCH] Add env var for test mode (#69) --- src/CroctProvider.test.tsx | 18 ++++++++++++++++++ src/CroctProvider.tsx | 1 + 2 files changed, 19 insertions(+) diff --git a/src/CroctProvider.test.tsx b/src/CroctProvider.test.tsx index db80c67..c543831 100644 --- a/src/CroctProvider.test.tsx +++ b/src/CroctProvider.test.tsx @@ -27,6 +27,7 @@ describe('', () => { delete process.env.NEXT_PUBLIC_CROCT_APP_ID; delete process.env.NEXT_PUBLIC_CROCT_DEBUG; + delete process.env.NEXT_PUBLIC_CROCT_TEST; delete process.env.NEXT_PUBLIC_CROCT_BASE_ENDPOINT_URL; delete process.env.NEXT_PUBLIC_CROCT_DEFAULT_FETCH_TIMEOUT; delete process.env.NEXT_PUBLIC_CROCT_DEFAULT_PREFERRED_LOCALE; @@ -69,6 +70,7 @@ describe('', () => { it('should allow overriding the environment configuration', () => { process.env.NEXT_PUBLIC_CROCT_APP_ID = '00000000-0000-0000-0000-000000000000'; process.env.NEXT_PUBLIC_CROCT_DEBUG = 'true'; + process.env.NEXT_PUBLIC_CROCT_TEST = 'true'; process.env.NEXT_PUBLIC_CROCT_BASE_ENDPOINT_URL = 'https://example.com'; process.env.NEXT_PUBLIC_CROCT_DEFAULT_FETCH_TIMEOUT = '3000'; process.env.NEXT_PUBLIC_CROCT_DEFAULT_PREFERRED_LOCALE = 'es'; @@ -76,6 +78,7 @@ describe('', () => { const config = { appId: '11111111-1111-1111-1111-111111111111', debug: false, + test: false, baseEndpointUrl: 'https://override.com', defaultFetchTimeout: 2000, defaultPreferredLocale: 'en', @@ -101,6 +104,7 @@ describe('', () => { { appId: config.appId, debug: config.debug, + test: config.test, cookie: config.cookie, baseEndpointUrl: config.baseEndpointUrl, defaultFetchTimeout: config.defaultFetchTimeout, @@ -138,6 +142,20 @@ describe('', () => { ); }); + it('should detect the test mode from the environment', () => { + process.env.NEXT_PUBLIC_CROCT_APP_ID = '00000000-0000-0000-0000-000000000000'; + process.env.NEXT_PUBLIC_CROCT_TEST = 'true'; + + render(); + + expect(UnderlyingProvider).toHaveBeenCalledWith<[ResolvedProviderProps, any]>( + expect.objectContaining({ + test: true, + }), + expect.anything(), + ); + }); + it('should detect the base endpoint URL from the environment', () => { process.env.NEXT_PUBLIC_CROCT_APP_ID = '00000000-0000-0000-0000-000000000000'; process.env.NEXT_PUBLIC_CROCT_BASE_ENDPOINT_URL = 'https://example.com'; diff --git a/src/CroctProvider.tsx b/src/CroctProvider.tsx index 6a78e86..ff15298 100644 --- a/src/CroctProvider.tsx +++ b/src/CroctProvider.tsx @@ -24,6 +24,7 @@ export const CroctProvider: FunctionComponent = props => { appId={appId} disableCidMirroring {...getEnvEntryFlag('debug', process.env.NEXT_PUBLIC_CROCT_DEBUG)} + {...getEnvEntryFlag('test', process.env.NEXT_PUBLIC_CROCT_TEST)} {...getEnvEntry('baseEndpointUrl', process.env.NEXT_PUBLIC_CROCT_BASE_ENDPOINT_URL)} {...getEnvEntry('defaultPreferredLocale', process.env.NEXT_PUBLIC_CROCT_DEFAULT_PREFERRED_LOCALE)} {...(defaultTimeout !== undefined ? {defaultFetchTimeout: defaultTimeout} : {})}