#49 test: cubrir páginas admin de provenance y NavBar
This commit is contained in:
80
tests/unit/components/NavBar.spec.js
Normal file
80
tests/unit/components/NavBar.spec.js
Normal file
@@ -0,0 +1,80 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { flushPromises, mount } from '@vue/test-utils'
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import NavBar from '@/components/NavBar.vue'
|
||||
import vuetify from '@/plugins/vuetify'
|
||||
|
||||
import AuthService from '@/services/auth'
|
||||
|
||||
vi.mock('@/services/auth', () => ({
|
||||
default: {
|
||||
isAuthenticated: vi.fn(),
|
||||
logout: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
function mountNavBar (api) {
|
||||
const pinia = createPinia()
|
||||
setActivePinia(pinia)
|
||||
const wrapper = mount(NavBar, {
|
||||
global: {
|
||||
plugins: [pinia, vuetify],
|
||||
provide: { api },
|
||||
mocks: { $router: { push: vi.fn() } },
|
||||
stubs: {
|
||||
VAppBar: { template: '<div><slot /></div>' },
|
||||
VNavigationDrawer: { template: '<div><slot /></div>' },
|
||||
},
|
||||
},
|
||||
})
|
||||
return { wrapper }
|
||||
}
|
||||
|
||||
describe('NavBar', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
localStorage.clear()
|
||||
})
|
||||
|
||||
it('sin sesión muestra el botón de login y no el menú de administración', async () => {
|
||||
AuthService.isAuthenticated.mockReturnValue(false)
|
||||
const { wrapper } = mountNavBar({ getCurrentUser: vi.fn() })
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.text()).toContain('Login')
|
||||
expect(wrapper.text()).not.toContain('Administracion')
|
||||
})
|
||||
|
||||
it('el administrador ve los ítems de administración de provenance', async () => {
|
||||
AuthService.isAuthenticated.mockReturnValue(true)
|
||||
const api = {
|
||||
getCurrentUser: vi.fn().mockResolvedValue({ username: 'admin', role: 'administrator' }),
|
||||
}
|
||||
const { wrapper } = mountNavBar(api)
|
||||
await flushPromises()
|
||||
|
||||
expect(api.getCurrentUser).toHaveBeenCalled()
|
||||
expect(wrapper.text()).toContain('Administracion')
|
||||
|
||||
const adminItem = wrapper.findAll('.v-list-item')
|
||||
.find(item => item.text().includes('Administracion'))
|
||||
await adminItem.trigger('click')
|
||||
|
||||
expect(wrapper.text()).toContain('Organizaciones')
|
||||
expect(wrapper.text()).toContain('Proveedores')
|
||||
expect(wrapper.text()).toContain('Geografía')
|
||||
})
|
||||
|
||||
it('el usuario público no ve el menú de administración y no ve "Comprar"', async () => {
|
||||
AuthService.isAuthenticated.mockReturnValue(true)
|
||||
const api = {
|
||||
getCurrentUser: vi.fn().mockResolvedValue({ username: 'cliente', role: 'publico' }),
|
||||
}
|
||||
const { wrapper } = mountNavBar(api)
|
||||
await flushPromises()
|
||||
|
||||
expect(wrapper.text()).not.toContain('Administracion')
|
||||
expect(wrapper.text()).not.toContain('Comprar')
|
||||
expect(wrapper.text()).toContain('Ver Catálogo')
|
||||
})
|
||||
})
|
||||
83
tests/unit/pages/admin/admin-pages.spec.js
Normal file
83
tests/unit/pages/admin/admin-pages.spec.js
Normal file
@@ -0,0 +1,83 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { flushPromises, mount } from '@vue/test-utils'
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import OrganizationsManagement from '@/components/provenance/admin/OrganizationsManagement.vue'
|
||||
import SuppliersManagement from '@/components/provenance/admin/SuppliersManagement.vue'
|
||||
import GeographyManagement from '@/components/provenance/admin/GeographyManagement.vue'
|
||||
import OrganizationsPage from '@/pages/admin/organizations.vue'
|
||||
import SuppliersPage from '@/pages/admin/suppliers.vue'
|
||||
import GeographyPage from '@/pages/admin/geography.vue'
|
||||
import vuetify from '@/plugins/vuetify'
|
||||
|
||||
function mockApi () {
|
||||
return {
|
||||
getOrganizations: vi.fn().mockResolvedValue([]),
|
||||
getSuppliers: vi.fn().mockResolvedValue([]),
|
||||
getMunicipalities: vi.fn().mockResolvedValue([]),
|
||||
getCountries: vi.fn().mockResolvedValue([]),
|
||||
getDepartments: vi.fn().mockResolvedValue([]),
|
||||
}
|
||||
}
|
||||
|
||||
function mountAdmin (page, api, role) {
|
||||
const pinia = createPinia()
|
||||
setActivePinia(pinia)
|
||||
const authStore = useAuthStore()
|
||||
authStore.setUser({ role, username: 'admin' })
|
||||
const wrapper = mount(page, {
|
||||
global: { plugins: [pinia, vuetify], provide: { api } },
|
||||
})
|
||||
return { wrapper }
|
||||
}
|
||||
|
||||
async function flushAll (wrapper) {
|
||||
await flushPromises()
|
||||
await wrapper.vm.$nextTick()
|
||||
}
|
||||
|
||||
describe('página admin/organizations', () => {
|
||||
it('renderiza OrganizationsManagement cuando el usuario es administrador', async () => {
|
||||
const { wrapper } = mountAdmin(OrganizationsPage, mockApi(), 'administrator')
|
||||
await flushAll(wrapper)
|
||||
|
||||
expect(wrapper.findComponent(OrganizationsManagement).exists()).toBe(true)
|
||||
})
|
||||
|
||||
it('no renderiza el componente cuando el usuario no es administrador', async () => {
|
||||
const { wrapper } = mountAdmin(OrganizationsPage, mockApi(), 'publico')
|
||||
|
||||
expect(wrapper.findComponent(OrganizationsManagement).exists()).toBe(false)
|
||||
expect(wrapper.text()).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('página admin/suppliers', () => {
|
||||
it('renderiza SuppliersManagement cuando el usuario es administrador', async () => {
|
||||
const { wrapper } = mountAdmin(SuppliersPage, mockApi(), 'administrator')
|
||||
await flushAll(wrapper)
|
||||
|
||||
expect(wrapper.findComponent(SuppliersManagement).exists()).toBe(true)
|
||||
})
|
||||
|
||||
it('no renderiza el componente cuando el usuario no es administrador', async () => {
|
||||
const { wrapper } = mountAdmin(SuppliersPage, mockApi(), 'publico')
|
||||
|
||||
expect(wrapper.findComponent(SuppliersManagement).exists()).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('página admin/geography', () => {
|
||||
it('renderiza GeographyManagement cuando el usuario es administrador', async () => {
|
||||
const { wrapper } = mountAdmin(GeographyPage, mockApi(), 'administrator')
|
||||
await flushAll(wrapper)
|
||||
|
||||
expect(wrapper.findComponent(GeographyManagement).exists()).toBe(true)
|
||||
})
|
||||
|
||||
it('no renderiza el componente cuando el usuario no es administrador', async () => {
|
||||
const { wrapper } = mountAdmin(GeographyPage, mockApi(), 'publico')
|
||||
|
||||
expect(wrapper.findComponent(GeographyManagement).exists()).toBe(false)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user