From fefc545508579d81cbe52b09aad1705d2a9ab218 Mon Sep 17 00:00:00 2001 From: Erik Engervall Date: Tue, 20 Apr 2021 22:16:47 +0200 Subject: [PATCH] Create tests for useGetGitHubBatchInfo Signed-off-by: Erik Engervall --- .../src/hooks/useGetGitHubBatchInfo.test.ts | 102 ++++++++++++++++++ .../src/hooks/useGetGitHubBatchInfo.ts | 2 +- plugins/github-release-manager/src/plugin.ts | 5 +- 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 plugins/github-release-manager/src/hooks/useGetGitHubBatchInfo.test.ts diff --git a/plugins/github-release-manager/src/hooks/useGetGitHubBatchInfo.test.ts b/plugins/github-release-manager/src/hooks/useGetGitHubBatchInfo.test.ts new file mode 100644 index 0000000000..54ecfa866a --- /dev/null +++ b/plugins/github-release-manager/src/hooks/useGetGitHubBatchInfo.test.ts @@ -0,0 +1,102 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { renderHook, act } from '@testing-library/react-hooks'; +import { waitFor } from '@testing-library/react'; + +import { mockApiClient, mockSemverProject } from '../test-helpers/test-helpers'; +import { useGetGitHubBatchInfo } from './useGetGitHubBatchInfo'; + +describe('useGetGitHubBatchInfo', () => { + it('should handle repositories with releases', async () => { + const { result } = renderHook(() => + useGetGitHubBatchInfo({ + pluginApiClient: mockApiClient, + project: mockSemverProject, + refetchTrigger: 1337, + }), + ); + + await act(async () => { + await waitFor(() => result.current.gitHubBatchInfo !== undefined); + }); + + expect(result.current.gitHubBatchInfo).toMatchInlineSnapshot(` + Object { + "loading": false, + "value": Object { + "latestRelease": Object { + "htmlUrl": "mock_release_html_url", + "id": 1, + "prerelease": false, + "tagName": "rc-2020.01.01_1", + "targetCommitish": "rc/1.2.3", + }, + "releaseBranch": Object { + "commit": Object { + "commit": Object { + "tree": Object { + "sha": "mock_branch_commit_commit_tree_sha", + }, + }, + "sha": "mock_branch_commit_sha", + }, + "links": Object { + "html": "mock_branch_links_html", + }, + "name": "rc/1.2.3", + }, + "repository": Object { + "defaultBranch": "mock_defaultBranch", + "name": "mock_repo", + "pushPermissions": true, + }, + }, + } + `); + }); + + it('should handle repositories without any releases', async () => { + (mockApiClient.getLatestRelease as jest.Mock).mockResolvedValueOnce(null); + + const { result } = renderHook(() => + useGetGitHubBatchInfo({ + pluginApiClient: mockApiClient, + project: mockSemverProject, + refetchTrigger: 1337, + }), + ); + + await act(async () => { + await waitFor(() => result.current.gitHubBatchInfo !== undefined); + }); + + expect(result.current.gitHubBatchInfo).toMatchInlineSnapshot(` + Object { + "loading": false, + "value": Object { + "latestRelease": null, + "releaseBranch": null, + "repository": Object { + "defaultBranch": "mock_defaultBranch", + "name": "mock_repo", + "pushPermissions": true, + }, + }, + } + `); + }); +}); diff --git a/plugins/github-release-manager/src/hooks/useGetGitHubBatchInfo.ts b/plugins/github-release-manager/src/hooks/useGetGitHubBatchInfo.ts index e09d165871..5cf5115116 100644 --- a/plugins/github-release-manager/src/hooks/useGetGitHubBatchInfo.ts +++ b/plugins/github-release-manager/src/hooks/useGetGitHubBatchInfo.ts @@ -36,7 +36,7 @@ export const useGetGitHubBatchInfo = ({ pluginApiClient.getLatestRelease({ ...project }), ]); - if (!latestRelease) { + if (latestRelease === null) { return { latestRelease, releaseBranch: null, diff --git a/plugins/github-release-manager/src/plugin.ts b/plugins/github-release-manager/src/plugin.ts index c0c6f4fda9..9c01c1e959 100644 --- a/plugins/github-release-manager/src/plugin.ts +++ b/plugins/github-release-manager/src/plugin.ts @@ -41,7 +41,10 @@ export const gitHubReleaseManagerPlugin = createPlugin({ githubAuthApi: githubAuthApiRef, }, factory: ({ configApi, githubAuthApi }) => { - return new PluginApiClient({ configApi, githubAuthApi }); + return new PluginApiClient({ + configApi, + githubAuthApi, + }); }, }), ],