test: add comprehensive non-admin user test cases for NowPlaying visibility

Enhances test coverage by making the usePermissions mock dynamic and adding test cases that verify:
- Admin users can see NowPlayingPanel when adminOnly is true
- Non-admin users cannot see NowPlayingPanel when adminOnly is true
- Non-admin users can see NowPlayingPanel when adminOnly is false
- Non-admin users cannot see NowPlayingPanel when feature is disabled

This ensures the admin-only permission check works correctly for all user types.
This commit is contained in:
Deluan
2025-12-15 13:04:49 -05:00
parent 2ff5379b0b
commit 27d81ffd96

View File

@@ -8,11 +8,12 @@ import AppBar from './AppBar'
import config from '../config'
let store
let mockPermissions = 'admin'
vi.mock('react-admin', () => ({
AppBar: ({ userMenu }) => <div data-testid="appbar">{userMenu}</div>,
useTranslate: () => (x) => x,
usePermissions: () => ({ permissions: 'admin' }),
usePermissions: () => ({ permissions: mockPermissions }),
getResources: () => [],
}))
@@ -40,6 +41,7 @@ describe('<AppBar />', () => {
config.devActivityPanel = true
config.enableNowPlaying = true
config.nowPlayingAdminOnly = true
mockPermissions = 'admin'
store = createStore(combineReducers({ activity: activityReducer }), {
activity: { nowPlayingCount: 0 },
})
@@ -73,4 +75,67 @@ describe('<AppBar />', () => {
)
expect(screen.getByTestId('now-playing-panel')).toBeInTheDocument()
})
describe('admin-only mode', () => {
beforeEach(() => {
config.nowPlayingAdminOnly = true
})
it('shows NowPlayingPanel to admin users', () => {
mockPermissions = 'admin'
render(
<Provider store={store}>
<AppBar />
</Provider>,
)
expect(screen.getByTestId('now-playing-panel')).toBeInTheDocument()
})
it('hides NowPlayingPanel from non-admin users', () => {
mockPermissions = 'user'
render(
<Provider store={store}>
<AppBar />
</Provider>,
)
expect(screen.queryByTestId('now-playing-panel')).toBeNull()
})
})
describe('non-admin users', () => {
beforeEach(() => {
mockPermissions = 'user'
})
it('cannot see NowPlayingPanel when adminOnly is true', () => {
config.nowPlayingAdminOnly = true
render(
<Provider store={store}>
<AppBar />
</Provider>,
)
expect(screen.queryByTestId('now-playing-panel')).toBeNull()
})
it('can see NowPlayingPanel when adminOnly is false', () => {
config.nowPlayingAdminOnly = false
render(
<Provider store={store}>
<AppBar />
</Provider>,
)
expect(screen.getByTestId('now-playing-panel')).toBeInTheDocument()
})
it('cannot see NowPlayingPanel when feature is disabled', () => {
config.enableNowPlaying = false
config.nowPlayingAdminOnly = false
render(
<Provider store={store}>
<AppBar />
</Provider>,
)
expect(screen.queryByTestId('now-playing-panel')).toBeNull()
})
})
})