|
| 1 | +import 'dart:typed_data'; |
| 2 | + |
| 3 | +import 'package:ble_temperature/src/about/domain/entities/app_info.dart'; |
| 4 | +import 'package:ble_temperature/src/bluetooth/data/datasources/ble_remote_data_source.dart'; |
| 5 | +import 'package:ble_temperature/src/bluetooth/data/repositories/ble_repository_impl.dart'; |
| 6 | +import 'package:ble_temperature/src/bluetooth/domain/enums/enums.dart'; |
| 7 | +import 'package:ble_temperature/src/bluetooth/domain/respositories/ble_repository.dart'; |
| 8 | +import 'package:ble_temperature/src/bluetooth/domain/value_objects/device_connection_state_update.dart'; |
| 9 | +import 'package:ble_temperature/src/bluetooth/domain/value_objects/discovered_device.dart'; |
| 10 | +import 'package:dartz/dartz.dart'; |
| 11 | +import 'package:flutter_test/flutter_test.dart'; |
| 12 | + |
| 13 | +import 'package:mockito/annotations.dart'; |
| 14 | +import 'package:mockito/mockito.dart'; |
| 15 | + |
| 16 | +@GenerateNiceMocks([MockSpec<BleRemoteDataSource>()]) |
| 17 | +import 'ble_repository_impl_test.mocks.dart'; |
| 18 | + |
| 19 | +void main() { |
| 20 | + late MockBleRemoteDataSource dataSource; |
| 21 | + late BleRepositoryImpl repository; |
| 22 | + |
| 23 | + setUp(() { |
| 24 | + dataSource = MockBleRemoteDataSource(); |
| 25 | + repository = BleRepositoryImpl(dataSource); |
| 26 | + }); |
| 27 | + |
| 28 | + test( |
| 29 | + '[AppInfoRepositoryImpl]' |
| 30 | + 'should be a subclass of [AppInfoRepository].', () async { |
| 31 | + expect(repository, isA<BleRepository>()); |
| 32 | + }); |
| 33 | + |
| 34 | + test('[Repository.bleState] successfully returns [BLEState.ready].', |
| 35 | + () async { |
| 36 | + when(dataSource.bleState()).thenReturn( |
| 37 | + BLEState.ready, |
| 38 | + ); |
| 39 | + |
| 40 | + final result = await repository.bleState(); |
| 41 | + |
| 42 | + expect( |
| 43 | + result, |
| 44 | + const Right<AppInfo, dynamic>( |
| 45 | + BLEState.ready, |
| 46 | + ), |
| 47 | + ); |
| 48 | + verify(dataSource.bleState()).called(1); |
| 49 | + verifyNoMoreInteractions(dataSource); |
| 50 | + }); |
| 51 | + |
| 52 | + test( |
| 53 | + '[Repository.connectToDevice] successfully returns ' |
| 54 | + '[DeviceConnectionStateUpdate].', () async { |
| 55 | + const tDeviceConnectionStateUpdate = DeviceConnectionStateUpdate( |
| 56 | + deviceId: '', |
| 57 | + deviceConnectionState: DeviceConnectionState.connected, |
| 58 | + ); |
| 59 | + |
| 60 | + when(dataSource.connectToDevice(deviceId: anyNamed('deviceId'))) |
| 61 | + .thenAnswer((_) => Stream.value(tDeviceConnectionStateUpdate)); |
| 62 | + |
| 63 | + final result = repository.connectToDevice(deviceId: ''); |
| 64 | + |
| 65 | + expect(result, emits(tDeviceConnectionStateUpdate)); |
| 66 | + verify(dataSource.connectToDevice(deviceId: '')).called(1); |
| 67 | + verifyNoMoreInteractions(dataSource); |
| 68 | + }); |
| 69 | + |
| 70 | + test( |
| 71 | + '[Repository.scanForDevices] successfully returns ' |
| 72 | + '[DiscoveredDevice].', () async { |
| 73 | + final tDevice = DiscoveredDevice( |
| 74 | + id: '', |
| 75 | + name: '', |
| 76 | + serviceData: const {}, |
| 77 | + manufacturerData: Uint8List(0), |
| 78 | + rssi: 0, |
| 79 | + serviceUuids: const [], |
| 80 | + ); |
| 81 | + |
| 82 | + when(dataSource.scanForDevices()).thenAnswer( |
| 83 | + (_) => Stream.value(tDevice), |
| 84 | + ); |
| 85 | + |
| 86 | + final result = repository.scanForDevices(); |
| 87 | + |
| 88 | + expect(result, emits(tDevice)); |
| 89 | + verify(dataSource.scanForDevices()).called(1); |
| 90 | + verifyNoMoreInteractions(dataSource); |
| 91 | + }); |
| 92 | + |
| 93 | + test( |
| 94 | + '[Repository.subscribeToCharacteristic] successfully returns ' |
| 95 | + '[0.0].', () async { |
| 96 | + when(dataSource.subscribeToCharacteristic(any)).thenAnswer( |
| 97 | + (_) => Stream<double>.value(0), |
| 98 | + ); |
| 99 | + |
| 100 | + final result = repository.subscribeToCharacteristic(''); |
| 101 | + |
| 102 | + expect(result, emits(0.0)); |
| 103 | + verify(dataSource.subscribeToCharacteristic('')).called(1); |
| 104 | + verifyNoMoreInteractions(dataSource); |
| 105 | + }); |
| 106 | +} |
0 commit comments