generated from muratkeremozcan/react-cypress-ts-vite-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' into 4267-vacancy-info-card
- Loading branch information
Showing
10 changed files
with
269 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import SearchBar from './SearchBar'; | ||
|
||
describe('<SearchBar />', () => { | ||
it('should not render with text "Buscas mais recentes" when search some location', () => { | ||
cy.mount(<SearchBar />); | ||
|
||
cy.get('input[placeholder="Localização"]').type('test'); | ||
|
||
cy.contains('Buscar vagas').click(); | ||
|
||
cy.contains('Buscas mais recentes').should('not.exist'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { render, screen } from '@testing-library/react'; | ||
import { expect, jest } from '@jest/globals'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import SearchBar from './SearchBar'; | ||
|
||
describe('<SearchBar />', () => { | ||
let container: HTMLElement; | ||
|
||
beforeEach(() => { | ||
container = render(<SearchBar />).container; | ||
|
||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should render the component', () => { | ||
expect(container).toBeDefined(); | ||
}); | ||
|
||
it('should render with the text "O quê você procura?"', () => { | ||
(expect as any)(container).toHaveTextContent('O quê você procura?'); | ||
}); | ||
|
||
it('should render with the text "Onde?"', () => { | ||
(expect as any)(container).toHaveTextContent('Onde?'); | ||
}); | ||
|
||
it('should not render with the text "Buscas mais recentes:"', () => { | ||
(expect as any)(container).not.toHaveTextContent('Buscas mais recentes:'); | ||
}); | ||
|
||
it('should render with text "Buscas mais recentes:" when search some tech or position', async () => { | ||
const input = screen.getByPlaceholderText( | ||
'Cargo, tecnologia ou palavra-chave', | ||
); | ||
|
||
const button = screen.getByText('Buscar vagas'); | ||
|
||
await userEvent.type(input, 'test'); | ||
await userEvent.click(button); | ||
|
||
(expect as any)(container).toHaveTextContent('Buscas mais recentes:'); | ||
}); | ||
|
||
it('should render the recent searches when search some tech or position', async () => { | ||
const input = screen.getByPlaceholderText( | ||
'Cargo, tecnologia ou palavra-chave', | ||
); | ||
|
||
const button = screen.getByText('Buscar vagas'); | ||
|
||
await userEvent.type(input, 'test'); | ||
|
||
await userEvent.click(button); | ||
|
||
(expect as any)(input).toHaveValue('test'); | ||
}); | ||
|
||
it('should render the text in input when click in recent search', async () => { | ||
const input = screen.getByPlaceholderText( | ||
'Cargo, tecnologia ou palavra-chave', | ||
); | ||
|
||
const button = screen.getByText('Buscar vagas'); | ||
|
||
await userEvent.type(input, 'test'); | ||
await userEvent.click(button); | ||
await userEvent.clear(input); | ||
|
||
const recentSearch = screen.getByText('test'); | ||
|
||
await userEvent.click(recentSearch); | ||
|
||
(expect as any)(input).toHaveValue('test'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import Text from './../Text'; | ||
import InputWithIcon from './../InputWithIcon'; | ||
import Button from './../Button'; | ||
|
||
import useField from '@hooks/useField'; | ||
import useRecentSearches from '@hooks/useRecentSearches'; | ||
|
||
import * as S from './styles'; | ||
|
||
const SearchBar = () => { | ||
const [location] = useField('text', 'location'); | ||
const [positionOrTech, , setPositionOrTech] = useField('text', 'magnifier'); | ||
|
||
const [recentSearches, addRecentSearch] = useRecentSearches(); | ||
|
||
const handleOnSearch = () => { | ||
positionOrTech.value && addRecentSearch(positionOrTech.value); | ||
}; | ||
|
||
const handleRecentSearch = (value: string) => { | ||
setPositionOrTech(value); | ||
}; | ||
|
||
return ( | ||
<S.Container className="search-bar"> | ||
<div className="search-form"> | ||
<div className="form-group"> | ||
<label htmlFor="magnifier"> | ||
<Text label="O quê você procura?" fontWeight="600" /> | ||
</label> | ||
<InputWithIcon | ||
icon="magnifier" | ||
placeholder="Cargo, tecnologia ou palavra-chave" | ||
{...positionOrTech} | ||
/> | ||
</div> | ||
<div className="form-group"> | ||
<label htmlFor="location"> | ||
<Text label="Onde?" fontWeight="600" /> | ||
</label> | ||
<InputWithIcon | ||
icon="location" | ||
placeholder="Localização" | ||
{...location} | ||
/> | ||
</div> | ||
<Button | ||
label="Buscar vagas" | ||
onClick={handleOnSearch} | ||
backgroundColor="yellow" | ||
fontWeight="600" | ||
/> | ||
</div> | ||
{recentSearches && ( | ||
<div className="recent-search"> | ||
<Text label="Buscas mais recentes:" fontSize="small" /> | ||
<div className="search"> | ||
{recentSearches.map(search => ( | ||
<Button | ||
label={search} | ||
onClick={() => handleRecentSearch(search)} | ||
fontSize="small" | ||
fontColor="purple-light" | ||
backgroundColor="white" | ||
borderColor="purple-light" | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
)} | ||
</S.Container> | ||
); | ||
}; | ||
|
||
export default SearchBar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './SearchBar'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const Container = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
padding: 2rem; | ||
background-color: var(--white); | ||
box-shadow: 0px 1rem 2rem rgba(207.7, 207.7, 207.7, 0.2); | ||
border-radius: 1rem; | ||
margin: 0 8.5rem; | ||
.search-form { | ||
display: flex; | ||
align-items: end; | ||
gap: 1.5rem; | ||
margin-bottom: 1rem; | ||
.input-with-icon { | ||
width: 28rem; | ||
} | ||
.form-group { | ||
label { | ||
p { | ||
justify-content: flex-start; | ||
margin-bottom: 0.5rem; | ||
} | ||
} | ||
} | ||
button { | ||
padding: 0.75rem 1.4rem; | ||
} | ||
} | ||
.recent-search { | ||
display: flex; | ||
gap: 1rem; | ||
p { | ||
justify-content: flex-start; | ||
} | ||
button { | ||
padding: 0.625rem; | ||
} | ||
.search { | ||
display: flex; | ||
gap: 0.5rem; | ||
} | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
const useRecentSearches = () => { | ||
const [recentSearches, setRecentSearch] = useState<string[] | null>(null); | ||
|
||
const addRecentSearch = (value: string) => { | ||
const recentSearches = localStorage.getItem('recentSearches'); | ||
const recentSearchesArray = recentSearches | ||
? JSON.parse(recentSearches) | ||
: []; | ||
|
||
if (recentSearchesArray.includes(value)) return; | ||
|
||
setRecentSearch([value, ...recentSearchesArray]); | ||
recentSearchesArray.unshift(value); | ||
localStorage.setItem('recentSearches', JSON.stringify(recentSearchesArray)); | ||
}; | ||
|
||
useEffect(() => { | ||
const recentSearchesJSON = localStorage.getItem('recentSearches'); | ||
if (recentSearchesJSON) { | ||
setRecentSearch(JSON.parse(recentSearchesJSON)); | ||
} | ||
}, []); | ||
|
||
useEffect(() => { | ||
recentSearches && | ||
localStorage.setItem('recentSearches', JSON.stringify(recentSearches)); | ||
}, [recentSearches]); | ||
|
||
return [recentSearches, addRecentSearch] as const; | ||
}; | ||
|
||
export default useRecentSearches; |