-
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.
* Installs vitest * Unit tests footer * Uses beforeEach * Unit tests kw-links * Tests p-l component * Unit tests rel posts component * Shared var for mock values * Unit tests HN link * Manually runs cleanup to remove DOM elements * Formatting * Global teardown after each test * Shared helper * Formatting * Uses svelte API to update inputs * Imports extender before every setup file
- Loading branch information
Showing
14 changed files
with
2,289 additions
and
44 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
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 |
---|---|---|
|
@@ -10,4 +10,4 @@ | |
a { | ||
font-size: 24px; | ||
} | ||
</style> | ||
</style> |
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,14 @@ | ||
import { describe, test, expect, beforeEach } from 'vitest'; | ||
import { render, screen } from '@testing-library/svelte'; | ||
import Footer from './footer.svelte'; | ||
|
||
describe('Footer', () => { | ||
beforeEach(() => { | ||
render(Footer); | ||
}); | ||
|
||
test('displays copyright for current year', () => { | ||
const currentYear = new Date().getFullYear(); | ||
expect(screen.getByTestId('copyright').textContent).toContain(currentYear); | ||
}); | ||
}); |
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 { describe, test, expect } from 'vitest'; | ||
import { render, screen } from '@testing-library/svelte'; | ||
import KeywordLinks from './keyword-links.svelte'; | ||
|
||
describe('KeywordLinks', () => { | ||
test('displays keywords', () => { | ||
render(KeywordLinks, { keywords: 'foo, bar' }); | ||
|
||
expect(screen.getByText('#foo')).toBeInTheDocument(); | ||
expect(screen.getByText('#bar')).toBeInTheDocument(); | ||
}); | ||
|
||
test('hides component entirely with no keywords', () => { | ||
const { container } = render(KeywordLinks, { keywords: undefined }); | ||
|
||
expect(container.querySelector('div')?.childElementCount).toBe(0); | ||
}); | ||
}); |
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,13 @@ | ||
import { describe, test, expect } from 'vitest'; | ||
import { render, screen } from '@testing-library/svelte'; | ||
import PaginationLinks from './pagination-links.svelte'; | ||
|
||
describe('PaginationLinks', () => { | ||
test('displays pagination links', () => { | ||
render(PaginationLinks, { currentPage: 1, totalPageCount: 3 }); | ||
|
||
expect(screen.getByText('1')).toBeInTheDocument(); | ||
expect(screen.getByText('2')).toBeInTheDocument(); | ||
expect(screen.getByText('3')).toBeInTheDocument(); | ||
}); | ||
}); |
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,86 @@ | ||
import { describe, test, expect, beforeEach, vi } from 'vitest'; | ||
import { render, screen } from '@testing-library/svelte'; | ||
import RelatedPosts from './related-posts.svelte'; | ||
import type { PostLink } from '$lib/types'; | ||
import { whenStable } from '../../tests/helpers'; | ||
|
||
const { mockRelatedPosts } = vi.hoisted(() => ({ | ||
mockRelatedPosts: [ | ||
{ | ||
metadata: { | ||
layout: 'post', | ||
title: 'foo title', | ||
description: 'foo', | ||
date: 'foo', | ||
image: 'foo', | ||
caption: 'foo', | ||
keywords: 'foo' | ||
}, | ||
postPath: 'foo' | ||
} | ||
] as PostLink[] | ||
})); | ||
|
||
describe('RelatedPosts', () => { | ||
beforeEach(() => { | ||
vi.mock('$lib/post-handlers'); | ||
}); | ||
|
||
test('renders nothing with no related posts', () => { | ||
vi.mock('$lib/post-handlers', () => { | ||
return { | ||
getRelatedPosts: vi.fn().mockResolvedValue([]) | ||
}; | ||
}); | ||
const { container } = render(RelatedPosts, { | ||
parentPostTitle: 'foo', | ||
parentPostHnLink: undefined, | ||
parentPostKeywords: undefined | ||
}); | ||
|
||
expect(container.querySelector('div')?.childElementCount).toBe(0); | ||
}); | ||
|
||
test('renders related posts', async () => { | ||
vi.mock('$lib/post-handlers', () => { | ||
return { | ||
getRelatedPosts: vi.fn().mockResolvedValue(mockRelatedPosts) | ||
}; | ||
}); | ||
render(RelatedPosts, { | ||
parentPostTitle: 'foo', | ||
parentPostHnLink: undefined, | ||
parentPostKeywords: undefined | ||
}); | ||
await whenStable(); | ||
|
||
expect(screen.getByText('foo title')).toBeInTheDocument(); | ||
}); | ||
|
||
test('render the HN discussion link', async () => { | ||
vi.mock('$lib/post-handlers', () => { | ||
return { | ||
getRelatedPosts: vi.fn().mockResolvedValue(mockRelatedPosts) | ||
}; | ||
}); | ||
const env = render(RelatedPosts, { | ||
parentPostTitle: 'foo', | ||
parentPostHnLink: undefined, | ||
parentPostKeywords: undefined | ||
}); | ||
await whenStable(); | ||
|
||
expect( | ||
screen.queryByText('Hacker News discussion') | ||
).not.toBeInTheDocument(); | ||
|
||
env.component.$set({ | ||
parentPostTitle: 'foo', | ||
parentPostHnLink: 'foo', | ||
parentPostKeywords: undefined | ||
}); | ||
await whenStable(); | ||
|
||
expect(screen.getByText('Hacker News discussion')).toBeInTheDocument(); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -22,4 +22,4 @@ | |
</li> | ||
{/each} | ||
</ul> | ||
</div> | ||
</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,5 @@ | ||
import { act } from '@testing-library/svelte'; | ||
|
||
export async function whenStable() { | ||
await act(async () => await new Promise(resolve => setTimeout(resolve, 1))); | ||
} |
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,7 @@ | ||
import { cleanup } from '@testing-library/svelte'; | ||
import { afterEach } from 'vitest'; | ||
import 'vitest-dom/extend-expect'; | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
}); |
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