Skip to content

Commit

Permalink
1310-feusuaris-add-mentor-section-for-admin-users (#1326)
Browse files Browse the repository at this point in the history
Update UserRole export/imports
  • Loading branch information
marmorrei authored Jun 6, 2024
1 parent ddaefc4 commit be94229
Show file tree
Hide file tree
Showing 28 changed files with 427 additions and 267 deletions.
7 changes: 7 additions & 0 deletions web/usuaris/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file.

## [0.22.0] - 2024-06-04

### Added

- Added Mentors page
- Added Dashboard component

## [0.21.1] - 2024-05-22

### Changed
Expand Down
20 changes: 17 additions & 3 deletions web/usuaris/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion web/usuaris/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "web_usuaris",
"private": true,
"version": "0.21.1",
"version": "0.22.0",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down Expand Up @@ -35,6 +35,7 @@
"@tanstack/eslint-plugin-query": "4.29.4",
"@testing-library/jest-dom": "6.2.1",
"@testing-library/react": "14.1.2",
"@testing-library/user-event": "^14.5.2",
"@types/jsdom": "21.1.1",
"@types/qs": "6.9.7",
"@types/react": "^18.2.43",
Expand Down
3 changes: 1 addition & 2 deletions web/usuaris/src/__mocks__/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { HttpResponse, http } from 'msw'
import { urls } from '../constants'
import { UserStatus } from '../types'
import { UserRole } from '../types/types'
import { UserRole, UserStatus } from '../types'

export const handlers = [
http.get(urls.getUsers, ({ request }) => {
Expand Down
3 changes: 2 additions & 1 deletion web/usuaris/src/__tests__/context/AuthProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { ReactElement } from 'react'
import { renderHook } from '@testing-library/react'
import { act } from 'react-dom/test-utils'
import { AuthProvider, useAuth, type TUser } from '../../context/AuthProvider'
import { UserRole } from '../../types'

const mockUser: TUser = {
dni: '12345678A',
email: 'user@example.cat',
role: 'ADMIN',
role: UserRole.ADMIN,
}
describe('AuthProvider', () => {
it('should render children', () => {
Expand Down
3 changes: 1 addition & 2 deletions web/usuaris/src/__tests__/hooks/useGetUsers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { useGetUsers } from '../../hooks/useGetUsers'
import { renderHook, waitFor } from '../test-utils'
import { queryClient } from '../setup'
import { type TFilters } from '../../types'
import { UserStatus } from '../../types'
import { UserRole } from '../../types/types'
import { UserRole, UserStatus } from '../../types'

describe('useGetUsers', () => {
it('fetches users successfully', async () => {
Expand Down
9 changes: 7 additions & 2 deletions web/usuaris/src/__tests__/main.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { createGlobalStyle } from 'styled-components'
import { font } from '@itacademy/ui'
import { QueryClientProvider } from '@tanstack/react-query'
import { queryClient } from './setup'
import { ErrorPage, Home } from '../pages'
import { ErrorPage, Home, Mentors } from '../pages'
import { Layout } from '../components/layout'
import { AuthProvider } from '../context/AuthProvider'
import { paths } from '../constants'

describe('main', () => {
it('renders correctly', () => {
Expand All @@ -18,10 +19,14 @@ describe('main', () => {

const routes = [
{
path: '/',
path: paths.home,
element: <Home />,
errorElement: <ErrorPage />,
},
{
path: paths.mentors,
element: <Mentors />,
},
]

const router = createMemoryRouter(routes, {
Expand Down
2 changes: 1 addition & 1 deletion web/usuaris/src/__tests__/molecules/RoleFilter.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { vi } from 'vitest'
import { fireEvent, render, screen, waitFor } from '../test-utils'
import { RoleFilter } from '../../components/molecules/RoleFilter'
import { UserRole } from '../../types/types'
import { UserRole } from '../../types'
import { roles } from '../../constants'

const mockHandleClick = vi.fn()
Expand Down
19 changes: 19 additions & 0 deletions web/usuaris/src/__tests__/organisms/Dashboard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { screen } from '@testing-library/react'
import { render } from '../test-utils'
import { Dashboard } from '../../components/organisms'

describe('Dashboard', () => {
it('renders correctly', () => {
render(<Dashboard />)

const mainDiv = screen.getByRole('main')
const sideMenuElement = screen.getByText(/Mentors/i)
const filtersWidgetElement = screen.getByAltText(/Calendar/i)
const actionsDropdown = screen.getByTestId('actions-dropdown')

expect(mainDiv).toBeInTheDocument()
expect(sideMenuElement).toBeInTheDocument()
expect(filtersWidgetElement).toBeInTheDocument()
expect(actionsDropdown).toBeInTheDocument()
})
})
75 changes: 48 additions & 27 deletions web/usuaris/src/__tests__/organisms/SideMenu.test.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,69 @@
import { fireEvent, screen } from '@testing-library/react'
import { fireEvent, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { colors } from '@itacademy/ui'
import { describe, beforeEach, it, expect } from 'vitest'
import { render } from '../test-utils'
import { SideMenu } from '../../components/organisms'
import { paths } from '../../constants'

describe('SideMenu', () => {
beforeEach(() => {
render(<SideMenu />)
})

it('renders correctly', () => {
it('renders correctly', async () => {
expect(screen.getByAltText('IT Academy')).toBeInTheDocument()
expect(screen.getByText('Usuaris')).toBeInTheDocument()
expect(screen.getByText('Mentors')).toBeInTheDocument()
expect(screen.getByText('Connector')).toBeInTheDocument()
expect(screen.getByText('Configuració')).toBeInTheDocument()
})
const usersItem = screen.getByTestId('test-title-Usuarios')
const mentorsItem = screen.getByTestId('test-title-Mentores')
const connectorItem = screen.getByTestId('test-title-Connector')
const settingsItem = screen.getByTestId('test-title-Configuración')

it('navigates to the correct page when a menu item is clicked', () => {
const firstMenuItem = screen.getByText('Usuaris')
fireEvent.click(firstMenuItem)
expect(window.location.pathname).toEqual('/')
})
expect(usersItem).toBeInTheDocument()
expect(mentorsItem).toBeInTheDocument()
expect(connectorItem).toBeInTheDocument()
expect(settingsItem).toBeInTheDocument()

it('changes style on hover over a category', () => {
const category = screen.getByText('Usuaris')
fireEvent.mouseOver(category)
expect(category).toHaveStyle(`color: ${colors.primary}`)
expect(screen.getAllByTestId('test-icon-person').length).toBe(2)
expect(screen.getByTestId('test-icon-bolt')).toBeInTheDocument()
expect(screen.getByTestId('test-icon-settings')).toBeInTheDocument()

waitFor(() => {
expect(usersItem).toHaveStyle(`color: ${colors.black.black3}`)
expect(mentorsItem).toHaveStyle(`color: ${colors.gray.gray3}`)
expect(connectorItem).toHaveStyle(`color: ${colors.gray.gray3}`)
expect(settingsItem).toHaveStyle(`color: ${colors.gray.gray3}`)
})
})

it('displays the correct icons for each menu item', () => {
const personIcons = screen.getAllByTestId('person')
expect(personIcons.length).toBe(2)
it('navigates to the correct page when a menu item is clicked and changes styles', async () => {
const mentorsMenuItemLink = screen.getByTestId('test-link-Mentores')
const mentorsTitle = screen.getByTestId('test-title-Mentores')

expect(screen.getByTestId(`test-path-${paths.home}`)).toBeInTheDocument()
waitFor(() => {
expect(mentorsTitle).toHaveStyle(`color: ${colors.gray.gray3}`)
})

const boltIcon = screen.getByTestId('bolt')
expect(boltIcon).toBeInTheDocument()
await userEvent.click(mentorsMenuItemLink)

expect(screen.getByTestId(`test-path-${paths.mentors}`)).toBeInTheDocument()
waitFor(() => {
expect(mentorsTitle).toHaveStyle(`color: ${colors.black.black3}`)
})
})

it('changes style on hover over a category', () => {
const activeCategory = screen.getByText('Usuaris')
fireEvent.mouseOver(activeCategory)
expect(activeCategory).toHaveStyle(`color: ${colors.black.black3}`)

const settingsIcon = screen.getByTestId('settings')
expect(settingsIcon).toBeInTheDocument()
const inactiveCategory = screen.getByText('Mentors')
fireEvent.mouseOver(inactiveCategory)
expect(inactiveCategory).toHaveStyle(`color: ${colors.primary}`)
})

it('ensures menu items are focusable for accessibility', () => {
const firstMenuItem = screen.getByText('Usuaris')
firstMenuItem.focus()
expect(firstMenuItem).toHaveFocus()
const usersMenuItem = screen.getByTestId('test-link-Usuarios')
usersMenuItem.focus()
expect(usersMenuItem).toHaveFocus()
})
})
3 changes: 1 addition & 2 deletions web/usuaris/src/__tests__/organisms/UsersTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { fireEvent, render, screen, waitFor } from '../test-utils'
import { UsersTable } from '../../components/organisms'
import { errorHandlers } from '../../__mocks__/handlers'
import { server } from '../../__mocks__/server'
import { UserStatus } from '../../types'
import { UserRole } from '../../types/types'
import { UserRole, UserStatus } from '../../types'

const defaultProps = {
filtersSelected: {},
Expand Down
15 changes: 4 additions & 11 deletions web/usuaris/src/__tests__/pages/Home.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { screen } from '@testing-library/react'
import { render } from '../test-utils'
import { Home } from '../../pages'
import { TAuthContext, useAuth } from '../../context/AuthProvider'
import { UserRole } from '../../types'

describe('Home page', () => {
beforeEach(() => {
Expand All @@ -24,25 +25,17 @@ describe('Home page', () => {
expect(screen.getByTestId('login-test')).toBeInTheDocument()
})

it('renders dashboard if user is logged in', () => {
it('renders Dashboard if user is logged in', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
dni: '12345678A',
email: 'user@example.cat',
role: 'ADMIN',
role: UserRole.ADMIN,
},
} as TAuthContext)

render(<Home />)

const mainDiv = screen.getByRole('main')
const sideMenuElement = screen.getByText(/Mentors/i)
const filtersWidgetElement = screen.getByAltText(/Calendar/i)
const actionsDropdown = screen.getByTestId('actions-dropdown')

expect(mainDiv).toBeInTheDocument()
expect(sideMenuElement).toBeInTheDocument()
expect(filtersWidgetElement).toBeInTheDocument()
expect(actionsDropdown).toBeInTheDocument()
expect(screen.getByTestId('dashboard-test')).toBeInTheDocument()
})
})
32 changes: 32 additions & 0 deletions web/usuaris/src/__tests__/pages/Mentors.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { screen } from '@testing-library/react'
import { render } from '../test-utils'
import { Mentors } from '../../pages'
import { TAuthContext, useAuth } from '../../context/AuthProvider'
import { UserRole } from '../../types'
import { paths } from '../../constants'

describe('Mentors page', () => {
beforeEach(() => {
vi.mock('../../context/AuthProvider', async () => {
const actual = await vi.importActual('../../context/AuthProvider')
return {
...actual,
useAuth: vi.fn(),
}
})
})

it('renders correctly Mentors page if Admin user is logged in', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
dni: '12345678A',
email: 'user@example.cat',
role: UserRole.ADMIN,
},
} as TAuthContext)

render(<Mentors />, { initialEntries: [paths.mentors] })

expect(screen.getByTestId('test-mentors-page')).toBeInTheDocument()
})
})
Loading

0 comments on commit be94229

Please sign in to comment.