From ea7f773a229bb79e48f2435115b5b479f092d70c Mon Sep 17 00:00:00 2001 From: Lowie Benoot Date: Wed, 31 Aug 2022 16:11:26 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9C=A8=20Allow=20overwriting=20the=20act?= =?UTF-8?q?ion=20endpoints.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It's not very useful, but it's mostly just for testing, so you can mock it. You can now mock it with jest, which actually overwrites the original function. For example: ``` jest.spyOn(API.companies, 'info').mockReturnValue(new Promise(jest.fn())); ``` --- src/__tests__/main.test.ts | 15 +++++++++++++++ src/main.ts | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/__tests__/main.test.ts b/src/__tests__/main.test.ts index 557fdf6..ca88064 100644 --- a/src/__tests__/main.test.ts +++ b/src/__tests__/main.test.ts @@ -53,4 +53,19 @@ describe('fetch response handling', () => { expect(api.companies.info).toEqual(api.companies.info); expect(api.companies.info).not.toEqual(api.contacts.info); }); + + it('can overwrite an endpoint', async () => { + const getAccessToken = () => 'thisisatoken'; + + const api = API({ + getAccessToken, + }); + + const mockFunction = () => {}; + + // @ts-ignore + api.contacts.info = mockFunction; + + expect(api.contacts.info).toEqual(mockFunction); + }); }); diff --git a/src/main.ts b/src/main.ts index 3dfda95..340d246 100644 --- a/src/main.ts +++ b/src/main.ts @@ -46,6 +46,18 @@ const API = (globalConfiguration: GlobalConfiguration) => { cachedActionEndpoints[domainName][actionName] = actionEndpoint; return actionEndpoint; }, + set(_target, actionNameKey, value) { + const domainName = String(domainNameKey); + const actionName = String(actionNameKey); + + if (typeof cachedActionEndpoints[domainName] === 'undefined') { + cachedActionEndpoints[domainName] = {}; + } + + cachedActionEndpoints[domainName][actionName] = value; + + return true; + }, }, ); }, From 93e150d5122c3db109eef8cd03e82e19ac6a72f0 Mon Sep 17 00:00:00 2001 From: Lowie Benoot Date: Wed, 31 Aug 2022 16:32:49 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9D=20update=20changelog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e319b65..7e56928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Changed +- Allow overwriting action endpoints. This is useful for mocking the API. ([@lowiebenoot](https://github.com/lowiebenoot) in [#334](https://github.com/teamleadercrm/sdk-js/pull/334)) + ### Deprecated ### Removed From aab76841ce28911ef1e81342fe566d0f4c625623 Mon Sep 17 00:00:00 2001 From: Lowie Benoot Date: Wed, 31 Aug 2022 20:07:41 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=85=20use=20jest.fn()=20instead=20of?= =?UTF-8?q?=20just=20a=20noop=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/main.test.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/__tests__/main.test.ts b/src/__tests__/main.test.ts index ca88064..ecc707d 100644 --- a/src/__tests__/main.test.ts +++ b/src/__tests__/main.test.ts @@ -61,9 +61,7 @@ describe('fetch response handling', () => { getAccessToken, }); - const mockFunction = () => {}; - - // @ts-ignore + const mockFunction = jest.fn(); api.contacts.info = mockFunction; expect(api.contacts.info).toEqual(mockFunction);