Skip to content

Commit

Permalink
feat: adds solved characters to CrosswordInput (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
AyadLaouissi authored May 2, 2024
1 parent ae520d1 commit f5bd3cc
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 1 deletion.
15 changes: 15 additions & 0 deletions api/packages/game_domain/lib/src/models/word.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ class Word extends Equatable {
@JsonKey()
final Mascots? mascot;

/// Returns the solved characters with the index position and character
/// solved.
Map<int, String> get solvedCharacters {
final map = <int, String>{};

for (var i = 0; i < answer.length; i++) {
final character = answer[i];
if (character != ' ') {
map[i] = character;
}
}

return map;
}

/// Returns a json representation from this instance.
Map<String, dynamic> toJson() => _$WordToJson(this);

Expand Down
35 changes: 35 additions & 0 deletions api/packages/game_domain/test/src/models/word_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,40 @@ void main() {
expect(word.isSolved, isFalse);
});
});

group('solvedCharacters', () {
test(' is empty when there are no characters solved', () {
final word = Word(
id: '1',
position: Point(1, 2),
axis: Axis.horizontal,
answer: ' ',
clue: 'clue',
);

expect(word.solvedCharacters, isEmpty);
});

test(' returns the solved characters', () {
final word = Word(
id: '1',
position: Point(1, 2),
axis: Axis.horizontal,
answer: ' ap y',
clue: 'clue',
);

expect(
word.solvedCharacters,
equals(
{
1: 'a',
2: 'p',
4: 'y',
},
),
);
});
});
});
}
1 change: 1 addition & 0 deletions lib/crossword2/view/crossword2_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class _CrosswordStack extends StatelessWidget {
style: theme.io.wordInput.secondary,
direction: word.axis.toAxis(),
length: selectedWord.word.length,
characters: selectedWord.word.solvedCharacters,
),
);
},
Expand Down
3 changes: 3 additions & 0 deletions lib/crossword2/widgets/crossword_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:io_crossword_ui/io_crossword_ui.dart';
class CrosswordInput extends StatefulWidget {
const CrosswordInput({
required this.length,
required this.characters,
this.style,
this.direction = Axis.horizontal,
super.key,
Expand All @@ -15,6 +16,7 @@ class CrosswordInput extends StatefulWidget {
final IoWordInputStyle? style;
final int length;
final Axis direction;
final Map<int, String>? characters;

@override
State<CrosswordInput> createState() => _CrosswordInputState();
Expand Down Expand Up @@ -53,6 +55,7 @@ class _CrosswordInputState extends State<CrosswordInput> {
style: widget.style,
direction: widget.direction,
length: widget.length,
characters: widget.characters,
onSubmit: (value) {
context
.read<WordSelectionBloc>()
Expand Down
1 change: 1 addition & 0 deletions lib/word_selection/view/word_solving_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class _WordSolvingSmallViewState extends State<WordSolvingSmallView> {
const SizedBox(height: 32),
CrosswordInput(
length: selectedWord.word.length,
characters: selectedWord.word.solvedCharacters,
),
const SizedBox(height: 16),
Text(
Expand Down
1 change: 1 addition & 0 deletions test/crossword2/view/crossword2_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void main() {
when(() => word.answer).thenReturn('word');
when(() => word.isSolved).thenReturn(false);
when(() => word.solvedTimestamp).thenReturn(null);
when(() => word.solvedCharacters).thenReturn({});
});

testWidgets('requests chunk', (tester) async {
Expand Down
35 changes: 34 additions & 1 deletion test/crossword2/widgets/crossword_input_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ void main() {
DefaultWordInputController(
child: BlocProvider.value(
value: wordSelectionBloc,
child: const CrosswordInput(length: 5),
child: const CrosswordInput(
length: 5,
characters: {},
),
),
),
);
Expand All @@ -42,5 +45,35 @@ void main() {
).called(1);
},
);

testWidgets(
'sends characters correctly to IoWordInput',
(tester) async {
await tester.pumpApp(
DefaultWordInputController(
child: BlocProvider.value(
value: wordSelectionBloc,
child: const CrosswordInput(
length: 5,
characters: {
1: 'a',
4: 'y',
},
),
),
),
);

expect(
tester.widget<IoWordInput>(find.byType(IoWordInput)).characters,
equals(
{
1: 'a',
4: 'y',
},
),
);
},
);
});
}
3 changes: 3 additions & 0 deletions test/word_focused/view/word_selection_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class _FakeWord extends Fake implements Word {

@override
Axis get axis => Axis.horizontal;

@override
Map<int, String> get solvedCharacters => {};
}

void main() {
Expand Down
42 changes: 42 additions & 0 deletions test/word_focused/view/word_solving_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class _FakeWord extends Fake implements Word {

@override
int get length => 6;

@override
Map<int, String> get solvedCharacters => {};
}

void main() {
Expand Down Expand Up @@ -257,6 +260,45 @@ void main() {
},
);

testWidgets(
'a $CrosswordInput',
(tester) async {
await tester.pumpApp(widget);
expect(find.byType(CrosswordInput), findsOneWidget);
},
);

testWidgets(
'a $CrosswordInput with solved characters',
(tester) async {
final word = Word(
id: 'id',
position: Point(1, 2),
axis: Axis.horizontal,
clue: '',
answer: ' a y',
);

when(() => wordSelectionBloc.state).thenReturn(
WordSelectionState(
status: WordSelectionStatus.solving,
word: SelectedWord(
section: (0, 0),
word: word,
),
),
);

await tester.pumpApp(widget);
expect(
tester
.widget<CrosswordInput>(find.byType(CrosswordInput))
.characters,
equals({1: 'a', 4: 'y'}),
);
},
);

testWidgets(
'incorrectAnswer text when the status is incorrect',
(tester) async {
Expand Down

0 comments on commit f5bd3cc

Please sign in to comment.