platform/graphql_schema/test/validation_test.dart

54 lines
1.3 KiB
Dart
Raw Normal View History

import 'package:graphql_schema/graphql_schema.dart';
import 'package:test/test.dart';
void main() {
2018-08-03 22:06:02 +00:00
var typeType = new GraphQLEnumType('Type', [
'FIRE',
'WATER',
'GRASS',
]);
var pokemonType = objectType('Pokémon', fields: [
2018-08-03 22:06:02 +00:00
field(
'name',
type: graphQLString.nonNullable(),
),
field(
'type',
type: typeType,
),
]);
var isValidPokemon = predicate(
(x) =>
pokemonType.validate('@root', x as Map<String, dynamic>).successful,
'is a valid Pokémon');
var throwsATypeError =
throwsA(predicate((x) => x is TypeError, 'is a type error'));
test('mismatched scalar type', () {
expect(() => pokemonType.validate('@root', {'name': 24}), throwsATypeError);
});
test('empty passed for non-nullable', () {
expect(<String, dynamic>{}, isNot(isValidPokemon));
});
test('null passed for non-nullable', () {
expect({'name': null}, isNot(isValidPokemon));
});
test('rejects extraneous fields', () {
expect({'name': 'Vulpix', 'foo': 'bar'}, isNot(isValidPokemon));
});
2018-08-03 22:06:02 +00:00
test('enum accepts valid value', () {
expect(typeType.validate('@root', 'FIRE').successful, true);
});
test('enum rejects invalid value', () {
expect(typeType.validate('@root', 'POISON').successful, false);
});
}