Skip to content

Commit

Permalink
Configure vitest and add test for Header component
Browse files Browse the repository at this point in the history
  • Loading branch information
davidalves1 committed Mar 19, 2024
1 parent efcf055 commit 161f26c
Show file tree
Hide file tree
Showing 7 changed files with 1,285 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
VITE_FIREBASE_API_KEY=
VITE_FIREBASE_APP_ID=
VITE_FIREBASE_MESSAGING_SENDER_ID=
VITE_FIREBASE_PROJECT_ID=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ node_modules
dist
dist-ssr
*.local
coverage

# Editor directories and files
.vscode/*
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"type": "module",
"scripts": {
"dev": "vite",
"test": "vitest",
"test:watch": "vitest --watch",
"test:cov": "vitest --coverage.enabled=true",
"coverage": "vitest run --coverage",
"build": "vue-tsc && vite build",
"preview": "vite preview"
},
Expand All @@ -17,11 +21,15 @@
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.0.4",
"@vitest/coverage-istanbul": "^1.4.0",
"@vue/test-utils": "^2.4.5",
"autoprefixer": "^10.4.17",
"jsdom": "^24.0.0",
"postcss": "^8.4.35",
"tailwindcss": "^3.4.1",
"typescript": "^5.2.2",
"vite": "^5.1.4",
"vitest": "^1.4.0",
"vue-tsc": "^1.8.27"
}
}
33 changes: 33 additions & 0 deletions src/components/Header.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { shallowMount } from '@vue/test-utils';
import Header from './Header.vue';
import * as logoutService from '../services/logout';


describe('<Header />', () => {
const render = () => shallowMount(Header);
const doLogout = vi.spyOn(logoutService, 'doLogout');

beforeEach(() => {
vi.clearAllMocks()
});

it('should render component correctly', () => {
const wrapper = render();
const links = wrapper.findAll('router-link');

expect(wrapper.exists()).toBeTruthy();
expect(links.length).toBe(2);
expect(links.at(0)?.text()).toContain('Links4Later');
expect(links.at(1)?.text()).toContain('Sign out');
});

it('should do logout when sign out buttons is clicked', () => {
const wrapper = render();
const signOutButton = wrapper.findAll('router-link').at(1);

signOutButton?.trigger('click')

expect(doLogout).toHaveBeenCalledTimes(1);
})
})
2 changes: 1 addition & 1 deletion src/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const onLogout = () => {
<router-link to="/" class="text-2xl text-white">Links4Later</router-link>
<router-link @click="onLogout" to="/login" replace class="text-md flex items-center gap-2 text-zinc-600 py-2 px-3 rounded-lg bg-zinc-50 hover:bg-zinc-100 transition text-sm">
<LogOut :size="16" />
Sign out
Sign outs
</router-link>
</header>
</template>
19 changes: 19 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference types="vitest" />

import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'

export default defineConfig({
plugins: [
Vue(),
],
test: {
globals: false,
watch: false,
environment: 'jsdom',
coverage: {
provider: 'istanbul', // or 'v8'
reporter: ['clover', 'html'],
},
},
})
Loading

0 comments on commit 161f26c

Please sign in to comment.