-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #444 from lifeparticle/refactor-newsfeed
Refactor newsfeed
- Loading branch information
Showing
18 changed files
with
401 additions
and
455 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import xml2js from "xml2js"; | ||
export function parseXML(value) { | ||
return new Promise((resolve, reject) => { | ||
const parser = new xml2js.Parser({ | ||
explicitArray: false, | ||
ignoreAttrs: true, | ||
}); | ||
parser.parseString(value, (err, result) => { | ||
if (err) reject(err); | ||
else { | ||
const items = result.rss.channel.item; | ||
const list = items.map((item) => ({ | ||
title: item.title, | ||
pubDate: item.pubDate, | ||
url: item.link, | ||
image: extractImage(item.description), | ||
})); | ||
resolve(list); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
function extractImage(description) { | ||
const regex = /<img.*?src=["'](.*?)["']/; | ||
const match = regex.exec(description); | ||
return match ? match[1] : null; | ||
} |
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 @@ | ||
VITE_VERCEL_NEWS_FEED_URL=http://localhost:3000/rss?name= |
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 @@ | ||
VITE_VERCEL_NEWS_FEED_URL=https://binarytree-rssfeed-api.vercel.app/rss?name= |
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 @@ | ||
VITE_VERCEL_NEWS_FEED_URL=http://localhost:3000/rss?name= |
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
### [10.6.1] - 2024-03-29 | ||
|
||
- Update Newsfeed tests | ||
|
||
### [10.6.0] - 2024-01-24 | ||
|
||
- Added Information Page tests | ||
|
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 was deleted.
Oops, something went wrong.
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,102 @@ | ||
// component | ||
import Newsfeed from "pages/Newsfeed"; | ||
// dependencies | ||
import { vi } from "vitest"; | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
// hooks | ||
import useNewsFeed from "pages/Newsfeed/useNewsFeed"; | ||
// types | ||
import { ListSearchResultsProps } from "components/ComponentInjector/ListSearchResults/ListSearchResults"; | ||
import { TAB_ITEMS } from "pages/Newsfeed/constants"; | ||
|
||
vi.mock("pages/Newsfeed/useNewsFeed"); | ||
|
||
type Items = unknown[]; | ||
|
||
const mockListSearchResultsProps = vi.fn(); | ||
vi.mock("components/ComponentInjector", () => ({ | ||
ListSearchResults: (props: ListSearchResultsProps<Items>) => { | ||
mockListSearchResultsProps(props); | ||
return <div data-testid="list-search-results" />; | ||
}, | ||
})); | ||
|
||
const mockSetTab = vi.fn(); | ||
|
||
// Mock the useNewsFeed hook | ||
vi.mock("./useNewsFeed", () => ({ | ||
data: [], // Assume empty data for simplicity | ||
isLoading: false, | ||
isError: false, | ||
setTab: mockSetTab, | ||
})); | ||
|
||
const user = userEvent.setup(); | ||
|
||
describe("News component", () => { | ||
const mockSetTab = vi.fn(); | ||
const newsFeedData = ["some data"]; | ||
|
||
// Setup phase | ||
beforeEach(() => { | ||
vi.mocked(useNewsFeed).mockReturnValue({ | ||
data: newsFeedData, | ||
isLoading: false, | ||
isError: false, | ||
setTab: mockSetTab, | ||
}); | ||
|
||
render(<Newsfeed />); | ||
}); | ||
|
||
it("renders search results list", () => { | ||
// Assert phase for the search results list | ||
const listSearchResults = screen.getByTestId("list-search-results"); | ||
expect(listSearchResults).toBeInTheDocument(); | ||
}); | ||
|
||
it("calls mockListSearchResultsProps with correct arguments", () => { | ||
// Assert phase for mock function call | ||
expect(mockListSearchResultsProps).toHaveBeenCalledWith({ | ||
isError: false, | ||
isLoading: false, | ||
itemComponent: expect.any(Function), | ||
items: ["some data"], | ||
resourceName: "news", | ||
}); | ||
}); | ||
|
||
describe("Tabs functionality", () => { | ||
// Setup phase for tabs, run only if TAB_ITEMS is defined | ||
let tabs: HTMLElement[]; | ||
beforeEach(() => { | ||
tabs = screen.getAllByRole("tab"); | ||
}); | ||
|
||
it("renders the correct number of tabs", () => { | ||
// Assert phase for the number of tabs | ||
|
||
expect(tabs).toHaveLength(TAB_ITEMS.length); | ||
}); | ||
|
||
it("renders tabs with correct labels", () => { | ||
// Assert phase for tab labels | ||
TAB_ITEMS.forEach((tabItem, index) => { | ||
expect(tabs[index]).toHaveTextContent(tabItem.label); | ||
}); | ||
}); | ||
|
||
it("calls setTab with the new tab value on tab change", async () => { | ||
// This example simulates clicking the second tab | ||
const secondTab = TAB_ITEMS[1]; | ||
await user.click( | ||
screen.getByRole("tab", { name: secondTab.label }) | ||
); | ||
|
||
// Verify setTab was called with the value of the second tab | ||
expect(mockSetTab).toHaveBeenCalledWith(secondTab.key); | ||
mockSetTab.mockClear(); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.