Skip to content

Commit 0413c21

Browse files
committed
add book link. Redirect to amazon store
1 parent c1e97d1 commit 0413c21

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

src/components/Book/Book.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { IBook } from "../../interface/IBook";
22
import './Book.css';
3+
import BookLink from "./BookLink";
34

45
const Book = ({
56
listBook,
@@ -18,15 +19,15 @@ const Book = ({
1819
) : (
1920
listBook.map((book: IBook) => (
2021
<div className="book-list">
21-
<a href="/#" target="_blank" className="book-link">
22+
<BookLink bookLink={book.buy_links} >
2223
<img
2324
src={book.book_image}
2425
width="213"
2526
height="271"
2627
alt={book.title}
2728
className="book-img"
2829
/>
29-
</a>
30+
</BookLink>
3031
<div>
3132
<a href="/#" target="_blank" className="book-link" data-testid="title-book">
3233
{book.title}

src/components/Book/BookLink.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react'
2+
3+
const BookLink = (props: any) => {
4+
5+
return (
6+
<>
7+
{props.bookLink.length !== 0 ? (
8+
<a href={props.bookLink[0].url} target="_blank" className="book-link">
9+
{props.children}
10+
</a>
11+
) : (
12+
props.children
13+
)}
14+
</>
15+
);
16+
}
17+
18+
export default BookLink

src/components/Book/__test__/Book.test.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ import { IBook } from '../../../interface/IBook';
33
import Book from '../Book';
44

55
describe("Book list", () => {
6+
7+
let fakeBooks: IBook[];
68

7-
test('renders "no book" when the are no book', () => {
8-
const {getByText} = render(<Book listBook={[]} category={"Fiction"}/>)
9-
expect(getByText(/No book/i)).toBeInTheDocument();
10-
})
11-
12-
test('should render list of books', () => {
13-
const fakeBooks: IBook[] = [
9+
beforeEach(() => {
10+
fakeBooks = [
1411
{
1512
author: "Clive Cussler and Justin Scott",
1613
contributor: "by Clive Cussler and Justin Scott",
@@ -19,11 +16,20 @@ describe("Book list", () => {
1916
primary_isbn10: "0698406427",
2017
publisher: "Putnam",
2118
rank: 1,
19+
buy_links: [{name: "Amazon", url: "www.example.com"}],
2220
book_image: "https://storage.googleapis.com/du-prd/books/images/9781501110375.jpg",
2321
title: "THE GANGSTER",
2422
weeks_on_list: "34"
2523
}
2624
];
25+
});
26+
27+
test('renders "no book" when the are no book', () => {
28+
const {getByText} = render(<Book listBook={[]} category={"Fiction"}/>)
29+
expect(getByText(/No book/i)).toBeInTheDocument();
30+
})
31+
32+
test('should render list of books', () => {
2733

2834
const {getAllByTestId} = render(<Book listBook={fakeBooks} category={"Fiction"}/>);
2935
const titleBook = getAllByTestId('title-book').map(title => title.textContent);
@@ -32,5 +38,8 @@ describe("Book list", () => {
3238
expect(titleBook).toEqual(fakeTitleBook);
3339
});
3440

35-
41+
test('should redirect to amazon link', () => {
42+
render(<Book listBook={fakeBooks} category={"Fiction"} />)
43+
expect(document.querySelector("a")?.getAttribute("href")).toBe("www.example.com")
44+
})
3645
});

src/interface/IBook.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ export interface IBook {
33
contributor?: string;
44
description?: string;
55
book_image?: string;
6-
buy_links?: string[];
6+
buy_links?: BuyLinks[];
77
primary_isbn13?: string;
88
primary_isbn10?: string;
99
publisher?: string;
1010
rank?: number;
1111
title?: string;
1212
weeks_on_list?: string;
1313
}
14+
15+
interface BuyLinks {
16+
name: string;
17+
url: string
18+
}

0 commit comments

Comments
 (0)