Skip to content

Commit 96ab047

Browse files
committed
added some tests and reworked expected output
1 parent f3c1118 commit 96ab047

File tree

2 files changed

+101
-59
lines changed

2 files changed

+101
-59
lines changed

src/analysis/buildTypeWeights.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { GraphQLSchema } from 'graphql/type/schema';
88
function buildTypeWeightsFromSchema(
99
schema: GraphQLSchema,
1010
typeWeightsConfig: TypeWeightConfig = {
11-
query: 1,
1211
mutation: 10,
1312
object: 1,
1413
scalar: 0,

test/analysis/buildTypeWeights.test.ts

Lines changed: 101 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ import buildTypeWeightsFromSchema from '../../src/analysis/buildTypeWeights';
55
describe('Test buildTypeWeightsFromSchema function', () => {
66
let schema: GraphQLSchema;
77

8-
describe('creates the type weight object from graphql schema object with...', () => {
8+
describe('creates the "type weight object" from a graphql schema object with...', () => {
99
test('a single query type', () => {
1010
schema = buildSchema(`
11-
Query {
11+
type Query {
1212
name: String
1313
email: String
14-
}`);
15-
16-
const typeWeightObject = buildTypeWeightsFromSchema(schema);
14+
}
15+
`);
1716

18-
expect(typeWeightObject).toEqual({
17+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
1918
Query: {
2019
weight: 1,
2120
fields: {
@@ -28,25 +27,21 @@ describe('Test buildTypeWeightsFromSchema function', () => {
2827

2928
test('multiple types', () => {
3029
schema = buildSchema(`
31-
User {
30+
type User {
3231
name: String
3332
email: String
3433
}
3534
36-
Movie {
35+
type Movie {
3736
name: String
3837
director: String
39-
}`);
40-
41-
const typeWeightObject = buildTypeWeightsFromSchema(schema);
38+
}
39+
`);
4240

43-
expect(typeWeightObject).toEqual({
41+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
4442
User: {
4543
weight: 1,
46-
fields: {
47-
name: 0,
48-
email: 0,
49-
},
44+
fields: {},
5045
},
5146
Movie: {
5247
weight: 1,
@@ -60,27 +55,26 @@ describe('Test buildTypeWeightsFromSchema function', () => {
6055

6156
test('nested object types', () => {
6257
schema = buildSchema(`
63-
Query {
64-
User {
65-
name: String
66-
email: String
67-
}
68-
69-
Movie {
70-
name: String
71-
director: User
72-
}
73-
}`);
74-
75-
const typeWeightObject = buildTypeWeightsFromSchema(schema);
58+
type Query {
59+
user: User
60+
movie: Movie
61+
}
62+
63+
type User {
64+
name: String
65+
email: String
66+
}
67+
68+
type Movie {
69+
name: String
70+
director: User
71+
}
72+
`);
7673

77-
expect(typeWeightObject).toEqual({
74+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
7875
Query: {
7976
weight: 1,
80-
fields: {
81-
User: 1,
82-
Movie: 1,
83-
},
77+
fields: {},
8478
},
8579
User: {
8680
weight: 1,
@@ -93,37 +87,62 @@ describe('Test buildTypeWeightsFromSchema function', () => {
9387
weight: 1,
9488
fields: {
9589
name: 0,
96-
director: 1,
90+
},
91+
},
92+
});
93+
});
94+
95+
test('all scalar types', () => {
96+
schema = buildSchema(`
97+
type Test {
98+
num: Int,
99+
id: ID,
100+
float: Float,
101+
bool: Boolean,
102+
string: String
103+
}
104+
`);
105+
106+
expect(buildTypeWeightsFromSchema(schema)).toEqual({
107+
Test: {
108+
weight: 1,
109+
fields: {
110+
num: 0,
111+
id: 0,
112+
float: 0,
113+
bool: 0,
114+
string: 0,
97115
},
98116
},
99117
});
100118
});
101119
});
102120

103-
describe('changes type weight object with user configuration of query of...', () => {
121+
describe('changes "type weight object" type weights with user configuration of...', () => {
104122
let expectedOutput: TypeWeightObject;
105123

106124
beforeEach(() => {
107125
schema = buildSchema(`
108-
Query {
109-
User {
110-
name: String
111-
email: String
112-
}
113-
114-
Movie {
115-
name: String
116-
director: User
117-
}
118-
}`);
126+
type Query {
127+
user: User
128+
movie: Movie
129+
}
130+
131+
type User {
132+
name: String
133+
email: String
134+
}
135+
136+
type Movie {
137+
name: String
138+
director: User
139+
}
140+
`);
119141

120142
expectedOutput = {
121143
Query: {
122144
weight: 1,
123-
fields: {
124-
User: 1,
125-
Movie: 1,
126-
},
145+
fields: {},
127146
},
128147
User: {
129148
weight: 1,
@@ -136,13 +155,13 @@ describe('Test buildTypeWeightsFromSchema function', () => {
136155
weight: 1,
137156
fields: {
138157
name: 0,
139-
director: 1,
140158
},
141159
},
142160
};
143161
});
144162

145-
test('query parameter', () => {
163+
// this is only if we choose to have 'query' as its own property (seperate from object types) in the user configuration options
164+
xtest('query parameter', () => {
146165
const typeWeightObject = buildTypeWeightsFromSchema(schema, {
147166
query: 2,
148167
});
@@ -151,19 +170,43 @@ describe('Test buildTypeWeightsFromSchema function', () => {
151170
expect(typeWeightObject).toEqual({ expectedOutput });
152171
});
153172

154-
test('all objects types', () => {
173+
test('object parameter', () => {
155174
const typeWeightObject = buildTypeWeightsFromSchema(schema, {
156175
object: 2,
157176
});
158-
expectedOutput.query.fields.user = 2;
159-
expectedOutput.query.fields.movie = 2;
177+
160178
expectedOutput.user.weight = 2;
161179
expectedOutput.movie.weight = 2;
162-
expectedOutput.movie.fields.director = 2;
163180

164181
expect(typeWeightObject).toEqual({ expectedOutput });
165182
});
166183
});
167184

168-
describe('throws an error on...', () => {});
185+
describe('throws an error if...', () => {
186+
beforeEach(() => {
187+
schema = buildSchema(`
188+
type Query {
189+
user: User
190+
movie: Movie
191+
}
192+
193+
type User {
194+
name: String
195+
email: String
196+
}
197+
198+
type Movie {
199+
name: String
200+
director: User
201+
}
202+
`);
203+
});
204+
205+
test('user configures the type weights with negative numbers', () => {
206+
expect(buildTypeWeightsFromSchema(schema, { object: -1 })).toThrow();
207+
expect(buildTypeWeightsFromSchema(schema, { mutation: -1 })).toThrow();
208+
expect(buildTypeWeightsFromSchema(schema, { connection: -1 })).toThrow();
209+
expect(buildTypeWeightsFromSchema(schema, { scalar: -1 })).toThrow();
210+
});
211+
});
169212
});

0 commit comments

Comments
 (0)