Skip to content

Commit 8bd6d95

Browse files
committed
test(fields): pin the notification-topic/channel fillable arm
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibzyvUbUU6pC3x3Amrx
1 parent 79a9c4c commit 8bd6d95

1 file changed

Lines changed: 218 additions & 0 deletions

File tree

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* `notification-topic` / `notification-channel` — the fillable arm of
11+
* `RegistryRefField` (#30208 row 6, ruling E on #30527).
12+
*
13+
* A notification preference's topic and channel are free text the server
14+
* stores as written; the preference options door only SUGGESTS values. These
15+
* pins hold what the arm promises:
16+
*
17+
* - the list renders from the door, in its order, `*` first;
18+
* - a failed or empty answer gives the plain text box, with no notice;
19+
* - a value outside the list is committed as typed and shown marked
20+
* "not in the list";
21+
* - a channel the door marks not configured is listed, labelled, selectable.
22+
*
23+
* The door is reached through the host's fetch on `SchemaRendererContext`,
24+
* stubbed here with the door's own success envelope.
25+
*/
26+
import { describe, it, expect, vi, afterEach } from 'vitest';
27+
import React from 'react';
28+
import { render, screen, fireEvent, cleanup, within } from '@testing-library/react';
29+
import '@testing-library/jest-dom';
30+
import { SchemaRendererProvider } from '@object-ui/react';
31+
import { NotificationTopicField, NotificationChannelField } from '../RegistryRefField';
32+
import { PREFERENCE_OPTIONS_URL } from '../preferenceOptions';
33+
34+
const ANSWER = {
35+
success: true,
36+
data: {
37+
topics: [
38+
{ value: '*', wildcard: true, sources: [] },
39+
{ value: 'approval.requested', wildcard: false, sources: ['platform', 'stored'] },
40+
{ value: 'invoice.paid', wildcard: false, sources: ['flow'] },
41+
],
42+
channels: [
43+
{ value: '*', wildcard: true, configured: true },
44+
{ value: 'inbox', wildcard: false, configured: true },
45+
{ value: 'push', wildcard: false, configured: false },
46+
],
47+
},
48+
};
49+
50+
function door(answer: () => Promise<Response>) {
51+
return vi.fn((_input: RequestInfo | URL, _init?: RequestInit) => answer());
52+
}
53+
54+
const ok = (body: unknown) => async () => new Response(JSON.stringify(body), { status: 200 });
55+
56+
function mount(
57+
Widget: typeof NotificationTopicField,
58+
{
59+
apiFetch,
60+
value = '',
61+
onChange = vi.fn(),
62+
readonly = false,
63+
}: { apiFetch: ReturnType<typeof door>; value?: string; onChange?: any; readonly?: boolean },
64+
) {
65+
render(
66+
<SchemaRendererProvider dataSource={null} apiFetch={apiFetch}>
67+
<Widget
68+
value={value}
69+
onChange={onChange}
70+
readonly={readonly}
71+
field={{ name: 'topic', type: 'text', label: 'Topic' } as any}
72+
id="f-topic"
73+
/>
74+
</SchemaRendererProvider>,
75+
);
76+
return { onChange };
77+
}
78+
79+
function optionTexts(): string[] {
80+
return within(screen.getByRole('listbox'))
81+
.getAllByRole('option')
82+
.map((option) => option.textContent ?? '');
83+
}
84+
85+
afterEach(() => cleanup());
86+
87+
describe('notification-topic — the list renders from the door (#30208 row 6)', () => {
88+
it('reads the preference options door through the host fetch and lists its topics, `*` first', async () => {
89+
const apiFetch = door(ok(ANSWER));
90+
mount(NotificationTopicField, { apiFetch });
91+
92+
fireEvent.click(await screen.findByTestId('preference-option-topics'));
93+
expect(apiFetch).toHaveBeenCalledTimes(1);
94+
expect(String(apiFetch.mock.calls[0][0])).toBe(PREFERENCE_OPTIONS_URL);
95+
// The door's order, kept: the wildcard leads, by its words and its value.
96+
expect(optionTexts()).toEqual(['All topics (*)', 'approval.requested', 'invoice.paid']);
97+
});
98+
99+
it('stores the picked value, the wildcard included', async () => {
100+
const apiFetch = door(ok(ANSWER));
101+
const { onChange } = mount(NotificationTopicField, { apiFetch });
102+
103+
fireEvent.click(await screen.findByTestId('preference-option-topics'));
104+
fireEvent.click(screen.getByText('All topics (*)'));
105+
expect(onChange).toHaveBeenCalledWith('*');
106+
});
107+
108+
it('shows a stored listed value by its own face, unmarked', async () => {
109+
const apiFetch = door(ok(ANSWER));
110+
mount(NotificationTopicField, { apiFetch, value: 'invoice.paid' });
111+
112+
const trigger = await screen.findByTestId('preference-option-topics');
113+
expect(trigger).toHaveTextContent('invoice.paid');
114+
expect(trigger).not.toHaveAttribute('data-not-in-list');
115+
});
116+
117+
it('a topic field and a channel field share one read of the door', async () => {
118+
const apiFetch = door(ok(ANSWER));
119+
render(
120+
<SchemaRendererProvider dataSource={null} apiFetch={apiFetch}>
121+
<NotificationTopicField value="" onChange={vi.fn()} field={{ name: 'topic' } as any} />
122+
<NotificationChannelField value="" onChange={vi.fn()} field={{ name: 'channel' } as any} />
123+
</SchemaRendererProvider>,
124+
);
125+
expect(await screen.findByTestId('preference-option-topics')).toBeInTheDocument();
126+
expect(await screen.findByTestId('preference-option-channels')).toBeInTheDocument();
127+
expect(apiFetch).toHaveBeenCalledTimes(1);
128+
});
129+
});
130+
131+
describe('notification-topic — the plain text box when there is no list (#30208 row 6)', () => {
132+
it('a failed read gives the text box, with no notice, writing exactly what is typed', async () => {
133+
const apiFetch = door(async () => new Response('{}', { status: 500 }));
134+
const { onChange } = mount(NotificationTopicField, { apiFetch, value: 'approval.requested' });
135+
136+
const box = await screen.findByRole('textbox');
137+
await vi.waitFor(() => expect(box).toBeEnabled());
138+
expect(box).toHaveValue('approval.requested');
139+
expect(screen.queryByRole('status')).toBeNull();
140+
expect(screen.queryByTestId('preference-option-topics')).toBeNull();
141+
142+
fireEvent.change(box, { target: { value: 'invoices.paid' } });
143+
expect(onChange).toHaveBeenCalledWith('invoices.paid');
144+
});
145+
146+
it('a rejected fetch gives the same text box', async () => {
147+
const apiFetch = door(async () => {
148+
throw new TypeError('Failed to fetch');
149+
});
150+
mount(NotificationTopicField, { apiFetch });
151+
152+
const box = await screen.findByRole('textbox');
153+
await vi.waitFor(() => expect(box).toBeEnabled());
154+
expect(screen.queryByRole('status')).toBeNull();
155+
});
156+
157+
it('an empty list gives the same text box', async () => {
158+
const apiFetch = door(ok({ success: true, data: { topics: [], channels: [] } }));
159+
mount(NotificationTopicField, { apiFetch });
160+
161+
const box = await screen.findByRole('textbox');
162+
await vi.waitFor(() => expect(box).toBeEnabled());
163+
expect(screen.queryByTestId('preference-option-topics')).toBeNull();
164+
});
165+
});
166+
167+
describe('notification-topic — a value outside the list (#30208 row 6)', () => {
168+
it('commits the typed text as written', async () => {
169+
const apiFetch = door(ok(ANSWER));
170+
const { onChange } = mount(NotificationTopicField, { apiFetch });
171+
172+
fireEvent.click(await screen.findByTestId('preference-option-topics'));
173+
fireEvent.change(screen.getByTestId('preference-option-topics-search'), {
174+
target: { value: 'Invoices.Paid' },
175+
});
176+
fireEvent.click(screen.getByTestId('preference-option-topics-use-typed'));
177+
178+
expect(onChange).toHaveBeenCalledTimes(1);
179+
expect(onChange).toHaveBeenCalledWith('Invoices.Paid');
180+
});
181+
182+
it('keeps a stored value the list lacks, marked "not in the list", and writes nothing', async () => {
183+
const apiFetch = door(ok(ANSWER));
184+
const { onChange } = mount(NotificationTopicField, { apiFetch, value: 'invoices.paid' });
185+
186+
const trigger = await screen.findByTestId('preference-option-topics');
187+
expect(trigger).toHaveTextContent('invoices.paid (not in the list)');
188+
expect(trigger).toHaveAttribute('data-not-in-list', 'true');
189+
expect(onChange).not.toHaveBeenCalled();
190+
191+
// It stays an option of its own, ahead of the listed values.
192+
fireEvent.click(trigger);
193+
expect(optionTexts()[0]).toBe('invoices.paid (not in the list)');
194+
});
195+
196+
it('marks it on the read face too', async () => {
197+
const apiFetch = door(ok(ANSWER));
198+
mount(NotificationTopicField, { apiFetch, value: 'invoices.paid', readonly: true });
199+
200+
expect(await screen.findByText('invoices.paid (not in the list)')).toHaveAttribute('data-not-in-list', 'true');
201+
});
202+
});
203+
204+
describe('notification-channel — a channel that is not configured (#30208 row 6)', () => {
205+
it('is listed, labelled not configured, and selectable', async () => {
206+
const apiFetch = door(ok(ANSWER));
207+
const { onChange } = mount(NotificationChannelField, { apiFetch });
208+
209+
fireEvent.click(await screen.findByTestId('preference-option-channels'));
210+
expect(optionTexts()).toEqual(['All channels (*)', 'inbox', 'push (not configured)']);
211+
212+
const push = screen.getByText('push (not configured)').closest('[role="option"]')!;
213+
expect(push).toHaveAttribute('data-not-configured', 'true');
214+
expect(push).not.toHaveAttribute('aria-disabled', 'true');
215+
fireEvent.click(push);
216+
expect(onChange).toHaveBeenCalledWith('push');
217+
});
218+
});

0 commit comments

Comments
 (0)