Skip to content

Commit d4f7dd3

Browse files
committed
add jest-plugin-set and rework tests to utilize set
- this offers better flow for writing tests and nested describes changing the test setup.
1 parent 71ba258 commit d4f7dd3

File tree

3 files changed

+63
-26
lines changed

3 files changed

+63
-26
lines changed

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"url": "git+https://github.com/f/graphql-client.git"
2121
},
2222
"devDependencies": {
23+
"babel-runtime": "^6.26.0",
2324
"jest": "^27.4.5",
25+
"jest-plugin-set": "^2.9.0",
2426
"jest-mock-random": "^1.1.1",
2527
"uglify-js": "^3.14.5"
2628
},
@@ -36,5 +38,10 @@
3638
"bugs": {
3739
"url": "https://github.com/f/graphql-client/issues"
3840
},
39-
"homepage": "https://github.com/f/graphql-client#readme"
41+
"homepage": "https://github.com/f/graphql-client#readme",
42+
"jest": {
43+
"setupFiles": [
44+
"jest-plugin-set/setup"
45+
]
46+
}
4047
}

test/test.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// to fake out graphql to use XHR so we can stub it
1+
// to fake out graphql to use XHR, so we can stub it
22
global.XMLHttpRequest = null;
33

44
let {mockRandom} = require('jest-mock-random');
@@ -27,33 +27,33 @@ function mockXHR(status, data) {
2727
return xhrMockObj;
2828
}
2929

30+
/* global client, method, url, fetchPost, fetchComments */
3031
describe('graphql.js', () => {
31-
let client = null;
32-
33-
beforeEach(() => {
34-
client = graphql(null, {
35-
method: 'put',
32+
set('url', () => null);
33+
set('method', () => 'put');
34+
set('client', () =>
35+
graphql(url, {
36+
method: method,
3637
asJSON: true,
3738
fragments: {
3839
user: 'on User {name}',
3940
auth: {
4041
user: 'on User {token, ...user}'
4142
}
4243
}
43-
});
44-
client.fragment({
45-
auth: {
46-
error: 'on Error {messages}'
47-
}
48-
});
49-
});
44+
}));
5045

5146
it('client should be a function', () => {
5247
expect(typeof client).toBe('function');
5348
});
5449

5550
describe('.fragment()', () => {
5651
it('registers a new fragment', () => {
52+
client.fragment({
53+
auth: {
54+
error: 'on Error {messages}'
55+
}
56+
});
5757

5858
expect(client.fragment('auth.error')).toBe(
5959
'fragment auth_error on Error {messages}'
@@ -77,7 +77,13 @@ describe('graphql.js', () => {
7777
);
7878
});
7979

80-
it('returns returns new fragments registered as well', () => {
80+
it('returns new registered fragments as well', () => {
81+
client.fragment({
82+
auth: {
83+
error: 'on Error {messages}'
84+
}
85+
});
86+
8187
expect(client.fragments()).toStrictEqual(
8288
expect.objectContaining({
8389
auth_error: '\nfragment auth_error on Error {messages}',
@@ -98,6 +104,12 @@ describe('graphql.js', () => {
98104
}`;
99105

100106
it('mixes in the requested fragments and sets the data types', () => {
107+
client.fragment({
108+
auth: {
109+
error: 'on Error {messages}'
110+
}
111+
});
112+
101113
var expectedQuery = `query ($name: String!, $bool: Boolean!, $int: Int!, $float: Float!, $id: ID!, $user_id: Int!, $postID: ID!, $custom_id: CustomType!, $customId: ID!, $target: [ID!]!) {
102114
user(name: $name, bool: $bool, int: $int, id: $id) {
103115
... auth_user
@@ -170,28 +182,23 @@ fragment auth_error on Error {messages}`;
170182
});
171183

172184
describe('query testing', () => {
173-
let fetchPost = null;
174-
let fetchComments = null;
175-
176-
beforeEach(() => {
177-
client.setUrl('https://example.org');
178-
fetchPost = client.query(`{
185+
set('fetchPost', () => client.query(`{
179186
post(id: $id) {
180187
id
181188
title
182189
text
183190
}
184-
}`);
185-
186-
fetchComments = client.query(`{
191+
}`));
192+
set('fetchComments', () => client.query(`{
187193
commentsOfPost: comments(postId: $postId) {
188194
comment
189195
owner {
190196
name
191197
}
192198
}
193-
}`);
194-
});
199+
}`));
200+
201+
set('url', () => 'https://example.org');
195202

196203
describe('when executing the queries normally', () => {
197204
it('sends a network request right away', () => {

yarn.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,14 @@ babel-preset-jest@^27.5.1:
767767
babel-plugin-jest-hoist "^27.5.1"
768768
babel-preset-current-node-syntax "^1.0.0"
769769

770+
babel-runtime@^6.26.0:
771+
version "6.26.0"
772+
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe"
773+
integrity sha512-ITKNuq2wKlW1fJg9sSW52eepoYgZBggvOAHC0u/CYu/qxQ9EVzThCgR69BnSXLHjy2f7SY5zaQ4yt7H9ZVxY2g==
774+
dependencies:
775+
core-js "^2.4.0"
776+
regenerator-runtime "^0.11.0"
777+
770778
balanced-match@^1.0.0:
771779
version "1.0.2"
772780
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
@@ -928,6 +936,11 @@ convert-source-map@^1.4.0, convert-source-map@^1.6.0, convert-source-map@^1.7.0:
928936
dependencies:
929937
safe-buffer "~5.1.1"
930938

939+
core-js@^2.4.0:
940+
version "2.6.12"
941+
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.12.tgz#d9333dfa7b065e347cc5682219d6f690859cc2ec"
942+
integrity sha512-Kb2wC0fvsWfQrgk8HU5lW6U/Lcs8+9aaYcy4ZFc6DDlo4nZ7n70dEgE5rtR0oG6ufKDUnrwfWL1mXR5ljDatrQ==
943+
931944
cross-spawn@^7.0.3:
932945
version "7.0.3"
933946
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
@@ -1606,6 +1619,11 @@ jest-mock@^27.5.1:
16061619
"@jest/types" "^27.5.1"
16071620
"@types/node" "*"
16081621

1622+
jest-plugin-set@^2.9.0:
1623+
version "2.9.0"
1624+
resolved "https://registry.yarnpkg.com/jest-plugin-set/-/jest-plugin-set-2.9.0.tgz#95d4cb0e25e117b8a0b19846598887bf4bb95fca"
1625+
integrity sha512-7woJ1+EvpjQJ0CdWert87b5/HjvNZruoV8qDeApJtf4ZPGMflCqPeOOX38pA8CBo34QyoLpkaV2hVM0wVLMS4Q==
1626+
16091627
jest-pnp-resolver@^1.2.2:
16101628
version "1.2.2"
16111629
resolved "https://registry.yarnpkg.com/jest-pnp-resolver/-/jest-pnp-resolver-1.2.2.tgz#b704ac0ae028a89108a4d040b3f919dfddc8e33c"
@@ -2117,6 +2135,11 @@ react-is@^17.0.1:
21172135
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
21182136
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==
21192137

2138+
regenerator-runtime@^0.11.0:
2139+
version "0.11.1"
2140+
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz#be05ad7f9bf7d22e056f9726cee5017fbf19e2e9"
2141+
integrity sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==
2142+
21202143
require-directory@^2.1.1:
21212144
version "2.1.1"
21222145
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"

0 commit comments

Comments
 (0)