From 907d021c2f0d146a167c1e90a0ded1fbb2c5c7da Mon Sep 17 00:00:00 2001 From: Raul Kele Date: Wed, 9 Nov 2022 13:26:47 +0200 Subject: [PATCH] feat: Added filter for tags tab Signed-off-by: Raul Kele --- src/__tests__/RepoPage/Tags.test.js | 39 ++++++++++++++++-- src/components/Tags.jsx | 63 +++++++++++++++++------------ 2 files changed, 73 insertions(+), 29 deletions(-) diff --git a/src/__tests__/RepoPage/Tags.test.js b/src/__tests__/RepoPage/Tags.test.js index 67e10ad6..4469a80c 100644 --- a/src/__tests__/RepoPage/Tags.test.js +++ b/src/__tests__/RepoPage/Tags.test.js @@ -1,4 +1,5 @@ import { fireEvent, waitFor, render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; import Tags from 'components/Tags'; import React from 'react'; @@ -19,16 +20,38 @@ const mockedTagsData = [ Os: 'linux', Arch: 'amd64' } + }, + { + Digest: 'sha256:adca4815c494becc1bf053af0c4640b2d81ab1a779e6d649e1b8b92a75f1d559', + Tag: 'bullseye', + LastUpdated: '2022-07-19T18:06:18.818788283Z', + Vendor: 'test1', + Size: '569130088', + Platform: { + Os: 'linux', + Arch: 'amd64' + } + }, + { + Digest: 'sha256:adca4815c494becc1bf053af0c4640b2d81ab1a779e6d649e1b8b92a75f1d559', + Tag: '1.5.2', + LastUpdated: '2022-07-19T18:06:18.818788283Z', + Vendor: 'test1', + Size: '569130088', + Platform: { + Os: 'linux', + Arch: 'amd64' + } } ]; describe('Tags component', () => { it('should open and close details dropdown for tags', async () => { render(); - const openBtn = screen.getByText(/digest/i); - fireEvent.click(openBtn); + const openBtn = screen.getAllByText(/digest/i); + fireEvent.click(openBtn[0]); expect(screen.getByText(/OS\/ARCH/i)).toBeInTheDocument(); - fireEvent.click(openBtn); + fireEvent.click(openBtn[0]); await waitFor(() => expect(screen.queryByText(/OS\/ARCH/i)).not.toBeInTheDocument()); }); @@ -40,4 +63,14 @@ describe('Tags component', () => { expect(mockedUsedNavigate).toHaveBeenCalledWith('tag/latest'); }); }); + + it('should filter tag list based on user input', async () => { + render(); + const tagFilterInput = await screen.findByPlaceholderText(/Filter tags/i); + expect(await screen.findByText(/latest/i)).toBeInTheDocument(); + expect(await screen.findByText(/bullseye/i)).toBeInTheDocument(); + userEvent.type(tagFilterInput, 'bull'); + await waitFor(() => expect(screen.queryByText(/latest/i)).not.toBeInTheDocument()); + expect(await screen.findByText(/bullseye/i)).toBeInTheDocument(); + }); }); diff --git a/src/components/Tags.jsx b/src/components/Tags.jsx index d3611313..5e7e1144 100644 --- a/src/components/Tags.jsx +++ b/src/components/Tags.jsx @@ -1,9 +1,9 @@ // react global -import * as React from 'react'; +import React, { useState } from 'react'; // components import Typography from '@mui/material/Typography'; -import { Card, CardContent, Divider } from '@mui/material'; +import { Card, CardContent, Divider, Stack, Input } from '@mui/material'; import { makeStyles } from '@mui/styles'; import TagCard from './TagCard'; @@ -50,37 +50,48 @@ const useStyles = makeStyles(() => ({ export default function Tags(props) { const classes = useStyles(); const { tags } = props; + const [tagsFilter, setTagsFilter] = useState(''); const renderTags = (tags) => { - const cmp = + return ( tags && - tags.map((tag) => { - return ( - - ); - }); - return cmp; + tags + .filter((t) => t.Tag?.includes(tagsFilter)) + .map((tag) => { + return ( + + ); + }) + ); + }; + + const handleTagsFilterChange = (e) => { + const { value } = e.target; + setTagsFilter(value); }; return ( - - Tags History - + + + Tags History + + +