Show and manage the linked partner in Settings
Settings never called GET /workspace/{id}/members -- confirmed by network
capture, not inference. There was no member list, no loading state and no
swallowed error; the code was simply absent. A workspace with a partner
therefore looked identical to a solo one, and nothing stopped a second
partner being added.
Settings now lists both members, marks the logged-in user, and offers
rename on either row and remove on the partner. The add-form appears only
when no partner exists. Removal asks for confirmation inline rather than
via window.confirm, which this codebase does not use anywhere and which
blocks the browser tooling used to verify it.
Own-name rename is offered too: the backend documents this route as the
only place a name can be corrected, so hiding it would leave that
capability dead.
Also fixes apiFetch's error parsing, which handled only string and array
detail bodies. A structured {message_key, data} object fell through and
was assigned to error.message, rendering as [object Object] or leaking
the raw key -- latent until this session's backend work started
returning exactly that shape.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RXMhc9nT6sNz1SL4j8dznL
This commit is contained in:
@@ -4,6 +4,7 @@ import { Settings } from './Settings'
|
||||
import * as hooks from '@/hooks'
|
||||
import { useWorkspace } from '@/context'
|
||||
import * as api from '@/services/api'
|
||||
import { ApiRequestError } from '@/services/errors'
|
||||
import { LanguageProvider } from '@/i18n'
|
||||
|
||||
vi.mock('@/hooks')
|
||||
@@ -82,10 +83,29 @@ describe('Settings', () => {
|
||||
createdAt: '2026-01-01T00:00:00',
|
||||
}
|
||||
|
||||
const selfMember = {
|
||||
uuid: 'pd-1',
|
||||
workspaceId: 'ws-1',
|
||||
name: 'Test Parent',
|
||||
salaryType: 'monthly' as const,
|
||||
employmentStatus: 'employed' as const,
|
||||
createdAt: '2026-01-01T00:00:00',
|
||||
}
|
||||
|
||||
const partnerMember = {
|
||||
uuid: 'pd-2',
|
||||
workspaceId: 'ws-1',
|
||||
name: 'Sam',
|
||||
salaryType: 'monthly' as const,
|
||||
employmentStatus: 'employed' as const,
|
||||
createdAt: '2026-01-01T00:00:00',
|
||||
}
|
||||
|
||||
let mockUpsertVacationDays: ReturnType<typeof vi.fn>
|
||||
let mockUpdateChildDetails: ReturnType<typeof vi.fn>
|
||||
let mockUpsertDayOverride: ReturnType<typeof vi.fn>
|
||||
let mockRemoveDayOverride: ReturnType<typeof vi.fn>
|
||||
let mockRefreshMembers: ReturnType<typeof vi.fn>
|
||||
|
||||
const mockCalendarWindow = {
|
||||
startYear: CURRENT_YEAR,
|
||||
@@ -101,8 +121,17 @@ describe('Settings', () => {
|
||||
mockUpdateChildDetails = vi.fn().mockResolvedValue(undefined)
|
||||
mockUpsertDayOverride = vi.fn().mockResolvedValue(undefined)
|
||||
mockRemoveDayOverride = vi.fn().mockResolvedValue(undefined)
|
||||
mockRefreshMembers = vi.fn().mockResolvedValue(undefined)
|
||||
|
||||
vi.mocked(useWorkspace).mockReturnValue(mockWorkspaceContext as any)
|
||||
// Default: solo workspace (only the logged-in user) — the add-partner
|
||||
// form should be visible. Tests that need a linked partner override this.
|
||||
vi.mocked(hooks.useWorkspaceMembers).mockReturnValue({
|
||||
data: [selfMember],
|
||||
isLoading: false,
|
||||
error: null,
|
||||
refresh: mockRefreshMembers,
|
||||
} as any)
|
||||
vi.mocked(hooks.useVacationDays).mockReturnValue({
|
||||
data: mockVacationDays,
|
||||
isLoading: false,
|
||||
@@ -131,6 +160,8 @@ describe('Settings', () => {
|
||||
vi.mocked(api.updateWorkspace).mockResolvedValue({ ...mockWorkspaceContext.workspace } as any)
|
||||
vi.mocked(api.updateParentSettings).mockResolvedValue({} as any)
|
||||
vi.mocked(api.addPartner).mockResolvedValue({} as any)
|
||||
vi.mocked(api.renameWorkspaceParent).mockResolvedValue({ ...partnerMember } as any)
|
||||
vi.mocked(api.removeWorkspaceParent).mockResolvedValue(undefined)
|
||||
})
|
||||
|
||||
it('renders loading spinner while workspace loads', () => {
|
||||
@@ -274,6 +305,166 @@ describe('Settings', () => {
|
||||
fireEvent.click(screen.getByTestId('add-partner-button'))
|
||||
|
||||
await waitFor(() => expect(api.addPartner).toHaveBeenCalledWith('ws-1', 'Partner Name'))
|
||||
await waitFor(() => expect(mockRefreshMembers).toHaveBeenCalled())
|
||||
})
|
||||
|
||||
it('shows an add-partner error without a raw message_key when the name is rejected', async () => {
|
||||
vi.mocked(api.addPartner).mockRejectedValue(
|
||||
new ApiRequestError(422, {
|
||||
code: 'workspace.parents.invalidName',
|
||||
message: 'workspace.parents.invalidName',
|
||||
details: { messageKey: 'workspace.parents.invalidName', data: {} },
|
||||
}),
|
||||
)
|
||||
renderSettings()
|
||||
fireEvent.change(screen.getByPlaceholderText('Partnerns namn'), { target: { value: 'X' } })
|
||||
fireEvent.click(screen.getByTestId('add-partner-button'))
|
||||
|
||||
const message = await screen.findByTestId('save-message')
|
||||
expect(message).toHaveTextContent('Partnerns namn är ogiltigt.')
|
||||
})
|
||||
|
||||
describe('workspace members', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(hooks.useWorkspaceMembers).mockReturnValue({
|
||||
data: [selfMember, partnerMember],
|
||||
isLoading: false,
|
||||
error: null,
|
||||
refresh: mockRefreshMembers,
|
||||
} as any)
|
||||
})
|
||||
|
||||
it('shows the linked partner and hides the add-partner form', () => {
|
||||
renderSettings()
|
||||
expect(screen.getByTestId('member-name-pd-2')).toHaveTextContent('Sam')
|
||||
expect(screen.queryByTestId('add-partner-button')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('shows a "you" badge on the logged-in user\'s own row', () => {
|
||||
renderSettings()
|
||||
expect(screen.getByTestId('member-name-pd-1')).toHaveTextContent('Test Parent')
|
||||
expect(screen.getByTestId('member-name-pd-1')).toHaveTextContent('(Du)')
|
||||
})
|
||||
|
||||
it('does not offer a remove button on the logged-in user\'s own row', () => {
|
||||
renderSettings()
|
||||
expect(screen.queryByTestId('remove-button-pd-1')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('renames a member and refreshes the member list', async () => {
|
||||
renderSettings()
|
||||
fireEvent.click(screen.getByTestId('rename-button-pd-2'))
|
||||
fireEvent.change(screen.getByTestId('rename-input-pd-2'), { target: { value: 'Samuel' } })
|
||||
fireEvent.click(screen.getByTestId('rename-save-pd-2'))
|
||||
|
||||
await waitFor(() =>
|
||||
expect(api.renameWorkspaceParent).toHaveBeenCalledWith('ws-1', 'pd-2', 'Samuel'),
|
||||
)
|
||||
await waitFor(() => expect(mockRefreshMembers).toHaveBeenCalled())
|
||||
})
|
||||
|
||||
it('allows renaming the logged-in user\'s own profile too', () => {
|
||||
renderSettings()
|
||||
fireEvent.click(screen.getByTestId('rename-button-pd-1'))
|
||||
expect(screen.getByTestId('rename-input-pd-1')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('cancels a rename without calling the API', () => {
|
||||
renderSettings()
|
||||
fireEvent.click(screen.getByTestId('rename-button-pd-2'))
|
||||
fireEvent.change(screen.getByTestId('rename-input-pd-2'), { target: { value: 'Something else' } })
|
||||
fireEvent.click(screen.getByTestId('rename-cancel-pd-2'))
|
||||
|
||||
expect(screen.queryByTestId('rename-input-pd-2')).not.toBeInTheDocument()
|
||||
expect(api.renameWorkspaceParent).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('requires an in-app confirmation before removing a member', async () => {
|
||||
renderSettings()
|
||||
fireEvent.click(screen.getByTestId('remove-button-pd-2'))
|
||||
|
||||
expect(api.removeWorkspaceParent).not.toHaveBeenCalled()
|
||||
expect(screen.getByTestId('remove-confirm-pd-2')).toBeInTheDocument()
|
||||
|
||||
fireEvent.click(screen.getByTestId('remove-confirm-pd-2'))
|
||||
await waitFor(() =>
|
||||
expect(api.removeWorkspaceParent).toHaveBeenCalledWith('ws-1', 'pd-2'),
|
||||
)
|
||||
})
|
||||
|
||||
it('cancelling the remove confirmation does not call the API', () => {
|
||||
renderSettings()
|
||||
fireEvent.click(screen.getByTestId('remove-button-pd-2'))
|
||||
fireEvent.click(screen.getByTestId('remove-cancel-pd-2'))
|
||||
|
||||
expect(screen.queryByTestId('remove-confirm-pd-2')).not.toBeInTheDocument()
|
||||
expect(api.removeWorkspaceParent).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('refreshes the member list after a successful removal', async () => {
|
||||
renderSettings()
|
||||
fireEvent.click(screen.getByTestId('remove-button-pd-2'))
|
||||
fireEvent.click(screen.getByTestId('remove-confirm-pd-2'))
|
||||
|
||||
await waitFor(() => expect(mockRefreshMembers).toHaveBeenCalled())
|
||||
})
|
||||
|
||||
it('shows the has-login refusal without a raw message_key', async () => {
|
||||
vi.mocked(api.removeWorkspaceParent).mockRejectedValue(
|
||||
new ApiRequestError(422, {
|
||||
code: 'workspace.parents.remove.hasLogin',
|
||||
message: 'workspace.parents.remove.hasLogin',
|
||||
details: { messageKey: 'workspace.parents.remove.hasLogin', data: {} },
|
||||
}),
|
||||
)
|
||||
renderSettings()
|
||||
fireEvent.click(screen.getByTestId('remove-button-pd-2'))
|
||||
fireEvent.click(screen.getByTestId('remove-confirm-pd-2'))
|
||||
|
||||
const message = await screen.findByTestId('member-action-error-pd-2')
|
||||
expect(message).toHaveTextContent('den här personen har ett eget konto och egen inloggning')
|
||||
expect(message).not.toHaveTextContent('workspace.parents.remove.hasLogin')
|
||||
})
|
||||
|
||||
it('names each blocker on a hasDependentData refusal, not a raw message_key', async () => {
|
||||
vi.mocked(api.removeWorkspaceParent).mockRejectedValue(
|
||||
new ApiRequestError(422, {
|
||||
code: 'workspace.parents.remove.hasDependentData',
|
||||
message: 'workspace.parents.remove.hasDependentData',
|
||||
details: {
|
||||
messageKey: 'workspace.parents.remove.hasDependentData',
|
||||
data: { blockers: ['leave_wishes', 'planned_leave'] },
|
||||
},
|
||||
}),
|
||||
)
|
||||
renderSettings()
|
||||
fireEvent.click(screen.getByTestId('remove-button-pd-2'))
|
||||
fireEvent.click(screen.getByTestId('remove-confirm-pd-2'))
|
||||
|
||||
const message = await screen.findByTestId('member-action-error-pd-2')
|
||||
expect(message).toHaveTextContent('önskemål om ledighet')
|
||||
expect(message).toHaveTextContent('planerade lediga dagar')
|
||||
expect(message).not.toHaveTextContent('hasDependentData')
|
||||
})
|
||||
|
||||
it('shows the remove-self refusal without a raw message_key', async () => {
|
||||
// Defence in depth: the UI never renders a remove button for the
|
||||
// caller's own row, but the handler still routes a self-removal
|
||||
// refusal from the backend through the same safe error mapping.
|
||||
vi.mocked(api.removeWorkspaceParent).mockRejectedValue(
|
||||
new ApiRequestError(422, {
|
||||
code: 'workspace.parents.remove.self',
|
||||
message: 'workspace.parents.remove.self',
|
||||
details: { messageKey: 'workspace.parents.remove.self', data: {} },
|
||||
}),
|
||||
)
|
||||
renderSettings()
|
||||
fireEvent.click(screen.getByTestId('remove-button-pd-2'))
|
||||
fireEvent.click(screen.getByTestId('remove-confirm-pd-2'))
|
||||
|
||||
const message = await screen.findByTestId('member-action-error-pd-2')
|
||||
expect(message).not.toHaveTextContent('workspace.parents.remove.self')
|
||||
})
|
||||
})
|
||||
|
||||
it('toggles week numbers', () => {
|
||||
|
||||
Reference in New Issue
Block a user