Skip to content

Commit f7d972d

Browse files
committed
test: add tests for DashbotActiveRouteNotifier(cv: 100)
1 parent dc64784 commit f7d972d

File tree

1 file changed

+327
-0
lines changed

1 file changed

+327
-0
lines changed
Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:flutter_riverpod/flutter_riverpod.dart';
3+
4+
import 'package:apidash/dashbot/core/providers/dashbot_active_route_provider.dart';
5+
import 'package:apidash/dashbot/core/routes/dashbot_routes.dart';
6+
import 'package:apidash/models/models.dart';
7+
import 'package:apidash/providers/collection_providers.dart';
8+
import 'package:apidash_core/apidash_core.dart';
9+
10+
import '../../../providers/helpers.dart';
11+
12+
void main() {
13+
TestWidgetsFlutterBinding.ensureInitialized();
14+
15+
group('DashbotActiveRouteNotifier', () {
16+
test('initial state computes base route from null request', () {
17+
final container = createContainer(
18+
overrides: [
19+
selectedRequestModelProvider.overrideWith((ref) => null),
20+
],
21+
);
22+
23+
final route = container.read(dashbotActiveRouteProvider);
24+
expect(route, DashbotRoutes.dashbotDefault); // null request -> default
25+
});
26+
27+
test('initial state computes base route from request without response', () {
28+
const request = RequestModel(
29+
id: '1',
30+
name: 'Test Request',
31+
httpRequestModel: HttpRequestModel(
32+
method: HTTPVerb.get,
33+
url: 'https://api.example.com/test',
34+
),
35+
);
36+
37+
final container = createContainer(
38+
overrides: [
39+
selectedRequestModelProvider.overrideWith((ref) => request),
40+
],
41+
);
42+
43+
final route = container.read(dashbotActiveRouteProvider);
44+
expect(route, DashbotRoutes.dashbotDefault); // no response -> default
45+
});
46+
47+
test('initial state computes base route from request with response', () {
48+
const request = RequestModel(
49+
id: '1',
50+
name: 'Test Request',
51+
httpRequestModel: HttpRequestModel(
52+
method: HTTPVerb.get,
53+
url: 'https://api.example.com/test',
54+
),
55+
httpResponseModel: HttpResponseModel(
56+
statusCode: 200,
57+
headers: {},
58+
body: 'OK',
59+
),
60+
);
61+
62+
final container = createContainer(
63+
overrides: [
64+
selectedRequestModelProvider.overrideWith((ref) => request),
65+
],
66+
);
67+
68+
final route = container.read(dashbotActiveRouteProvider);
69+
expect(route, DashbotRoutes.dashbotHome); // has response -> home
70+
});
71+
72+
test('goToChat() sets route to chat and pins it', () {
73+
final container = createContainer(
74+
overrides: [
75+
selectedRequestModelProvider.overrideWith((ref) => null),
76+
],
77+
);
78+
79+
final notifier = container.read(dashbotActiveRouteProvider.notifier);
80+
81+
// Initially default route
82+
expect(container.read(dashbotActiveRouteProvider),
83+
DashbotRoutes.dashbotDefault);
84+
85+
// Go to chat
86+
notifier.goToChat();
87+
expect(container.read(dashbotActiveRouteProvider),
88+
DashbotRoutes.dashbotChat);
89+
});
90+
91+
test('goToChat() does nothing when already on chat route', () {
92+
final container = createContainer(
93+
overrides: [
94+
selectedRequestModelProvider.overrideWith((ref) => null),
95+
],
96+
);
97+
98+
final notifier = container.read(dashbotActiveRouteProvider.notifier);
99+
100+
// First go to chat
101+
notifier.goToChat();
102+
expect(container.read(dashbotActiveRouteProvider),
103+
DashbotRoutes.dashbotChat);
104+
105+
// Call again - should return early
106+
final stateBefore = container.read(dashbotActiveRouteProvider);
107+
notifier.goToChat();
108+
final stateAfter = container.read(dashbotActiveRouteProvider);
109+
110+
expect(stateAfter, stateBefore); // No change
111+
expect(stateAfter, DashbotRoutes.dashbotChat);
112+
});
113+
114+
test('chat pinned state persists when request changes', () {
115+
const requestWithoutResponse = RequestModel(
116+
id: '1',
117+
name: 'Test Request',
118+
httpRequestModel: HttpRequestModel(
119+
method: HTTPVerb.get,
120+
url: 'https://api.example.com/test',
121+
),
122+
);
123+
124+
const requestWithResponse = RequestModel(
125+
id: '2',
126+
name: 'Test Request 2',
127+
httpRequestModel: HttpRequestModel(
128+
method: HTTPVerb.get,
129+
url: 'https://api.example.com/test2',
130+
),
131+
httpResponseModel: HttpResponseModel(
132+
statusCode: 200,
133+
headers: {},
134+
body: 'OK',
135+
),
136+
);
137+
138+
final requestProvider = StateProvider<RequestModel?>((ref) => null);
139+
140+
final container = createContainer(
141+
overrides: [
142+
selectedRequestModelProvider
143+
.overrideWith((ref) => ref.watch(requestProvider)),
144+
],
145+
);
146+
147+
final notifier = container.read(dashbotActiveRouteProvider.notifier);
148+
149+
// Start with request without response
150+
container.read(requestProvider.notifier).state = requestWithoutResponse;
151+
expect(container.read(dashbotActiveRouteProvider),
152+
DashbotRoutes.dashbotDefault);
153+
154+
// Pin to chat
155+
notifier.goToChat();
156+
expect(container.read(dashbotActiveRouteProvider),
157+
DashbotRoutes.dashbotChat);
158+
159+
// Change to request with response - should stay pinned to chat
160+
container.read(requestProvider.notifier).state = requestWithResponse;
161+
expect(container.read(dashbotActiveRouteProvider),
162+
DashbotRoutes.dashbotChat);
163+
});
164+
165+
test('resetToBaseRoute() unpins chat and recalculates from current request',
166+
() {
167+
const requestWithResponse = RequestModel(
168+
id: '1',
169+
name: 'Test Request',
170+
httpRequestModel: HttpRequestModel(
171+
method: HTTPVerb.get,
172+
url: 'https://api.example.com/test',
173+
),
174+
httpResponseModel: HttpResponseModel(
175+
statusCode: 200,
176+
headers: {},
177+
body: 'OK',
178+
),
179+
);
180+
181+
final container = createContainer(
182+
overrides: [
183+
selectedRequestModelProvider
184+
.overrideWith((ref) => requestWithResponse),
185+
],
186+
);
187+
188+
final notifier = container.read(dashbotActiveRouteProvider.notifier);
189+
190+
// Initially home route (has response)
191+
expect(container.read(dashbotActiveRouteProvider),
192+
DashbotRoutes.dashbotHome);
193+
194+
// Pin to chat
195+
notifier.goToChat();
196+
expect(container.read(dashbotActiveRouteProvider),
197+
DashbotRoutes.dashbotChat);
198+
199+
// Reset to base route
200+
notifier.resetToBaseRoute();
201+
expect(container.read(dashbotActiveRouteProvider),
202+
DashbotRoutes.dashbotHome);
203+
});
204+
205+
test('resetToBaseRoute() with null request returns default route', () {
206+
final container = createContainer(
207+
overrides: [
208+
selectedRequestModelProvider.overrideWith((ref) => null),
209+
],
210+
);
211+
212+
final notifier = container.read(dashbotActiveRouteProvider.notifier);
213+
214+
// Pin to chat
215+
notifier.goToChat();
216+
expect(container.read(dashbotActiveRouteProvider),
217+
DashbotRoutes.dashbotChat);
218+
219+
// Reset to base route
220+
notifier.resetToBaseRoute();
221+
expect(container.read(dashbotActiveRouteProvider),
222+
DashbotRoutes.dashbotDefault);
223+
});
224+
225+
test('setRoute() to chat delegates to goToChat()', () {
226+
final container = createContainer(
227+
overrides: [
228+
selectedRequestModelProvider.overrideWith((ref) => null),
229+
],
230+
);
231+
232+
final notifier = container.read(dashbotActiveRouteProvider.notifier);
233+
234+
// Initially default route
235+
expect(container.read(dashbotActiveRouteProvider),
236+
DashbotRoutes.dashbotDefault);
237+
238+
// Set route to chat - should delegate to goToChat
239+
notifier.setRoute(DashbotRoutes.dashbotChat);
240+
expect(container.read(dashbotActiveRouteProvider),
241+
DashbotRoutes.dashbotChat);
242+
});
243+
244+
test('setRoute() to non-chat route unpins chat and sets route', () {
245+
final container = createContainer(
246+
overrides: [
247+
selectedRequestModelProvider.overrideWith((ref) => null),
248+
],
249+
);
250+
251+
final notifier = container.read(dashbotActiveRouteProvider.notifier);
252+
253+
// Pin to chat first
254+
notifier.goToChat();
255+
expect(container.read(dashbotActiveRouteProvider),
256+
DashbotRoutes.dashbotChat);
257+
258+
// Set route to home - should unpin and set directly
259+
notifier.setRoute(DashbotRoutes.dashbotHome);
260+
expect(container.read(dashbotActiveRouteProvider),
261+
DashbotRoutes.dashbotHome);
262+
});
263+
264+
test('setRoute() to custom route works', () {
265+
final container = createContainer(
266+
overrides: [
267+
selectedRequestModelProvider.overrideWith((ref) => null),
268+
],
269+
);
270+
271+
final notifier = container.read(dashbotActiveRouteProvider.notifier);
272+
273+
// Set custom route
274+
notifier.setRoute('/custom-route');
275+
expect(container.read(dashbotActiveRouteProvider), '/custom-route');
276+
});
277+
278+
test('request changes are reflected when chat is not pinned', () {
279+
const requestWithoutResponse = RequestModel(
280+
id: '1',
281+
name: 'Test Request',
282+
httpRequestModel: HttpRequestModel(
283+
method: HTTPVerb.get,
284+
url: 'https://api.example.com/test',
285+
),
286+
);
287+
288+
const requestWithResponse = RequestModel(
289+
id: '2',
290+
name: 'Test Request 2',
291+
httpRequestModel: HttpRequestModel(
292+
method: HTTPVerb.get,
293+
url: 'https://api.example.com/test2',
294+
),
295+
httpResponseModel: HttpResponseModel(
296+
statusCode: 200,
297+
headers: {},
298+
body: 'OK',
299+
),
300+
);
301+
302+
final requestProvider =
303+
StateProvider<RequestModel?>((ref) => requestWithoutResponse);
304+
305+
final container = createContainer(
306+
overrides: [
307+
selectedRequestModelProvider
308+
.overrideWith((ref) => ref.watch(requestProvider)),
309+
],
310+
);
311+
312+
// Initially default route (no response)
313+
expect(container.read(dashbotActiveRouteProvider),
314+
DashbotRoutes.dashbotDefault);
315+
316+
// Change to request with response
317+
container.read(requestProvider.notifier).state = requestWithResponse;
318+
expect(container.read(dashbotActiveRouteProvider),
319+
DashbotRoutes.dashbotHome);
320+
321+
// Change back to request without response
322+
container.read(requestProvider.notifier).state = requestWithoutResponse;
323+
expect(container.read(dashbotActiveRouteProvider),
324+
DashbotRoutes.dashbotDefault);
325+
});
326+
});
327+
}

0 commit comments

Comments
 (0)