Skip to content

Commit

Permalink
1410-Eliminate-avatarId-in-tests-that-use-useAuth-mock (#1416)
Browse files Browse the repository at this point in the history
* Fixed issue with avatarId in tests using useAuth mock

* Bump version to 0.20.6  and update CHANGELOG

* Resolve version conflict in CHANGELOG.md and package.json

* update version and changelog
  • Loading branch information
Fanur1991 authored Jul 25, 2024
1 parent 672f537 commit 6115f55
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 31 deletions.
7 changes: 6 additions & 1 deletion web/wiki/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

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

## [0.22.1] - 2024-07-25

## Changed

- Eliminated avatarId in tests that use useAuth mock

## [0.22.0] - 2024-07-24

### Deleted

- Deleted AccountAdmin component and test
- Deleted UsersManager component and test


## [0.21.0] - 2024-06-25

### Added
Expand Down
4 changes: 2 additions & 2 deletions web/wiki/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "web_wiki",
"private": true,
"version": "0.22.0",
"version": "0.22.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down Expand Up @@ -61,4 +61,4 @@
"vite": "5.0.13",
"vitest": "1.2.2"
}
}
}
1 change: 0 additions & 1 deletion web/wiki/src/__tests__/molecules/FavoritesIcon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ beforeEach(() => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'TestName',
avatarId: 'TestAvatar',
role: 'REGISTERED',
},
} as TAuthContext)
Expand Down
1 change: 0 additions & 1 deletion web/wiki/src/__tests__/molecules/UserButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ describe('UserButton', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'User',
avatarId: 'Avatar',
},
} as TAuthContext)

Expand Down
6 changes: 0 additions & 6 deletions web/wiki/src/__tests__/organisms/CardResource.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ describe('CardResource component', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Test author name',
avatarId: 'profileAvatar.jpg',
},
} as TAuthContext)

Expand All @@ -111,7 +110,6 @@ describe('CardResource component', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Hola',
avatarId: 'Adios',
},
} as TAuthContext)

Expand Down Expand Up @@ -151,7 +149,6 @@ describe('CardResource component', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Test author name',
avatarId: 'profileAvatar.jpg',
},
} as TAuthContext)

Expand Down Expand Up @@ -182,7 +179,6 @@ describe('CardResource component', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Test author name',
avatarId: 'profileAvatar.jpg',
},
} as TAuthContext)

Expand Down Expand Up @@ -215,7 +211,6 @@ describe('CardResource component', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Test author name',
avatarId: 'profileAvatar.jpg',
},
} as TAuthContext)

Expand Down Expand Up @@ -251,7 +246,6 @@ describe('CardResource component', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Test author name',
avatarId: 'profileAvatar.jpg',
},
} as TAuthContext)

Expand Down
1 change: 0 additions & 1 deletion web/wiki/src/__tests__/organisms/MyFavoritesList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ beforeEach(() => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'TestName',
avatarId: 'TestAvatar',
},
} as TAuthContext)
vi.mock('react-router-dom', async () => {
Expand Down
2 changes: 0 additions & 2 deletions web/wiki/src/__tests__/organisms/MyResources.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ beforeEach(() => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Hola',
avatarId: 'Adios',
},
} as TAuthContext)
vi.mock('react-router-dom', async () => {
Expand Down Expand Up @@ -96,7 +95,6 @@ describe('MyResources component', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Hola',
avatarId: 'Adios',
},
} as TAuthContext)

Expand Down
2 changes: 0 additions & 2 deletions web/wiki/src/__tests__/organisms/Navbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ beforeEach(() => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'TestName',
avatarId: 'TestAvatar',
role: 'REGISTERED',
},
} as TAuthContext)
Expand Down Expand Up @@ -185,7 +184,6 @@ describe('Navbar', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'TestName',
avatarId: 'TestAvatar',
role: 'MENTOR',
},
} as TAuthContext)
Expand Down
4 changes: 0 additions & 4 deletions web/wiki/src/__tests__/organisms/SettingsManager.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const mockUsers = [
email: 'user1@example.com',
dni: '12345678',
name: 'User One',
avatarId: 'testAvatar.jpg',
itineraryId: 'react',
status: 'ACTIVE',
role: 'REGISTERED',
Expand Down Expand Up @@ -41,7 +40,6 @@ beforeEach(() => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'AdminName',
avatarId: 'AdminAvatar',
role: 'ADMIN',
},
} as TAuthContext)
Expand Down Expand Up @@ -84,7 +82,6 @@ describe('SettingsManager component', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'MentorName',
avatarId: 'MentorAvatar',
role: 'MENTOR',
},
} as TAuthContext)
Expand All @@ -105,7 +102,6 @@ describe('User Permissions', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'MentorName',
avatarId: 'MentorAvatar',
role: 'MENTOR',
},
} as TAuthContext)
Expand Down
2 changes: 0 additions & 2 deletions web/wiki/src/__tests__/organisms/TopicsManagerBoard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ import { server } from '../../__mocks__/server'

const userMentor = {
name: 'Name',
avatarId: 'Avatar',
role: 'MENTOR',
}

const userStudent = {
name: 'Name',
avatarId: 'Avatar',
role: 'REGISTERED',
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ beforeEach(() => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Hola',
avatarId: 'Adios',
},
} as TAuthContext)
})
Expand Down
85 changes: 85 additions & 0 deletions web/wiki/src/__tests__/organisms/UsersManager.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen, waitFor, fireEvent } from '../test-utils'
import { TAuthContext, useAuth } from '../../context/AuthProvider'
import { UsersManager } from '../../components/organisms/UsersManager'

const mockUsers = [
{
id: '2',
email: 'user1@example.com',
dni: '12345678',
name: 'User Two',
status: 'ACTIVE',
role: 'user',
createdAt: '2023-01-01T00:00:00Z',
updatedAt: '2023-01-01T00:00:00Z',
},
]

vi.mock('/api/v1/users', () => mockUsers)
const queryClient = new QueryClient()

const renderWithQueryClient = (component: React.ReactNode) =>
render(
<QueryClientProvider client={queryClient}>{component}</QueryClientProvider>
)

beforeEach(() => {
vi.mock('../../context/AuthProvider', async () => {
const actual: Record<number, unknown> = await vi.importActual(
'../../context/AuthProvider'
)
return {
...actual,
useAuth: vi.fn(),
}
})
})

afterEach(() => {
vi.resetAllMocks()
})

describe('UsersManager component', () => {
it('does not render for mentor roles', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'MentorName',
role: 'MENTOR',
},
} as TAuthContext)
render(<UsersManager />)

expect(screen.queryByText('Users Manager')).not.toBeInTheDocument()
expect(screen.queryByText('AccountAdmin')).not.toBeInTheDocument()
})

it('renders correctly for admin roles and allows editing', async () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'AdminName',
role: 'ADMIN',
},
} as TAuthContext)
queryClient.setQueryData(['users'], mockUsers)
renderWithQueryClient(<UsersManager />)

expect(screen.getByText("Llista d'usuaris")).toBeInTheDocument()
expect(screen.queryByText('Error fetching users')).toBeNull()

const user2 = screen.getByText('User Two')
expect(user2).toBeInTheDocument()

const searchInput = screen.getByPlaceholderText('Introdueix el DNI')
fireEvent.change(searchInput, { target: { value: '12345678' } })

const statusButton = screen.getByTestId('status-desactivar')
fireEvent.click(statusButton)

await waitFor(() =>
expect(screen.getByText('Desactivar')).toBeInTheDocument()
)
})
})

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ beforeEach(() => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'TestName',
avatarId: 'TestAvatar',
},
} as TAuthContext)
})
Expand All @@ -33,7 +32,7 @@ describe('VotesDate component', () => {
it('changes Votos and Fecha styles on click', () => {
render(
<VotesDateController
sortOrder='desc'
sortOrder="desc"
handleSortOrder={mockHandleSortOrder}
handleSortByVotes={mockHandleSortByVotes}
handleSortByDates={mockHandleSortByDates}
Expand Down
3 changes: 0 additions & 3 deletions web/wiki/src/__tests__/pages/Category.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ beforeEach(() => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Name',
avatarId: 'Avatar',
},
} as TAuthContext)
})
Expand Down Expand Up @@ -68,7 +67,6 @@ it('renders Navbar for logged in users', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Name',
avatarId: 'Avatar',
},
} as TAuthContext)
render(<Category />)
Expand Down Expand Up @@ -147,7 +145,6 @@ it('status filter widget appears for users who are logged in', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Name',
avatarId: 'Avatar',
},
} as TAuthContext)
render(<Category />)
Expand Down
2 changes: 0 additions & 2 deletions web/wiki/src/__tests__/pages/Home.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe('Home page', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Hola',
avatarId: 'Adios',
},
} as TAuthContext)
render(<Home />)
Expand Down Expand Up @@ -49,7 +48,6 @@ describe('Home page', () => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'Hola',
avatarId: 'Adios',
},
} as TAuthContext)
render(<Home />)
Expand Down
1 change: 0 additions & 1 deletion web/wiki/src/__tests__/pages/UserProfile.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ beforeEach(() => {
vi.mocked(useAuth).mockReturnValue({
user: {
name: 'User Test Name',
avatarId: 'Adios',
email: 'mail@test.com',
},
} as TAuthContext)
Expand Down

0 comments on commit 6115f55

Please sign in to comment.