Skip to content

Commit

Permalink
test: fix tests and fix section positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
B0berman committed Mar 7, 2024
1 parent 23e7ddc commit b21bbf4
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 58 deletions.
4 changes: 2 additions & 2 deletions lib/crossword/bloc/crossword_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class CrosswordBloc extends Bloc<CrosswordEvent, CrosswordState> {
final currentState = state;
if (currentState is CrosswordLoaded) {
emit(
currentState.withSelectedWord(
WordSelection(
currentState.copyWith(
selectedWord: WordSelection(
section: event.section,
wordId: event.wordId,
),
Expand Down
20 changes: 2 additions & 18 deletions lib/crossword/bloc/crossword_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ class CrosswordLoaded extends CrosswordState {
this.selectedWord,
});

const CrosswordLoaded.initial()
: width = 40,
height = 40,
sectionSize = 300,
sections = const {},
selectedWord = null;

final int width;
final int height;
final int sectionSize;
Expand All @@ -50,23 +43,14 @@ class CrosswordLoaded extends CrosswordState {
int? height,
int? sectionSize,
Map<(int, int), BoardSection>? sections,
Map<(int, int), BoardSection>? allSections,
WordSelection? selectedWord,
}) {
return CrosswordLoaded(
width: width ?? this.width,
height: height ?? this.height,
sectionSize: sectionSize ?? this.sectionSize,
sections: sections ?? this.sections,
);
}

CrosswordState withSelectedWord(WordSelection? selectedWord) {
return CrosswordLoaded(
width: width,
height: height,
sectionSize: sectionSize,
sections: sections,
selectedWord: selectedWord,
selectedWord: selectedWord ?? this.selectedWord,
);
}

Expand Down
9 changes: 4 additions & 5 deletions lib/crossword/game/components/section_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,10 @@ class SectionComponent extends PositionComponent

final y =
word.axis == Axis.vertical ? word.position.y + c : word.position.y;
final offset = sectionPosition +
Vector2(
x * CrosswordGame.cellSize.toDouble(),
y * CrosswordGame.cellSize.toDouble(),
);
final offset = Vector2(
x * CrosswordGame.cellSize.toDouble(),
y * CrosswordGame.cellSize.toDouble(),
);

spriteBatch.add(
source: rect,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void main() {
],
borderWords: const [],
);
const sectionsCollection = 'sections';
const sectionsCollection = 'boardSections';

late FirebaseFirestore firebaseFirestore;
late CrosswordRepository crosswordRepository;
Expand Down Expand Up @@ -56,7 +56,7 @@ void main() {
});
});

group('watchSectionsFromPositions', () {
group('watchSectionFromPosition', () {
final section = BoardSection(
id: 'id2',
position: Point(0, 1),
Expand All @@ -74,7 +74,7 @@ void main() {
.set(section.toJson());
});

test('returns the requested sections depending on position', () {
test('returns the requested section depending on position', () {
expect(
crosswordRepository.watchSectionFromPosition(Point(0, 1)),
emits(section),
Expand Down
11 changes: 3 additions & 8 deletions test/crossword/bloc/crossword_state_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -343,25 +343,20 @@ void main() {
);

test('returns a copy with selected word', () {
final newState = state.withSelectedWord(
WordSelection(
final newState = state.copyWith(
selectedWord: WordSelection(
section: (0, 0),
wordId: '1',
),
);
expect(
(newState as CrosswordLoaded).selectedWord,
newState.selectedWord,
WordSelection(
section: (0, 0),
wordId: '1',
),
);
});

test('returns a copy with null selected word', () {
final newState = state.withSelectedWord(null);
expect((newState as CrosswordLoaded).selectedWord, null);
});
});

group('WordSelection', () {
Expand Down
55 changes: 34 additions & 21 deletions test/crossword/game/crossword_game_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ void main() {
height: 40,
sectionSize: 400,
sections: {
(2, 2): BoardSection(
(0, 0): BoardSection(
id: '1',
position: const Point(2, 2),
position: const Point(0, 0),
size: 400,
words: [
Word(
Expand All @@ -126,13 +126,14 @@ void main() {
),
},
);

mockState(state);

await game.ready();

final targetSection = game.world.children
.whereType<SectionComponent>()
.where((element) => element.index == (2, 2))
.where((element) => element.index == (0, 0))
.first;

final event = _MockTapUpEvent();
Expand All @@ -145,7 +146,7 @@ void main() {
verify(
() => bloc.add(
const WordSelected(
(2, 2),
(0, 0),
'Point(0, 0)-Axis.vertical',
),
),
Expand All @@ -160,9 +161,9 @@ void main() {
height: 40,
sectionSize: 400,
sections: {
(2, 2): BoardSection(
(0, 0): BoardSection(
id: '1',
position: const Point(2, 2),
position: const Point(0, 0),
size: 400,
words: [
Word(
Expand All @@ -188,7 +189,7 @@ void main() {
),
},
selectedWord: const WordSelection(
section: (2, 2),
section: (0, 0),
wordId: 'Point(0, 0)-Axis.vertical',
),
);
Expand All @@ -210,16 +211,16 @@ void main() {

final targetSection = game.world.children
.whereType<SectionComponent>()
.where((element) => element.index == (2, 2))
.where((element) => element.index == (0, 0))
.first;

expect(targetSection.lastSelectedWord, 'Point(0, 0)-Axis.vertical');
expect(targetSection.lastSelectedSection, (2, 2));
expect(targetSection.lastSelectedSection, (0, 0));

stateController.add(
state.withSelectedWord(
const WordSelection(
section: (2, 2),
state.copyWith(
selectedWord: const WordSelection(
section: (0, 0),
wordId: 'Point(2, 2)-Axis.horizontal',
),
),
Expand All @@ -228,7 +229,7 @@ void main() {
await Future.microtask(() {});

expect(targetSection.lastSelectedWord, 'Point(2, 2)-Axis.horizontal');
expect(targetSection.lastSelectedSection, (2, 2));
expect(targetSection.lastSelectedSection, (0, 0));
},
);
});
Expand All @@ -243,32 +244,44 @@ void main() {
});

testWithGame(
'remove sections that are not visible when paning',
() {
'remove sections that are not visible when panning',
createGame,
(game) async {
const state = CrosswordLoaded(
width: 40,
height: 40,
sectionSize: 300,
sections: {
(2, 2): BoardSection(
(-1, 0): BoardSection(
id: '',
position: Point(-1, 0),
size: 300,
words: [],
borderWords: [],
),
(0, 0): BoardSection(
id: '',
position: Point(2, 2),
position: Point(0, 0),
size: 300,
words: [],
borderWords: [],
),
(1, 0): BoardSection(
id: '',
position: Point(1, 0),
size: 300,
words: [],
borderWords: [],
),
},
);
mockState(state);
return createGame();
},
(game) async {
await game.ready();
final currentSections =
game.world.children.whereType<SectionComponent>();

final subjectComponent =
currentSections.firstWhere((element) => element.index == (2, 2));
currentSections.firstWhere((element) => element.index == (-1, 0));

final removed = subjectComponent.removed;

Expand Down
2 changes: 1 addition & 1 deletion test/crossword/view/crossword_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void main() {
whenListen(
bloc,
Stream.fromIterable(const <CrosswordState>[]),
initialState: const CrosswordLoaded.initial(),
initialState: const CrosswordInitial(),
);
});

Expand Down

0 comments on commit b21bbf4

Please sign in to comment.