parsimony/admin/src/tabs/MembersTab.test.tsx
tgiachi 10f593e9bd feat(admin): publishers in the Settings and Members tabs
- Settings: the Posting select offers Publishers only (newsletter)
- Members: a Can publish checkbox in the add form (unchecked sends no flag, so
  adding an existing publisher again keeps it), a Publisher badge in the table,
  and a Make publisher / Make subscriber row action
- api client: publisher on Member, the publishers post policy, setPublisher
- the fake API keeps the flag on re-add and answers the member PATCH
- tests for each
2026-11-10 12:41:44 +01:00

208 lines
11 KiB
TypeScript

import { screen, waitFor, within } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
import type { Member } from '@/lib/api'
import { aList, installFakeApi, newState } from '@/test/fakeApi'
import { renderApp } from '@/test/render'
const member = (address: string, over: Partial<Member> = {}): Member => ({
address,
display_name: '',
disabled: false,
bounce_count: 0,
publisher: false,
...over,
})
async function setup(members: Member[] = []) {
const state = installFakeApi(newState({ lists: [aList({ id: 1 })], members: { 1: members } }))
const ctx = renderApp('/lists/1/members', { token: 'good' })
await screen.findByRole('heading', { name: /^members/i, level: 2 })
return { ...ctx, state }
}
describe('members tab', () => {
it('lists members with their name and status', async () => {
await setup([
member('alice@example.com', { display_name: 'Alice' }),
member('bob@example.com', { disabled: true, bounce_count: 3 }),
member('carol@example.com', { bounce_count: 1 }),
])
const alice = (await screen.findByText('alice@example.com')).closest('tr')!
expect(within(alice).getByText('Alice')).toBeInTheDocument()
expect(within(alice).getByText('Active')).toBeInTheDocument()
const bob = screen.getByText('bob@example.com').closest('tr')!
expect(within(bob).getByText(/disabled/i)).toBeInTheDocument()
expect(within(bob).getByText(/3 bounces/i)).toBeInTheDocument()
const carol = screen.getByText('carol@example.com').closest('tr')!
expect(within(carol).getByText('Active')).toBeInTheDocument()
expect(within(carol).getByText(/1 bounce$/i)).toBeInTheDocument()
expect(screen.getByRole('heading', { name: 'Members (3)', level: 2 })).toBeInTheDocument()
})
it('has an empty state', async () => {
await setup()
expect(await screen.findByText(/no members yet/i)).toBeInTheDocument()
})
it('filters the table as you type', async () => {
const { user } = await setup([member('alice@example.com', { display_name: 'Alice' }), member('bob@example.com')])
await screen.findByText('bob@example.com')
await user.type(screen.getByRole('searchbox', { name: /filter members/i }), 'ALI')
expect(screen.getByText('alice@example.com')).toBeInTheDocument()
expect(screen.queryByText('bob@example.com')).not.toBeInTheDocument()
await user.clear(screen.getByRole('searchbox', { name: /filter members/i }))
await user.type(screen.getByRole('searchbox', { name: /filter members/i }), 'zzz')
expect(await screen.findByText(/no members match/i)).toBeInTheDocument()
})
it('filters by display name too', async () => {
const { user } = await setup([member('x@example.com', { display_name: 'Zoe Quinn' }), member('y@example.com')])
await screen.findByText('y@example.com')
await user.type(screen.getByRole('searchbox', { name: /filter members/i }), 'zoe')
expect(screen.getByText('x@example.com')).toBeInTheDocument()
expect(screen.queryByText('y@example.com')).not.toBeInTheDocument()
})
it('adds a member and clears the form', async () => {
const { user, state } = await setup([member('alice@example.com')])
await user.type(screen.getByLabelText(/email address/i), 'Dave@Example.com')
await user.type(screen.getByLabelText(/display name/i), 'Dave')
await user.click(screen.getByRole('button', { name: /add member/i }))
expect(await screen.findByText('dave@example.com')).toBeInTheDocument()
expect(state.members[1].map((m) => m.address)).toEqual(['alice@example.com', 'dave@example.com'])
expect(screen.getByLabelText(/email address/i)).toHaveValue('')
expect(screen.getByLabelText(/display name/i)).toHaveValue('')
expect(await screen.findByText(/added dave@example.com/i)).toBeInTheDocument()
})
it('asks for an address before calling the API', async () => {
const { user, state } = await setup()
await user.click(screen.getByRole('button', { name: /add member/i }))
expect(await screen.findByText(/enter an email address/i)).toBeInTheDocument()
expect(state.calls.filter((c) => c.startsWith('POST'))).toEqual([])
})
it('shows the server error for a bad address and keeps what was typed', async () => {
const { user } = await setup()
await user.type(screen.getByLabelText(/email address/i), 'not-an-address')
await user.click(screen.getByRole('button', { name: /add member/i }))
expect(await screen.findByRole('alert')).toHaveTextContent('a valid email address is required')
expect(screen.getByLabelText(/email address/i)).toHaveValue('not-an-address')
})
it('re-enables a disabled member', async () => {
const { user, state } = await setup([member('bob@example.com', { display_name: 'Bob', disabled: true, bounce_count: 3 })])
const row = (await screen.findByText('bob@example.com')).closest('tr')!
await user.click(within(row).getByRole('button', { name: /re-enable/i }))
await waitFor(() => expect(state.members[1][0].disabled).toBe(false))
expect(state.members[1][0].display_name).toBe('Bob')
expect(await screen.findByText('Active')).toBeInTheDocument()
expect(screen.queryByRole('button', { name: /re-enable/i })).not.toBeInTheDocument()
})
it('does not offer re-enable for active members', async () => {
await setup([member('alice@example.com')])
await screen.findByText('alice@example.com')
expect(screen.queryByRole('button', { name: /re-enable/i })).not.toBeInTheDocument()
})
describe('publishers', () => {
it('shows a Publisher badge on publishers only', async () => {
await setup([member('alice@example.com', { publisher: true }), member('bob@example.com')])
const alice = (await screen.findByText('alice@example.com')).closest('tr')!
expect(within(alice).getByText('Publisher')).toBeInTheDocument()
const bob = screen.getByText('bob@example.com').closest('tr')!
expect(within(bob).queryByText('Publisher')).not.toBeInTheDocument()
})
it('adds a publisher with the Can publish checkbox, then clears it', async () => {
const { user, state } = await setup()
await user.type(screen.getByLabelText(/email address/i), 'dave@example.com')
await user.click(screen.getByRole('checkbox', { name: /can publish/i }))
await user.click(screen.getByRole('button', { name: /add member/i }))
const row = (await screen.findByText('dave@example.com')).closest('tr')!
expect(await within(row).findByText('Publisher')).toBeInTheDocument()
expect(state.members[1][0].publisher).toBe(true)
expect(screen.getByRole('checkbox', { name: /can publish/i })).not.toBeChecked()
})
it('adding without the checkbox leaves an existing publisher alone', async () => {
const { user, state } = await setup([member('dave@example.com', { publisher: true })])
await screen.findByText('dave@example.com')
expect(screen.getByRole('checkbox', { name: /can publish/i })).not.toBeChecked()
await user.type(screen.getByLabelText(/email address/i), 'dave@example.com')
await user.type(screen.getByLabelText(/display name/i), 'Dave')
await user.click(screen.getByRole('button', { name: /add member/i }))
await waitFor(() => expect(state.members[1][0].display_name).toBe('Dave'))
expect(state.members[1][0].publisher).toBe(true)
})
it('re-enabling keeps the publisher flag', async () => {
const { user, state } = await setup([member('bob@example.com', { disabled: true, publisher: true })])
const row = (await screen.findByText('bob@example.com')).closest('tr')!
await user.click(within(row).getByRole('button', { name: /re-enable/i }))
await waitFor(() => expect(state.members[1][0].disabled).toBe(false))
expect(state.members[1][0].publisher).toBe(true)
})
it('makes a member a publisher and back', async () => {
const { user, state } = await setup([member('a+b@example.com')])
let row = (await screen.findByText('a+b@example.com')).closest('tr')!
await user.click(within(row).getByRole('button', { name: /make publisher/i }))
await waitFor(() => expect(state.members[1][0].publisher).toBe(true))
expect(state.calls).toContain('PATCH /lists/1/members/a%2Bb%40example.com')
row = screen.getByText('a+b@example.com').closest('tr')!
expect(await within(row).findByText('Publisher')).toBeInTheDocument()
expect(await screen.findByText(/a\+b@example.com can now publish/i)).toBeInTheDocument()
await user.click(within(row).getByRole('button', { name: /make subscriber/i }))
await waitFor(() => expect(state.members[1][0].publisher).toBe(false))
row = screen.getByText('a+b@example.com').closest('tr')!
await waitFor(() => expect(within(row).queryByText('Publisher')).not.toBeInTheDocument())
expect(within(row).getByRole('button', { name: /make publisher/i })).toBeInTheDocument()
})
it('shows the server error of the row action', async () => {
const { user, state } = await setup([member('alice@example.com')])
const row = (await screen.findByText('alice@example.com')).closest('tr')!
state.fail['PATCH /lists/1/members/alice%40example.com'] = { status: 500, error: 'locked' }
await user.click(within(row).getByRole('button', { name: /make publisher/i }))
expect(await screen.findByText('locked')).toBeInTheDocument()
expect(state.members[1][0].publisher).toBe(false)
})
})
describe('removing', () => {
async function open() {
const ctx = await setup([member('alice@example.com'), member('a+b@example.com')])
const row = (await screen.findByText('a+b@example.com')).closest('tr')!
await ctx.user.click(within(row).getByRole('button', { name: /remove/i }))
return { ...ctx, dialog: await screen.findByRole('alertdialog') }
}
it('confirms first, naming the address; cancel keeps the member', async () => {
const { user, state, dialog } = await open()
expect(dialog).toHaveTextContent('a+b@example.com')
await user.click(within(dialog).getByRole('button', { name: /cancel/i }))
await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument())
expect(state.members[1]).toHaveLength(2)
})
it('removes the member, encoding the address in the URL', async () => {
const { user, state, dialog } = await open()
await user.click(within(dialog).getByRole('button', { name: /^remove$/i }))
await waitFor(() => expect(screen.queryByText('a+b@example.com')).not.toBeInTheDocument())
expect(state.members[1].map((m) => m.address)).toEqual(['alice@example.com'])
expect(state.calls).toContain('DELETE /lists/1/members/a%2Bb%40example.com')
expect(screen.getByText('alice@example.com')).toBeInTheDocument()
})
it('shows a server error inside the dialog', async () => {
const { user, state, dialog } = await open()
state.fail['DELETE /lists/1/members/a%2Bb%40example.com'] = { status: 500, error: 'locked' }
await user.click(within(dialog).getByRole('button', { name: /^remove$/i }))
expect(await within(dialog).findByRole('alert')).toHaveTextContent('locked')
expect(state.members[1]).toHaveLength(2)
})
})
})