Skip to content

Commit 4b456ee

Browse files
committed
Split tests into seperate files
1 parent fcfcec7 commit 4b456ee

9 files changed

+730
-680
lines changed

src/__tests__/aliases.tests.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
import { expect } from 'chai';
3+
import { jsonToGraphQLQuery } from '../';
4+
5+
describe('jsonToGraphQL() - aliases', () => {
6+
7+
it('uses aliases for fields', () => {
8+
// Deprecated...
9+
const query = {
10+
query: {
11+
Posts: {
12+
__alias: 'lorem',
13+
__args: {
14+
arg1: 20,
15+
},
16+
id: true
17+
}
18+
}
19+
};
20+
expect(jsonToGraphQLQuery(query)).to.equal(
21+
'query { lorem: Posts (arg1: 20) { id } }'
22+
);
23+
});
24+
25+
it('supports multiple aliases for one type', () => {
26+
const query = {
27+
query: {
28+
lorem: {
29+
__aliasFor: 'Posts',
30+
__args: {
31+
arg1: 20,
32+
},
33+
id: true
34+
},
35+
larem: {
36+
__aliasFor: 'Posts',
37+
__args: {
38+
arg2: 10,
39+
},
40+
id: true
41+
}
42+
}
43+
};
44+
expect(jsonToGraphQLQuery(query)).to.equal(
45+
'query { lorem: Posts (arg1: 20) { id } larem: Posts (arg2: 10) { id } }'
46+
);
47+
});
48+
49+
});

src/__tests__/arguments.tests.ts

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
2+
import { expect } from 'chai';
3+
import { jsonToGraphQLQuery } from '../';
4+
5+
describe('jsonToGraphQL() - arguments', () => {
6+
7+
it('converts a query with simple arguments', () => {
8+
const query = {
9+
query: {
10+
Posts: {
11+
__args: {
12+
orderBy: 'post_date',
13+
userId: 12
14+
},
15+
id: true,
16+
title: true,
17+
post_date: true
18+
}
19+
}
20+
};
21+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
22+
`query {
23+
Posts (orderBy: "post_date", userId: 12) {
24+
id
25+
title
26+
post_date
27+
}
28+
}`);
29+
});
30+
31+
it('converts a query with JSON arguments', () => {
32+
const query = {
33+
query: {
34+
Posts: {
35+
__args: {
36+
where: {
37+
published: true,
38+
rating: { _gt: 3 }
39+
},
40+
orderBy: 'post_date'
41+
},
42+
id: true,
43+
title: true,
44+
post_date: true
45+
}
46+
}
47+
};
48+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
49+
`query {
50+
Posts (where: {published: true, rating: {_gt: 3}}, orderBy: "post_date") {
51+
id
52+
title
53+
post_date
54+
}
55+
}`);
56+
});
57+
58+
it('converts a query with JSON arguments containing arrays of objects', () => {
59+
const query = {
60+
query: {
61+
Posts: {
62+
__args: {
63+
or: [
64+
{ published: true },
65+
{ rating: [{ _gt: 3 }] }
66+
],
67+
orderBy: 'post_date'
68+
},
69+
id: true,
70+
title: true,
71+
post_date: true
72+
}
73+
}
74+
};
75+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
76+
`query {
77+
Posts (or: [{published: true}, {rating: [{_gt: 3}]}], orderBy: "post_date") {
78+
id
79+
title
80+
post_date
81+
}
82+
}`);
83+
});
84+
85+
it('converts a query with null arguments and nested nulls', () => {
86+
const query = {
87+
query: {
88+
Posts: {
89+
__args: {
90+
where: {
91+
id: null,
92+
},
93+
orderBy: null
94+
},
95+
id: true,
96+
title: true,
97+
post_date: true
98+
}
99+
}
100+
} as any;
101+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
102+
`query {
103+
Posts (where: {id: null}, orderBy: null) {
104+
id
105+
title
106+
post_date
107+
}
108+
}`);
109+
});
110+
111+
it('converts a query with nested objects and arguments', () => {
112+
const query = {
113+
query: {
114+
Posts: {
115+
__args: {
116+
arg1: 20,
117+
arg2: 'flibble'
118+
},
119+
id: true,
120+
title: true,
121+
comments: {
122+
__args: {
123+
offensiveOnly: true
124+
},
125+
id: true,
126+
comment: true,
127+
user: true
128+
}
129+
}
130+
}
131+
};
132+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
133+
`query {
134+
Posts (arg1: 20, arg2: "flibble") {
135+
id
136+
title
137+
comments (offensiveOnly: true) {
138+
id
139+
comment
140+
user
141+
}
142+
}
143+
}`);
144+
});
145+
146+
it('works with pretty mode turned off', () => {
147+
const query = {
148+
query: {
149+
Posts: {
150+
__args: {
151+
arg1: 20,
152+
arg2: 'flibble'
153+
},
154+
id: true,
155+
title: true
156+
}
157+
}
158+
};
159+
expect(jsonToGraphQLQuery(query)).to.equal(
160+
'query { Posts (arg1: 20, arg2: "flibble") { id title } }'
161+
);
162+
});
163+
164+
});

src/__tests__/directives.tests.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
2+
import { expect } from 'chai';
3+
import { jsonToGraphQLQuery } from '../';
4+
5+
describe('jsonToGraphQL() - directives', () => {
6+
7+
it('converts a simple query with args and directives with no arguments', () => {
8+
const query = {
9+
query: {
10+
Posts: {
11+
__args: {
12+
where: {
13+
id: 10,
14+
},
15+
orderBy: 'flibble'
16+
},
17+
__directives: {
18+
client: true
19+
},
20+
id: true,
21+
title: true,
22+
post_date: true
23+
}
24+
}
25+
} as any;
26+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
27+
`query {
28+
Posts @client (where: {id: 10}, orderBy: "flibble") {
29+
id
30+
title
31+
post_date
32+
}
33+
}`);
34+
});
35+
36+
it('converts a complex query with directives with no arguments', () => {
37+
const query = {
38+
query: {
39+
diet: {
40+
__directives: {
41+
client: true
42+
},
43+
id: 'diet',
44+
options: {
45+
mood: {
46+
category: 'Diet',
47+
id: 'mood',
48+
selected: true,
49+
},
50+
weight: {
51+
category: 'Diet',
52+
icon: 'fa fa-question-circle',
53+
id: 'weight',
54+
selected: false,
55+
text: 'Weight'
56+
},
57+
},
58+
title: 'Diet'
59+
},
60+
someOtherAbritraryKey: {
61+
__directives: {
62+
client: true
63+
},
64+
arb1: 'arbitrary value',
65+
arb2: 'some other arbitrary value'
66+
}
67+
}
68+
};
69+
const expected = 'query { diet @client { id options { ' +
70+
'mood { category id selected } weight { category icon id text } } ' +
71+
'title } someOtherAbritraryKey @client { arb1 arb2 } }';
72+
expect(jsonToGraphQLQuery(query)).to.equal(expected);
73+
});
74+
75+
// TODO: Need this test still? How to handle variables unless $ declared explicitly?
76+
// it('converts a JavaScript object into a valid query, including single directives ' +
77+
// 'with args, so long as any variables used are enclosed in a string with "$" included', () => {
78+
// interface ILooseObject { [key: string]: any; }
79+
// let input: ILooseObject = {
80+
// someOtherAbritraryKey: {
81+
// __typename: 'someArbitraryObjType',
82+
// arb1: 'arbitrary value',
83+
// arb2: 'some other arbitrary value'
84+
// }
85+
// };
86+
// Object.keys(input)
87+
// .filter(filterNonConfigFields)
88+
// .forEach((key) => {
89+
// input[key]['__directives'] = { include: {if: '$isAwesome'}, };
90+
// });
91+
// input = {query: input};
92+
// const expected = 'query { someOtherAbritraryKey @include(if: $isAwesome) { arb1 arb2 } }';
93+
// expect(jsonToGraphQLQuery(input)).to.equal(expected);
94+
// });
95+
96+
// TODO
97+
// it('converts a JavaScript object into a valid query, including *multiple* directives ' +
98+
// 'with args, so long as any variables used are enclosed in a string with "$" included', () => {
99+
// });
100+
101+
// TODO
102+
// it('creates a query, stripping/ignoring certain, specified keys', () => {
103+
// // Example usage: jsonToGraphqlQuery(preInput, { keysToStrip: ['__typename'] });
104+
// });
105+
106+
});

src/__tests__/falsykeys.tests.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
import { expect } from 'chai';
3+
import { jsonToGraphQLQuery } from '../';
4+
5+
describe('jsonToGraphQL() - falsy keys', () => {
6+
7+
it('does not include fields which value is false', () => {
8+
const query = {
9+
query: {
10+
Posts: {
11+
__args: {
12+
a: false
13+
},
14+
id: true,
15+
name: false
16+
},
17+
Lorem: {
18+
id: true
19+
},
20+
Ipsum: false
21+
}
22+
};
23+
expect(jsonToGraphQLQuery(query)).to.equal(
24+
'query { Posts (a: false) { id } Lorem { id } }'
25+
);
26+
});
27+
28+
it('includes fields with falsy values if includeFalsyKeys is true', () => {
29+
const query = {
30+
query: {
31+
Posts: {
32+
__args: {
33+
a: false
34+
},
35+
id: '',
36+
name: ''
37+
},
38+
Lorem: {
39+
id: ''
40+
},
41+
Ipsum: false
42+
}
43+
};
44+
expect(jsonToGraphQLQuery(query, { includeFalsyKeys: true })).to.equal(
45+
'query { Posts (a: false) { id name } Lorem { id } Ipsum }'
46+
);
47+
});
48+
49+
});

0 commit comments

Comments
 (0)