-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
fetch-graphql.test.js
113 lines (96 loc) · 2.45 KB
/
fetch-graphql.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import chalk from "chalk"
import fetchGraphQL, { moduleHelpers } from "../dist/utils/fetch-graphql"
import store from "../dist/store"
jest.mock(`async-retry`, () => {
return {
__esModule: true,
default: jest.fn((tryFunction) => {
const bail = (e) => {
throw e
}
return tryFunction(bail)
})
}
})
describe(`fetchGraphQL helper`, () => {
let mock
const panicMessages = []
beforeAll(() => {
const sharedError = `Request failed with status code`
try {
mock = jest.spyOn(moduleHelpers, `getHttp`).mockImplementation(() => {
return {
post: (_url, { query }) => {
if (typeof query === `number`) {
throw new Error(`${sharedError} ${query}`)
}
if (query === `wpgraphql-deactivated`) {
return Promise.resolve({
request: {},
headers: {
[`content-type`]: `text/html`,
},
})
}
return null
},
}
})
} catch (e) {
if (!e.message.includes(sharedError)) {
throw e
}
}
const fakeReporter = {
panic: ({ context: { sourceMessage } }) => {
panicMessages.push(sourceMessage)
},
}
store.dispatch.gatsbyApi.setState({
helpers: {
reporter: fakeReporter,
},
})
})
test(`handles 500 errors`, async () => {
await fetchGraphQL({
query: 500,
url: `fake url`,
})
expect(
panicMessages[0]
).toInclude(`Your WordPress server is either overloaded or encountered a PHP error.`)
})
test(`handles 502, 503, and 504 errors`, async () => {
const errorMessage = `Your WordPress server at ${chalk.bold(
`fake url`
)} appears to be overloaded.`
await fetchGraphQL({
query: 502,
url: `fake url`,
})
expect(panicMessages[1]).toInclude(errorMessage)
await fetchGraphQL({
query: 503,
url: `fake url`,
})
expect(panicMessages[2]).toInclude(errorMessage)
await fetchGraphQL({
query: 504,
url: `fake url`,
})
expect(panicMessages[3]).toInclude(errorMessage)
})
test(`errors when WPGraphQL is not active`, async () => {
await fetchGraphQL({
query: `wpgraphql-deactivated`,
url: `fake url`,
})
expect(
panicMessages[4]
).toInclude(`Unable to connect to WPGraphQL.`)
})
afterAll(() => {
mock.mockRestore()
})
})