-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic tests for admin panel mange role pages (#179)
- Loading branch information
Showing
18 changed files
with
2,653 additions
and
81 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
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,110 @@ | ||
import { | ||
fireEvent, screen, | ||
} from '@testing-library/react' | ||
import { | ||
describe, it, expect, vi, beforeEach, Mock, | ||
} from 'vitest' | ||
import Page from 'app/[lang]/roles/[id]/page' | ||
import { roles } from 'tests/roleMock' | ||
import { render } from 'vitest.setup' | ||
import { | ||
useGetApiV1RolesByIdQuery, | ||
usePutApiV1RolesByIdMutation, | ||
useDeleteApiV1RolesByIdMutation, | ||
} from 'services/auth/api' | ||
|
||
vi.mock( | ||
'next/navigation', | ||
() => ({ | ||
useParams: vi.fn().mockReturnValue({ id: '2' }), | ||
useRouter: vi.fn().mockReturnValue({ push: () => {} }), | ||
}), | ||
) | ||
|
||
vi.mock( | ||
'services/auth/api', | ||
() => ({ | ||
useGetApiV1RolesByIdQuery: vi.fn(), | ||
usePutApiV1RolesByIdMutation: vi.fn(), | ||
useDeleteApiV1RolesByIdMutation: vi.fn(), | ||
}), | ||
) | ||
|
||
const mockUpdate = vi.fn() | ||
const mockDelete = vi.fn() | ||
describe( | ||
'Page Component', | ||
() => { | ||
beforeEach(() => { | ||
(useGetApiV1RolesByIdQuery as Mock).mockReturnValue({ data: { role: roles[1] } }); | ||
(usePutApiV1RolesByIdMutation as Mock).mockReturnValue([mockUpdate, { isLoading: false }]); | ||
(useDeleteApiV1RolesByIdMutation as Mock).mockReturnValue([mockDelete, { isLoading: false }]) | ||
}) | ||
|
||
it( | ||
'render role', | ||
async () => { | ||
render(<Page />) | ||
|
||
const nameInput = screen.queryByTestId('nameInput') as HTMLInputElement | ||
const noteInput = screen.queryByTestId('noteInput') as HTMLInputElement | ||
const saveBtn = screen.queryByTestId('saveButton') as HTMLButtonElement | ||
const deleteBtn = screen.queryByTestId('deleteButton') | ||
expect(nameInput?.value).toBe(roles[1].name) | ||
expect(noteInput?.value).toBe(roles[1].note) | ||
expect(saveBtn?.disabled).toBeTruthy() | ||
expect(deleteBtn).toBeInTheDocument() | ||
}, | ||
) | ||
|
||
it( | ||
'update role', | ||
async () => { | ||
render(<Page />) | ||
|
||
const nameInput = screen.queryByTestId('nameInput') as HTMLInputElement | ||
const noteInput = screen.queryByTestId('noteInput') as HTMLInputElement | ||
const saveBtn = screen.queryByTestId('saveButton') as HTMLButtonElement | ||
|
||
fireEvent.change( | ||
nameInput, | ||
{ target: { value: 'new name' } }, | ||
) | ||
fireEvent.change( | ||
noteInput, | ||
{ target: { value: 'new note' } }, | ||
) | ||
|
||
expect(nameInput?.value).toBe('new name') | ||
expect(noteInput?.value).toBe('new note') | ||
expect(saveBtn?.disabled).toBeFalsy() | ||
fireEvent.click(saveBtn) | ||
|
||
expect(mockUpdate).toHaveBeenLastCalledWith({ | ||
id: 2, | ||
putRoleReq: { | ||
name: 'new name', | ||
note: 'new note', | ||
}, | ||
}) | ||
}, | ||
) | ||
|
||
it( | ||
'delete role', | ||
async () => { | ||
render(<Page />) | ||
|
||
const deleteBtn = screen.queryByTestId('deleteButton') as HTMLButtonElement | ||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument() | ||
|
||
fireEvent.click(deleteBtn) | ||
expect(screen.queryByRole('dialog')).toBeInTheDocument() | ||
|
||
fireEvent.click(screen.queryByTestId('confirmButton') as HTMLButtonElement) | ||
|
||
expect(mockDelete).toHaveBeenLastCalledWith({ id: 2 }) | ||
}, | ||
) | ||
}, | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
fireEvent, screen, | ||
} from '@testing-library/react' | ||
import { | ||
describe, it, expect, vi, beforeEach, Mock, | ||
} from 'vitest' | ||
import Page from 'app/[lang]/roles/new/page' | ||
import { render } from 'vitest.setup' | ||
import { usePostApiV1RolesMutation } from 'services/auth/api' | ||
|
||
vi.mock( | ||
'next/navigation', | ||
() => ({ useRouter: vi.fn().mockReturnValue({ push: () => {} }) }), | ||
) | ||
|
||
vi.mock( | ||
'services/auth/api', | ||
() => ({ usePostApiV1RolesMutation: vi.fn() }), | ||
) | ||
|
||
const mockCreate = vi.fn().mockReturnValue({ data: { role: { id: 1 } } }) | ||
describe( | ||
'Page Component', | ||
() => { | ||
beforeEach(() => { | ||
(usePostApiV1RolesMutation as Mock).mockReturnValue([mockCreate, { isLoading: false }]) | ||
}) | ||
|
||
it( | ||
'render page', | ||
async () => { | ||
render(<Page />) | ||
|
||
const nameInput = screen.queryByTestId('nameInput') as HTMLInputElement | ||
const noteInput = screen.queryByTestId('noteInput') as HTMLInputElement | ||
const saveBtn = screen.queryByTestId('saveButton') as HTMLButtonElement | ||
expect(nameInput?.value).toBe('') | ||
expect(noteInput?.value).toBe('') | ||
expect(saveBtn).toBeInTheDocument() | ||
}, | ||
) | ||
|
||
it( | ||
'create role', | ||
async () => { | ||
render(<Page />) | ||
|
||
const nameInput = screen.queryByTestId('nameInput') as HTMLInputElement | ||
const noteInput = screen.queryByTestId('noteInput') as HTMLInputElement | ||
const saveBtn = screen.queryByTestId('saveButton') as HTMLButtonElement | ||
|
||
fireEvent.change( | ||
nameInput, | ||
{ target: { value: 'new name' } }, | ||
) | ||
fireEvent.change( | ||
noteInput, | ||
{ target: { value: 'new note' } }, | ||
) | ||
|
||
fireEvent.click(saveBtn) | ||
|
||
expect(mockCreate).toHaveBeenLastCalledWith({ | ||
postRoleReq: { | ||
name: 'new name', | ||
note: 'new note', | ||
}, | ||
}) | ||
}, | ||
) | ||
}, | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
describe, it, expect, vi, beforeEach, Mock, | ||
} from 'vitest' | ||
import { | ||
waitFor, screen, | ||
} from '@testing-library/react' | ||
import { render } from '../../../vitest.setup' | ||
import Page from 'app/[lang]/roles/page' | ||
import { roles } from 'tests/roleMock' | ||
import { useGetApiV1RolesQuery } from 'services/auth/api' | ||
|
||
vi.mock( | ||
'services/auth/api', | ||
() => ({ useGetApiV1RolesQuery: vi.fn() }), | ||
) | ||
|
||
describe( | ||
'Page Component', | ||
() => { | ||
beforeEach(() => { | ||
(useGetApiV1RolesQuery as Mock).mockReturnValue({ data: { roles } }) | ||
}) | ||
|
||
it( | ||
'render roles', | ||
async () => { | ||
render(<Page />) | ||
|
||
await waitFor(() => { | ||
const rows = screen.queryAllByTestId('roleRow') | ||
expect(rows.length).toBe(2) | ||
expect(rows[0].querySelector('td')?.innerHTML).toContain(roles[0].name) | ||
const editLinks = rows[0].querySelector('td')?.getElementsByTagName('a') | ||
expect(editLinks?.length).toBe(0) | ||
|
||
expect(rows[1].querySelector('td')?.innerHTML).toContain(roles[1].name) | ||
const editLinks1 = rows[1].querySelector('td')?.getElementsByTagName('a') | ||
expect(editLinks1?.length).toBe(1) | ||
expect(editLinks1?.[0].getAttribute('href')).toBe('/en/roles/2') | ||
|
||
const createButton = screen.getByTestId('createButton') | ||
expect(createButton.getAttribute('href')).toBe('/en/roles/new') | ||
}) | ||
}, | ||
) | ||
}, | ||
) |
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
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
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
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.