Skip to content

Commit 21058af

Browse files
committed
chore: add tests for proxy feature
1 parent 239b75c commit 21058af

File tree

3 files changed

+214
-1
lines changed

3 files changed

+214
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import 'package:test/test.dart';
2+
import 'package:apidash_core/models/proxy_settings_model.dart';
3+
4+
void main() {
5+
const proxySettings = ProxySettings(
6+
host: 'localhost',
7+
port: '8080',
8+
username: 'user',
9+
password: 'pass',
10+
);
11+
12+
test('Testing toJson()', () {
13+
const expectedResult = {
14+
'host': 'localhost',
15+
'port': '8080',
16+
'username': 'user',
17+
'password': 'pass',
18+
};
19+
expect(proxySettings.toJson(), expectedResult);
20+
});
21+
22+
test('Testing fromJson()', () {
23+
const input = {
24+
'host': 'localhost',
25+
'port': '8080',
26+
'username': 'user',
27+
'password': 'pass',
28+
};
29+
expect(ProxySettings.fromJson(input), proxySettings);
30+
});
31+
32+
test('Testing default values', () {
33+
const defaultSettings = ProxySettings();
34+
expect(defaultSettings.host, '');
35+
expect(defaultSettings.port, '');
36+
expect(defaultSettings.username, null);
37+
expect(defaultSettings.password, null);
38+
});
39+
40+
test('Testing equality', () {
41+
const settings1 = ProxySettings(
42+
host: 'localhost',
43+
port: '8080',
44+
username: 'user',
45+
password: 'pass',
46+
);
47+
const settings2 = ProxySettings(
48+
host: 'localhost',
49+
port: '8080',
50+
username: 'user',
51+
password: 'pass',
52+
);
53+
expect(settings1, settings2);
54+
});
55+
}

test/models/settings_model_test.dart

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import 'package:apidash/models/settings_model.dart';
55
import 'package:apidash/consts.dart';
66

77
void main() {
8+
const proxySettings = ProxySettings(
9+
host: 'localhost',
10+
port: '8080',
11+
username: 'user',
12+
password: 'pass',
13+
);
14+
815
const sm = SettingsModel(
916
isDark: false,
1017
alwaysShowCollectionPaneScrollbar: true,
@@ -18,6 +25,7 @@ void main() {
1825
historyRetentionPeriod: HistoryRetentionPeriod.oneWeek,
1926
workspaceFolderPath: null,
2027
isSSLDisabled: true,
28+
proxySettings: proxySettings,
2129
);
2230

2331
test('Testing toJson()', () {
@@ -36,6 +44,12 @@ void main() {
3644
"historyRetentionPeriod": "oneWeek",
3745
"workspaceFolderPath": null,
3846
"isSSLDisabled": true,
47+
"proxySettings": {
48+
"host": "localhost",
49+
"port": "8080",
50+
"username": "user",
51+
"password": "pass",
52+
},
3953
};
4054
expect(sm.toJson(), expectedResult);
4155
});
@@ -56,6 +70,12 @@ void main() {
5670
"historyRetentionPeriod": "oneWeek",
5771
"workspaceFolderPath": null,
5872
"isSSLDisabled": true,
73+
"proxySettings": {
74+
"host": "localhost",
75+
"port": "8080",
76+
"username": "user",
77+
"password": "pass",
78+
},
5979
};
6080
expect(SettingsModel.fromJson(input), sm);
6181
});
@@ -73,12 +93,14 @@ void main() {
7393
activeEnvironmentId: null,
7494
historyRetentionPeriod: HistoryRetentionPeriod.oneWeek,
7595
isSSLDisabled: false,
96+
proxySettings: null,
7697
);
7798
expect(
7899
sm.copyWith(
79100
isDark: true,
80101
saveResponses: false,
81102
isSSLDisabled: false,
103+
proxySettings: null,
82104
),
83105
expectedResult);
84106
});
@@ -98,11 +120,31 @@ void main() {
98120
"activeEnvironmentId": null,
99121
"historyRetentionPeriod": "oneWeek",
100122
"workspaceFolderPath": null,
101-
"isSSLDisabled": true
123+
"isSSLDisabled": true,
124+
"proxySettings": {
125+
"host": "localhost",
126+
"port": "8080",
127+
"username": "user",
128+
"password": "pass"
129+
}
102130
}''';
103131
expect(sm.toString(), expectedResult);
104132
});
105133

134+
test('Testing proxy settings update', () {
135+
const newProxySettings = ProxySettings(
136+
host: 'proxy.example.com',
137+
port: '3128',
138+
);
139+
final updatedSettings = sm.copyWith(proxySettings: newProxySettings);
140+
expect(updatedSettings.proxySettings, newProxySettings);
141+
});
142+
143+
test('Testing proxy settings disable', () {
144+
final disabledSettings = sm.copyWith(proxySettings: null);
145+
expect(disabledSettings.proxySettings, null);
146+
});
147+
106148
test('Testing hashcode', () {
107149
expect(sm.hashCode, greaterThan(0));
108150
});
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:flutter_riverpod/flutter_riverpod.dart';
4+
import 'package:apidash/widgets/proxy_settings_dialog.dart';
5+
6+
void main() {
7+
testWidgets('ProxySettingsDialog shows correctly', (WidgetTester tester) async {
8+
await tester.pumpWidget(
9+
ProviderScope(
10+
child: MaterialApp(
11+
home: Scaffold(
12+
body: Builder(
13+
builder: (context) => TextButton(
14+
onPressed: () {
15+
showDialog(
16+
context: context,
17+
builder: (context) => const ProxySettingsDialog(),
18+
);
19+
},
20+
child: const Text('Show Dialog'),
21+
),
22+
),
23+
),
24+
),
25+
),
26+
);
27+
28+
// Open dialog
29+
await tester.tap(find.text('Show Dialog'));
30+
await tester.pumpAndSettle();
31+
32+
// Verify dialog is shown
33+
expect(find.byType(ProxySettingsDialog), findsOneWidget);
34+
expect(find.text('Proxy Settings'), findsOneWidget);
35+
expect(find.text('Proxy Host'), findsOneWidget);
36+
expect(find.text('Proxy Port'), findsOneWidget);
37+
expect(find.text('Username (Optional)'), findsOneWidget);
38+
expect(find.text('Password (Optional)'), findsOneWidget);
39+
});
40+
41+
testWidgets('ProxySettingsDialog saves settings', (WidgetTester tester) async {
42+
await tester.pumpWidget(
43+
ProviderScope(
44+
child: MaterialApp(
45+
home: Scaffold(
46+
body: Builder(
47+
builder: (context) => TextButton(
48+
onPressed: () {
49+
showDialog(
50+
context: context,
51+
builder: (context) => const ProxySettingsDialog(),
52+
);
53+
},
54+
child: const Text('Show Dialog'),
55+
),
56+
),
57+
),
58+
),
59+
),
60+
);
61+
62+
// Open dialog
63+
await tester.tap(find.text('Show Dialog'));
64+
await tester.pumpAndSettle();
65+
66+
// Enter proxy settings
67+
await tester.enterText(find.widgetWithText(TextField, 'Proxy Host'), 'localhost');
68+
await tester.enterText(find.widgetWithText(TextField, 'Proxy Port'), '8080');
69+
await tester.enterText(find.widgetWithText(TextField, 'Username (Optional)'), 'user');
70+
await tester.enterText(find.widgetWithText(TextField, 'Password (Optional)'), 'pass');
71+
72+
// Save settings
73+
await tester.tap(find.text('Save'));
74+
await tester.pumpAndSettle();
75+
76+
// Verify dialog is closed
77+
expect(find.byType(ProxySettingsDialog), findsNothing);
78+
});
79+
80+
testWidgets('ProxySettingsDialog cancels without saving', (WidgetTester tester) async {
81+
await tester.pumpWidget(
82+
ProviderScope(
83+
child: MaterialApp(
84+
home: Scaffold(
85+
body: Builder(
86+
builder: (context) => TextButton(
87+
onPressed: () {
88+
showDialog(
89+
context: context,
90+
builder: (context) => const ProxySettingsDialog(),
91+
);
92+
},
93+
child: const Text('Show Dialog'),
94+
),
95+
),
96+
),
97+
),
98+
),
99+
);
100+
101+
// Open dialog
102+
await tester.tap(find.text('Show Dialog'));
103+
await tester.pumpAndSettle();
104+
105+
// Enter proxy settings
106+
await tester.enterText(find.widgetWithText(TextField, 'Proxy Host'), 'localhost');
107+
await tester.enterText(find.widgetWithText(TextField, 'Proxy Port'), '8080');
108+
109+
// Cancel without saving
110+
await tester.tap(find.text('Cancel'));
111+
await tester.pumpAndSettle();
112+
113+
// Verify dialog is closed
114+
expect(find.byType(ProxySettingsDialog), findsNothing);
115+
});
116+
}

0 commit comments

Comments
 (0)