From 23e4b32a963609d5540f22fae96281f9d78ba285 Mon Sep 17 00:00:00 2001 From: Dustin Schie Date: Sat, 31 Oct 2020 20:47:53 -0400 Subject: [PATCH] test(__tests__/getters): adding memoized number getters adding tests for the memoized number getters re #1108 --- __tests__/getters.test.ts | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/__tests__/getters.test.ts b/__tests__/getters.test.ts index a9db8a341..b433de218 100644 --- a/__tests__/getters.test.ts +++ b/__tests__/getters.test.ts @@ -90,3 +90,68 @@ describe('string getters', () => { } ); }); + +const memoizedNumberGetters = [ + 'getApiLevel', + 'getPreviewSdkInt', + 'getFirstInstallTime', + 'getLastUpdateTime', + 'getTotalMemory', + 'getMaxMemory', +].map(makeTable); + +describe('number getters', () => { + describe.each(memoizedNumberGetters)( + '%s*', + (_name, asyncGetter, syncGetter, asyncNativeGetter, syncNativeGetter) => { + beforeEach(() => { + clearMemo(); + Platform.OS = 'android'; + asyncNativeGetter.mockClear(); + syncNativeGetter.mockClear(); + }); + + it('should have an async version', () => { + expect(typeof asyncGetter).toBe('function'); + }); + + it('should have a sync version', () => { + expect(typeof syncGetter).toBe('function'); + }); + + it('should call native async module function', async () => { + const resp = await asyncGetter(); + expect(resp).toEqual(-1); + expect(asyncNativeGetter).toHaveBeenCalled(); + }); + + it('should call native sync module function', () => { + const resp = syncGetter(); + expect(resp).toEqual(-1); + expect(syncNativeGetter).toHaveBeenCalled(); + }); + + it('should not call native sync module function on unsupported OS', () => { + Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything + const resp = syncGetter(); + expect(resp).toEqual(-1); + expect(syncNativeGetter).not.toHaveBeenCalled(); + }); + + it('should not call native async module function on unsupported OS', async () => { + Platform.OS = 'GLaDOS' as any; // setting OS to something that won't match anything + const resp = await asyncGetter(); + expect(resp).toEqual(-1); + expect(asyncNativeGetter).not.toHaveBeenCalled(); + }); + + it('should use memoized value if there exists one', async () => { + const resp = await asyncGetter(); + const resp2 = syncGetter(); + expect(resp).toBe(resp2); + expect(asyncNativeGetter).toHaveBeenCalled(); + expect(syncNativeGetter).not.toHaveBeenCalled(); + }); + } + ); +});