-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: ✅ update components for svelte 5
add test files
- Loading branch information
Showing
7 changed files
with
231 additions
and
26 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
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 |
---|---|---|
@@ -1,11 +1,15 @@ | ||
<script lang="ts"> | ||
import { formatDistance } from 'date-fns' | ||
export let date | ||
interface Props { | ||
date: number | string | Date | ||
} | ||
let { date }: Props = $props() | ||
const distance = formatDistance( | ||
new Date(Date.now()), | ||
new Date(date) | ||
new Date(date), | ||
) | ||
</script> | ||
|
||
<span data-testid="date-distance">{distance}</span> | ||
<span data-testid="date-distance">{distance}</span> |
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
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,96 @@ | ||
import { | ||
cleanup, | ||
render, | ||
screen, | ||
} from '@testing-library/svelte/svelte5' | ||
import { format } from 'date-fns' | ||
import { afterEach, expect, test, vi } from 'vitest' | ||
import DateUpdated from './date-updated.svelte' | ||
|
||
vi.mock('date-fns', () => ({ | ||
format: vi.fn(), | ||
})) | ||
|
||
// Clean up after each test | ||
afterEach(() => { | ||
cleanup() | ||
vi.resetAllMocks() | ||
}) | ||
|
||
test('renders formatted date with default size', async () => { | ||
const mockDate = '2023-06-15' | ||
const mockFormattedDate = 'June 15th 2023' | ||
|
||
vi.mocked(format).mockReturnValue(mockFormattedDate) | ||
|
||
await vi.waitFor(async () => { | ||
render(DateUpdated, { date: mockDate }) | ||
}) | ||
|
||
const element = screen.getByText(mockFormattedDate) | ||
expect(element).toBeDefined() | ||
expect(element.classList.contains('text-base')).toBe(true) | ||
|
||
expect(format).toHaveBeenCalledWith( | ||
new Date(mockDate), | ||
'MMMM do yyyy', | ||
) | ||
}) | ||
|
||
test('renders formatted date with small size', async () => { | ||
const mockDate = '2023-06-15' | ||
const mockFormattedDate = 'June 15th 2023' | ||
|
||
vi.mocked(format).mockReturnValue(mockFormattedDate) | ||
|
||
await vi.waitFor(async () => { | ||
render(DateUpdated, { date: mockDate, small: 'true' }) | ||
}) | ||
|
||
const element = screen.getByText(mockFormattedDate) | ||
expect(element).toBeDefined() | ||
expect(element.classList.contains('text-xs')).toBe(true) | ||
|
||
expect(format).toHaveBeenCalledWith( | ||
new Date(mockDate), | ||
'MMMM do yyyy', | ||
) | ||
}) | ||
|
||
test('renders current date when date prop is null', async () => { | ||
const mockCurrentDate = new Date('2023-06-15') | ||
vi.setSystemTime(mockCurrentDate) | ||
|
||
const mockFormattedDate = 'June 15th 2023' | ||
vi.mocked(format).mockReturnValue(mockFormattedDate) | ||
|
||
await vi.waitFor(async () => { | ||
render(DateUpdated, { date: null }) | ||
}) | ||
|
||
const element = screen.getByText(mockFormattedDate) | ||
expect(element).toBeDefined() | ||
|
||
expect(format).toHaveBeenCalledWith(mockCurrentDate, 'MMMM do yyyy') | ||
|
||
vi.useRealTimers() | ||
}) | ||
|
||
test('renders current date when date prop is undefined', async () => { | ||
const mockCurrentDate = new Date('2023-06-15') | ||
vi.setSystemTime(mockCurrentDate) | ||
|
||
const mockFormattedDate = 'June 15th 2023' | ||
vi.mocked(format).mockReturnValue(mockFormattedDate) | ||
|
||
await vi.waitFor(async () => { | ||
render(DateUpdated, { date: undefined }) | ||
}) | ||
|
||
const element = screen.getByText(mockFormattedDate) | ||
expect(element).toBeDefined() | ||
|
||
expect(format).toHaveBeenCalledWith(mockCurrentDate, 'MMMM do yyyy') | ||
|
||
vi.useRealTimers() | ||
}) |
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 |
---|---|---|
@@ -1,20 +1,32 @@ | ||
<script lang="ts"> | ||
import type { Snippet } from 'svelte' | ||
import { slide } from 'svelte/transition' | ||
export let buttonText = '' | ||
export let isOpen = false | ||
export let styles = '' | ||
interface Props { | ||
buttonText?: string | ||
isOpen?: boolean | ||
styles?: string | ||
children?: Snippet | ||
} | ||
let { | ||
buttonText = '', | ||
isOpen = $bindable(false), | ||
styles = '', | ||
children, | ||
}: Props = $props() | ||
</script> | ||
|
||
<div> | ||
<button | ||
class="btn {styles} shadow-xl" | ||
on:click={() => (isOpen = !isOpen)} | ||
onclick={() => (isOpen = !isOpen)} | ||
data-testid="details-button" | ||
> | ||
{isOpen ? `Close` : buttonText} | ||
</button> | ||
{#if isOpen} | ||
<div transition:slide|global> | ||
<slot /> | ||
<div transition:slide|global data-testid="details-content"> | ||
{@render children?.()} | ||
</div> | ||
{/if} | ||
</div> |
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,87 @@ | ||
import { | ||
fireEvent, | ||
render, | ||
screen, | ||
} from '@testing-library/svelte/svelte5' | ||
import { describe, expect, test } from 'vitest' | ||
import Details from './details.svelte' | ||
|
||
describe('Details', () => { | ||
test('renders with default props', async () => { | ||
render(Details) | ||
const button = screen.getByTestId('details-button') | ||
expect(button.textContent?.trim()).toBe('') | ||
expect(screen.queryByTestId('details-content')).toBeNull() | ||
}) | ||
|
||
test('renders with custom button text', async () => { | ||
render(Details, { props: { buttonText: 'Show Details' } }) | ||
const button = screen.getByTestId('details-button') | ||
expect(button.textContent?.trim()).toBe('Show Details') | ||
}) | ||
|
||
test.skip('opens and closes details', async () => { | ||
const { container } = render(Details, { | ||
props: { | ||
buttonText: 'Show Details', | ||
// children: () => 'Test Content', | ||
}, | ||
}) | ||
|
||
console.log('=====================') | ||
console.log(container.innerHTML) | ||
console.log('=====================') | ||
|
||
const button = screen.getByTestId('details-button') | ||
expect(screen.queryByTestId('details-content')).toBeNull() | ||
|
||
// Open details | ||
await fireEvent.click(button) | ||
const content = screen.getByTestId('details-content') | ||
expect(content).toBeDefined() | ||
expect(content.textContent).toContain('Test Content') | ||
expect(button.textContent?.trim()).toBe('Close') | ||
|
||
// Close details | ||
await fireEvent.click(button) | ||
expect(screen.queryByTestId('details-content')).toBeNull() | ||
expect(button.textContent?.trim()).toBe('Show Details') | ||
}) | ||
|
||
test('applies custom styles', async () => { | ||
render(Details, { props: { styles: 'custom-class' } }) | ||
const button = screen.getByTestId('details-button') | ||
expect(button.classList.contains('custom-class')).toBe(true) | ||
}) | ||
|
||
test.skip('renders children when open', async () => { | ||
render(Details, { | ||
props: { | ||
buttonText: 'Show Details', | ||
// children: () => 'Test Content', | ||
}, | ||
}) | ||
|
||
const button = screen.getByTestId('details-button') | ||
await fireEvent.click(button) | ||
|
||
const content = screen.getByTestId('details-content') | ||
expect(content.textContent).toContain('Test Content') | ||
}) | ||
|
||
test.skip('respects initial isOpen prop', async () => { | ||
render(Details, { | ||
props: { | ||
buttonText: 'Show Details', | ||
isOpen: true, | ||
// children: () => 'Test Content', | ||
}, | ||
}) | ||
|
||
const content = screen.getByTestId('details-content') | ||
expect(content.textContent).toContain('Test Content') | ||
expect( | ||
screen.getByTestId('details-button').textContent?.trim(), | ||
).toBe('Close') | ||
}) | ||
}) |
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