Skip to content

Commit

Permalink
plugins/welcome: add error button that shows an example of consuming …
Browse files Browse the repository at this point in the history
…the error api
  • Loading branch information
Rugvip committed Mar 23, 2020
1 parent 5ad9dfb commit a9aede1
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
41 changes: 41 additions & 0 deletions plugins/welcome/src/components/WelcomePage/ErrorButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import ErrorButton from './ErrorButton';
import { ApiRegistry, errorApiRef, ApiProvider } from '@spotify-backstage/core';

describe('ErrorButton', () => {
it('should trigger an error', () => {
const errorApi = { post: jest.fn() };

const rendered = render(
<ApiProvider apis={ApiRegistry.from([[errorApiRef, errorApi]])}>
<ErrorButton />
</ApiProvider>,
);

const button = rendered.getByText('Trigger an error!');
expect(button).toBeInTheDocument();

expect(errorApi.post).not.toHaveBeenCalled();
fireEvent.click(button);
expect(errorApi.post).toHaveBeenCalledWith(
expect.objectContaining({ message: 'Oh no!' }),
);
});
});
35 changes: 35 additions & 0 deletions plugins/welcome/src/components/WelcomePage/ErrorButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import React, { FC } from 'react';
import { Button } from '@material-ui/core';
import { errorApiRef, useApi } from '@spotify-backstage/core';

const ErrorButton: FC<{}> = () => {
const errorApi = useApi(errorApiRef);

const handleClick = () => {
errorApi.post(new Error('Oh no!'));
};

return (
<Button variant="contained" color="primary" onClick={handleClick}>
Trigger an error!
</Button>
);
};

export default ErrorButton;
18 changes: 14 additions & 4 deletions plugins/welcome/src/components/WelcomePage/WelcomePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ import React from 'react';
import { render } from '@testing-library/react';
import WelcomePage from './WelcomePage';
import { ThemeProvider } from '@material-ui/core';
import { BackstageTheme } from '@spotify-backstage/core';
import {
BackstageTheme,
ApiProvider,
ApiRegistry,
errorApiRef,
} from '@spotify-backstage/core';

describe('WelcomePage', () => {
it('should render', () => {
// TODO: use common test app with mock implementations of all core APIs
const rendered = render(
<ThemeProvider theme={BackstageTheme}>
<WelcomePage />
</ThemeProvider>,
<ApiProvider
apis={ApiRegistry.from([[errorApiRef, { post: jest.fn() }]])}
>
<ThemeProvider theme={BackstageTheme}>
<WelcomePage />
</ThemeProvider>
</ApiProvider>,
);
expect(rendered.baseElement).toBeInTheDocument();
});
Expand Down
10 changes: 10 additions & 0 deletions plugins/welcome/src/components/WelcomePage/WelcomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
ContentHeader,
SupportButton,
} from '@spotify-backstage/core';
import ErrorButton from './ErrorButton';

const WelcomePage: FC<{}> = () => {
const profile = { givenName: '' };
Expand Down Expand Up @@ -113,6 +114,15 @@ const WelcomePage: FC<{}> = () => {
</List>
</InfoCard>
</Grid>
<Grid item>
<InfoCard title="APIs">
<Typography>
The button below is an example of how to consume APIs.
</Typography>
<br />
<ErrorButton />
</InfoCard>
</Grid>
</Grid>
</Content>
</Page>
Expand Down

0 comments on commit a9aede1

Please sign in to comment.