-
Notifications
You must be signed in to change notification settings - Fork 0
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 #48 from Kyutech-C3/feat/Date-label-ui-#16
Feat/date label UI #16
- Loading branch information
Showing
7 changed files
with
151 additions
and
0 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,35 @@ | ||
import { DateLabel } from '.'; | ||
|
||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
const meta: Meta<typeof DateLabel> = { | ||
component: DateLabel, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof DateLabel>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
date: '2023-01-01T00:00:00', | ||
}, | ||
}; | ||
|
||
export const FullDate: Story = { | ||
args: { | ||
date: '2023-01-01T00:00:00', | ||
format: 'yyyy/MM/dd', | ||
}, | ||
}; | ||
|
||
export const Date: Story = { | ||
args: { | ||
date: '2023-01-01T00:00:00', | ||
format: 'MM/dd', | ||
}, | ||
}; |
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,38 @@ | ||
import { render } from '@testing-library/react'; | ||
|
||
import '@testing-library/jest-dom'; | ||
import { DateLabel } from '.'; | ||
|
||
describe('ui/DateLabelのテスト', () => { | ||
const testDate = '2023-01-01T00:00:00'; | ||
const isoString = new Date(testDate).toISOString(); | ||
it('renders the component with default date format', () => { | ||
const screen = render(<DateLabel date={testDate} />); | ||
const timeElement = screen.getByText(/2023\/01\/01 00:00:00/); | ||
expect(timeElement).toBeInTheDocument(); | ||
expect(timeElement).toHaveAttribute('dateTime', isoString); | ||
}); | ||
|
||
it('renders the component with custom date format', () => { | ||
const customFormat = 'yyyy-MM-dd'; | ||
const screen = render(<DateLabel date={testDate} format={customFormat} />); | ||
const timeElement = screen.getByText(/2023-01-01/); | ||
expect(timeElement).toBeInTheDocument(); | ||
expect(timeElement).toHaveAttribute('dateTime', isoString); | ||
}); | ||
|
||
it('applies custom class names', () => { | ||
const customClassName = 'custom-class'; | ||
const screen = render( | ||
<DateLabel date={testDate} className={customClassName} /> | ||
); | ||
const timeElement = screen.getByText(/2023\/01\/01 00:00:00/); | ||
expect(timeElement).toHaveClass(customClassName); | ||
}); | ||
|
||
it('renders the correct dateTime attribute', () => { | ||
const screen = render(<DateLabel date={testDate} />); | ||
const timeElement = screen.getByText(/2023\/01\/01 00:00:00/); | ||
expect(timeElement).toHaveAttribute('dateTime', isoString); | ||
}); | ||
}); |
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,18 @@ | ||
import { dateFormat } from '@/libs/dateFormat'; | ||
import { cn } from '@/libs/utils'; | ||
|
||
type Props = { | ||
date: string; | ||
format?: string; | ||
className?: string; | ||
}; | ||
|
||
export const DateLabel: React.FC<Props> = ({ | ||
date, | ||
format = 'yyyy/MM/dd HH:mm:ss', | ||
className, | ||
}) => ( | ||
<time className={cn(className)} dateTime={new Date(date).toISOString()}> | ||
{dateFormat(date, format)} | ||
</time> | ||
); |
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,35 @@ | ||
import { dateFormat } from '.'; | ||
|
||
describe('dateFormat function', () => { | ||
it('formats the date correctly according to the given format', () => { | ||
const date = '2023-01-01T00:00:00.000Z'; | ||
const formatString = 'yyyy-MM-dd'; | ||
const result = dateFormat(date, formatString); | ||
expect(result).toBe('2023-01-01'); | ||
}); | ||
|
||
it('throws an error with invalid date object', () => { | ||
const invalidDate = 'not a real date'; | ||
const formatString = 'yyyy-MM-dd'; | ||
expect(() => dateFormat(invalidDate, formatString)).toThrowError(); | ||
}); | ||
|
||
it('throws an error with invalid date format', () => { | ||
const date = '2023-01-01T00:00:00.000Z'; | ||
const invalidFormatString = ''; | ||
expect(() => dateFormat(date, invalidFormatString)).toThrowError(); | ||
}); | ||
|
||
it('handles incorrect format strings', () => { | ||
const date = '2023-01-01T00:00:00.000Z'; | ||
const formatString = 'not a format'; | ||
expect(() => dateFormat(date, formatString)).toThrowError(); | ||
}); | ||
|
||
it('formats the date correctly in a specific locale', () => { | ||
const date = '2023-01-01T00:00:00.000Z'; | ||
const formatString = 'PPP'; | ||
const result = dateFormat(date, formatString); | ||
expect(result).toBe('January 1st, 2023'); | ||
}); | ||
}); |
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,4 @@ | ||
import { format } from 'date-fns'; | ||
|
||
export const dateFormat = (dateString: string, _format: string): string => | ||
format(new Date(dateString), _format); |