From 1c39e619a78cf1c1ee84705bc034d6373425335f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Costa?= Date: Tue, 16 Jan 2024 16:08:49 +0000 Subject: [PATCH] added test to assure mutation is not applied when request fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Costa --- .../providers/GithubOrgEntityProvider.test.ts | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/plugins/catalog-backend-module-github/src/providers/GithubOrgEntityProvider.test.ts b/plugins/catalog-backend-module-github/src/providers/GithubOrgEntityProvider.test.ts index c054f26522..1c563b01b8 100644 --- a/plugins/catalog-backend-module-github/src/providers/GithubOrgEntityProvider.test.ts +++ b/plugins/catalog-backend-module-github/src/providers/GithubOrgEntityProvider.test.ts @@ -171,6 +171,53 @@ describe('GithubOrgEntityProvider', () => { type: 'full', }); }); + + // New test case for handling request failure + it('should not apply mutation if a request fails', async () => { + const mockClient = jest.fn(); + + // Simulate a request failure + mockClient.mockRejectedValue(new Error('Network error')); + + (graphql.defaults as jest.Mock).mockReturnValue(mockClient); + + const entityProviderConnection: EntityProviderConnection = { + applyMutation: jest.fn(), + refresh: jest.fn(), + }; + + const logger = getVoidLogger(); + + const gitHubConfig: GithubIntegrationConfig = { + host: 'https://github.com', + }; + + const mockGetCredentials = jest.fn().mockReturnValue({ + headers: { token: 'blah' }, + type: 'app', + }); + + const githubCredentialsProvider: GithubCredentialsProvider = { + getCredentials: mockGetCredentials, + }; + + const entityProvider = new GithubOrgEntityProvider({ + id: 'my-id', + githubCredentialsProvider, + orgUrl: 'https://github.com/backstage', + gitHubConfig, + logger, + }); + + entityProvider.connect(entityProviderConnection); + + try { + await entityProvider.read(); + } catch (e) { + // Failed successfuly! + } + expect(entityProviderConnection.applyMutation).not.toHaveBeenCalled(); + }); }); describe('withLocations', () => {