From 81120964df04c25e7ac47b166a155c5ab9fddd37 Mon Sep 17 00:00:00 2001 From: Daniel Murray Date: Fri, 13 Sep 2024 12:57:27 +0100 Subject: [PATCH 1/3] add generic get and post github functions and related unit tests --- src/api-sdk/github/github.ts | 12 +++++++++++ test/unit/api-sdk/github/github.spec.ts | 28 ++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/api-sdk/github/github.ts b/src/api-sdk/github/github.ts index 1ad195a..a49b979 100644 --- a/src/api-sdk/github/github.ts +++ b/src/api-sdk/github/github.ts @@ -66,6 +66,18 @@ export class Github { return this.responseHandler(response, collaboratorsPerRepoMapping); } + // generic get and post functions + + public async getData(url: string): Promise | ApiErrorResponse> { + const response = await this.request.httpGet(url); + return this.responseHandler(response); + } + + public async postData(url: string, body: any): Promise | ApiErrorResponse> { + const response = await this.request.httpPost(url, JSON.stringify(body)); + return this.responseHandler(response); + } + private responseHandler( response: any, responseMap?: (body: any) => T diff --git a/test/unit/api-sdk/github/github.spec.ts b/test/unit/api-sdk/github/github.spec.ts index 2f36a1d..24849fe 100644 --- a/test/unit/api-sdk/github/github.spec.ts +++ b/test/unit/api-sdk/github/github.spec.ts @@ -23,7 +23,11 @@ import { MOCK_FULL_RESPONSE_REPO, MOCK_FULL_RESPONSE_MEMBERS, MOCK_ISSUE_RESPONSE, - MOCK_ISSUE_BODY + MOCK_ISSUE_BODY, + MOCK_GET, + MOCK_GET_RESPONSE, + MOCK_POST, + MOCK_POST_RESPONSE } from '../../../mock/data.mock'; import { HttpResponse } from '../../../../src/http-request/type'; @@ -137,6 +141,28 @@ describe('Github sdk module test suites', () => { expect(result).toEqual(MOCK_COLLABORATORS_PER_REPO_RESPONSE); }); + test('should return get data ', async () => { + httpRequestMock.httpGet.mockResolvedValue(createMockHttpResponse(MOCK_GET)); + + const url = 'https://api.github.com/test/get'; + const result = await github.getData(url); + + expect(httpRequestMock.httpGet).toHaveBeenCalledWith(url); + expect(result).toEqual(MOCK_GET_RESPONSE); + }); + + test('should post data successfully', async () => { + httpRequestMock.httpPost.mockResolvedValue(createMockHttpResponse(MOCK_POST)); + + const url = 'https://api.github.com/test/post'; + const body = { key: 'value' }; + + const result = await github.postData(url, body); + + expect(httpRequestMock.httpPost).toHaveBeenCalledWith(url, JSON.stringify(body)); + expect(result).toEqual(MOCK_POST_RESPONSE); + }); + test('Should return an object with an error property', async () => { httpRequestMock.httpGet.mockResolvedValue(createMockHttpResponse(MOCK_TEAMS, 500, MOCK_ERROR)); From f22be12173c68619253707f9eef989c91c4595a1 Mon Sep 17 00:00:00 2001 From: Daniel Murray Date: Fri, 13 Sep 2024 12:57:39 +0100 Subject: [PATCH 2/3] add mocks for generic functions and fix linting issues --- test/mock/data.mock.ts | 44 ++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/test/mock/data.mock.ts b/test/mock/data.mock.ts index cd99525..9cdc778 100644 --- a/test/mock/data.mock.ts +++ b/test/mock/data.mock.ts @@ -8,7 +8,7 @@ export const MOCK_HEADERS: ApiOptions = { export const MOCK_REPOS = [ { - name: "repo1", + name: 'repo1', archived: false, created_at: '2015-12-10T11:48:11Z', description: 'Best repo 1', @@ -18,7 +18,7 @@ export const MOCK_REPOS = [ visibility: 'public' }, { - name: "repo2", + name: 'repo2', archived: false, created_at: '2018-12-10T11:48:11Z', description: 'Best repo 2', @@ -31,7 +31,7 @@ export const MOCK_REPOS = [ export const MOCK_UNMAPPED_RESPONSE_REPO = [ { - name: "repo1", + name: 'repo1', archived: false, created_at: '2015-12-10T11:48:11Z', description: 'Best repo 1', @@ -42,7 +42,7 @@ export const MOCK_UNMAPPED_RESPONSE_REPO = [ test: 'test' }, { - name: "repo2", + name: 'repo2', archived: false, created_at: '2018-12-10T11:48:11Z', description: 'Best repo 2', @@ -80,7 +80,7 @@ export const MOCK_FULL_RESPONSE_MEMBERS = [ ...MOCK_MEMBERS ]; export const MOCK_MEMBER_FETCH_RESPONSE = { httpStatusCode: 200, - resource: MOCK_MEMBERS.map(({ repos_url, ...rest }) => rest) + resource: MOCK_MEMBERS.map(({ repos_url: _repos_url, ...rest }) => rest) }; export const MOCK_TEAMS = [ @@ -104,7 +104,7 @@ export const MOCK_TEAMS = [ export const MOCK_TEAM_FETCH_RESPONSE = { httpStatusCode: 200, - resource: MOCK_TEAMS.map(({ members_url, repositories_url, ...rest }) => rest) + resource: MOCK_TEAMS.map(({ members_url: _members_url, repositories_url: _repositories_url, ...rest }) => rest) }; export const MOCK_MEMBERS_PER_TEAM = [ @@ -150,22 +150,22 @@ export const MOCK_COLLABORATORS_PER_REPO = [ export const MOCK_COLLABORATORS_PER_REPO_RESPONSE = { httpStatusCode: 200, - resource: MOCK_COLLABORATORS_PER_REPO.map(({ other, ...rest }) => rest) + resource: MOCK_COLLABORATORS_PER_REPO.map(({ other: _other, ...rest }) => rest) }; export const MOCK_ISSUE_BODY = { - title: "Add member to the team", - body: "Add member Bob to the team Alice.", - assignees: ["IDC TEAM"], - labels: ["GIT"] + title: 'Add member to the team', + body: 'Add member Bob to the team Alice.', + assignees: ['IDC TEAM'], + labels: ['GIT'] }; export const MOCK_ISSUE_RESPONSE = { id: 1, number: 1347, - state: "open", - title: "Add member to the team", - body: "Add member Bob to the team Alice." + state: 'open', + title: 'Add member to the team', + body: 'Add member Bob to the team Alice.' }; export const MOCK_ERROR = { @@ -177,9 +177,23 @@ export const MOCK_ERROR_RESPONSE = { errors: [MOCK_ERROR] }; -export const MOCK_403_ERROR_MSG = { "message": "Must have admin rights to Repository." }; +export const MOCK_403_ERROR_MSG = { 'message': 'Must have admin rights to Repository.' }; export const MOCK_403_ERROR_RESPONSE = { httpStatusCode: 403, errors: [MOCK_403_ERROR_MSG] }; + +export const MOCK_GET = { data: 'test data' }; + +export const MOCK_GET_RESPONSE = { + httpStatusCode: 200, + resource: MOCK_GET +}; + +export const MOCK_POST = { success: true }; + +export const MOCK_POST_RESPONSE = { + httpStatusCode: 200, + resource: MOCK_POST +}; From c5e318f3cd1c27969db2fc66ccfb18cf90d2334a Mon Sep 17 00:00:00 2001 From: Daniel Murray Date: Mon, 23 Sep 2024 11:40:09 +0100 Subject: [PATCH 3/3] bump version to 1.0.8 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index e150329..d866ed7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@co-digital/api-sdk", - "version": "1.0.7", + "version": "1.0.8", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@co-digital/api-sdk", - "version": "1.0.7", + "version": "1.0.8", "license": "MIT", "dependencies": { "axios": "^1.7.4" diff --git a/package.json b/package.json index 01ccb0a..7a7a682 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@co-digital/api-sdk", - "version": "1.0.7", + "version": "1.0.8", "description": "An API SDK for Node.JS applications in CO Digital.", "homepage": "https://github.com/cabinetoffice/node-api-sdk#README.md", "main": "./lib/index.js",