Skip to content

Commit a2cc1c4

Browse files
committed
test(): added tests for enum capitalization
1 parent 628e73c commit a2cc1c4

File tree

7 files changed

+86
-5
lines changed

7 files changed

+86
-5
lines changed

packages/apollo/tests/code-first/app.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import { Module } from '@nestjs/common';
22
import { GraphQLModule } from '@nestjs/graphql';
33
import { ApolloDriverConfig } from '../../lib';
44
import { ApolloDriver } from '../../lib/drivers';
5+
import { CatsModule } from './cats/cats.module';
56
import { DirectionsModule } from './directions/directions.module';
67
import { RecipesModule } from './recipes/recipes.module';
78

89
@Module({
910
imports: [
1011
RecipesModule,
1112
DirectionsModule,
12-
GraphQLModule.forRoot<ApolloDriverConfig>({
13-
driver: ApolloDriver,
13+
CatsModule,
14+
GraphQLModule.forRoot({
1415
debug: false,
1516
installSubscriptionHandlers: true,
1617
autoSchemaFile: true,
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import { Query, Resolver } from '@nestjs/graphql';
1+
import { Query, Resolver, Args } from '@nestjs/graphql';
2+
import { CatType } from '../enums/cat-type.enum';
23

34
@Resolver()
45
export class CatsResolver {
56
@Query((returns) => String)
67
getAnimalName(): string {
78
return 'cat';
89
}
10+
11+
@Resolver()
12+
@Query((returns) => CatType)
13+
catType(
14+
@Args({ name: 'catType', type: () => CatType })
15+
catType: CatType,
16+
): CatType {
17+
return catType;
18+
}
919
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { registerEnumType } from '@nestjs/graphql';
2+
3+
export enum CatType {
4+
PersianCat = 'persian-cat',
5+
MaineCoon = 'maine-coon',
6+
Ragdoll = 'ragdoll',
7+
}
8+
9+
registerEnumType(CatType, {
10+
name: 'CatType',
11+
description: 'Distinguish cats',
12+
mapToUppercase: true,
13+
});

packages/apollo/tests/e2e/code-first-schema.spec.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
getSubscriptionByName,
3030
} from '../utils/introspection-schema.utils';
3131
import { printedSchemaSnapshot } from '../utils/printed-schema.snapshot';
32+
import { CatsResolver } from '../code-first/cats/cats.resolver';
3233

3334
describe('Code-first - schema factory', () => {
3435
let schemaFactory: GraphQLSchemaFactory;
@@ -50,6 +51,7 @@ describe('Code-first - schema factory', () => {
5051
[
5152
RecipesResolver,
5253
DirectionsResolver,
54+
CatsResolver,
5355
AbstractResolver,
5456
IRecipesResolver,
5557
],
@@ -68,17 +70,18 @@ describe('Code-first - schema factory', () => {
6870
printedSchemaSnapshot,
6971
);
7072
});
71-
it('should define 5 queries', async () => {
73+
it('should define 6 queries', async () => {
7274
const type = getQuery(introspectionSchema);
7375

74-
expect(type.fields.length).toEqual(5);
76+
expect(type.fields.length).toEqual(6);
7577
expect(type.fields.map((item) => item.name)).toEqual(
7678
expect.arrayContaining([
7779
'recipes',
7880
'search',
7981
'categories',
8082
'move',
8183
'recipe',
84+
'catType',
8285
]),
8386
);
8487
});
@@ -154,6 +157,40 @@ describe('Code-first - schema factory', () => {
154157
);
155158
});
156159

160+
it('should define "CatType" enum to use CAPITALIZED_UNDERSCORE', () => {
161+
const type = introspectionSchema.types.find(
162+
({ name }) => name === 'CatType',
163+
);
164+
165+
expect(type).toEqual(
166+
expect.objectContaining({
167+
kind: TypeKind.ENUM,
168+
name: 'CatType',
169+
description: 'Distinguish cats',
170+
enumValues: [
171+
{
172+
deprecationReason: null,
173+
description: null,
174+
isDeprecated: false,
175+
name: 'PERSIAN_CAT',
176+
},
177+
{
178+
deprecationReason: null,
179+
description: null,
180+
isDeprecated: false,
181+
name: 'MAINE_COON',
182+
},
183+
{
184+
deprecationReason: null,
185+
description: null,
186+
isDeprecated: false,
187+
name: 'RAGDOLL',
188+
},
189+
],
190+
}),
191+
);
192+
});
193+
157194
it('should define "SearchResultUnion" union', () => {
158195
const type = introspectionSchema.types.find(
159196
({ name }) => name === 'SearchResultUnion',

packages/apollo/tests/graphql/sort-auto-schema.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { GraphQLModule } from '@nestjs/graphql';
33
import { ApolloDriver } from '../../lib/drivers';
44
import { DirectionsModule } from '../code-first/directions/directions.module';
55
import { RecipesModule } from '../code-first/recipes/recipes.module';
6+
import { CatsModule } from '../code-first/cats/cats.module';
67

78
@Module({
89
imports: [
910
RecipesModule,
1011
DirectionsModule,
12+
CatsModule,
1113
GraphQLModule.forRoot({
1214
driver: ApolloDriver,
1315
autoSchemaFile: 'schema.graphql',

packages/apollo/tests/graphql/transform-auto-schema-file.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { GraphQLSchema, lexicographicSortSchema } from 'graphql';
44
import { ApolloDriver } from '../../lib/drivers';
55
import { DirectionsModule } from '../code-first/directions/directions.module';
66
import { RecipesModule } from '../code-first/recipes/recipes.module';
7+
import { CatsModule } from '../code-first/cats/cats.module';
78

89
@Module({
910
imports: [
1011
RecipesModule,
1112
DirectionsModule,
13+
CatsModule,
1214
GraphQLModule.forRoot({
1315
driver: ApolloDriver,
1416
autoSchemaFile: 'schema.graphql',

packages/apollo/tests/utils/printed-schema.snapshot.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type Query {
7575
take: Int = 25
7676
): [Recipe!]!
7777
move(direction: Direction!): Direction!
78+
catType(catType: CatType!): CatType!
7879
}
7980
8081
"""Search result description"""
@@ -90,6 +91,13 @@ enum Direction {
9091
Sideways @deprecated(reason: "Replaced with Left or Right")
9192
}
9293
94+
"""Distinguish cats"""
95+
enum CatType {
96+
PERSIAN_CAT
97+
MAINE_COON
98+
RAGDOLL
99+
}
100+
93101
type Mutation {
94102
addRecipe(newRecipeData: NewRecipeInput!): Recipe!
95103
removeRecipe(id: String!): Boolean!
@@ -113,6 +121,13 @@ export const sortedPrintedSchemaSnapshot = `# ----------------------------------
113121
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
114122
# ------------------------------------------------------
115123
124+
"""Distinguish cats"""
125+
enum CatType {
126+
MAINE_COON
127+
PERSIAN_CAT
128+
RAGDOLL
129+
}
130+
116131
type Category {
117132
description: String!
118133
name: String!
@@ -162,6 +177,7 @@ input NewRecipeInput {
162177
}
163178
164179
type Query {
180+
catType(catType: CatType!): CatType!
165181
categories: [Category!]!
166182
move(direction: Direction!): Direction!
167183

0 commit comments

Comments
 (0)