|
| 1 | +import 'package:app_flowy/plugins/board/application/board_bloc.dart'; |
| 2 | +import 'package:app_flowy/plugins/grid/application/field/field_editor_bloc.dart'; |
| 3 | +import 'package:app_flowy/plugins/grid/application/field/type_option/type_option_context.dart'; |
| 4 | +import 'package:bloc_test/bloc_test.dart'; |
| 5 | +import 'package:flowy_sdk/protobuf/flowy-grid/field_entities.pb.dart'; |
| 6 | +import 'package:flutter_test/flutter_test.dart'; |
| 7 | +import 'util.dart'; |
| 8 | + |
| 9 | +void main() { |
| 10 | + late AppFlowyBoardTest boardTest; |
| 11 | + |
| 12 | + setUpAll(() async { |
| 13 | + boardTest = await AppFlowyBoardTest.ensureInitialized(); |
| 14 | + }); |
| 15 | + |
| 16 | + group('The grouped field is not changed after editing a field:', () { |
| 17 | + late BoardBloc boardBloc; |
| 18 | + late FieldEditorBloc editorBloc; |
| 19 | + setUpAll(() async { |
| 20 | + await boardTest.context.createTestBoard(); |
| 21 | + }); |
| 22 | + |
| 23 | + setUp(() async { |
| 24 | + boardBloc = BoardBloc(view: boardTest.context.gridView) |
| 25 | + ..add(const BoardEvent.initial()); |
| 26 | + |
| 27 | + final fieldContext = boardTest.context.singleSelectFieldContext(); |
| 28 | + final loader = FieldTypeOptionLoader( |
| 29 | + gridId: boardTest.context.gridView.id, |
| 30 | + field: fieldContext.field, |
| 31 | + ); |
| 32 | + |
| 33 | + editorBloc = FieldEditorBloc( |
| 34 | + gridId: boardTest.context.gridView.id, |
| 35 | + fieldName: fieldContext.name, |
| 36 | + isGroupField: fieldContext.isGroupField, |
| 37 | + loader: loader, |
| 38 | + )..add(const FieldEditorEvent.initial()); |
| 39 | + |
| 40 | + await boardResponseFuture(); |
| 41 | + }); |
| 42 | + |
| 43 | + blocTest<BoardBloc, BoardState>( |
| 44 | + "initial", |
| 45 | + build: () => boardBloc, |
| 46 | + wait: boardResponseDuration(), |
| 47 | + verify: (bloc) { |
| 48 | + assert(bloc.groupControllers.values.length == 4); |
| 49 | + assert(boardTest.context.fieldContexts.length == 2); |
| 50 | + }, |
| 51 | + ); |
| 52 | + |
| 53 | + blocTest<FieldEditorBloc, FieldEditorState>( |
| 54 | + "edit a field", |
| 55 | + build: () => editorBloc, |
| 56 | + act: (bloc) async { |
| 57 | + editorBloc.add(const FieldEditorEvent.updateName('Hello world')); |
| 58 | + }, |
| 59 | + wait: boardResponseDuration(), |
| 60 | + verify: (bloc) { |
| 61 | + bloc.state.field.fold( |
| 62 | + () => throw Exception("The field should not be none"), |
| 63 | + (field) { |
| 64 | + assert(field.name == 'Hello world'); |
| 65 | + }, |
| 66 | + ); |
| 67 | + }, |
| 68 | + ); |
| 69 | + |
| 70 | + blocTest<BoardBloc, BoardState>( |
| 71 | + "assert the groups were not changed", |
| 72 | + build: () => boardBloc, |
| 73 | + wait: boardResponseDuration(), |
| 74 | + verify: (bloc) { |
| 75 | + assert(bloc.groupControllers.values.length == 4, |
| 76 | + "Expected 4, but receive ${bloc.groupControllers.values.length}"); |
| 77 | + |
| 78 | + assert(boardTest.context.fieldContexts.length == 2, |
| 79 | + "Expected 2, but receive ${boardTest.context.fieldContexts.length}"); |
| 80 | + }, |
| 81 | + ); |
| 82 | + }); |
| 83 | + group('The grouped field is not changed after creating a new field:', () { |
| 84 | + late BoardBloc boardBloc; |
| 85 | + setUpAll(() async { |
| 86 | + await boardTest.context.createTestBoard(); |
| 87 | + }); |
| 88 | + |
| 89 | + setUp(() async { |
| 90 | + boardBloc = BoardBloc(view: boardTest.context.gridView) |
| 91 | + ..add(const BoardEvent.initial()); |
| 92 | + await boardResponseFuture(); |
| 93 | + }); |
| 94 | + |
| 95 | + blocTest<BoardBloc, BoardState>( |
| 96 | + "initial", |
| 97 | + build: () => boardBloc, |
| 98 | + wait: boardResponseDuration(), |
| 99 | + verify: (bloc) { |
| 100 | + assert(bloc.groupControllers.values.length == 4); |
| 101 | + assert(boardTest.context.fieldContexts.length == 2); |
| 102 | + }, |
| 103 | + ); |
| 104 | + |
| 105 | + test('create a field', () async { |
| 106 | + await boardTest.context.createField(FieldType.Checkbox); |
| 107 | + await boardResponseFuture(); |
| 108 | + final checkboxField = boardTest.context.fieldContexts.last.field; |
| 109 | + assert(checkboxField.fieldType == FieldType.Checkbox); |
| 110 | + }); |
| 111 | + |
| 112 | + blocTest<BoardBloc, BoardState>( |
| 113 | + "assert the groups were not changed", |
| 114 | + build: () => boardBloc, |
| 115 | + wait: boardResponseDuration(), |
| 116 | + verify: (bloc) { |
| 117 | + assert(bloc.groupControllers.values.length == 4, |
| 118 | + "Expected 4, but receive ${bloc.groupControllers.values.length}"); |
| 119 | + |
| 120 | + assert(boardTest.context.fieldContexts.length == 3, |
| 121 | + "Expected 3, but receive ${boardTest.context.fieldContexts.length}"); |
| 122 | + }, |
| 123 | + ); |
| 124 | + }); |
| 125 | +} |
0 commit comments