Skip to content

Commit

Permalink
test(app): fix test data setting in drug test
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Sep 23, 2024
1 parent e0ce898 commit 5070948
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
17 changes: 12 additions & 5 deletions app/integration_test/drugs_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'fixtures/drugs/with_any_not_handled_guideline.dart';
import 'fixtures/drugs/with_multiple_any_not_handled_fallback_guidelines.dart';
import 'fixtures/drugs/with_proper_guideline.dart';
import 'fixtures/drugs/without_guidelines.dart';
import 'fixtures/set_user_data.dart';
import 'fixtures/set_app_data.dart';
import 'mocks/drug_cubit.dart';

void main() {
Expand All @@ -25,7 +25,10 @@ void main() {
UserData.instance.activeDrugNames = [];
UserData.instance.labData = null;
UserData.instance.genotypeResults = null;
setUserDataForGuideline(drugWithProperGuideline.guidelines.first);
setAppData(
drug: drugWithProperGuideline,
guideline: drugWithProperGuideline.guidelines.first,
);
mockDrugsCubit = MockDrugsCubit();
});

Expand Down Expand Up @@ -93,7 +96,6 @@ void main() {
testWidgets(
'$description (${index + 1}/${drug.guidelines.length})',
(tester) async {
setUserDataForGuideline(guideline);
await _expectDrugContent(
tester,
mockDrugsCubit,
Expand All @@ -116,6 +118,11 @@ Future<void> _expectDrugContent(
bool expectDrugToBeActive = false,
Guideline? guideline,
}) async {
Guideline? relevantGuideline;
if (!expectNoGuidelines) {
relevantGuideline = guideline ?? drug.guidelines.first;
setAppData(drug: drug, guideline: relevantGuideline);
}
when(() => mockDrugsCubit.state)
.thenReturn(isLoading ? DrugState.loading() : DrugState.loaded());
await tester.pumpWidget(
Expand All @@ -139,6 +146,7 @@ Future<void> _expectDrugContent(
),
),
);

// Title
final drugName = isInhibitor(drug.name) ? '${drug.name}*' : drug.name;
expect(
Expand Down Expand Up @@ -182,8 +190,7 @@ Future<void> _expectDrugContent(
findsOneWidget,
);
} else {
final relevantGuideline = guideline ?? drug.guidelines.first;
expect(card.color, relevantGuideline.annotations.warningLevel.color);
expect(card.color, relevantGuideline!.annotations.warningLevel.color);
expect(
find.byTooltip(context.l10n.drugs_page_tooltip_guideline_present(
relevantGuideline.externalData.first.source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class _UserDataConfig {
final String allelesTested = 'allelesTested does not matter for test';
}

void setGenotypeResult(GenotypeResult genotypeResult ) {
void setGenotypeResult(GenotypeResult genotypeResult) {
UserData.instance.genotypeResults = UserData.instance.genotypeResults ?? {};
UserData.instance.genotypeResults![genotypeResult.key.value] = genotypeResult;
}
Expand Down Expand Up @@ -59,3 +59,18 @@ void setUserDataForGuideline(Guideline guideline) {
));
}
}

void addDrugToCachedDrugs(Drug drug) {
CachedDrugs.instance.drugs = CachedDrugs.instance.drugs ?? [];
final drugIsPresent = CachedDrugs.instance.drugs!.any(
(presentDrug) => presentDrug.name == drug.name,
);
if (drugIsPresent) return;
CachedDrugs.instance.drugs!.add(drug);
}

void setAppData({required Drug drug, required Guideline guideline}) {
addDrugToCachedDrugs(drug);
initializeGenotypeResultKeys().values.forEach(setGenotypeResult);
setUserDataForGuideline(guideline);
}
2 changes: 1 addition & 1 deletion app/integration_test/report_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:provider/provider.dart';

import 'fixtures/guidelines/aripiprazole_cyp2d6_poor.dart';
import 'fixtures/guidelines/pazopanib_hlab5701_positive_ugt1a1_poor_guideline.dart';
import 'fixtures/set_user_data.dart';
import 'fixtures/set_app_data.dart';

void main() {
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
Expand Down
5 changes: 3 additions & 2 deletions app/lib/common/utilities/genome_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ String formatLookupMapKey(String gene, String variant) {
return '${gene}__$variant';
}

Map<String, GenotypeResult> _initializeGenotypeResultKeys() {
@visibleForTesting
Map<String, GenotypeResult> initializeGenotypeResultKeys() {
final emptyGenotypeResults = <String, GenotypeResult>{};
for (final drug in CachedDrugs.instance.drugs ?? <Drug>[]) {
for (final guideline in drug.guidelines) {
Expand All @@ -68,7 +69,7 @@ Future<void> updateGenotypeResults() async {
final skipUpdate = !shouldUpdateGenotypeResults();
if (skipUpdate) return;

final genotypeResults = _initializeGenotypeResultKeys();
final genotypeResults = initializeGenotypeResultKeys();

// fetch lookups
final response = await get(Uri.parse(cpicLookupUrl));
Expand Down

0 comments on commit 5070948

Please sign in to comment.