|
1 |
| -import { expect } from 'vitest' |
| 1 | +import { expect, vi } from 'vitest' |
| 2 | +import React, { act } from 'react' |
| 3 | +import { render, screen, waitFor } from '@testing-library/react' |
| 4 | +import { ThemeProvider } from '@/components/theme-provider.tsx' |
| 5 | +import { Provider } from 'react-redux' |
| 6 | +import { store } from '@/store/store.ts' |
| 7 | +import { BrowserRouter } from 'react-router-dom' |
| 8 | +import App from '@/App.tsx' |
| 9 | +import Keycloak, { KeycloakProfile } from 'keycloak-js' |
2 | 10 |
|
3 |
| -describe('Page', () => { |
4 |
| - it('some logic', () => { |
5 |
| - expect(1).toEqual(1) |
| 11 | +function mockUseKeycloak() { |
| 12 | + const token = 'A random string that is non zero length' |
| 13 | + const userProfile: KeycloakProfile = { |
| 14 | + username: 'test', |
| 15 | + |
| 16 | + firstName: 'Test', |
| 17 | + lastName: 'User', |
| 18 | + } |
| 19 | + const realmAccess = { roles: ['user'] } |
| 20 | + |
| 21 | + const authClient: Keycloak = { |
| 22 | + authenticated: true, |
| 23 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 24 | + hasRealmRole(_ignored: string) { |
| 25 | + return true |
| 26 | + }, |
| 27 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 28 | + hasResourceRole(_ignored: string) { |
| 29 | + return true |
| 30 | + }, |
| 31 | + idToken: token, |
| 32 | + profile: userProfile, |
| 33 | + realm: 'TestRealm', |
| 34 | + realmAccess, |
| 35 | + refreshToken: token, |
| 36 | + token, |
| 37 | + } as Keycloak |
| 38 | + return { initialized: true, keycloak: authClient } |
| 39 | +} |
| 40 | + |
| 41 | +describe('App tests', () => { |
| 42 | + beforeAll(() => { |
| 43 | + vi.mock('@react-keycloak/web', () => ({ useKeycloak: mockUseKeycloak })) |
| 44 | + }) |
| 45 | + it('home render with keycloak authorized', async () => { |
| 46 | + await act(async () => |
| 47 | + render( |
| 48 | + <React.StrictMode> |
| 49 | + <ThemeProvider> |
| 50 | + <Provider store={store}> |
| 51 | + <BrowserRouter> |
| 52 | + <App /> |
| 53 | + </BrowserRouter> |
| 54 | + </Provider> |
| 55 | + </ThemeProvider> |
| 56 | + </React.StrictMode> |
| 57 | + ) |
| 58 | + ) |
| 59 | + |
| 60 | + await waitFor(() => { |
| 61 | + // note: menu items shown only after login |
| 62 | + expect(screen.getByText(/counter/i)).toBeInTheDocument() |
| 63 | + expect(screen.getByText(/users/i)).toBeInTheDocument() |
| 64 | + }) |
6 | 65 | })
|
7 | 66 | })
|
0 commit comments