|
| 1 | +// Copyright 2021 Workiva Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import 'dart:async'; |
| 16 | + |
| 17 | +import 'package:over_react/src/over_react_redux/over_react_flux.dart'; |
| 18 | +import 'package:test/test.dart'; |
| 19 | +import 'package:w_flux/w_flux.dart' as flux; |
| 20 | + |
| 21 | +main() { |
| 22 | + // See connect_flux_integration_test.dart for more test coverage of this class |
| 23 | + group('ConnectFluxAdapterStore', () { |
| 24 | + group('teardown', () { |
| 25 | + test('calls super, closing change subscriptions', () async { |
| 26 | + final adapterStore = SimpleStore().asConnectFluxStore(null); |
| 27 | + expect(() => adapterStore.onChange.listen((_) {}), returnsNormally, |
| 28 | + reason: 'test setup check: the stream should be open'); |
| 29 | + |
| 30 | + await adapterStore.teardown(); |
| 31 | + }); |
| 32 | + |
| 33 | + test('stops listening to the flux store', () async { |
| 34 | + final store = SpyStore(); |
| 35 | + final adapterStore = store.asConnectFluxStore(null); |
| 36 | + |
| 37 | + expect(store.spiedSubscriptions, hasLength(1)); |
| 38 | + // ignore: cancel_subscriptions |
| 39 | + final sub = store.spiedSubscriptions.single; |
| 40 | + |
| 41 | + final subCalls = []; |
| 42 | + sub.onData(subCalls.add); |
| 43 | + |
| 44 | + store.trigger(); |
| 45 | + await pumpEventQueue(); |
| 46 | + expect(subCalls, hasLength(1), |
| 47 | + reason: 'test setup check; subscription should have gotten event'); |
| 48 | + |
| 49 | + await adapterStore.teardown(); |
| 50 | + store.trigger(); |
| 51 | + await pumpEventQueue(); |
| 52 | + expect(subCalls, hasLength(1), reason: 'subscription should have been cancelled'); |
| 53 | + }); |
| 54 | + |
| 55 | + group('can be called multiple times without throwing', () { |
| 56 | + ConnectFluxAdapterStore adapterStore; |
| 57 | + |
| 58 | + setUp(() { |
| 59 | + adapterStore = SimpleStore().asConnectFluxStore(null); |
| 60 | + }); |
| 61 | + |
| 62 | + test('synchronously', () async { |
| 63 | + await Future.wait([ |
| 64 | + adapterStore.teardown(), |
| 65 | + adapterStore.teardown(), |
| 66 | + ]); |
| 67 | + }); |
| 68 | + |
| 69 | + test('synchronously', () async { |
| 70 | + await adapterStore.teardown(); |
| 71 | + await adapterStore.teardown(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + group('calls teardown when the flux store is disposed', () { |
| 77 | + flux.Store store; |
| 78 | + ConnectFluxAdapterStore adapterStore; |
| 79 | + |
| 80 | + setUp(() { |
| 81 | + store = SimpleStore(); |
| 82 | + adapterStore = store.asConnectFluxStore(null); |
| 83 | + }); |
| 84 | + |
| 85 | + test('', () async { |
| 86 | + expect(adapterStore.teardownCalled, isFalse); |
| 87 | + |
| 88 | + await store.dispose(); |
| 89 | + await pumpEventQueue(); |
| 90 | + expect(adapterStore.teardownCalled, isTrue); |
| 91 | + }); |
| 92 | + |
| 93 | + group('and doesn\'t break if teardown', () { |
| 94 | + test('has already been called', () async { |
| 95 | + expect(adapterStore.teardownCalled, isFalse); |
| 96 | + |
| 97 | + await adapterStore.teardown(); |
| 98 | + expect(adapterStore.teardownCalled, isTrue); |
| 99 | + |
| 100 | + await store.dispose(); |
| 101 | + await pumpEventQueue(); |
| 102 | + }); |
| 103 | + |
| 104 | + test('is called after', () async { |
| 105 | + expect(adapterStore.teardownCalled, isFalse); |
| 106 | + |
| 107 | + await store.dispose(); |
| 108 | + await pumpEventQueue(); |
| 109 | + expect(adapterStore.teardownCalled, isTrue); |
| 110 | + |
| 111 | + await adapterStore.teardown(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +class SimpleStore extends flux.Store {} |
| 119 | + |
| 120 | +class SpyStore extends flux.Store { |
| 121 | + final spiedSubscriptions = <StreamSubscription>[]; |
| 122 | + |
| 123 | + @override |
| 124 | + listen(onData, {onError, onDone, cancelOnError}) { |
| 125 | + final sub = |
| 126 | + super.listen(onData, onError: onError, onDone: onDone, cancelOnError: cancelOnError); |
| 127 | + spiedSubscriptions.add(sub); |
| 128 | + return sub; |
| 129 | + } |
| 130 | +} |
0 commit comments