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

Fix/badge #9

Merged
merged 6 commits into from
Jan 26, 2024
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
6 changes: 6 additions & 0 deletions app/revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { revalidatePath } from "next/cache";

export async function GET() {
revalidatePath("/", "layout")
return Response.json({revalidated: true})
}
32 changes: 23 additions & 9 deletions components/badge/badge.test.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
import { render } from '@/lib/test-utils'
import { render, screen } from '@/lib/test-utils'

import { Badge, BadgeType } from './badge'
import { Badge } from './badge'

describe('<Badge />', () => {
const today = new Date().toString()

const today = "2024-01-26T15:00:00.000Z"
const yesterday = "2024-01-25T15:00:00.000Z"
const lastWeek = "2024-01-18T15:00:00.000Z"

test('renders the new badge', () => {
const { getByText } = render(<Badge createdAt={today} updatedAt={today} />)
const { getByText } = render(<Badge createdAt={today} updatedAt={yesterday} />)
expect(getByText('new')).toBeInTheDocument()
})
test('renders the updated badge', () => {
const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)

test('renders the updated badge', () => {
const { getByText } = render(
<Badge
createdAt={yesterday.toString()}
updatedAt={yesterday.toString()}
createdAt={yesterday}
updatedAt={yesterday}
/>
)
expect(getByText('updated')).toBeInTheDocument()
})

test('renders the updated badge', () => {
render(
<Badge
createdAt={yesterday}
updatedAt={lastWeek}
/>
)

expect(screen.queryByTestId('badge')).not.toBeInTheDocument();
})

test('renders nothing', () => {
const { queryByText } = render(<Badge createdAt={''} updatedAt={''} />)
expect(queryByText('new')).not.toBeInTheDocument()
Expand Down
2 changes: 1 addition & 1 deletion components/sidebar-menu/sidebar-menu-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const SidebarMenuItem = ({ items }: SidebarMenuItemProps) => {
<li key={index}>
<LinkWrapper href={item.url} className='flex justify-between flex-wrap'>
{item.name}{' '}
<Badge createdAt={item.createdAt} updatedAt={item.createdAt} />
<Badge createdAt={item.createdAt} updatedAt={item.updatedAt} />
</LinkWrapper>
</li>
)
Expand Down
14 changes: 7 additions & 7 deletions lib/date-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isThisWeekButNotToday, isToday } from './date-utils'
import { isUpdated, isToday } from './date-utils'

describe('date-utils', () => {
describe('isToday', () => {
Expand All @@ -15,24 +15,24 @@ describe('date-utils', () => {
})
})

describe('isThisWeekButNotToday', () => {
describe('isUpdated', () => {
test('returns true if date is this week but not today', () => {
const today = new Date()
const tomorrow = new Date(today)
tomorrow.setDate(today.getDate() + 1)
expect(isThisWeekButNotToday(tomorrow)).toBe(true)
expect(isUpdated(tomorrow)).toBe(true)
})

test('returns false if date is not this week', () => {
const today = new Date()
const nextWeek = new Date(today)
nextWeek.setDate(today.getDate() + 7)
expect(isThisWeekButNotToday(nextWeek)).toBe(false)
expect(isUpdated(nextWeek)).toBe(false)
})

test('returns false if date is today', () => {
const today = new Date()
expect(isThisWeekButNotToday(today)).toBe(false)
test('returns true if updated date is today', () => {
const updatedToday = new Date()
expect(isUpdated(updatedToday)).toBe(true)
})
})
})
12 changes: 6 additions & 6 deletions lib/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export const isToday = (date: Date) => {
)
}

export const isThisWeekButNotToday = (date: Date) => {
export const isUpdated = (updatedAt: Date) => {
if(isToday(updatedAt)) return true

const today = new Date()

const isSameWeek =
date.getDate() - today.getDate() >= -today.getDay() &&
date.getDate() - today.getDate() <= 6 - today.getDay()

const isNotToday = !isToday(date)
updatedAt.getDate() - today.getDate() >= -today.getDay() &&
updatedAt.getDate() - today.getDate() <= 6 - today.getDay()

return isSameWeek && isNotToday
return isSameWeek
}
4 changes: 2 additions & 2 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { twMerge } from 'tailwind-merge'

import { BadgeType } from '@/components/badge/badge'

import { isThisWeekButNotToday, isToday } from './date-utils'
import { isUpdated, isToday } from './date-utils'

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
Expand All @@ -14,7 +14,7 @@ export const getBadgeType = (createdAt: Date, updatedAt: Date) => {
return BadgeType.new
}

if (isThisWeekButNotToday(updatedAt)) {
if (isUpdated(updatedAt)) {
return BadgeType.updated
}

Expand Down
Loading