-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply actions to multiple users (#1259)
- Loading branch information
Showing
28 changed files
with
663 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
web/usuaris/src/__tests__/hooks/useDeleteMultipleUsers.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { renderHook, waitFor } from '@testing-library/react' | ||
import { QueryClientProvider } from '@tanstack/react-query' | ||
import { act } from 'react-dom/test-utils' | ||
import { useDeleteMultipleUsers } from '../../hooks' | ||
import { queryClient } from '../setup' | ||
import { server } from '../../__mocks__/server' | ||
|
||
beforeEach(() => { | ||
server.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
server.resetHandlers() | ||
queryClient.clear() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
describe('useDeleteMultipleUsers hook', () => { | ||
it('should initialize the useDeleteMultipleUsers hook', async () => { | ||
const { result } = renderHook(() => useDeleteMultipleUsers(), { | ||
wrapper: ({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
), | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(result.current.deleteMultipleUsersMutation).toBeDefined() | ||
expect(result.current.isError).toBe(false) | ||
expect(result.current.isLoading).toBe(false) | ||
expect(result.current.isSuccess).toBe(false) | ||
}) | ||
}) | ||
|
||
it('should delete users successfully and invalidate queries', async () => { | ||
const { result } = renderHook(() => useDeleteMultipleUsers(), { | ||
wrapper: ({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
), | ||
}) | ||
const mockUsersIds = ['1', '3', '4'] | ||
|
||
act(() => { | ||
result.current.deleteMultipleUsersMutation(mockUsersIds) | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(result.current.isSuccess).toBe(true) | ||
expect(result.current.isError).toBe(false) | ||
expect(result.current.isLoading).toBe(false) | ||
expect(queryClient.getQueryData(['users'])).toBeUndefined() | ||
}) | ||
}) | ||
}) |
61 changes: 61 additions & 0 deletions
61
web/usuaris/src/__tests__/hooks/useUpdateUsersStatus.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { renderHook, waitFor } from '@testing-library/react' | ||
import { QueryClientProvider } from '@tanstack/react-query' | ||
import { act } from 'react-dom/test-utils' | ||
import { useUpdateUsersStatus } from '../../hooks' | ||
import { queryClient } from '../setup' | ||
import { server } from '../../__mocks__/server' | ||
import { UserStatus } from '../../types' | ||
|
||
beforeEach(() => { | ||
server.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
server.resetHandlers() | ||
queryClient.clear() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
describe('useUpdateUsersStatus hook', () => { | ||
it('should initialize the useUpdateUsersStatus hook', async () => { | ||
const { result } = renderHook(() => useUpdateUsersStatus(), { | ||
wrapper: ({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
), | ||
}) | ||
await waitFor(() => { | ||
expect(result.current.changeUsersStatus).toBeDefined() | ||
expect(result.current.error).toBe(null) | ||
expect(result.current.isSuccess).toBe(false) | ||
}) | ||
}) | ||
|
||
it('should call changeUsersStatus on users status update and refetch users on success', async () => { | ||
const { result } = renderHook(() => useUpdateUsersStatus(), { | ||
wrapper: ({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
), | ||
}) | ||
|
||
const invalidateQueriesSpy = vi.spyOn(queryClient, 'invalidateQueries') | ||
|
||
act(() => { | ||
result.current.changeUsersStatus.mutate({ | ||
ids: ['1', '2', '5'], | ||
status: UserStatus.BLOCKED, | ||
}) | ||
}) | ||
await waitFor(() => { | ||
expect(result.current.error).toBe(null) | ||
expect(result.current.isSuccess).toBe(true) | ||
expect(invalidateQueriesSpy).toHaveBeenCalledWith(['users']) | ||
}) | ||
}) | ||
}) |
52 changes: 52 additions & 0 deletions
52
web/usuaris/src/__tests__/molecules/ActionsDropdown.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { vi } from 'vitest' | ||
import { fireEvent, render, screen } from '../test-utils' | ||
import { ActionsDropdown } from '../../components/molecules' | ||
import { UserStatus } from '../../types' | ||
|
||
const mockHandleClick = vi.fn() | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
describe('ActionsDropdown', () => { | ||
it('renders correctly', () => { | ||
render( | ||
<ActionsDropdown | ||
selectedStatus={undefined} | ||
handleAction={mockHandleClick} | ||
isActionFinished={false} | ||
/> | ||
) | ||
const actionsDropdown = screen.getByTestId('actions-dropdown') | ||
|
||
expect(actionsDropdown).toHaveTextContent(/accions/i) | ||
expect(actionsDropdown).toHaveAttribute('disabled') | ||
expect(screen.getByTitle(/obre/i)).toBeInTheDocument() | ||
}) | ||
|
||
it('renders corresponding actions when users selected and returns selected action to parent', () => { | ||
render( | ||
<ActionsDropdown | ||
selectedStatus={UserStatus.ACTIVE} | ||
handleAction={mockHandleClick} | ||
isActionFinished={false} | ||
/> | ||
) | ||
|
||
const actionsHeader = screen.getByTestId('dropdown-header') | ||
|
||
expect(actionsHeader).toHaveTextContent(/accions/i) | ||
|
||
fireEvent.click(actionsHeader) | ||
|
||
const blockOption = screen.getByTestId('BLOCKED') | ||
expect(blockOption).toBeInTheDocument() | ||
expect(screen.getByTestId('DELETE')).toBeInTheDocument() | ||
|
||
fireEvent.click(blockOption) | ||
|
||
expect(actionsHeader).toHaveTextContent(/bloquejar/i) | ||
expect(mockHandleClick).toHaveBeenCalledWith(UserStatus.BLOCKED) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.