Skip to content

Commit

Permalink
Disable adding items for regular users (#96)
Browse files Browse the repository at this point in the history
* Disable adding items for regular users

* Fix typo
  • Loading branch information
ANDREYDEN authored Feb 8, 2024
1 parent 35f480d commit 3f793fb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 54 deletions.
2 changes: 1 addition & 1 deletion lib/pages/home_page/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class _HomePageState extends State<HomePage> {
),
],
),
floatingActionButton: AddInventoryItemButton(),
floatingActionButton: isAdmin ? AddInventoryItemButton() : null,
body: layout == LayoutType.desktop
? SizedBox(
child: Row(
Expand Down
120 changes: 67 additions & 53 deletions test/pages/home_page/inventory_view/inventory_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,71 +89,85 @@ void main() {
});
});

testWidgets(
'Adds new item to inventory list',
(WidgetTester tester) async {
await tester.binding.setSurfaceSize(Size(800, 1000));

const itemName = 'Table #3';
const itemType = 'Desk';
const itemStorageLocation = 'Waterloo';
const itemDescription = 'Lorem ipsum';
const isPrivate = true;

group('adding a new item', () {
testWidgets('is disabled for users', (WidgetTester tester) async {
await pumpPage(
HomePage(),
tester,
userRole: UserRole.admin,
userRole: UserRole.user,
firestore: firestore,
);

final fab = find.byIcon(Icons.add);
expect(fab, findsNothing);
});

await tester.tap(fab);
await tester.pumpAndSettle();
testWidgets(
'adds a new item to inventory list',
(WidgetTester tester) async {
await tester.binding.setSurfaceSize(Size(800, 1000));

await tester.enterTextByLabel('Name *', itemName);
await tester.enterTextByLabel('Description', itemDescription);
await tester.selectDropdownOption('Item Type', itemType);
await tester.selectDropdownOption(
'Storage Location',
itemStorageLocation,
);
if (isPrivate) {
// TODO: there is an issue where this switch is not found due to the height of the form.
// Scrolling down to the switch did not work.
await tester
.tap(find.widgetWithText(SwitchListTile, 'Only visible to admins'));
}

await tester.tap(find.text('Save'));
await tester.pumpAndSettle();
const itemName = 'Table #3';
const itemType = 'Desk';
const itemStorageLocation = 'Waterloo';
const itemDescription = 'Lorem ipsum';
const isPrivate = true;

final newItemListItem = find.text(itemName);
expect(newItemListItem, findsOneWidget);
await pumpPage(
HomePage(),
tester,
userRole: UserRole.admin,
firestore: firestore,
);

await tester.tap(newItemListItem);
await tester.pumpAndSettle();
final fab = find.byIcon(Icons.add);

expect(
find.descendant(
of: find.byType(Card),
matching: find.textContaining(itemName),
),
findsOneWidget,
);
expect(find.text(itemDescription), findsOneWidget);
expect(
find.descendant(
of: find.byType(ItemPage),
matching: find.byIcon(itemTypes[itemType]!),
),
findsOneWidget,
);
expect(find.textContaining(itemStorageLocation), findsOneWidget);
expect(find.byIcon(Icons.visibility_off), findsOneWidget);
},
);
await tester.tap(fab);
await tester.pumpAndSettle();

await tester.enterTextByLabel('Name *', itemName);
await tester.enterTextByLabel('Description', itemDescription);
await tester.selectDropdownOption('Item Type', itemType);
await tester.selectDropdownOption(
'Storage Location',
itemStorageLocation,
);
if (isPrivate) {
// TODO: there is an issue where this switch is not found due to the height of the form.
// Scrolling down to the switch did not work.
await tester.tap(
find.widgetWithText(SwitchListTile, 'Only visible to admins'));
}

await tester.tap(find.text('Save'));
await tester.pumpAndSettle();

final newItemListItem = find.text(itemName);
expect(newItemListItem, findsOneWidget);

await tester.tap(newItemListItem);
await tester.pumpAndSettle();

expect(
find.descendant(
of: find.byType(Card),
matching: find.textContaining(itemName),
),
findsOneWidget,
);
expect(find.text(itemDescription), findsOneWidget);
expect(
find.descendant(
of: find.byType(ItemPage),
matching: find.byIcon(itemTypes[itemType]!),
),
findsOneWidget,
);
expect(find.textContaining(itemStorageLocation), findsOneWidget);
expect(find.byIcon(Icons.visibility_off), findsOneWidget);
},
);
});

group('Editing an item', () {
testWidgets(
Expand Down

0 comments on commit 3f793fb

Please sign in to comment.