Skip to content

Commit 69929fa

Browse files
committed
test: refactor tests
1 parent ff8cc1a commit 69929fa

File tree

6 files changed

+160
-43
lines changed

6 files changed

+160
-43
lines changed

src/__tests__/__snapshots__/requireAstToSchema-test.ts.snap renamed to src/__tests__/__snapshots__/astToSchema-test.ts.snap

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`requireAstToSchema() Schema ../../examples/forTests/schema schema 1`] = `
3+
exports[`astToSchema() Schema ./__testSchema__ schema 1`] = `
44
"# Provides default value for input field.
55
directive @default(value: JSON!) on INPUT_FIELD_DEFINITION
66
77
# The \`JSON\` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
88
scalar JSON
99
10-
type MeType {
11-
address: MeTypeAddress
12-
name: Boolean
13-
}
14-
15-
type MeTypeAddress {
16-
city: Boolean
17-
street: Boolean
18-
}
19-
2010
type Mutation {
2111
logs: MutationLogs
2212
auth: MutationAuth
@@ -26,6 +16,11 @@ type Mutation {
2616
type MutationAuth {
2717
login: Boolean
2818
logout: Boolean
19+
nested: MutationAuthNested
20+
}
21+
22+
type MutationAuthNested {
23+
method: Boolean
2924
}
3025
3126
type MutationLogs {
@@ -42,12 +37,34 @@ type MutationUser {
4237
}
4338
4439
type Query {
45-
me: MeType
40+
field: String
41+
me(arg: Int): QueryMe
4642
some: QuerySome
4743
user: UserAwesomeType
44+
auth: QueryAuth
45+
}
46+
47+
type QueryAuth {
48+
isLoggedIn: Boolean
49+
nested: QueryAuthNested
50+
}
51+
52+
type QueryAuthNested {
53+
method: Boolean
54+
}
55+
56+
type QueryMe {
57+
address: QueryMeAddress
58+
name: String
59+
}
60+
61+
type QueryMeAddress {
62+
city: Boolean
63+
street: Boolean
4864
}
4965
5066
type QuerySome {
67+
nested: Int
5168
type: SomeIndexFileType
5269
}
5370

src/__tests__/__snapshots__/requireSchemaDirectory-test.ts.snap renamed to src/__tests__/__snapshots__/directoryToAst-test.ts.snap

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`requireSchemaDirectory() Schema ../../examples/forTests/schema mutation has proper values 1`] = `
3+
exports[`directoryToAst() Schema ./__testSchema__ mutation has proper values 1`] = `
44
Object {
55
"absPath": Any<String>,
66
"children": Object {
@@ -16,6 +16,9 @@ Object {
1616
"logout": ObjectContaining {
1717
"kind": "file",
1818
},
19+
"nested": ObjectContaining {
20+
"kind": "dir",
21+
},
1922
},
2023
"kind": "dir",
2124
"name": "auth",
@@ -49,10 +52,16 @@ Object {
4952
}
5053
`;
5154
52-
exports[`requireSchemaDirectory() Schema ../../examples/forTests/schema query has proper values 1`] = `
55+
exports[`directoryToAst() Schema ./__testSchema__ query has proper values 1`] = `
5356
Object {
5457
"absPath": Any<String>,
5558
"children": Object {
59+
"auth": ObjectContaining {
60+
"kind": "dir",
61+
},
62+
"field": ObjectContaining {
63+
"kind": "file",
64+
},
5665
"index": Object {
5766
"absPath": Any<String>,
5867
"code": Any<Object>,
@@ -81,6 +90,9 @@ Object {
8190
"kind": "dir",
8291
"name": "me",
8392
},
93+
"some.nested": ObjectContaining {
94+
"kind": "file",
95+
},
8496
"some.type.index": Any<Object>,
8597
"user": Object {
8698
"absPath": Any<String>,

src/__tests__/astToSchema-test.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { directoryToAst, getAstForDir, getAstForFile } from '../directoryToAst';
2+
import { astToSchema, createFields } from '../astToSchema';
3+
import { SchemaComposer } from 'graphql-compose';
4+
import { printSchema } from 'graphql/utilities';
5+
import path from 'path';
6+
7+
describe('astToSchema()', () => {
8+
describe('Schema ./__testSchema__', () => {
9+
const ast = directoryToAst(module, { relativePath: './__testSchema__' });
10+
const sc = astToSchema(ast);
11+
12+
it('should return schema composer', () => {
13+
expect(sc).toBeInstanceOf(SchemaComposer);
14+
});
15+
16+
it('schema', async () => {
17+
const printedSchema = await printSchema(sc.buildSchema(), { commentDescriptions: true });
18+
expect(printedSchema).toMatchSnapshot();
19+
});
20+
});
21+
22+
describe('createFields', () => {
23+
let sc: SchemaComposer<any>;
24+
beforeEach(() => {
25+
sc = new SchemaComposer();
26+
});
27+
28+
it('file: query/field.ts', () => {
29+
createFields(
30+
sc,
31+
getAstForFile(module, path.resolve(__dirname, './__testSchema__/query/field.ts')),
32+
'Query',
33+
sc.Query
34+
);
35+
expect(sc.Query.hasField('field')).toBeTruthy();
36+
expect(sc.Query.getFieldTypeName('field')).toBe('String');
37+
});
38+
39+
it('file: query/some.nested.ts', () => {
40+
createFields(
41+
sc,
42+
getAstForFile(module, path.resolve(__dirname, './__testSchema__/query/some.nested.ts')),
43+
'Query',
44+
sc.Query
45+
);
46+
expect(sc.Query.hasField('some')).toBeTruthy();
47+
expect(sc.Query.getFieldTypeName('some')).toBe('QuerySome');
48+
expect(sc.Query.getFieldOTC('some').hasField('nested')).toBeTruthy();
49+
expect(sc.Query.getFieldOTC('some').getFieldTypeName('nested')).toBe('Int');
50+
});
51+
52+
it('file: query/some.type.index.ts', () => {
53+
createFields(
54+
sc,
55+
getAstForFile(module, path.resolve(__dirname, './__testSchema__/query/some.type.index.ts')),
56+
'Query',
57+
sc.Query
58+
);
59+
expect(sc.Query.hasField('some')).toBeTruthy();
60+
expect(sc.Query.getFieldTypeName('some')).toBe('QuerySome');
61+
expect(sc.Query.getFieldOTC('some').hasField('type')).toBeTruthy();
62+
expect(sc.Query.getFieldOTC('some').getFieldTypeName('type')).toBe('SomeIndexFileType');
63+
expect((sc.Query.getFieldOTC('some').getFieldConfig('type') as any).resolve()).toEqual({
64+
awesomeValue: 'awesomeValue',
65+
});
66+
});
67+
68+
it('dir: query/me/', () => {
69+
createFields(
70+
sc,
71+
getAstForDir(module, path.resolve(__dirname, './__testSchema__/query/me')),
72+
'Query',
73+
sc.Query
74+
);
75+
expect(sc.Query.hasField('me')).toBeTruthy();
76+
expect(sc.Query.getFieldTypeName('me')).toBe('QueryMe');
77+
78+
// check query/me/index.ts
79+
// should provide args for `me` field
80+
expect(sc.Query.getFieldArgTypeName('me', 'arg')).toBe('Int');
81+
expect(() => (sc.Query.getFieldConfig('me') as any).resolve()).toThrow(
82+
'You should be the ADMIN'
83+
);
84+
// check that fields from sibling files was added
85+
expect(sc.Query.getFieldOTC('me').getFieldTypeName('name')).toBe('String');
86+
expect((sc.Query.getFieldOTC('me').getFieldConfig('name') as any).resolve()).toBe('nodkz');
87+
expect(
88+
sc.Query.getFieldOTC('me')
89+
.getFieldOTC('address')
90+
.getTypeName()
91+
).toBe('QueryMeAddress');
92+
expect(
93+
sc.Query.getFieldOTC('me')
94+
.getFieldOTC('address')
95+
.getFieldNames()
96+
.sort()
97+
).toEqual(['city', 'street']);
98+
});
99+
});
100+
101+
it('should properly set name for nested fields with dot notation', () => {
102+
const ast = directoryToAst(module, {
103+
relativePath: './__testSchema__',
104+
include: /query\.auth$|query\.auth\/nested/,
105+
});
106+
const sc = astToSchema(ast);
107+
expect(sc.Query.getFieldOTC('auth').getFieldTypeName('nested')).toBe('QueryAuthNested');
108+
});
109+
});

src/__tests__/requireSchemaDirectory-test.ts renamed to src/__tests__/directoryToAst-test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { requireSchemaDirectory } from '../requireSchemaDirectory';
1+
import { directoryToAst } from '../directoryToAst';
22

3-
describe('requireSchemaDirectory()', () => {
4-
describe('Schema ../../examples/forTests/schema', () => {
5-
const ast = requireSchemaDirectory(module, { relativePath: '../../examples/forTests/schema' });
3+
describe('directoryToAst()', () => {
4+
describe('Schema ./__testSchema__', () => {
5+
const ast = directoryToAst(module, { relativePath: './__testSchema__' });
66

77
it('should return root types', () => {
88
expect(Object.keys(ast)).toEqual(expect.arrayContaining(['query', 'mutation']));
@@ -14,6 +14,8 @@ describe('requireSchemaDirectory()', () => {
1414
kind: 'rootType',
1515
absPath: expect.any(String),
1616
children: {
17+
auth: expect.objectContaining({ kind: 'dir' }),
18+
field: expect.objectContaining({ kind: 'file' }),
1719
me: {
1820
absPath: expect.any(String),
1921
children: {
@@ -29,6 +31,7 @@ describe('requireSchemaDirectory()', () => {
2931
},
3032
},
3133
index: { kind: 'file', absPath: expect.any(String), code: expect.any(Object) },
34+
'some.nested': expect.objectContaining({ kind: 'file' }),
3235
'some.type.index': expect.any(Object),
3336
user: {
3437
absPath: expect.any(String),
@@ -54,6 +57,7 @@ describe('requireSchemaDirectory()', () => {
5457
index: expect.objectContaining({ kind: 'file' }),
5558
login: expect.objectContaining({ kind: 'file' }),
5659
logout: expect.objectContaining({ kind: 'file' }),
60+
nested: expect.objectContaining({ kind: 'dir' }),
5761
},
5862
},
5963
user: {

src/__tests__/index-test.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/__tests__/requireAstToSchema-test.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)