update test

Signed-off-by: Andrew Johnson <ajohnson@gocardless.com>
This commit is contained in:
Andrew Johnson
2021-04-08 12:30:13 +01:00
parent c05da9a5d9
commit ebce9fdbee
3 changed files with 57 additions and 28 deletions
+12 -8
View File
@@ -21,15 +21,19 @@ import { graphql } from '@octokit/graphql';
const getBaseUrl = (
scmIntegrationsApi: ScmIntegrationRegistry,
location?: string,
locations: string[],
): string => {
if (!location) {
const targets = locations
.map(parseLocationReference)
.filter(location => location.type === 'url')
.map(location => location.target);
if (targets.length === 0) {
return 'https://api.github.com';
}
const config = scmIntegrationsApi.github.byUrl(
parseLocationReference(location).target,
);
const location = targets[0];
const config = scmIntegrationsApi.github.byUrl(location);
if (!config) {
throw new InputError(
@@ -65,7 +69,7 @@ type QueryOptions = {
export interface GithubDeploymentsApi {
listDeployments(
options: QueryOptions,
location?: string,
location: string[],
): Promise<GithubDeployment[]>;
}
@@ -116,9 +120,9 @@ export class GithubDeploymentsApiClient implements GithubDeploymentsApi {
async listDeployments(
options: QueryOptions,
location?: string,
locations: string[],
): Promise<GithubDeployment[]> {
const baseUrl = getBaseUrl(this.scmIntegrationsApi, location);
const baseUrl = getBaseUrl(this.scmIntegrationsApi, locations);
const token = await this.githubAuthApi.getAccessToken(['repo']);
const graphQLWithAuth = graphql.defaults({
@@ -71,8 +71,12 @@ const configApi: ConfigApi = new ConfigReader({
});
const GRAPHQL_GITHUB_API = graphql.link('https://api.github.com/graphql');
const GRAPHQL_CUSTOM_API_1 = graphql.link('https://api.github-1.com/graphql');
const GRAPHQL_CUSTOM_API_2 = graphql.link('https://api.github-2.com/graphql');
const GRAPHQL_CUSTOM_API_1 = graphql.link(
'https://api.my-github-1.com/graphql',
);
const GRAPHQL_CUSTOM_API_2 = graphql.link(
'https://api.my-github-2.com/graphql',
);
const scmIntegrationsApi = ScmIntegrations.fromConfig(configApi);
const githubAuthApi: OAuthApi = {
@@ -167,7 +171,7 @@ describe('github-deployments', () => {
).toBeInTheDocument();
});
it('should shows new data on reload', async () => {
it('should show new data on reload', async () => {
worker.use(
GRAPHQL_GITHUB_API.query('deployments', (_, res, ctx) =>
res(ctx.data(responseStub)),
@@ -212,10 +216,8 @@ describe('github-deployments', () => {
});
it('should fetch data using custom api', async () => {
const customAPI = graphql.link('https://api.my-github-1.com/graphql');
worker.use(
customAPI.query('deployments', (_, res, ctx) =>
GRAPHQL_CUSTOM_API_1.query('deployments', (_, res, ctx) =>
res(ctx.data(responseStub)),
),
);
@@ -236,10 +238,8 @@ describe('github-deployments', () => {
});
it('should fetch data using custom api', async () => {
const customAPI = graphql.link('https://api.my-github-2.com/graphql');
worker.use(
customAPI.query('deployments', (_, res, ctx) =>
GRAPHQL_CUSTOM_API_2.query('deployments', (_, res, ctx) =>
res(ctx.data(responseStub)),
),
);
@@ -259,7 +259,7 @@ describe('github-deployments', () => {
};
});
it('shows error when no matching github host', async () => {
it('shows no apiBaseUrl error', async () => {
const rendered = await renderInTestApp(
<ApiProvider apis={apis}>
<GithubDeploymentsCard />
@@ -268,7 +268,7 @@ describe('github-deployments', () => {
expect(
await rendered.findByText(
'Warning: No apiBaseUrl available for location url:https://my-github-3.com/org/repo, please check your integrations config',
'Warning: No apiBaseUrl available for location https://my-github-3.com/org/repo, please check your integrations config',
),
).toBeInTheDocument();
});
@@ -284,7 +284,7 @@ describe('github-deployments', () => {
};
});
it('shows error when no matching github host', async () => {
it('shows no matching host error', async () => {
const rendered = await renderInTestApp(
<ApiProvider apis={apis}>
<GithubDeploymentsCard />
@@ -293,10 +293,34 @@ describe('github-deployments', () => {
expect(
await rendered.findByText(
'Warning: No matching GitHub integration configuration for location url:https://my-github-unknown.com/org/repo, please check your integrations config',
'Warning: No matching GitHub integration configuration for location https://my-github-unknown.com/org/repo, please check your integrations config',
),
).toBeInTheDocument();
});
});
describe('entity with non url location', () => {
beforeEach(() => {
entity = entityStub;
entity.entity.metadata.annotations = {
'github.com/project-slug': 'org/repo',
'backstage.io/source-location':
'some-other-managed-location:my-favourite-location/org/repo',
'backstage.io/managed-by-location':
'file:my-favourite-file/org/repo.yaml',
};
});
it('displays fetched data using default github api', async () => {
worker.use(
GRAPHQL_GITHUB_API.query('deployments', (_, res, ctx) =>
res(ctx.data(responseStub)),
),
);
await assertFetchedData();
expect.assertions(7);
});
});
});
});
@@ -35,17 +35,17 @@ import {
const GithubDeploymentsComponent = ({
projectSlug,
last,
location,
locations,
}: {
projectSlug: string;
last: number;
location?: string;
locations: string[];
}) => {
const api = useApi(githubDeploymentsApiRef);
const [owner, repo] = projectSlug.split('/');
const { loading, value, error, retry: reload } = useAsyncRetry(
async () => await api.listDeployments({ owner, repo, last }, location),
async () => await api.listDeployments({ owner, repo, last }, locations),
);
if (error) {
@@ -63,9 +63,10 @@ const GithubDeploymentsComponent = ({
export const GithubDeploymentsCard = ({ last }: { last?: number }) => {
const { entity } = useEntity();
const location =
entity?.metadata.annotations?.[SOURCE_LOCATION_ANNOTATION] ||
entity?.metadata.annotations?.[LOCATION_ANNOTATION];
const locations = [
entity?.metadata.annotations?.[SOURCE_LOCATION_ANNOTATION],
entity?.metadata.annotations?.[LOCATION_ANNOTATION],
].filter(location => location !== undefined) as string[];
return !isGithubDeploymentsAvailable(entity) ? (
<MissingAnnotationEmptyState annotation={GITHUB_PROJECT_SLUG_ANNOTATION} />
@@ -75,7 +76,7 @@ export const GithubDeploymentsCard = ({ last }: { last?: number }) => {
entity?.metadata.annotations?.[GITHUB_PROJECT_SLUG_ANNOTATION] || ''
}
last={last || 10}
location={location}
locations={locations}
/>
);
};