added test to assure mutation is not applied when request fails

Signed-off-by: José Costa <jose.j.costa@shell.com>
This commit is contained in:
José Costa
2024-01-16 16:08:49 +00:00
parent eb896509a2
commit 1c39e619a7
@@ -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', () => {