From 7897d3488cbda2eb9b83abcc3bab30c0f34094cc Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 25 Apr 2020 16:43:18 +0200 Subject: [PATCH] plugins/graphiql: update tests for GraphiQLPage + fixes --- plugins/graphiql/.eslintrc.js | 4 + plugins/graphiql/package.json | 1 + .../GraphiQLPage/GraphiQLPage.test.tsx | 73 ++++++++++++++++--- .../components/GraphiQLPage/GraphiQLPage.tsx | 5 +- 4 files changed, 69 insertions(+), 14 deletions(-) diff --git a/plugins/graphiql/.eslintrc.js b/plugins/graphiql/.eslintrc.js index 13573efa9c..6a513fbafe 100644 --- a/plugins/graphiql/.eslintrc.js +++ b/plugins/graphiql/.eslintrc.js @@ -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, + }, }; diff --git a/plugins/graphiql/package.json b/plugins/graphiql/package.json index a068eb24c0..f2cf8b7930 100644 --- a/plugins/graphiql/package.json +++ b/plugins/graphiql/package.json @@ -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", diff --git a/plugins/graphiql/src/components/GraphiQLPage/GraphiQLPage.test.tsx b/plugins/graphiql/src/components/GraphiQLPage/GraphiQLPage.test.tsx index 4dd18c326f..d19e05524e 100644 --- a/plugins/graphiql/src/components/GraphiQLPage/GraphiQLPage.test.tsx +++ b/plugins/graphiql/src/components/GraphiQLPage/GraphiQLPage.test.tsx @@ -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', () => () => ''); +jest.mock('components/GraphiQLBrowser', () => ({ + GraphiQLBrowser: () => '', +})); describe('GraphiQLPage', () => { - it('should render', () => { - mockFetch.mockResponse(() => new Promise(() => {})); - const rendered = render( - - - , + it('should show progress', async () => { + const loadingApi: GraphQLBrowseApi = { + async getEndpoints() { + await new Promise(() => {}); + return []; + }, + }; + + const rendered = await renderWithEffects( + + + + + , + , ); - expect(rendered.getByText('GraphiQL')).toBeInTheDocument(); - expect(rendered.getByText('')).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( + + + + + , + ); + + 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( + + + + + , + ); + + rendered.getByText('GraphiQL'); + rendered.getByText(''); }); }); diff --git a/plugins/graphiql/src/components/GraphiQLPage/GraphiQLPage.tsx b/plugins/graphiql/src/components/GraphiQLPage/GraphiQLPage.tsx index 90a0bb92c0..39ee015bed 100644 --- a/plugins/graphiql/src/components/GraphiQLPage/GraphiQLPage.tsx +++ b/plugins/graphiql/src/components/GraphiQLPage/GraphiQLPage.tsx @@ -46,9 +46,8 @@ export const GraphiQLPage: FC<{}> = () => { content = ( - Failed to load GraphQL endpoints, {endpoints.error} -
- We also need a proper error component + {/* TODO: provide a proper error component */} + Failed to load GraphQL endpoints, {String(endpoints.error)}
);