plugins/graphiql: update tests for GraphiQLPage + fixes
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
module.exports = {
|
||||
extends: [require.resolve('@backstage/cli/config/eslint')],
|
||||
rules: {
|
||||
// Prefer to use rendered.getBy*, which will throw an error
|
||||
'jest/expect-expect': 0,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@backstage/core": "^0.1.1-alpha.4",
|
||||
"@backstage/test-utils": "^0.1.1-alpha.4",
|
||||
"@backstage/theme": "^0.1.1-alpha.4",
|
||||
"@material-ui/core": "^4.9.1",
|
||||
"@material-ui/icons": "^4.9.1",
|
||||
|
||||
@@ -15,23 +15,74 @@
|
||||
*/
|
||||
|
||||
import React from 'react';
|
||||
import { render } from '@testing-library/react';
|
||||
import mockFetch from 'jest-fetch-mock';
|
||||
import { GraphiQLPage } from './GraphiQLPage';
|
||||
import { ThemeProvider } from '@material-ui/core';
|
||||
import { lightTheme } from '@backstage/theme';
|
||||
import { ApiProvider, ApiRegistry } from '@backstage/core';
|
||||
import { renderWithEffects } from '@backstage/test-utils';
|
||||
import { GraphQLBrowseApi, graphQlBrowseApiRef } from 'lib/api';
|
||||
|
||||
jest.mock('graphiql', () => () => '<GraphiQL />');
|
||||
jest.mock('components/GraphiQLBrowser', () => ({
|
||||
GraphiQLBrowser: () => '<GraphiQLBrowser />',
|
||||
}));
|
||||
|
||||
describe('GraphiQLPage', () => {
|
||||
it('should render', () => {
|
||||
mockFetch.mockResponse(() => new Promise(() => {}));
|
||||
const rendered = render(
|
||||
<ThemeProvider theme={lightTheme}>
|
||||
<GraphiQLPage />
|
||||
</ThemeProvider>,
|
||||
it('should show progress', async () => {
|
||||
const loadingApi: GraphQLBrowseApi = {
|
||||
async getEndpoints() {
|
||||
await new Promise(() => {});
|
||||
return [];
|
||||
},
|
||||
};
|
||||
|
||||
const rendered = await renderWithEffects(
|
||||
<ApiProvider apis={ApiRegistry.from([[graphQlBrowseApiRef, loadingApi]])}>
|
||||
<ThemeProvider theme={lightTheme}>
|
||||
<GraphiQLPage />
|
||||
</ThemeProvider>
|
||||
,
|
||||
</ApiProvider>,
|
||||
);
|
||||
expect(rendered.getByText('GraphiQL')).toBeInTheDocument();
|
||||
expect(rendered.getByText('<GraphiQL />')).toBeInTheDocument();
|
||||
|
||||
rendered.getByText('GraphiQL');
|
||||
rendered.getByTestId('progress');
|
||||
});
|
||||
|
||||
it('should show error', async () => {
|
||||
const loadingApi: GraphQLBrowseApi = {
|
||||
async getEndpoints() {
|
||||
throw new Error('NOPE');
|
||||
},
|
||||
};
|
||||
|
||||
const rendered = await renderWithEffects(
|
||||
<ApiProvider apis={ApiRegistry.from([[graphQlBrowseApiRef, loadingApi]])}>
|
||||
<ThemeProvider theme={lightTheme}>
|
||||
<GraphiQLPage />
|
||||
</ThemeProvider>
|
||||
</ApiProvider>,
|
||||
);
|
||||
|
||||
rendered.getByText('GraphiQL');
|
||||
rendered.getByText('Failed to load GraphQL endpoints, Error: NOPE');
|
||||
});
|
||||
|
||||
it('should show GraphiQLBrowser', async () => {
|
||||
const loadingApi: GraphQLBrowseApi = {
|
||||
async getEndpoints() {
|
||||
return [];
|
||||
},
|
||||
};
|
||||
|
||||
const rendered = await renderWithEffects(
|
||||
<ApiProvider apis={ApiRegistry.from([[graphQlBrowseApiRef, loadingApi]])}>
|
||||
<ThemeProvider theme={lightTheme}>
|
||||
<GraphiQLPage />
|
||||
</ThemeProvider>
|
||||
</ApiProvider>,
|
||||
);
|
||||
|
||||
rendered.getByText('GraphiQL');
|
||||
rendered.getByText('<GraphiQLBrowser />');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -46,9 +46,8 @@ export const GraphiQLPage: FC<{}> = () => {
|
||||
content = (
|
||||
<Content>
|
||||
<Typography variant="h4" color="error">
|
||||
Failed to load GraphQL endpoints, {endpoints.error}
|
||||
<br />
|
||||
We also need a proper error component
|
||||
{/* TODO: provide a proper error component */}
|
||||
Failed to load GraphQL endpoints, {String(endpoints.error)}
|
||||
</Typography>
|
||||
</Content>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user