Skip to content

Commit

Permalink
test(create drive cubit): attempts to fix skipped test PE-4597
Browse files Browse the repository at this point in the history
  • Loading branch information
matibat committed Sep 19, 2023
1 parent 68a11d0 commit c90af04
Showing 1 changed file with 132 additions and 103 deletions.
235 changes: 132 additions & 103 deletions test/blocs/drive_create_cubit_test.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
@Tags(['broken'])

import 'package:ardrive/blocs/blocs.dart';
import 'package:ardrive/core/crypto/crypto.dart';
import 'package:ardrive/entities/entities.dart';
import 'package:ardrive/core/arfs/entities/arfs_entities.dart';
import 'package:ardrive/entities/entity.dart';
import 'package:ardrive/models/models.dart';
import 'package:ardrive/services/services.dart';
import 'package:ardrive/turbo/services/upload_service.dart';
import 'package:ardrive/utils/app_flavors.dart';
import 'package:ardrive/utils/app_platform.dart';
import 'package:arweave/arweave.dart';
import 'package:bloc_test/bloc_test.dart';
Expand All @@ -19,109 +18,139 @@ import 'package:test/test.dart';
import '../test_utils/fakes.dart';
import '../test_utils/utils.dart';

class FakeEntity extends Fake implements Entity {}

void main() {
group('DriveCreateCubit', () {
late Database db;
late DriveDao driveDao;

late ArweaveService arweave;
late TurboUploadService turboUploadService;
late DrivesCubit drivesCubit;
late ProfileCubit profileCubit;
late DriveCreateCubit driveCreateCubit;

const validDriveName = 'valid-drive-name';

setUp(() async {
registerFallbackValue(DrivesStateFake());
registerFallbackValue(ProfileStateFake());

db = getTestDb();
driveDao = db.driveDao;
final configService = ConfigService(
appFlavors: AppFlavors(MockEnvFetcher()),
configFetcher: MockConfigFetcher());
final config = await configService.loadConfig();

AppPlatform.setMockPlatform(platform: SystemPlatform.unknown);
arweave = ArweaveService(
Arweave(gatewayUrl: Uri.parse(config.defaultArweaveGatewayUrl!)),
ArDriveCrypto(),
group(
'DriveCreateCubit',
() {
late Database db;
late DriveDao driveDao;

late ArweaveService arweave;
late TurboUploadService turboUploadService;
late DrivesCubit drivesCubit;
late ProfileCubit profileCubit;
late DriveCreateCubit driveCreateCubit;

late Wallet wallet;

const validDriveName = 'valid-drive-name';

setUp(() async {
wallet = getTestWallet();

registerFallbackValue(DrivesStateFake());
registerFallbackValue(ProfileStateFake());
registerFallbackValue(DataBundle(blob: Uint8List.fromList([])));
registerFallbackValue(wallet);
registerFallbackValue(FakeEntity());

db = getTestDb();
driveDao = db.driveDao;
AppPlatform.setMockPlatform(platform: SystemPlatform.unknown);
arweave = MockArweaveService();
turboUploadService = DontUseUploadService();
drivesCubit = MockDrivesCubit();
profileCubit = MockProfileCubit();

final walletAddress = await wallet.getAddress();
final walletOwner = await wallet.getAddress();

final keyBytes = Uint8List(32);
fillBytesWithSecureRandom(keyBytes);

when(() => profileCubit.state).thenReturn(
ProfileLoggedIn(
username: 'Test',
password: '123',
wallet: wallet,
walletAddress: walletAddress,
walletBalance: BigInt.from(10000001),
cipherKey: SecretKey(keyBytes),
useTurbo: turboUploadService.useTurboUpload,
),
);

when(() => profileCubit.logoutIfWalletMismatch()).thenAnswer(
(invocation) => Future.value(false),
);

when(() => arweave.prepareBundledDataItem(any(), any())).thenAnswer(
(invocation) => Future.value(
DataItem.withBlobData(
owner: walletOwner,
data: Uint8List.fromList([]),
),
),
);

when(() => arweave.prepareEntityDataItem(any(), any())).thenAnswer(
(invocation) => Future.value(
DataItem.withBlobData(
owner: walletOwner,
data: Uint8List.fromList([]),
),
),
);

driveCreateCubit = DriveCreateCubit(
arweave: arweave,
turboUploadService: turboUploadService,
driveDao: driveDao,
drivesCubit: drivesCubit,
profileCubit: profileCubit,
);
});

tearDown(() async {
await db.close();
});

blocTest<DriveCreateCubit, DriveCreateState>(
'create public drive',
build: () => driveCreateCubit,
act: (bloc) async {
bloc.form.value = {
'name': validDriveName,
'privacy': DrivePrivacy.public.name,
};
await bloc.submit('');
},
expect: () => [
const DriveCreateInProgress(privacy: DrivePrivacy.public),
const DriveCreateSuccess(privacy: DrivePrivacy.public),
],
verify: (_) {},
);
turboUploadService = DontUseUploadService();
drivesCubit = MockDrivesCubit();
profileCubit = MockProfileCubit();

final wallet = getTestWallet();
final walletAddress = await wallet.getAddress();

final keyBytes = Uint8List(32);
fillBytesWithSecureRandom(keyBytes);

when(() => profileCubit.state).thenReturn(
ProfileLoggedIn(
username: 'Test',
password: '123',
wallet: wallet,
walletAddress: walletAddress,
walletBalance: BigInt.one,
cipherKey: SecretKey(keyBytes),
useTurbo: turboUploadService.useTurboUpload,
),

blocTest<DriveCreateCubit, DriveCreateState>(
'create private drive',
build: () => driveCreateCubit,
act: (bloc) async {
bloc.form.value = {
'name': validDriveName,
'privacy': DrivePrivacy.private.name,
};

bloc.onPrivacyChanged();

await bloc.submit('');
},
expect: () => [
const DriveCreateInProgress(privacy: DrivePrivacy.public),
const DriveCreateInProgress(privacy: DrivePrivacy.private),
const DriveCreateSuccess(privacy: DrivePrivacy.private),
],
verify: (_) {},
);

driveCreateCubit = DriveCreateCubit(
arweave: arweave,
turboUploadService: turboUploadService,
driveDao: driveDao,
drivesCubit: drivesCubit,
profileCubit: profileCubit,
blocTest<DriveCreateCubit, DriveCreateState>(
'does nothing when submitted without valid form',
build: () => driveCreateCubit,
act: (bloc) => bloc.submit(''),
expect: () => [],
);
});

tearDown(() async {
await db.close();
});

blocTest<DriveCreateCubit, DriveCreateState>(
'create public drive',
build: () => driveCreateCubit,
act: (bloc) async {
bloc.form.value = {
'name': validDriveName,
'privacy': DrivePrivacy.public,
};
await bloc.submit('');
},
expect: () => [
DriveCreateInProgress(),
DriveCreateSuccess(),
],
verify: (_) {},
);

blocTest<DriveCreateCubit, DriveCreateState>(
'create private drive',
build: () => driveCreateCubit,
act: (bloc) async {
bloc.form.value = {
'name': validDriveName,
'privacy': DrivePrivacy.private,
};
await bloc.submit('');
},
expect: () => [
DriveCreateInProgress(),
DriveCreateSuccess(),
],
verify: (_) {},
);

blocTest<DriveCreateCubit, DriveCreateState>(
'does nothing when submitted without valid form',
build: () => driveCreateCubit,
act: (bloc) => bloc.submit(''),
expect: () => [],
);
}, skip: 'Needs to update the tests');
},
);
}

0 comments on commit c90af04

Please sign in to comment.