Skip to content

Commit 0076de1

Browse files
committed
test(): added tests for enum capitalization
1 parent c5e5cfe commit 0076de1

File tree

8 files changed

+96
-5
lines changed

8 files changed

+96
-5
lines changed

tests/code-first/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Module } from '@nestjs/common';
22
import { GraphQLModule } from '../../lib';
3+
import { CatsModule } from './cats/cats.module';
34
import { DirectionsModule } from './directions/directions.module';
45
import { RecipesModule } from './recipes/recipes.module';
56

67
@Module({
78
imports: [
89
RecipesModule,
910
DirectionsModule,
11+
CatsModule,
1012
GraphQLModule.forRoot({
1113
debug: false,
1214
installSubscriptionHandlers: true,

tests/code-first/cats/cats.module.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Module } from '@nestjs/common';
2+
import { CatsResolver } from './cats.resolver';
3+
4+
@Module({
5+
providers: [CatsResolver],
6+
})
7+
export class CatsModule {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Args, Query, Resolver } from '../../../lib';
2+
import { CatType } from '../enums/cat-type.enum';
3+
4+
@Resolver()
5+
export class CatsResolver {
6+
@Query((returns) => CatType)
7+
catType(
8+
@Args({ name: 'catType', type: () => CatType })
9+
catType: CatType,
10+
): CatType {
11+
return catType;
12+
}
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { registerEnumType } from '../../../lib/type-factories/register-enum-type.factory';
2+
export enum CatType {
3+
PersianCat = 'persian-cat',
4+
MaineCoon = 'maine-coon',
5+
Ragdoll = 'ragdoll',
6+
}
7+
8+
registerEnumType(CatType, {
9+
name: 'CatType',
10+
description: 'Distinguish cats',
11+
mapToUppercase: true,
12+
});

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import {
66
IntrospectionField,
77
IntrospectionSchema,
88
printSchema,
9-
TypeKind
9+
TypeKind,
1010
} from 'graphql';
1111
import { GRAPHQL_SDL_FILE_HEADER } from '../../lib/graphql.constants';
1212
import {
1313
GraphQLSchemaBuilderModule,
14-
GraphQLSchemaFactory
14+
GraphQLSchemaFactory,
1515
} from '../../lib/schema-builder';
1616
import { DirectionsResolver } from '../code-first/directions/directions.resolver';
1717
import { AbstractResolver } from '../code-first/other/abstract.resolver';
@@ -25,9 +25,10 @@ import {
2525
getQuery,
2626
getQueryByName,
2727
getSubscription,
28-
getSubscriptionByName
28+
getSubscriptionByName,
2929
} from '../utils/introspection-schema.utils';
3030
import { printedSchemaSnapshot } from '../utils/printed-schema.snapshot';
31+
import { CatsResolver } from '../code-first/cats/cats.resolver';
3132

3233
describe('Code-first - schema factory', () => {
3334
let schemaFactory: GraphQLSchemaFactory;
@@ -49,6 +50,7 @@ describe('Code-first - schema factory', () => {
4950
[
5051
RecipesResolver,
5152
DirectionsResolver,
53+
CatsResolver,
5254
AbstractResolver,
5355
IRecipesResolver,
5456
],
@@ -66,17 +68,18 @@ describe('Code-first - schema factory', () => {
6668
printedSchemaSnapshot,
6769
);
6870
});
69-
it('should define 5 queries', async () => {
71+
it('should define 6 queries', async () => {
7072
const type = getQuery(introspectionSchema);
7173

72-
expect(type.fields.length).toEqual(5);
74+
expect(type.fields.length).toEqual(6);
7375
expect(type.fields.map((item) => item.name)).toEqual(
7476
expect.arrayContaining([
7577
'recipes',
7678
'search',
7779
'categories',
7880
'move',
7981
'recipe',
82+
'catType',
8083
]),
8184
);
8285
});
@@ -152,6 +155,40 @@ describe('Code-first - schema factory', () => {
152155
);
153156
});
154157

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { Module } from '@nestjs/common';
22
import { GraphQLModule } from '../../lib';
33
import { RecipesModule } from '../code-first/recipes/recipes.module';
44
import { DirectionsModule } from '../code-first/directions/directions.module';
5+
import { CatsModule } from '../code-first/cats/cats.module';
56

67
@Module({
78
imports: [
89
RecipesModule,
910
DirectionsModule,
11+
CatsModule,
1012
GraphQLModule.forRoot({
1113
autoSchemaFile: 'schema.graphql',
1214
sortSchema: true,

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

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

78
@Module({
89
imports: [
910
RecipesModule,
1011
DirectionsModule,
12+
CatsModule,
1113
GraphQLModule.forRoot({
1214
autoSchemaFile: 'schema.graphql',
1315
transformSchema: (schema: GraphQLSchema) =>

tests/utils/printed-schema.snapshot.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type Query {
6767
take: Int = 25
6868
): [Recipe!]!
6969
move(direction: Direction!): Direction!
70+
catType(catType: CatType!): CatType!
7071
}
7172
7273
"""Search result description"""
@@ -82,6 +83,13 @@ enum Direction {
8283
Sideways @deprecated(reason: "Replaced with Left or Right")
8384
}
8485
86+
"""Distinguish cats"""
87+
enum CatType {
88+
PERSIAN_CAT
89+
MAINE_COON
90+
RAGDOLL
91+
}
92+
8593
type Mutation {
8694
addRecipe(newRecipeData: NewRecipeInput!): Recipe!
8795
removeRecipe(id: String!): Boolean!
@@ -105,6 +113,13 @@ export const sortedPrintedSchemaSnapshot = `# ----------------------------------
105113
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
106114
# ------------------------------------------------------
107115
116+
"""Distinguish cats"""
117+
enum CatType {
118+
MAINE_COON
119+
PERSIAN_CAT
120+
RAGDOLL
121+
}
122+
108123
type Category {
109124
description: String!
110125
name: String!
@@ -154,6 +169,7 @@ input NewRecipeInput {
154169
}
155170
156171
type Query {
172+
catType(catType: CatType!): CatType!
157173
categories: [Category!]!
158174
move(direction: Direction!): Direction!
159175

0 commit comments

Comments
 (0)