Skip to content

Commit b6fe40a

Browse files
committed
add subscription test
1 parent e36a955 commit b6fe40a

File tree

1 file changed

+284
-0
lines changed

1 file changed

+284
-0
lines changed

test/subscription_test.dart

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:onesignal_flutter/src/subscription.dart';
3+
4+
final pushSubState = {
5+
'id': 'test-id-123',
6+
'token': 'test-token-456',
7+
'optedIn': true,
8+
};
9+
10+
final pushChangeState = {
11+
'current': {
12+
'id': 'current-id',
13+
'token': 'current-token',
14+
'optedIn': true,
15+
},
16+
'previous': {
17+
'id': 'previous-id',
18+
'token': 'previous-token',
19+
'optedIn': false,
20+
},
21+
};
22+
23+
final pushNullChangeState = {
24+
'current': {'id': null, 'token': null, 'optedIn': false},
25+
'previous': {
26+
'id': 'previous-id',
27+
'token': 'previous-token',
28+
'optedIn': true,
29+
},
30+
};
31+
32+
void main() {
33+
TestWidgetsFlutterBinding.ensureInitialized();
34+
35+
group('OSPushSubscriptionState', () {
36+
group('constructor', () {
37+
test('initializes with all fields when provided', () {
38+
final state = OSPushSubscriptionState(pushSubState);
39+
40+
expect(state.id, 'test-id-123');
41+
expect(state.token, 'test-token-456');
42+
expect(state.optedIn, true);
43+
});
44+
45+
test('initializes with optedIn false', () {
46+
final state = OSPushSubscriptionState({
47+
...pushSubState,
48+
'optedIn': false,
49+
});
50+
51+
expect(state.id, 'test-id-123');
52+
expect(state.token, 'test-token-456');
53+
expect(state.optedIn, false);
54+
});
55+
56+
test('handles null/missing id', () {
57+
// with null id
58+
final stateWithNull = OSPushSubscriptionState({
59+
...pushSubState,
60+
'id': null,
61+
});
62+
expect(stateWithNull.id, isNull);
63+
expect(stateWithNull.token, 'test-token-456');
64+
expect(stateWithNull.optedIn, true);
65+
66+
// with missing id key
67+
final stateWithMissing = OSPushSubscriptionState({
68+
'token': 'test-token',
69+
'optedIn': true,
70+
});
71+
expect(stateWithMissing.id, isNull);
72+
expect(stateWithMissing.token, 'test-token');
73+
expect(stateWithMissing.optedIn, true);
74+
});
75+
76+
test('handles null/missing token', () {
77+
// with null token
78+
final stateWithNull = OSPushSubscriptionState({
79+
...pushSubState,
80+
'token': null,
81+
});
82+
expect(stateWithNull.id, 'test-id-123');
83+
expect(stateWithNull.token, isNull);
84+
expect(stateWithNull.optedIn, true);
85+
86+
// with missing token key
87+
final stateWithMissing = OSPushSubscriptionState({
88+
'id': 'test-id',
89+
'optedIn': false,
90+
});
91+
expect(stateWithMissing.id, 'test-id');
92+
expect(stateWithMissing.token, isNull);
93+
expect(stateWithMissing.optedIn, false);
94+
});
95+
96+
test('handles all null values', () {
97+
final state = OSPushSubscriptionState({
98+
'id': null,
99+
'token': null,
100+
'optedIn': false,
101+
});
102+
103+
expect(state.id, isNull);
104+
expect(state.token, isNull);
105+
expect(state.optedIn, false);
106+
});
107+
});
108+
109+
group('jsonRepresentation', () {
110+
test('returns json string with all fields', () {
111+
final state = OSPushSubscriptionState(pushSubState);
112+
final jsonString = state.jsonRepresentation();
113+
114+
expect(jsonString, contains('"id": "test-id-123"'));
115+
expect(jsonString, contains('"token": "test-token-456"'));
116+
expect(jsonString, contains('"optedIn": true'));
117+
});
118+
119+
test('returns json string with null fields', () {
120+
final state = OSPushSubscriptionState({
121+
...pushSubState,
122+
'id': null,
123+
'token': null,
124+
'optedIn': false,
125+
});
126+
final jsonString = state.jsonRepresentation();
127+
128+
expect(jsonString, contains('"id": null'));
129+
expect(jsonString, contains('"token": null'));
130+
expect(jsonString, contains('"optedIn": false'));
131+
});
132+
});
133+
134+
group('field modification', () {
135+
test('fields can be modified after construction', () {
136+
final state = OSPushSubscriptionState(pushSubState);
137+
138+
// Verify initial values
139+
expect(state.id, 'test-id-123');
140+
expect(state.token, 'test-token-456');
141+
expect(state.optedIn, true);
142+
143+
// Modify id
144+
state.id = 'new-id';
145+
expect(state.id, 'new-id');
146+
147+
// Modify token
148+
state.token = 'new-token';
149+
expect(state.token, 'new-token');
150+
151+
// Modify optedIn
152+
state.optedIn = false;
153+
expect(state.optedIn, false);
154+
});
155+
156+
test('modifications reflect in jsonRepresentation', () {
157+
final state = OSPushSubscriptionState(pushSubState);
158+
state.id = 'modified-id';
159+
state.token = 'modified-token';
160+
state.optedIn = true;
161+
162+
final jsonString = state.jsonRepresentation();
163+
164+
expect(jsonString, contains('"id": "modified-id"'));
165+
expect(jsonString, contains('"token": "modified-token"'));
166+
expect(jsonString, contains('"optedIn": true'));
167+
});
168+
});
169+
});
170+
171+
group('OSPushSubscriptionChangedState', () {
172+
group('constructor', () {
173+
test('initializes with current and previous states', () {
174+
final changedState = OSPushSubscriptionChangedState(pushChangeState);
175+
176+
expect(changedState.current.id, 'current-id');
177+
expect(changedState.current.token, 'current-token');
178+
expect(changedState.current.optedIn, true);
179+
180+
expect(changedState.previous.id, 'previous-id');
181+
expect(changedState.previous.token, 'previous-token');
182+
expect(changedState.previous.optedIn, false);
183+
});
184+
185+
test('handles null values in current state', () {
186+
final changedState =
187+
OSPushSubscriptionChangedState(pushNullChangeState);
188+
189+
expect(changedState.current.id, isNull);
190+
expect(changedState.current.token, isNull);
191+
expect(changedState.current.optedIn, false);
192+
193+
expect(changedState.previous.id, 'previous-id');
194+
expect(changedState.previous.token, 'previous-token');
195+
expect(changedState.previous.optedIn, true);
196+
});
197+
198+
test('handles null values in previous state', () {
199+
final changedState = OSPushSubscriptionChangedState({
200+
'current': {
201+
'id': 'current-id',
202+
'token': 'current-token',
203+
'optedIn': true,
204+
},
205+
'previous': {'id': null, 'token': null, 'optedIn': false},
206+
});
207+
208+
expect(changedState.current.id, 'current-id');
209+
expect(changedState.current.token, 'current-token');
210+
expect(changedState.current.optedIn, true);
211+
212+
expect(changedState.previous.id, isNull);
213+
expect(changedState.previous.token, isNull);
214+
expect(changedState.previous.optedIn, false);
215+
});
216+
});
217+
218+
group('jsonRepresentation', () {
219+
test('returns json string with current and previous states', () {
220+
final changedState = OSPushSubscriptionChangedState(pushChangeState);
221+
final jsonString = changedState.jsonRepresentation();
222+
223+
expect(jsonString, contains('"current":'));
224+
expect(jsonString, contains('"previous":'));
225+
expect(jsonString, contains('"id": "current-id"'));
226+
expect(jsonString, contains('"token": "current-token"'));
227+
expect(jsonString, contains('"optedIn": true'));
228+
expect(jsonString, contains('"id": "previous-id"'));
229+
expect(jsonString, contains('"token": "previous-token"'));
230+
expect(jsonString, contains('"optedIn": false'));
231+
});
232+
233+
test('handles null values in json representation', () {
234+
final changedState =
235+
OSPushSubscriptionChangedState(pushNullChangeState);
236+
final jsonString = changedState.jsonRepresentation();
237+
238+
expect(jsonString, contains('"current"'));
239+
expect(jsonString, contains('"previous"'));
240+
expect(jsonString, contains('"id": null'));
241+
expect(jsonString, contains('"token": null'));
242+
expect(jsonString, contains('"optedIn": false'));
243+
expect(jsonString, contains('"id": "previous-id"'));
244+
expect(jsonString, contains('"token": "previous-token"'));
245+
expect(jsonString, contains('"optedIn": true'));
246+
});
247+
});
248+
249+
group('state modification', () {
250+
test('current state can be modified', () {
251+
final changedState = OSPushSubscriptionChangedState(pushChangeState);
252+
changedState.current.id = 'modified-id';
253+
changedState.current.token = 'modified-token';
254+
changedState.current.optedIn = false;
255+
256+
expect(changedState.current.id, 'modified-id');
257+
expect(changedState.current.token, 'modified-token');
258+
expect(changedState.current.optedIn, false);
259+
});
260+
261+
test('previous state can be modified', () {
262+
final changedState = OSPushSubscriptionChangedState(pushChangeState);
263+
changedState.previous.id = 'modified-id';
264+
changedState.previous.token = 'modified-token';
265+
changedState.previous.optedIn = true;
266+
267+
expect(changedState.previous.id, 'modified-id');
268+
expect(changedState.previous.token, 'modified-token');
269+
expect(changedState.previous.optedIn, true);
270+
});
271+
272+
test('modifications to states reflect in jsonRepresentation', () {
273+
final changedState = OSPushSubscriptionChangedState(pushChangeState);
274+
changedState.current.id = 'new-current-id';
275+
changedState.previous.id = 'new-previous-id';
276+
277+
final jsonString = changedState.jsonRepresentation();
278+
279+
expect(jsonString, contains('"id": "new-current-id"'));
280+
expect(jsonString, contains('"id": "new-previous-id"'));
281+
});
282+
});
283+
});
284+
}

0 commit comments

Comments
 (0)