Added tests to GithubMultiOrgEntityProvider, reduced redundancies in the mocks

Signed-off-by: José Costa <jose.j.costa@shell.com>
This commit is contained in:
José Costa
2024-01-18 11:54:25 +00:00
parent a16c772dd8
commit 7866861572
@@ -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', () => {