From 7866861572955369ebbc1c04ebf4af30305392c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Costa?= Date: Thu, 18 Jan 2024 11:54:25 +0000 Subject: [PATCH] Added tests to GithubMultiOrgEntityProvider, reduced redundancies in the mocks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Costa --- .../GithubMultiOrgEntityProvider.test.ts | 102 +++++++++--------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/plugins/catalog-backend-module-github/src/providers/GithubMultiOrgEntityProvider.test.ts b/plugins/catalog-backend-module-github/src/providers/GithubMultiOrgEntityProvider.test.ts index 504e5772dd..51ff514ce9 100644 --- a/plugins/catalog-backend-module-github/src/providers/GithubMultiOrgEntityProvider.test.ts +++ b/plugins/catalog-backend-module-github/src/providers/GithubMultiOrgEntityProvider.test.ts @@ -43,11 +43,50 @@ jest.mock('@backstage/integration', () => ({ describe('GithubMultiOrgEntityProvider', () => { describe('read', () => { + let mockClient; + let entityProviderConnection; + let logger; + let gitHubConfig; + let mockGetCredentials; + let entityProvider; + + beforeEach(() => { + mockClient = jest.fn(); + (graphql.defaults as jest.Mock).mockReturnValue(mockClient); + + entityProviderConnection = { + applyMutation: jest.fn(), + refresh: jest.fn(), + }; + + logger = getVoidLogger(); + + gitHubConfig = { host: 'github.com' }; + + mockGetCredentials = jest.fn().mockReturnValue({ + headers: { token: 'blah' }, + type: 'app', + }); + + const githubCredentialsProvider: GithubCredentialsProvider = { + getCredentials: mockGetCredentials, + }; + + entityProvider = new GithubMultiOrgEntityProvider({ + id: 'my-id', + gitHubConfig, + githubCredentialsProvider, + githubUrl: 'https://github.com', + logger, + orgs: ['orgA', 'orgB'], // only include for tests that require it + }); + + entityProvider.connect(entityProviderConnection); + }); + afterEach(() => jest.resetAllMocks()); it('should read specified orgs', async () => { - const mockClient = jest.fn(); - mockClient .mockResolvedValueOnce({ organization: { @@ -155,36 +194,6 @@ describe('GithubMultiOrgEntityProvider', () => { (graphql.defaults as jest.Mock).mockReturnValue(mockClient); - const entityProviderConnection: EntityProviderConnection = { - applyMutation: jest.fn(), - refresh: jest.fn(), - }; - - const logger = getVoidLogger(); - const gitHubConfig: GithubIntegrationConfig = { - host: 'github.com', - }; - - const mockGetCredentials = jest.fn().mockReturnValue({ - headers: { token: 'blah' }, - type: 'app', - }); - - const githubCredentialsProvider: GithubCredentialsProvider = { - getCredentials: mockGetCredentials, - }; - - const entityProvider = new GithubMultiOrgEntityProvider({ - id: 'my-id', - gitHubConfig, - githubCredentialsProvider, - githubUrl: 'https://github.com', - logger, - orgs: ['orgA', 'orgB'], - }); - - entityProvider.connect(entityProviderConnection); - await entityProvider.read(); expect(mockGetCredentials).toHaveBeenCalledWith({ @@ -353,8 +362,6 @@ describe('GithubMultiOrgEntityProvider', () => { }, ]); - const mockClient = jest.fn(); - mockClient .mockResolvedValueOnce({ organization: { @@ -462,26 +469,11 @@ describe('GithubMultiOrgEntityProvider', () => { (graphql.defaults as jest.Mock).mockReturnValue(mockClient); - const entityProviderConnection: EntityProviderConnection = { - applyMutation: jest.fn(), - refresh: jest.fn(), - }; - - const logger = getVoidLogger(); - const gitHubConfig: GithubIntegrationConfig = { - host: 'github.com', - }; - - const mockGetCredentials = jest.fn().mockReturnValue({ - headers: { token: 'blah' }, - type: 'app', - }); - const githubCredentialsProvider: GithubCredentialsProvider = { getCredentials: mockGetCredentials, }; - const entityProvider = new GithubMultiOrgEntityProvider({ + entityProvider = new GithubMultiOrgEntityProvider({ id: 'my-id', gitHubConfig, githubCredentialsProvider, @@ -642,6 +634,16 @@ describe('GithubMultiOrgEntityProvider', () => { type: 'full', }); }); + + it('should not call applyMutation if an error is thrown', async () => { + mockClient.mockImplementationOnce(() => { + throw new Error('Network Error'); + }); + + await expect(entityProvider.read()).rejects.toThrow('Network Error'); + + expect(entityProviderConnection.applyMutation).not.toHaveBeenCalled(); + }); }); describe('withLocations', () => {