@@ -166,6 +166,84 @@ void main() {
166
166
// TODO test database gets updated correctly (an integration test with sqlite?)
167
167
});
168
168
169
+ group ('GlobalStore.removeAccount' , () {
170
+ void checkGlobalStore (GlobalStore store, int accountId, {
171
+ required bool expectAccount,
172
+ required bool expectStore,
173
+ }) {
174
+ expectAccount
175
+ ? check (store.getAccount (accountId)).isNotNull ()
176
+ : check (store.getAccount (accountId)).isNull ();
177
+ expectStore
178
+ ? check (store.perAccountSync (accountId)).isNotNull ()
179
+ : check (store.perAccountSync (accountId)).isNull ();
180
+ }
181
+
182
+ test ('when store loaded' , () async {
183
+ final globalStore = eg.globalStore ();
184
+ await globalStore.add (eg.selfAccount, eg.initialSnapshot ());
185
+ await globalStore.perAccount (eg.selfAccount.id);
186
+
187
+ checkGlobalStore (globalStore, eg.selfAccount.id,
188
+ expectAccount: true , expectStore: true );
189
+ int notifyCount = 0 ;
190
+ globalStore.addListener (() => notifyCount++ );
191
+
192
+ await globalStore.removeAccount (eg.selfAccount.id);
193
+
194
+ // TODO test that the removed store got disposed and its connection closed
195
+ checkGlobalStore (globalStore, eg.selfAccount.id,
196
+ expectAccount: false , expectStore: false );
197
+ check (notifyCount).equals (1 );
198
+ });
199
+
200
+ test ('when store not loaded' , () async {
201
+ final globalStore = eg.globalStore ();
202
+ await globalStore.add (eg.selfAccount, eg.initialSnapshot ());
203
+
204
+ checkGlobalStore (globalStore, eg.selfAccount.id,
205
+ expectAccount: true , expectStore: false );
206
+ int notifyCount = 0 ;
207
+ globalStore.addListener (() => notifyCount++ );
208
+
209
+ await globalStore.removeAccount (eg.selfAccount.id);
210
+
211
+ checkGlobalStore (globalStore, eg.selfAccount.id,
212
+ expectAccount: false , expectStore: false );
213
+ check (notifyCount).equals (1 );
214
+ });
215
+
216
+ test ('when store loading' , () async {
217
+ final globalStore = LoadingTestGlobalStore (accounts: [eg.selfAccount]);
218
+ checkGlobalStore (globalStore, eg.selfAccount.id,
219
+ expectAccount: true , expectStore: false );
220
+
221
+ // don't await; we'll complete/await it manually after removeAccount
222
+ final loadingFuture = globalStore.perAccount (eg.selfAccount.id);
223
+
224
+ checkGlobalStore (globalStore, eg.selfAccount.id,
225
+ expectAccount: true , expectStore: false );
226
+ int notifyCount = 0 ;
227
+ globalStore.addListener (() => notifyCount++ );
228
+
229
+ await globalStore.removeAccount (eg.selfAccount.id);
230
+
231
+ checkGlobalStore (globalStore, eg.selfAccount.id,
232
+ expectAccount: false , expectStore: false );
233
+ check (notifyCount).equals (1 );
234
+
235
+ globalStore.completers[eg.selfAccount.id]! .single
236
+ .complete (eg.store (account: eg.selfAccount, initialSnapshot: eg.initialSnapshot ()));
237
+ // TODO test that the never-used store got disposed and its connection closed
238
+ await check (loadingFuture).throws <AccountNotFoundException >();
239
+ checkGlobalStore (globalStore, eg.selfAccount.id,
240
+ expectAccount: false , expectStore: false );
241
+ check (notifyCount).equals (1 ); // no extra notify
242
+
243
+ check (globalStore.debugNumPerAccountStoresLoading).equals (0 );
244
+ });
245
+ });
246
+
169
247
group ('PerAccountStore.handleEvent' , () {
170
248
// Mostly this method just dispatches to ChannelStore and MessageStore etc.,
171
249
// and so most of the tests live in the test files for those
0 commit comments