Skip to content

Commit 555115c

Browse files
authored
docs(examples): add counter example (#234)
1 parent d9763a8 commit 555115c

File tree

9 files changed

+144
-0
lines changed

9 files changed

+144
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: examples/counter
2+
3+
concurrency:
4+
group: ${{ github.workflow }}-${{ github.ref }}
5+
cancel-in-progress: true
6+
7+
on:
8+
pull_request:
9+
paths:
10+
- "examples/counter/routes/**"
11+
- "examples/counter/lib/**"
12+
- "examples/counter/test/**"
13+
- ".github/workflows/examples_counter.yaml"
14+
branches:
15+
- main
16+
17+
jobs:
18+
build:
19+
uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/dart_package.yml@v1
20+
with:
21+
working_directory: examples/counter
22+
analyze_directories: "routes test"
23+
report_on: "routes"

examples/counter/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# See https://www.dartlang.org/guides/libraries/private-files
2+
3+
# Files and directories created by pub
4+
.dart_tool/
5+
.packages
6+
pubspec.lock
7+
8+
# Files and directories created by dart_frog
9+
build/
10+
.dart_frog
11+
12+
# Test related files
13+
coverage/

examples/counter/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# counter
2+
3+
[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link]
4+
[![License: MIT][license_badge]][license_link]
5+
6+
An example counter app built with `dart_frog`.
7+
8+
[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg
9+
[license_link]: https://opensource.org/licenses/MIT
10+
[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg
11+
[very_good_analysis_link]: https://pub.dev/packages/very_good_analysis
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include: package:very_good_analysis/analysis_options.3.0.1.yaml
2+
analyzer:
3+
exclude:
4+
- build/**
5+
linter:
6+
rules:
7+
file_names: false

examples/counter/pubspec.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: counter
2+
description: An example counter app built with Dart Frog.
3+
version: 1.0.0+1
4+
publish_to: none
5+
6+
environment:
7+
sdk: ">=2.17.0 <3.0.0"
8+
9+
dependencies:
10+
dart_frog: ^0.0.1-dev
11+
12+
dev_dependencies:
13+
mocktail: ^0.3.0
14+
test: ^1.19.2
15+
very_good_analysis: ^3.0.1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'package:dart_frog/dart_frog.dart';
2+
3+
int _count = 0;
4+
5+
Handler middleware(Handler handler) {
6+
return handler.use(provider<int>((_) => ++_count));
7+
}

examples/counter/routes/index.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'package:dart_frog/dart_frog.dart';
2+
3+
Response onRequest(RequestContext context) {
4+
final count = context.read<int>();
5+
return Response(
6+
body: 'You have requested this route $count time(s).',
7+
);
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'package:dart_frog/dart_frog.dart';
2+
import 'package:mocktail/mocktail.dart';
3+
import 'package:test/test.dart';
4+
5+
import '../../routes/_middleware.dart';
6+
7+
class _MockRequestContext extends Mock implements RequestContext {}
8+
9+
void main() {
10+
group('middleware', () {
11+
test('provides incremented count', () async {
12+
int? count;
13+
final handler = middleware(
14+
(context) {
15+
count = context.read<int>();
16+
return Response(body: '');
17+
},
18+
);
19+
final request = Request.get(Uri.parse('http://localhost/'));
20+
final context = _MockRequestContext();
21+
when(() => context.request).thenReturn(request);
22+
23+
await handler(context);
24+
expect(count, equals(1));
25+
26+
await handler(context);
27+
expect(count, equals(2));
28+
29+
await handler(context);
30+
expect(count, equals(3));
31+
});
32+
});
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'dart:io';
2+
3+
import 'package:dart_frog/dart_frog.dart';
4+
import 'package:mocktail/mocktail.dart';
5+
import 'package:test/test.dart';
6+
7+
import '../../routes/index.dart' as route;
8+
9+
class _MockRequestContext extends Mock implements RequestContext {}
10+
11+
void main() {
12+
group('GET /', () {
13+
test('responds with a 200 and count.', () async {
14+
const count = 42;
15+
final context = _MockRequestContext();
16+
when(() => context.read<int>()).thenReturn(count);
17+
final response = route.onRequest(context);
18+
expect(response.statusCode, equals(HttpStatus.ok));
19+
expect(
20+
response.body(),
21+
completion(
22+
equals('You have requested this route $count time(s).'),
23+
),
24+
);
25+
});
26+
});
27+
}

0 commit comments

Comments
 (0)