Skip to content

Commit

Permalink
chore: add unit tests to image
Browse files Browse the repository at this point in the history
  • Loading branch information
nank1ro committed Jan 8, 2025
1 parent 1591b4d commit a1a1f79
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 1 deletion.
32 changes: 32 additions & 0 deletions .github/workflows/flutter-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Flutter test

on:
pull_request:
paths-ignore:
- "**.md"
push:
branches:
- main
- dev
paths-ignore:
- "**.md"

jobs:
build:
runs-on: ubuntu-latest
concurrency:
group: ${{ github.head_ref || github.run_id }}
cancel-in-progress: true

steps:
- uses: actions/checkout@v4

- uses: subosito/flutter-action@v2.18.0
with:
channel: "stable"

- name: Install dependencies
run: flutter pub get

- name: Test
run: flutter test
3 changes: 3 additions & 0 deletions lib/src/components/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import 'package:vector_graphics/vector_graphics.dart'

typedef ShadImageSrc = Object;

/// {@template ShadImageData}
/// The data image provided to the [ShadImage] with a Provider.
///
/// This value is used only if the properties are not overriden in the
/// [ShadImage].
/// {@endtemplate}
@immutable
class ShadImageData {
/// {@macro ShadImageData}
const ShadImageData({this.size, this.color});

/// The size of the image.
Expand Down
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
mocktail: ^1.0.4
very_good_analysis: ^7.0.0

flutter:
Expand Down Expand Up @@ -75,3 +76,5 @@ flutter:
weight: 800
- asset: fonts/GeistMono-UltraBlack.otf
weight: 900
assets:
- test/assets/
Binary file added test/assets/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/assets/image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/assets/image.svg.vec
Binary file not shown.
8 changes: 8 additions & 0 deletions test/extra/diagnosticable_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:flutter/widgets.dart';

mixin DiagnosticableToStringMixin on Object {
@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
return super.toString();
}
}
1 change: 0 additions & 1 deletion test/shadcn_ui_test.dart

This file was deleted.

174 changes: 174 additions & 0 deletions test/src/components/image_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

import '../../extra/diagnosticable_mixin.dart';

// Create a Mock class for SvgPicture using mocktail
class MockSvgPicture extends Mock
with DiagnosticableToStringMixin
implements SvgPicture {}

Future<void> main() async {
setUpAll(() async {});

testWidgets('renders jpg image correctly', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: ShadImage('test/assets/image.jpg'),
),
),
);

expect(find.byType(Image), findsOneWidget);
expect(find.byType(SvgPicture), findsNothing);
});

testWidgets('renders svg image correctly', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: ShadImage('test/assets/image.svg'),
),
),
);

expect(find.byType(SvgPicture), findsOneWidget);
expect(find.byType(Image), findsNothing);
});

testWidgets('renders svg.vec image correctly', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: ShadImage('test/assets/image.svg.vec'),
),
),
);

expect(find.byType(SvgPicture), findsOneWidget);
expect(find.byType(Image), findsNothing);
});

testWidgets('renders icon correctly', (WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: ShadImage(Icons.star, width: 50, height: 50),
),
),
);

expect(find.byType(Icon), findsOneWidget);
});

testWidgets('applies gradient correctly', (WidgetTester tester) async {
const gradient = LinearGradient(colors: [Colors.red, Colors.blue]);

await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: ShadImage(LucideIcons.mail, gradient: gradient),
),
),
);

expect(find.byType(ShaderMask), findsOneWidget);
});
testWidgets('renders ShadImage with specified width and height',
(WidgetTester tester) async {
const testWidth = 150.0;
const testHeight = 200.0;

await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: ShadImage(
'test/assets/image.jpg',
width: testWidth,
height: testHeight,
),
),
),
);

// Verify that the rendered Image widget has the correct dimensions.
final imageWidget = tester.widget<Image>(find.byType(Image));
expect(imageWidget.width, testWidth);
expect(imageWidget.height, testHeight);
});

testWidgets('renders ShadImage with specified color',
(WidgetTester tester) async {
const testColor = Colors.red;

await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: ShadImage(
'test/assets/image.jpg',
color: testColor,
),
),
),
);

final imageWidget = tester.widget<Image>(find.byType(Image));
final color = imageWidget.color;

expect(color, equals(testColor));
});

testWidgets('renders ShadImage with inherited color',
(WidgetTester tester) async {
const testColor = Colors.red;

await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: ShadProvider(
data: ShadImageData(color: testColor),
child: ShadImage(
'test/assets/image.jpg',
),
),
),
),
);

final imageWidget = tester.widget<Image>(find.byType(Image));
final color = imageWidget.color;

expect(color, equals(testColor));
});

testWidgets('renders ShadImage with correct colorFilter for SvgPicture',
(WidgetTester tester) async {
const testColor = Colors.green;

await tester.pumpWidget(
const MaterialApp(
home: Scaffold(
body: ShadImage(
'test/assets/image.svg',
color: testColor,
),
),
),
);

// Verify that an SvgPicture widget is rendered.
final svgPicture = tester.widget<SvgPicture>(find.byType(SvgPicture));

// Expect the SvgPicture to have a ColorFilter applied.
expect(svgPicture.colorFilter, isA<ColorFilter>());

// Verify that the ColorFilter is correctly set up with the provided color.
expect(
svgPicture.colorFilter,
equals(const ColorFilter.mode(testColor, BlendMode.srcIn)),
);
});
}

0 comments on commit a1a1f79

Please sign in to comment.