Skip to content

Commit 849c318

Browse files
Add word of the day section (#158)
* Implement past-days function * Implement DailyWord component * Add DailyWord to index page and fix free spaces * Move DailyWord component to src/components folder * Change daily word header type to h1 * Change margin of containers in pages/index.tsx * Remove web3GlossaryHeading from pages/index.xts * Change styles of daily-word component
1 parent 3c4b149 commit 849c318

7 files changed

Lines changed: 112 additions & 8 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React from 'react';
2+
import {render, screen} from '@testing-library/react';
3+
import DailyWord from '../../../src/components/daily-word/daily-word';
4+
5+
jest.mock("react-i18next", () => ({
6+
useTranslation: () => ({ t: (key) => key }),
7+
}));
8+
9+
const words = ["eip", "ethereum", "whale", "web3", "web2"];
10+
11+
it('it shows a word of words in a link', () => {
12+
render(<DailyWord words={words}/>);
13+
14+
const wordLink = screen.getByRole('link');
15+
16+
expect(wordLink).toBeInTheDocument();
17+
expect(words.includes(wordLink.text)).toBe(true);
18+
})
19+
20+
it('it links the word to the correct page', () => {
21+
render(<DailyWord words={words}/>);
22+
23+
const wordLink = screen.getByRole('link');
24+
const pattern = `search\\?term=${wordLink.text}`;
25+
const regex = new RegExp(pattern);
26+
27+
expect(wordLink).toBeInTheDocument();
28+
expect(wordLink.href).toMatch(regex);
29+
})
30+
31+
it('it shows a unique word in each day that matches with it\'s link', () => {
32+
const shownWords = new Set();
33+
const element = render(<DailyWord words={words}/>);
34+
35+
for(let i=0;i<words.length;i++){
36+
const singleDay = 24 * 60 * 60 * 1000;
37+
const date = new Date((new Date).getTime() + singleDay * i);
38+
jest.useFakeTimers().setSystemTime(date);
39+
element.rerender(<DailyWord words={words}/>);
40+
41+
const wordLink = screen.getByRole('link');
42+
const word = wordLink.text;
43+
const pattern = `search\\?term=${word}`;
44+
const regex = new RegExp(pattern);
45+
46+
expect(wordLink).toBeInTheDocument();
47+
expect(wordLink.href).toMatch(regex);
48+
49+
expect(shownWords.has(word)).toBe(false);
50+
shownWords.add(word);
51+
jest.useRealTimers();
52+
}
53+
});
54+
55+
it('it shows the title correctly', () => {
56+
render(<DailyWord words={words}/>);
57+
58+
const title = screen.getByText("wordOfTheDay");
59+
60+
expect(title).toBeInTheDocument();
61+
})
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pastDays from '../../../src/components/daily-word/past-days';
2+
3+
it("it returns past days of 1970", () => {
4+
const singleDay = 24 * 60 * 60 * 1000;
5+
6+
const timeInJan1970 = singleDay * 5 + 30 * 60 * 1000;
7+
const dateInJan1970 = new Date(timeInJan1970);
8+
9+
const timeIn1970 = singleDay * 98 + 154 * 60 * 1000;
10+
const dateIn1970 = new Date(timeIn1970);
11+
12+
const timeAfter1970 = singleDay * 42195 + 23 * 60 * 60 * 1000 + 30 * 60 * 1000;
13+
const date = new Date(timeAfter1970);
14+
15+
expect(pastDays(dateInJan1970)).toBe(5);
16+
expect(pastDays(dateIn1970)).toBe(98);
17+
expect(pastDays(date)).toBe(42195);
18+
})

_tests_/pages/index.test.jsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ describe("Glosseta Landing Page", () => {
1515
render(<Home />);
1616

1717
const glossetaContainer = screen.getByTitle("glosseta-landing-page");
18-
const glossetaHeading = screen.getByText("web3GlossaryHeading");
1918
const glossetaDescription = screen.getByText("glossetaDescription");
2019
const logo = screen.getByTitle("glosseta-logo");
2120

@@ -24,8 +23,6 @@ describe("Glosseta Landing Page", () => {
2423
expect(logo).toHaveAttribute("src", "/glosseta.png");
2524
expect(logo).toHaveAttribute("alt", "Glosseta logo");
2625

27-
expect(glossetaHeading).toBeInTheDocument();
28-
2926
expect(glossetaDescription).toBeInTheDocument();
3027
});
3128
});

public/locales/en/common.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"enspectButtonTitle": "ENSpect",
77
"scrollToTheTopButton": "Click here to scroll to the top of the page",
88

9-
"web3GlossaryHeading": "Web3 Glossary",
109
"glossetaDescription": "Glosseta is an open-source glossary meant to help people explore and learn the terminology behind web3",
1110

1211
"searchInputGroupAriaLabel" : "Search Bar",
@@ -60,5 +59,7 @@
6059

6160
"footerTwitterA11yText" : "Opens the Glosseta Twitter page in a new window",
6261
"footerGitHubA11yText" : "Opens the GitHub project repo in a new window",
63-
"footerArweaveA11yText" : "Opens Arweave.org in a new window"
62+
"footerArweaveA11yText" : "Opens Arweave.org in a new window",
63+
64+
"wordOfTheDay" : "Word of the day"
6465
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import { Text, Link, VStack, Heading } from '@chakra-ui/react';
3+
import pastDays from "./past-days";
4+
import { useTranslation } from "next-i18next";
5+
6+
const DailyWord = ({words}:{words:any[]}): JSX.Element => {
7+
const { t } = useTranslation();
8+
9+
const indexOfWords = pastDays(new Date()) % words.length;
10+
return (
11+
<VStack fontSize={{ base: "lg", md: "xl" }}>
12+
<Heading as="h1" color="white" textAlign="center">{t("wordOfTheDay")}</Heading>
13+
<Link color="white" href={`/search?term=${words[indexOfWords]}`}>
14+
<Text casing="capitalize">{words[indexOfWords]}</Text>
15+
</Link>
16+
</VStack>
17+
)
18+
}
19+
20+
export default DailyWord;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const pastDays = (date: Date): number => {
2+
const day = 24 * 60 * 60 * 1000;
3+
return Math.floor(date.getTime()/day);
4+
}
5+
6+
export default pastDays;

src/pages/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import SearchBar from "./components/input/search-bar";
1414
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
1515
import { useTranslation } from "next-i18next";
1616
import { termFilter } from "../filter/termConfig";
17+
import DailyWord from "../components/daily-word/daily-word";
1718

1819
const Home: NextPage = () => {
1920
const { t } = useTranslation();
@@ -37,9 +38,9 @@ const Home: NextPage = () => {
3738
marginTop="-65px"
3839
>
3940
<VStack>
40-
<Heading as="h1" padding={1} color="white" textAlign="center">
41-
{t("web3GlossaryHeading")}
42-
</Heading>
41+
<HStack marginBottom="60px">
42+
<DailyWord words={termFilter}/>
43+
</HStack>
4344
<HStack spacing={3}>
4445
<SearchBar
4546
baseWidth={"80vw"}

0 commit comments

Comments
 (0)