From 7da5e932a0f67720db206b5cdcb61321711b791f Mon Sep 17 00:00:00 2001 From: David Date: Thu, 25 Jul 2024 12:32:03 -0400 Subject: [PATCH 1/3] add enabled field --- src/__tests__/use-killswitch.test.tsx | 15 ++++++++++++++- src/killswitch.ts | 12 ++++++++++-- src/use-killswitch.ts | 16 ++++++++++++---- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/__tests__/use-killswitch.test.tsx b/src/__tests__/use-killswitch.test.tsx index b85c771..fd91d7d 100644 --- a/src/__tests__/use-killswitch.test.tsx +++ b/src/__tests__/use-killswitch.test.tsx @@ -12,7 +12,7 @@ import { useKillswitch } from '../use-killswitch'; import fetchMock from 'jest-fetch-mock'; import spyOnAlert from '../__utils__/spy-on-alert'; -function App() { +function App({ enabled = true }: { enabled?: boolean }) { const { isOk } = useKillswitch({ iosApiKey: 'apiKey', androidApiKey: 'apiKey', @@ -20,6 +20,7 @@ function App() { version: '1.0.0', apiHost: 'https://killswitch.mirego.com', timeout: 200, + enabled, }); return ( @@ -53,6 +54,18 @@ describe('useKillswitch()', () => { appStateSpy.mockRestore(); }); + it('should display "is ok" when enabled is false', async () => { + fetchMock.mockResponseOnce(JSON.stringify({ isOk: true })); + + const { rerender, getByTestId } = render(); + + rerender(); + + await waitFor(() => { + expect(getByTestId('killswitch-text').props.children).toBe('is ok'); + }); + }); + describe('when it receives an "ok" signal', () => { beforeEach(() => { fetchMock.mockResponse((_request) => { diff --git a/src/killswitch.ts b/src/killswitch.ts index 5d870c0..db2ef65 100644 --- a/src/killswitch.ts +++ b/src/killswitch.ts @@ -42,6 +42,7 @@ interface KillswitchOptions { androidApiKey: string; useNativeUI?: boolean; timeout?: number; + enabled?: boolean; } class Killswitch { @@ -50,6 +51,7 @@ class Killswitch { androidApiKey: string; useNativeUI: boolean; timeout: number; + enabled: boolean; behavior?: z.infer; @@ -59,12 +61,14 @@ class Killswitch { androidApiKey, useNativeUI = true, timeout = 2000, + enabled = true, }: KillswitchOptions) { this.apiHost = apiHost; this.iosApiKey = iosApiKey; this.androidApiKey = androidApiKey; this.useNativeUI = useNativeUI; this.timeout = timeout; + this.enabled = enabled; } get isOk() { @@ -79,7 +83,8 @@ class Killswitch { return this.behavior?.action === KillswitchBehaviorAction.KILL; } - async check(language: string, version: string) { + async check(language: string, version: string, enabled: boolean) { + if (!enabled) return { isOk: true }; try { const payload = await this.fetch(language, version); @@ -120,7 +125,6 @@ class Killswitch { signal, } ); - return response.json(); } finally { clearTimeout(timeout); @@ -134,6 +138,10 @@ class Killswitch { return; } + if (!this.enabled) { + return; + } + if (this.isAlert || this.isKill) { return Alert.alert('', this.behavior.message, this.buildAlertButtons(), { cancelable: false, diff --git a/src/use-killswitch.ts b/src/use-killswitch.ts index 63b972e..779c9f1 100644 --- a/src/use-killswitch.ts +++ b/src/use-killswitch.ts @@ -11,6 +11,7 @@ interface UseKillswitchOptions { apiHost: string; useNativeUI?: boolean; timeout?: number; + enabled?: boolean; } export function useKillswitch({ @@ -20,6 +21,7 @@ export function useKillswitch({ version, apiHost, useNativeUI = true, + enabled = true, timeout = 2000, }: UseKillswitchOptions) { const killswitchRef = useRef(null); @@ -28,7 +30,11 @@ export function useKillswitch({ const [isOk, setIsOk] = useState(null); const getKillswitch = useCallback(() => { - if (killswitchRef.current !== null) return killswitchRef.current; + if ( + killswitchRef.current !== null && + killswitchRef.current?.enabled === enabled + ) + return killswitchRef.current; const killswitch = new Killswitch({ iosApiKey, @@ -36,19 +42,21 @@ export function useKillswitch({ apiHost, useNativeUI, timeout, + enabled, }); killswitchRef.current = killswitch; return killswitch; - }, [androidApiKey, apiHost, iosApiKey, timeout, useNativeUI]); + }, [androidApiKey, apiHost, iosApiKey, timeout, useNativeUI, enabled]); useEffect(() => { async function run() { if (previousAppState !== 'active' && appState === 'active') { const { isOk: newIsOk } = await getKillswitch().check( language, - version + version, + enabled ); setIsOk(newIsOk); @@ -56,7 +64,7 @@ export function useKillswitch({ } run(); - }, [appState, getKillswitch, language, previousAppState, version]); + }, [appState, getKillswitch, language, previousAppState, version, enabled]); return { isOk, killswitch: getKillswitch() }; } From 88c547c4ca1f4e2e08bbccd8cce74922b574e900 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 25 Jul 2024 12:53:23 -0400 Subject: [PATCH 2/3] update readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index ed09f39..1bde319 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ const { isOk } = useKillswitch({ androidApiKey: androidApiKey, language: myAppLanguage, version: myAppVersion, + enabled: true, }); ``` @@ -63,6 +64,9 @@ const { isOk } = useKillswitch({ - `timeout` A number of milliseconds to wait for the back-end before returning `isOk = true`. Defaults to `2000` +- `enabled` + Toggle if you want to disabled. Defaults to `true` + ## License react-native-killswitch is © 2023 [Mirego](https://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](./LICENSE.md) file. From 0c6136406899c41cb6d62751433308f20dead020 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 25 Jul 2024 13:33:28 -0400 Subject: [PATCH 3/3] test --- src/__tests__/use-killswitch.test.tsx | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/__tests__/use-killswitch.test.tsx b/src/__tests__/use-killswitch.test.tsx index fd91d7d..6786896 100644 --- a/src/__tests__/use-killswitch.test.tsx +++ b/src/__tests__/use-killswitch.test.tsx @@ -54,18 +54,6 @@ describe('useKillswitch()', () => { appStateSpy.mockRestore(); }); - it('should display "is ok" when enabled is false', async () => { - fetchMock.mockResponseOnce(JSON.stringify({ isOk: true })); - - const { rerender, getByTestId } = render(); - - rerender(); - - await waitFor(() => { - expect(getByTestId('killswitch-text').props.children).toBe('is ok'); - }); - }); - describe('when it receives an "ok" signal', () => { beforeEach(() => { fetchMock.mockResponse((_request) => { @@ -78,6 +66,16 @@ describe('useKillswitch()', () => { await waitFor(() => screen.getByText('is ok')); }, 8000); + + it('should display "is ok" when enabled is false', async () => { + fetchMock.mockResponseOnce(JSON.stringify({ isOk: true })); + + const { rerender } = render(); + + rerender(); + + await waitFor(() => screen.getByText('is ok')); + }, 8000); }); describe('when it receives an "alert" signal', () => {