Skip to content

fragment generation tests #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions gql_example_build/test/fragments/shape_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ String getShapeInfo($Shape data) {
}
}

String getShapeInfoNoRefs($Shape data) {
final shape = data.shape;
final type = shape.$__typename;
final area = shape.area;

final square = shape.asSquare();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smkhalsa, here's the API I originally imagined. What do you think? I like this approach because it lets user avoid using generated classes directly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor note: if we did this, we'd probably want to prefix the asSubclass() methods with $ to avoid collisions with data fields.

What I like about using the generated subclasses directly is that if (class is classAsSubclass) feels intuitive and doesn't require any additional typecasting.

That said, if we did go this route, it would feel more intuitive to me to also have a $isSubclass property rather than checking for null with the $asSubclass() method.

if (shape.$isSquare) {
  final square = shape.$asSquare();
  return "$type(area: $area, sideLength: ${square.sideLength})";
}

if (shape.$isRectangle) {
  final rectangle = shape.$asRectangle();
  return "$type(area: $area, sideLengthA: ${rectangle.sideLengthA}, sideLengthB: ${rectangle.sideLengthB})";
}

if (square != null) {
return "$type(area: $area, sideLength: ${square.sideLength})";
}

final rectangle = shape.asRectangle();
if (rectangle != null) {
return "$type(area: $area, sideLengthA: ${rectangle.sideLengthA}, sideLengthB: ${rectangle.sideLengthB})";
}
}

void main() {
group("shape", () {
test("square", () async {
Expand Down Expand Up @@ -48,4 +64,37 @@ void main() {
);
});
});

group("shape without ref to generated classes", () {
test("square", () async {
const shapeData = <String, dynamic>{
"shape": <String, dynamic>{
"__typename": "Square",
"area": 4,
"sideLength": 2,
},
};

expect(
getShapeInfoNoRefs($Shape(shapeData)),
"Square(area: 4.0, sideLength: 2.0)",
);
});

test("rectangle", () async {
const shapeData = <String, dynamic>{
"shape": <String, dynamic>{
"__typename": "Rectangle",
"area": 3,
"sideLengthA": 3,
"sideLengthB": 1,
},
};

expect(
getShapeInfoNoRefs($Shape(shapeData)),
"Rectangle(area: 3.0, sideLengthA: 3.0, sideLengthB: 1.0)",
);
});
});
}