|
| 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 | +}) |
0 commit comments