Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

K-allagbe/issue16-greyed-out-query #61

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "finesse-frontend",
"version": "0.2.0",
"version": "0.2.2",
"private": true,
"dependencies": {
"@saithodev/ts-appversion": "^2.2.0",
Expand Down
30 changes: 24 additions & 6 deletions src/__snapshots__/App.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ exports[`App Component Tests debug button is not visible when debug mode is fals
class="input-wrapper"
>
<svg
data-testid="search-icon"
fill="currentColor"
height="1em"
id="fa-arrow-right"
Expand All @@ -80,11 +81,13 @@ exports[`App Component Tests debug button is not visible when debug mode is fals
/>
</svg>
<input
data-testid="search-input"
placeholder="Type to search..."
value=""
/>
<button
class="button"
data-testid="submit-button"
type="submit"
>
<svg
Expand Down Expand Up @@ -121,7 +124,7 @@ exports[`App Component Tests debug button is not visible when debug mode is fals
<div
class="versionText"
>
v0.2.0
v0.2.2
</div>
</div>
</div>
Expand Down Expand Up @@ -208,6 +211,7 @@ exports[`App Component Tests debug button is visible when debug mode is true 1`]
class="input-wrapper"
>
<svg
data-testid="search-icon"
fill="currentColor"
height="1em"
id="fa-arrow-right"
Expand All @@ -222,11 +226,13 @@ exports[`App Component Tests debug button is visible when debug mode is true 1`]
/>
</svg>
<input
data-testid="search-input"
placeholder="Type to search..."
value=""
/>
<button
class="button"
data-testid="submit-button"
type="submit"
>
<svg
Expand Down Expand Up @@ -263,7 +269,7 @@ exports[`App Component Tests debug button is visible when debug mode is true 1`]
<div
class="versionText"
>
v0.2.0
v0.2.2
</div>
</div>
</div>
Expand Down Expand Up @@ -350,6 +356,7 @@ exports[`App Component Tests debug panel toggles visibility when debug button is
class="input-wrapper"
>
<svg
data-testid="search-icon"
fill="currentColor"
height="1em"
id="fa-arrow-right"
Expand All @@ -364,11 +371,13 @@ exports[`App Component Tests debug panel toggles visibility when debug button is
/>
</svg>
<input
data-testid="search-input"
placeholder="Type to search..."
value=""
/>
<button
class="button"
data-testid="submit-button"
type="submit"
>
<svg
Expand Down Expand Up @@ -405,7 +414,7 @@ exports[`App Component Tests debug panel toggles visibility when debug button is
<div
class="versionText"
>
v0.2.0
v0.2.2
</div>
</div>
</div>
Expand Down Expand Up @@ -544,6 +553,7 @@ exports[`App Component Tests debug panel toggles visibility when debug button is
class="input-wrapper"
>
<svg
data-testid="search-icon"
fill="currentColor"
height="1em"
id="fa-arrow-right"
Expand All @@ -558,11 +568,13 @@ exports[`App Component Tests debug panel toggles visibility when debug button is
/>
</svg>
<input
data-testid="search-input"
placeholder="Type to search..."
value=""
/>
<button
class="button"
data-testid="submit-button"
type="submit"
>
<svg
Expand Down Expand Up @@ -599,7 +611,7 @@ exports[`App Component Tests debug panel toggles visibility when debug button is
<div
class="versionText"
>
v0.2.0
v0.2.2
</div>
</div>
</div>
Expand Down Expand Up @@ -686,6 +698,7 @@ exports[`App Component Tests debug panel toggles visibility when debug button is
class="input-wrapper"
>
<svg
data-testid="search-icon"
fill="currentColor"
height="1em"
id="fa-arrow-right"
Expand All @@ -700,11 +713,13 @@ exports[`App Component Tests debug panel toggles visibility when debug button is
/>
</svg>
<input
data-testid="search-input"
placeholder="Type to search..."
value=""
/>
<button
class="button"
data-testid="submit-button"
type="submit"
>
<svg
Expand Down Expand Up @@ -741,7 +756,7 @@ exports[`App Component Tests debug panel toggles visibility when debug button is
<div
class="versionText"
>
v0.2.0
v0.2.2
</div>
</div>
</div>
Expand Down Expand Up @@ -828,6 +843,7 @@ exports[`App Component Tests version matches the environment version 1`] = `
class="input-wrapper"
>
<svg
data-testid="search-icon"
fill="currentColor"
height="1em"
id="fa-arrow-right"
Expand All @@ -842,11 +858,13 @@ exports[`App Component Tests version matches the environment version 1`] = `
/>
</svg>
<input
data-testid="search-input"
placeholder="Type to search..."
value=""
/>
<button
class="button"
data-testid="submit-button"
type="submit"
>
<svg
Expand Down Expand Up @@ -883,7 +901,7 @@ exports[`App Component Tests version matches the environment version 1`] = `
<div
class="versionText"
>
v0.2.0
v0.2.2
</div>
</div>
</div>
Expand Down
8 changes: 4 additions & 4 deletions src/_versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export interface TsAppVersion {
gitTag?: string;
};
export const versions: TsAppVersion = {
version: '0.2.0',
version: '0.2.2',
name: 'finesse-frontend',
versionDate: '2023-12-04T21:25:19.734Z',
gitCommitHash: '1b203ac',
versionLong: '0.2.0-1b203ac',
versionDate: '2023-12-14T10:10:26.754Z',
gitCommitHash: '93e9f5d',
versionLong: '0.2.2-93e9f5d',
};
export default versions;
69 changes: 69 additions & 0 deletions src/components/searchbar/SearchBar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { fireEvent, render, screen } from "@testing-library/react";
import { useNavigate } from "react-router-dom";
import { useData } from "../../contexts/DataContext";
import { SearchBar } from "./SearchBar";

jest.mock("../../contexts/DataContext", () => ({
useData: jest.fn(),
}));

jest.mock("react-router-dom", () => ({
useNavigate: jest.fn(),
}));

describe("SearchBar", () => {
const mockSetSearchTerm = jest.fn();
const mockNavigate = jest.fn();

beforeEach(() => {
(useData as jest.Mock).mockReturnValue({
searchTerm: "initial",
setSearchTerm: mockSetSearchTerm,
});
(useNavigate as jest.Mock).mockReturnValue(mockNavigate);
jest.spyOn(console, "warn").mockImplementation(() => {});
});

afterEach(() => {
jest.clearAllMocks();
});

it("renders correctly", () => {
const { asFragment } = render(<SearchBar />);
expect(asFragment()).toMatchSnapshot();
});

it("updates input value on change", () => {
const { asFragment } = render(<SearchBar />);
const input = screen.getByTestId("search-input");
fireEvent.change(input, { target: { value: "test query" } });
expect(asFragment()).toMatchSnapshot();
});

it("trims search query and navigates on valid form submit", () => {
const { asFragment } = render(<SearchBar />);
const input = screen.getByTestId("search-input");
const button = screen.getByTestId("submit-button");
fireEvent.change(input, { target: { value: " test query " } });
fireEvent.click(button);
expect(asFragment()).toMatchSnapshot();
});

it("does not navigate or set search term with empty trimmed query", () => {
const { asFragment } = render(<SearchBar />);
const input = screen.getByTestId("search-input");
const button = screen.getByTestId("submit-button");
fireEvent.change(input, { target: { value: " " } });
fireEvent.click(button);
expect(asFragment()).toMatchSnapshot();
});

it("keeps the search query in the input after submission", () => {
const { asFragment } = render(<SearchBar />);
const input = screen.getByTestId("search-input");
const button = screen.getByTestId("submit-button");
fireEvent.change(input, { target: { value: "test query" } });
fireEvent.click(button);
expect(asFragment()).toMatchSnapshot();
});
});
50 changes: 20 additions & 30 deletions src/components/searchbar/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,47 @@
import * as React from "react";
import { useEffect, useState } from "react";
import { useState } from "react";
import { FaArrowRight, FaSearch } from "react-icons/fa";
import { useNavigate } from "react-router-dom";
import { useData } from "../../contexts/DataContext";
import { SearchSource } from "../../types";
import "./SearchBar.css";

interface SearchBarProps {
term?: string;
}
const SEARCH_PLACEHOLDER = "Type to search...";

export const SearchBar: React.FC<SearchBarProps> = ({ term }) => {
const { currentSearchSource, setSearchTerm } = useData();
const [searchQuery, setSearchQuery] = useState("");
export const SearchBar: React.FC = () => {
const { searchTerm, setSearchTerm } = useData();
const [searchQuery, setSearchQuery] = useState(searchTerm ?? "");
const navigate = useNavigate();
const [previousSearchQuery, setPreviousSearchQuery] = useState<
string | undefined
>("");

useEffect(() => {
if (typeof term === "undefined") {
setPreviousSearchQuery("Type to search...");
} else {
setPreviousSearchQuery(term);
}
}, [term]);

const search = (e: React.FormEvent): void => {
e.preventDefault();
navigate("/search");

// Ignore dispatch if useSimulatedData is true
// TODO: revisit
if (currentSearchSource === SearchSource.Simulated && searchQuery === "") {
console.log("Search dispatch ignored because useSimulatedData is true");
const trimmedSearchQuery = searchQuery.trim();
if (trimmedSearchQuery === "") {
console.warn("Empty string search not allowed.");
return;
}

setSearchTerm(searchQuery);
// TODO: do we want to navigate every time?
navigate("/search");
setSearchTerm(trimmedSearchQuery);
};

return (
<form className="form-wrapper">
<div className="input-wrapper">
<FaSearch id="fa-arrow-right" />
<FaSearch id="fa-arrow-right" data-testid="search-icon" />
<input
placeholder={previousSearchQuery}
placeholder={SEARCH_PLACEHOLDER}
value={searchQuery}
onChange={(e) => {
setSearchQuery(e.target.value);
}}
data-testid="search-input"
/>
<button className="button" type="submit" onClick={search}>
<button
className="button"
type="submit"
onClick={search}
data-testid="submit-button"
>
<FaArrowRight id="fa-arrow-right" style={{ color: "#05486c" }} />
</button>
</div>
Expand Down
Loading
Loading