From c6e98956a2f089da1547fbe3056047e7363c3f2b Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 9 Mar 2021 20:39:11 +0100 Subject: [PATCH] todo: replace example components with TodoList + test Signed-off-by: Patrik Oldsberg --- plugins/todo/package.json | 1 + .../ExampleComponent.test.tsx | 44 -------- .../ExampleComponent/ExampleComponent.tsx | 53 --------- .../ExampleFetchComponent.test.tsx | 40 ------- .../ExampleFetchComponent.tsx | 105 ------------------ .../components/ExampleFetchComponent/index.ts | 16 --- .../src/components/TodoList/TodoList.test.tsx | 46 ++++++++ .../todo/src/components/TodoList/TodoList.tsx | 76 +++++++++++++ .../{ExampleComponent => TodoList}/index.ts | 2 +- plugins/todo/src/plugin.ts | 3 +- 10 files changed, 125 insertions(+), 261 deletions(-) delete mode 100644 plugins/todo/src/components/ExampleComponent/ExampleComponent.test.tsx delete mode 100644 plugins/todo/src/components/ExampleComponent/ExampleComponent.tsx delete mode 100644 plugins/todo/src/components/ExampleFetchComponent/ExampleFetchComponent.test.tsx delete mode 100644 plugins/todo/src/components/ExampleFetchComponent/ExampleFetchComponent.tsx delete mode 100644 plugins/todo/src/components/ExampleFetchComponent/index.ts create mode 100644 plugins/todo/src/components/TodoList/TodoList.test.tsx create mode 100644 plugins/todo/src/components/TodoList/TodoList.tsx rename plugins/todo/src/components/{ExampleComponent => TodoList}/index.ts (91%) diff --git a/plugins/todo/package.json b/plugins/todo/package.json index 59b66dabb0..de51fe2f18 100644 --- a/plugins/todo/package.json +++ b/plugins/todo/package.json @@ -22,6 +22,7 @@ "dependencies": { "@backstage/catalog-model": "^0.7.3", "@backstage/core": "^0.7.0", + "@backstage/plugin-catalog-react": "^0.1.1", "@backstage/theme": "^0.2.3", "@material-ui/core": "^4.11.0", "@material-ui/icons": "^4.9.1", diff --git a/plugins/todo/src/components/ExampleComponent/ExampleComponent.test.tsx b/plugins/todo/src/components/ExampleComponent/ExampleComponent.test.tsx deleted file mode 100644 index 555f3be75d..0000000000 --- a/plugins/todo/src/components/ExampleComponent/ExampleComponent.test.tsx +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2021 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 { ExampleComponent } from './ExampleComponent'; -import { ThemeProvider } from '@material-ui/core'; -import { lightTheme } from '@backstage/theme'; -import { rest } from 'msw'; -import { setupServer } from 'msw/node'; -import { msw, renderInTestApp } from '@backstage/test-utils'; - -describe('ExampleComponent', () => { - const server = setupServer(); - // Enable sane handlers for network requests - msw.setupDefaultHandlers(server); - - // setup mock response - beforeEach(() => { - server.use( - rest.get('/*', (_, res, ctx) => res(ctx.status(200), ctx.json({}))), - ); - }); - - it('should render', async () => { - const rendered = await renderInTestApp( - - - , - ); - expect(rendered.getByText('Welcome to todo!')).toBeInTheDocument(); - }); -}); diff --git a/plugins/todo/src/components/ExampleComponent/ExampleComponent.tsx b/plugins/todo/src/components/ExampleComponent/ExampleComponent.tsx deleted file mode 100644 index 3dc0345ee4..0000000000 --- a/plugins/todo/src/components/ExampleComponent/ExampleComponent.tsx +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2021 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 { Typography, Grid } from '@material-ui/core'; -import { - InfoCard, - Header, - Page, - Content, - ContentHeader, - HeaderLabel, - SupportButton, -} from '@backstage/core'; -import { ExampleFetchComponent } from '../ExampleFetchComponent'; - -export const ExampleComponent = () => ( - -
- - -
- - - A description of your plugin goes here. - - - - - - All content should be wrapped in a card like this. - - - - - - - - -
-); diff --git a/plugins/todo/src/components/ExampleFetchComponent/ExampleFetchComponent.test.tsx b/plugins/todo/src/components/ExampleFetchComponent/ExampleFetchComponent.test.tsx deleted file mode 100644 index 6a5c0351d6..0000000000 --- a/plugins/todo/src/components/ExampleFetchComponent/ExampleFetchComponent.test.tsx +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2021 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 } from '@testing-library/react'; -import { ExampleFetchComponent } from './ExampleFetchComponent'; -import { rest } from 'msw'; -import { setupServer } from 'msw/node'; -import { msw } from '@backstage/test-utils'; - -describe('ExampleFetchComponent', () => { - const server = setupServer(); - // Enable sane handlers for network requests - msw.setupDefaultHandlers(server); - - // setup mock response - beforeEach(() => { - server.use( - rest.get('https://randomuser.me/*', (_, res, ctx) => - res(ctx.status(200), ctx.delay(2000), ctx.json({})), - ), - ); - }); - it('should render', async () => { - const rendered = render(); - expect(await rendered.findByTestId('progress')).toBeInTheDocument(); - }); -}); diff --git a/plugins/todo/src/components/ExampleFetchComponent/ExampleFetchComponent.tsx b/plugins/todo/src/components/ExampleFetchComponent/ExampleFetchComponent.tsx deleted file mode 100644 index 1390c8950f..0000000000 --- a/plugins/todo/src/components/ExampleFetchComponent/ExampleFetchComponent.tsx +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright 2021 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 { makeStyles } from '@material-ui/core/styles'; -import { Table, TableColumn, Progress } from '@backstage/core'; -import Alert from '@material-ui/lab/Alert'; -import { useAsync } from 'react-use'; - -const useStyles = makeStyles({ - avatar: { - height: 32, - width: 32, - borderRadius: '50%', - }, -}); - -type User = { - gender: string; // "male" - name: { - title: string; // "Mr", - first: string; // "Duane", - last: string; // "Reed" - }; - location: object; // {street: {number: 5060, name: "Hickory Creek Dr"}, city: "Albany", state: "New South Wales",…} - email: string; // "duane.reed@example.com" - login: object; // {uuid: "4b785022-9a23-4ab9-8a23-cb3fb43969a9", username: "blackdog796", password: "patch",…} - dob: object; // {date: "1983-06-22T12:30:23.016Z", age: 37} - registered: object; // {date: "2006-06-13T18:48:28.037Z", age: 14} - phone: string; // "07-2154-5651" - cell: string; // "0405-592-879" - id: { - name: string; // "TFN", - value: string; // "796260432" - }; - picture: { medium: string }; // {medium: "https://randomuser.me/api/portraits/men/95.jpg",…} - nat: string; // "AU" -}; - -type DenseTableProps = { - users: User[]; -}; - -export const DenseTable = ({ users }: DenseTableProps) => { - const classes = useStyles(); - - const columns: TableColumn[] = [ - { title: 'Avatar', field: 'avatar' }, - { title: 'Name', field: 'name' }, - { title: 'Email', field: 'email' }, - { title: 'Nationality', field: 'nationality' }, - ]; - - const data = users.map(user => { - return { - avatar: ( - {user.name.first} - ), - name: `${user.name.first} ${user.name.last}`, - email: user.email, - nationality: user.nat, - }; - }); - - return ( - - ); -}; - -export const ExampleFetchComponent = () => { - const { value, loading, error } = useAsync(async (): Promise => { - const response = await fetch('https://randomuser.me/api/?results=20'); - const data = await response.json(); - return data.results; - }, []); - - if (loading) { - return ; - } else if (error) { - return {error.message}; - } - - return ; -}; diff --git a/plugins/todo/src/components/ExampleFetchComponent/index.ts b/plugins/todo/src/components/ExampleFetchComponent/index.ts deleted file mode 100644 index e7c8364039..0000000000 --- a/plugins/todo/src/components/ExampleFetchComponent/index.ts +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright 2021 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. - */ -export { ExampleFetchComponent } from './ExampleFetchComponent'; diff --git a/plugins/todo/src/components/TodoList/TodoList.test.tsx b/plugins/todo/src/components/TodoList/TodoList.test.tsx new file mode 100644 index 0000000000..2f44b36673 --- /dev/null +++ b/plugins/todo/src/components/TodoList/TodoList.test.tsx @@ -0,0 +1,46 @@ +/* + * Copyright 2021 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 { Entity } from '@backstage/catalog-model'; +import { ApiProvider, ApiRegistry } from '@backstage/core'; +import { EntityProvider } from '@backstage/plugin-catalog-react'; +import { renderWithEffects } from '@backstage/test-utils'; +import React from 'react'; +import { TodoApi, todoApiRef } from '../../api'; +import { TodoList } from './TodoList'; + +describe('TodoList', () => { + it('should render', async () => { + const mockApi: jest.Mocked = { + listTodos: jest.fn().mockResolvedValue({ + items: [{ text: 'My TODO' }], + totalCount: 1, + cursors: { prev: 'prev', self: 'self', next: 'next' }, + }), + }; + const mockEntity = { metadata: { name: 'mock' } } as Entity; + + const rendered = await renderWithEffects( + + + + + , + ); + + await expect(rendered.findByText('My TODO')).resolves.toBeInTheDocument(); + }); +}); diff --git a/plugins/todo/src/components/TodoList/TodoList.tsx b/plugins/todo/src/components/TodoList/TodoList.tsx new file mode 100644 index 0000000000..a27734c80b --- /dev/null +++ b/plugins/todo/src/components/TodoList/TodoList.tsx @@ -0,0 +1,76 @@ +/* + * Copyright 2021 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 { Progress, Table, TableColumn, useApi } from '@backstage/core'; +import { useEntity } from '@backstage/plugin-catalog-react'; +import Alert from '@material-ui/lab/Alert'; +import React from 'react'; +import { useAsync } from 'react-use'; +import { todoApiRef } from '../../api'; +import { TodoItem } from '../../api/types'; + +const columns: TableColumn[] = [ + { title: 'Text', field: 'text' }, + { title: 'Author', field: 'author' }, + { + title: 'View', + field: 'viewUrl', + render({ viewUrl }) { + return ( + + {viewUrl} + + ); + }, + }, + { + title: 'Edit', + field: 'editUrl', + render({ editUrl }) { + return ( + + {editUrl} + + ); + }, + }, +]; + +export const TodoList = () => { + const { entity } = useEntity(); + const todoApi = useApi(todoApiRef); + + const { value, loading, error } = useAsync( + async () => todoApi.listTodos({ entity }), + [todoApi, entity], + ); + + if (loading) { + return ; + } else if (error) { + return {error.message}; + } + + return ( +
+ ); +}; diff --git a/plugins/todo/src/components/ExampleComponent/index.ts b/plugins/todo/src/components/TodoList/index.ts similarity index 91% rename from plugins/todo/src/components/ExampleComponent/index.ts rename to plugins/todo/src/components/TodoList/index.ts index 337d24d5c5..416a52fd7a 100644 --- a/plugins/todo/src/components/ExampleComponent/index.ts +++ b/plugins/todo/src/components/TodoList/index.ts @@ -13,4 +13,4 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -export { ExampleComponent } from './ExampleComponent'; +export { TodoList } from './TodoList'; diff --git a/plugins/todo/src/plugin.ts b/plugins/todo/src/plugin.ts index 4a202390be..7cc439940c 100644 --- a/plugins/todo/src/plugin.ts +++ b/plugins/todo/src/plugin.ts @@ -40,8 +40,7 @@ export const todoPlugin = createPlugin({ export const EntityTodoContent = todoPlugin.provide( createRoutableExtension({ - component: () => - import('./components/ExampleComponent').then(m => m.ExampleComponent), + component: () => import('./components/TodoList').then(m => m.TodoList), mountPoint: rootRouteRef, }), );