import 'dart:async'; import 'package:core/src/models/theater.dart'; import 'package:core/src/redux/_common/common_actions.dart'; import 'package:core/src/redux/theater/theater_middleware.dart'; import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import '../mocks.dart'; void main() { group('TheaterMiddleware', () { final log = []; final next = (dynamic action) => log.add(action); MockKeyValueStore mockKeyValueStore; TheaterMiddleware middleware; setUp(() { mockKeyValueStore = MockKeyValueStore(); middleware = TheaterMiddleware(mockKeyValueStore); }); tearDown(() { log.clear(); }); group('called with InitAction', () { test('loads the preloaded theaters', () async { // When await middleware.call(null, InitAction(), next); // Then final InitCompleteAction action = log.single; expect(action.theaters.size, 21); }); test('when a persisted theater id exists, uses that as a default', () async { when(mockKeyValueStore.getString(TheaterMiddleware.kDefaultTheaterId)) .thenReturn('1012'); await middleware.call(null, InitAction(), next); final InitCompleteAction action = log.single; Theater theater = action.selectedTheater; expect(theater.id, '1012'); expect(theater.name, 'Espoo'); }); test( 'when no persisted theater id, defaults to Helsinki', () async { when(mockKeyValueStore.getString(TheaterMiddleware.kDefaultTheaterId)) .thenReturn(null); await middleware.call(null, InitAction(), next); final InitCompleteAction action = log.single; Theater theater = action.selectedTheater; expect(theater.id, '1033'); expect(theater.name, 'Helsinki: Tennispalatsi'); }, ); }); test( 'when called with ChangeCurrentTheaterAction, persists and dispatches the same action', () async { final theater = Theater(id: 'test-123', name: 'Test Theater'); await middleware.call(null, ChangeCurrentTheaterAction(theater), next); verify(mockKeyValueStore.setString( TheaterMiddleware.kDefaultTheaterId, 'test-123')); final ChangeCurrentTheaterAction action = log.single; expect(action.selectedTheater, theater); }, ); }); } Future theatersXml() => Future.value(''' 1029 Valitse alue/teatteri 001 Gotham: THEATER ONE 002 Gotham: THEATER TWO ''');