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 test #15

Merged
merged 1 commit into from
Jan 27, 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
65 changes: 29 additions & 36 deletions components/badge/badge.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,33 @@ import { render, screen } from '@/lib/test-utils'
import { Badge } from './badge'

describe('<Badge />', () => {

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={yesterday} />)
expect(getByText('new')).toBeInTheDocument()
})

test('renders the updated badge', () => {
const { getByText } = render(
<Badge
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()
expect(queryByText('updated')).not.toBeInTheDocument()
})
const today = new Date().toISOString()
const yesterday = new Date(Date.now() - 86400000).toISOString()
const lastWeek = new Date(Date.now() - 604800000).toISOString()

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

test('renders the updated badge', () => {
const { getByText } = render(
<Badge 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()
expect(queryByText('updated')).not.toBeInTheDocument()
})
})
67 changes: 32 additions & 35 deletions lib/date-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
import { isUpdated, isToday } from './date-utils'
import { isToday, isUpdated } from './date-utils'

describe('date-utils', () => {
describe('isToday', () => {
test('returns true if date is today', () => {
const today = new Date()
expect(isToday(today)).toBe(true)
})

test('returns false if date is not today', () => {
const today = new Date()
const yesterday = new Date(today)
yesterday.setDate(today.getDate() - 1)
expect(isToday(yesterday)).toBe(false)
})
})

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(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(isUpdated(nextWeek)).toBe(false)
})

test('returns true if updated date is today', () => {
const updatedToday = new Date()
expect(isUpdated(updatedToday)).toBe(true)
})
})
const today = new Date()

const yesterday = new Date(today)
yesterday.setDate(today.getDate() - 1)

const lastWeek = new Date(today)
lastWeek.setDate(today.getDate() - 7)

describe('isToday', () => {
test('returns true if createdAt is today', () => {
expect(isToday(today)).toBe(true)
})

test('returns false if createdAt is not today', () => {
expect(isToday(yesterday)).toBe(false)
})
})

describe('isUpdated', () => {
test('returns true if updatedDate is today', () => {
expect(isUpdated(today)).toBe(true)
})

test('returns true if updatedDate is this week', () => {
expect(isUpdated(yesterday)).toBe(true)
})

test('returns false if date is not this week', () => {
expect(isUpdated(lastWeek)).toBe(false)
})
})
})
Loading