Skip to content

Commit e866d0d

Browse files
committed
test: add unit tests for BaseButton and ExternalLink components
1 parent 7b54251 commit e866d0d

File tree

6 files changed

+80
-11
lines changed

6 files changed

+80
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"lint:fix": "eslint --fix ./app",
1616
"test:ui": "vitest --ui",
1717
"test:unit": "vitest --run --reporter verbose",
18-
"test:ci": "vitest --run --reporter verbose --exclude \"**/app.test.ts\"",
18+
"test:ci": "vitest --run --reporter verbose",
1919
"test:coverage": "vitest run --coverage",
2020
"postinstall": "nuxt prepare",
2121
"release": "npm run lint && npm run build && changelogen --patch --release && git push --follow-tags"

test/basic/vitest_base.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,17 @@ it('jSON', () => {
1919
expect(output).eq('{"foo":"hello","bar":"world"}')
2020
assert.deepEqual(JSON.parse(output), input, 'matches original')
2121
})
22+
23+
it('array operations', () => {
24+
const arr = [1, 2, 3, 4]
25+
expect(arr.length).toBe(4)
26+
expect(arr.includes(2)).toBe(true)
27+
expect(arr.map(x => x * 2)).toEqual([2, 4, 6, 8])
28+
})
29+
30+
it('string manipulation', () => {
31+
const str = 'NuxtPrimeVue'
32+
expect(str.toLowerCase()).toBe('nuxtprimevue')
33+
expect(str.replace('Prime', '-')).toBe('Nuxt-Vue')
34+
expect(str.split('V')).toEqual(['NuxtPrime', 'ue'])
35+
})

test/e2e/app.test.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/nuxt/BaseButton.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { mount } from '@vue/test-utils'
2+
import { describe, expect, it } from 'vitest'
3+
import BaseButton from '../../app/components/BaseButton.vue'
4+
5+
describe('baseButton', () => {
6+
it('renders slot content', () => {
7+
const wrapper = mount(BaseButton, {
8+
slots: {
9+
default: 'Click me',
10+
},
11+
})
12+
expect(wrapper.text()).toContain('Click me')
13+
})
14+
15+
it('applies color and size classes', () => {
16+
const wrapper = mount(BaseButton, {
17+
props: {
18+
color: 'green',
19+
size: 'big',
20+
},
21+
slots: {
22+
default: 'Button',
23+
},
24+
})
25+
expect(wrapper.classes()).toContain('btn-color-green')
26+
expect(wrapper.classes()).toContain('btn-size-big')
27+
})
28+
29+
it('emits click event', async () => {
30+
const wrapper = mount(BaseButton, {
31+
slots: {
32+
default: 'Click',
33+
},
34+
})
35+
await wrapper.trigger('click')
36+
expect(wrapper.emitted('click')).toBeTruthy()
37+
})
38+
})

test/nuxt/ExternalLink.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { mount } from '@vue/test-utils'
2+
import { describe, expect, it } from 'vitest'
3+
import ExternalLink from '../../app/components/ExternalLink.vue'
4+
5+
describe('externalLink', () => {
6+
it('renders link with correct text and href', () => {
7+
const wrapper = mount(ExternalLink, {
8+
props: {
9+
text: 'Nuxt',
10+
href: 'https://nuxt.com',
11+
},
12+
})
13+
const a = wrapper.find('a')
14+
expect(a.exists()).toBe(true)
15+
expect(a.text()).toBe('Nuxt')
16+
expect(a.attributes('href')).toBe('https://nuxt.com')
17+
expect(a.attributes('target')).toBe('_blank')
18+
})
19+
20+
it('renders default props', () => {
21+
const wrapper = mount(ExternalLink)
22+
const a = wrapper.find('a')
23+
expect(a.text()).toBe('')
24+
expect(a.attributes('href')).toBe('#')
25+
})
26+
})

vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { defineVitestConfig } from '@nuxt/test-utils/config'
22

33
export default defineVitestConfig({
44
test: {
5+
environment: 'nuxt',
56
},
67
// any custom Vitest config you require
78
})

0 commit comments

Comments
 (0)