Skip to content

Commit 77e4e19

Browse files
authored
feat(gatsby-source-wordpress): support multiple instances of plugin (#38119)
1 parent 7d30a58 commit 77e4e19

File tree

80 files changed

+888
-738
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+888
-738
lines changed

integration-tests/gatsby-source-wordpress/__tests__/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ const {
1818
resetSchema,
1919
} = require(`../test-fns/test-utils/increment-remote-data`)
2020

21-
const {
22-
default: fetchGraphql,
23-
} = require("gatsby-source-wordpress/dist/utils/fetch-graphql")
21+
const { fetchGraphql } = require("../test-fns/test-utils/graphql")
2422

2523
jest.setTimeout(100000)
2624

integration-tests/gatsby-source-wordpress/test-fns/data-resolution.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
* @jest-environment node
33
*/
44

5-
const {
6-
default: fetchGraphql,
7-
} = require("gatsby-source-wordpress/dist/utils/fetch-graphql")
5+
const { fetchGraphql } = require("./test-utils/graphql")
86
const { URL } = require("url")
97

108
const gatsbyConfig = require("../gatsby-config")
@@ -340,8 +338,8 @@ describe(`data resolution`, () => {
340338
)
341339
)
342340

343-
expect(gatsbyResult.data.wpPage).toStrictEqual(wpGraphQLPageNormalizedPaths)
344-
expect(gatsbyResult.data.wp.seo).toStrictEqual(WPGraphQLData.seo)
341+
expect(gatsbyResult.data.wpPage).toEqual(wpGraphQLPageNormalizedPaths)
342+
expect(gatsbyResult.data.wp.seo).toEqual(WPGraphQLData.seo)
345343
})
346344

347345
it(`Does not download files whose size exceed the maxFileSizeBytes option`, async () => {

integration-tests/gatsby-source-wordpress/test-fns/filtered-type-defs.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const {
2-
default: fetchGraphql,
3-
} = require("gatsby-source-wordpress/dist/utils/fetch-graphql")
1+
const { fetchGraphql } = require("./test-utils/graphql")
42

53
describe(`filtered type definitions`, () => {
64
test(`Date field resolver is working`, async () => {

integration-tests/gatsby-source-wordpress/test-fns/gatsby-image.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const {
2-
default: fetchGraphql,
3-
} = require("gatsby-source-wordpress/dist/utils/fetch-graphql")
1+
const { fetchGraphql } = require("./test-utils/graphql")
42

53
const execall = require("execall")
64

integration-tests/gatsby-source-wordpress/test-fns/integrity.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
* @jest-environment node
33
*/
44

5-
const {
6-
default: fetchGraphql,
7-
} = require("gatsby-source-wordpress/dist/utils/fetch-graphql")
5+
const { fetchGraphql } = require("./test-utils/graphql")
86
const { authedWPGQLRequest } = require("./test-utils/authed-wpgql-request")
97

108
const sortBy = require("lodash/sortBy")

integration-tests/gatsby-source-wordpress/test-fns/plugin-options.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const {
2-
default: fetchGraphql,
3-
} = require("gatsby-source-wordpress/dist/utils/fetch-graphql")
1+
const { fetchGraphql } = require("./test-utils/graphql")
42

53
describe(`plugin options`, () => {
64
test(`Type.exclude option removes types from the schema`, async () => {

integration-tests/gatsby-source-wordpress/test-fns/static-file-transformation.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const {
2-
default: fetchGraphql,
3-
} = require("gatsby-source-wordpress/dist/utils/fetch-graphql")
1+
const { fetchGraphql } = require("./test-utils/graphql")
42

53
const isWarmCache = process.env.WARM_CACHE
64
const testOnWarmCacheOnly = isWarmCache ? test : test.skip

integration-tests/gatsby-source-wordpress/test-fns/test-utils/authed-wpgql-request.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const {
2-
default: fetchGraphql,
3-
} = require("gatsby-source-wordpress/dist/utils/fetch-graphql")
1+
const { fetchGraphql } = require("./graphql")
42

53
exports.authedWPGQLRequest = async (query, { variables } = {}) => {
64
if (!process.env.WPGRAPHQL_URL) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
*
3+
* @param {Object} options
4+
* @param {string} options.url
5+
* @param {string} options.query
6+
* @param {Object} options.variables
7+
* @param {Object} options.headers
8+
*
9+
* @returns {Promise<Object>}
10+
*/
11+
exports.fetchGraphql = async ({ url, query, variables = {}, headers = {} }) => {
12+
const response = await fetch(url, {
13+
method: `POST`,
14+
headers: {
15+
"Content-Type": `application/json`,
16+
...headers,
17+
},
18+
body: JSON.stringify({
19+
query,
20+
variables,
21+
}),
22+
})
23+
const data = await response.json()
24+
if (data.errors) {
25+
throw new Error(JSON.stringify(data.errors))
26+
}
27+
return data
28+
}

integration-tests/gatsby-source-wordpress/test-fns/test-utils/test-resolved-data.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const {
2-
default: fetchGraphql,
3-
} = require("gatsby-source-wordpress/dist/utils/fetch-graphql")
1+
const { fetchGraphql } = require("./graphql")
42
const { authedWPGQLRequest } = require("./authed-wpgql-request")
53

64
const normalizeResponse = data =>

packages/gatsby-source-wordpress/__tests__/fetch-graphql.test.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import chalk from "chalk"
22
import fetchGraphQL, { moduleHelpers } from "../dist/utils/fetch-graphql"
3-
import store from "../dist/store"
3+
import { getStore, createStore, asyncLocalStorage } from "../dist/store"
4+
5+
const store = {store: createStore(), key: `test`}
6+
7+
const withGlobalStore = (fn) => () => asyncLocalStorage.run(store, fn)
48

59
jest.mock(`async-retry`, () => {
610
return {
@@ -19,7 +23,8 @@ describe(`fetchGraphQL helper`, () => {
1923
let mock
2024
const panicMessages = []
2125

22-
beforeAll(() => {
26+
beforeAll(withGlobalStore(() => {
27+
2328
const sharedError = `Request failed with status code`
2429
try {
2530
mock = jest.spyOn(moduleHelpers, `getHttp`).mockImplementation(() => {
@@ -54,14 +59,14 @@ describe(`fetchGraphQL helper`, () => {
5459
},
5560
}
5661

57-
store.dispatch.gatsbyApi.setState({
62+
getStore().dispatch.gatsbyApi.setState({
5863
helpers: {
5964
reporter: fakeReporter,
6065
},
6166
})
62-
})
67+
}))
6368

64-
test(`handles 500 errors`, async () => {
69+
test(`handles 500 errors`, withGlobalStore(async () => {
6570
await fetchGraphQL({
6671
query: 500,
6772
url: `fake url`,
@@ -70,9 +75,9 @@ describe(`fetchGraphQL helper`, () => {
7075
expect(
7176
panicMessages[0]
7277
).toInclude(`Your WordPress server is either overloaded or encountered a PHP error.`)
73-
})
78+
}))
7479

75-
test(`handles 502, 503, and 504 errors`, async () => {
80+
test(`handles 502, 503, and 504 errors`, withGlobalStore(async () => {
7681
const errorMessage = `Your WordPress server at ${chalk.bold(
7782
`fake url`
7883
)} appears to be overloaded.`
@@ -94,9 +99,9 @@ describe(`fetchGraphQL helper`, () => {
9499
url: `fake url`,
95100
})
96101
expect(panicMessages[3]).toInclude(errorMessage)
97-
})
102+
}))
98103

99-
test(`errors when WPGraphQL is not active`, async () => {
104+
test(`errors when WPGraphQL is not active`, withGlobalStore(async () => {
100105
await fetchGraphQL({
101106
query: `wpgraphql-deactivated`,
102107
url: `fake url`,
@@ -105,9 +110,9 @@ describe(`fetchGraphQL helper`, () => {
105110
expect(
106111
panicMessages[4]
107112
).toInclude(`Unable to connect to WPGraphQL.`)
108-
})
113+
}))
109114

110-
afterAll(() => {
115+
afterAll(withGlobalStore(() => {
111116
mock.mockRestore()
112-
})
117+
}))
113118
})

packages/gatsby-source-wordpress/__tests__/fetch-referenced-media-items.test.js

+25-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ jest.mock(`../dist/utils/fetch-graphql`, () => jest.fn())
33
import fetchGraphql from "../dist/utils/fetch-graphql"
44
import { fetchMediaItemsBySourceUrl, fetchMediaItemsById } from "../dist/steps/source-nodes/fetch-nodes/fetch-referenced-media-items"
55
import { createContentDigest } from "gatsby-core-utils"
6-
import store from "../dist/store"
6+
import { getStore, createStore, asyncLocalStorage } from "../dist/store"
77

88
const fakeReporter = {
99
panic: msg => {
@@ -18,16 +18,25 @@ const getNodeMock = jest.fn()
1818

1919
const btoa = (input) => Buffer.from(input).toString(`base64`)
2020

21+
const store = {store: createStore(), key: `test`}
22+
23+
const runWithGlobalStore = async (fn) => {
24+
asyncLocalStorage.run(store, fn)
25+
}
26+
27+
const withGlobalStore = (fn) => () => {
28+
runWithGlobalStore(fn)
29+
}
2130
describe(`fetch-referenced-media-items`, () => {
22-
beforeAll(() => {
23-
store.dispatch.gatsbyApi.setState({
31+
beforeAll(withGlobalStore(() => {
32+
getStore().dispatch.gatsbyApi.setState({
2433
pluginOptions: {
2534
schema: {
2635
perPage: 2,
2736
},
2837
},
2938
})
30-
})
39+
}))
3140

3241
afterEach(() => {
3342
jest.resetAllMocks()
@@ -54,7 +63,7 @@ const createApi = () => {
5463
}
5564
}
5665

57-
it(`should properly download multiple pages`, async () => {
66+
it(`should properly download multiple pages`, withGlobalStore(async () => {
5867
fetchGraphql
5968
.mockResolvedValueOnce({
6069
data: {
@@ -97,11 +106,11 @@ const createApi = () => {
97106
helpers: createApi(),
98107
})
99108
expect(result).toHaveLength(2)
100-
})
109+
}))
101110

102111

103-
it(`should properly download a single page if there is only 1`, async () => {
104-
store.dispatch.gatsbyApi.setState({
112+
it(`should properly download a single page if there is only 1`, withGlobalStore(async () => {
113+
getStore().dispatch.gatsbyApi.setState({
105114
pluginOptions: {
106115
schema: {
107116
perPage: 5,
@@ -133,7 +142,7 @@ const createApi = () => {
133142
helpers: createApi(),
134143
})
135144
expect(result).toHaveLength(2)
136-
})
145+
}))
137146
})
138147

139148

@@ -152,7 +161,7 @@ const createApi = () => {
152161
}
153162
}
154163

155-
it(`should properly download multiple pages of ids`, async () => {
164+
it(`should properly download multiple pages of ids`, withGlobalStore(async () => {
156165
getNodeMock
157166
.mockReturnValueOnce(undefined)
158167
.mockReturnValueOnce(undefined)
@@ -174,7 +183,7 @@ const createApi = () => {
174183
localFile: {
175184
id: 3,
176185
}})
177-
store.dispatch.gatsbyApi.setState({
186+
getStore().dispatch.gatsbyApi.setState({
178187
pluginOptions: {
179188
schema: {
180189
perPage: 2,
@@ -229,10 +238,10 @@ const createApi = () => {
229238
helpers: createApi(),
230239
})
231240
expect(result).toHaveLength(4)
232-
})
241+
}))
233242

234243

235-
xit(`should properly download a single page of ids if there is only 1`, async () => {
244+
it(`should properly download a single page of ids if there is only 1`, withGlobalStore(async () => {
236245
getNodeMock
237246
.mockReturnValueOnce(undefined)
238247
.mockReturnValueOnce(undefined)
@@ -245,7 +254,7 @@ const createApi = () => {
245254
id: 1,
246255
}})
247256

248-
store.dispatch.gatsbyApi.setState({
257+
getStore().dispatch.gatsbyApi.setState({
249258
pluginOptions: {
250259
schema: {
251260
perPage: 5,
@@ -285,6 +294,6 @@ const createApi = () => {
285294
helpers: createApi(),
286295
})
287296
expect(result).toHaveLength(2)
288-
})
297+
}))
289298
})
290-
})
299+
})

packages/gatsby-source-wordpress/__tests__/process-node.test.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import {
99
searchAndReplaceNodeStrings,
1010
} from "../dist/steps/source-nodes/create-nodes/process-node"
1111

12+
import { createStore, asyncLocalStorage } from "../dist/store"
13+
14+
15+
const store = { store: createStore(), key: `test` }
16+
17+
const withGlobalStore = fn => () => asyncLocalStorage.run(store, fn)
18+
1219
const wpUrl = `wp.fakesite.com`
1320

1421
test(`HTML image transformation regex matches images`, async () => {
@@ -30,7 +37,7 @@ test(`HTML image transformation regex matches images`, async () => {
3037
expect(imgTagMatches.length).toBe(3)
3138
})
3239

33-
test(`HTML link transformation regex matches links`, async () => {
40+
test(`HTML link transformation regex matches links`, withGlobalStore(async () => {
3441
const nodeString = `<a href=\\"https://${wpUrl}/wp-content/uploads/2020/01/©SDM-Yep-©Hi-000-Header.jpg\\" />Not a transformable link</a>
3542
3643
<a href=\\"https://other-site.com/hi\\" />Not a transformable link</a>
@@ -45,9 +52,9 @@ test(`HTML link transformation regex matches links`, async () => {
4552
const matches = execall(wpLinkRegex, nodeString)
4653

4754
expect(matches.length).toBe(2)
48-
})
55+
}))
4956

50-
test(`Search and replace node strings using regex matches`, async () => {
57+
test(`Search and replace node strings using regex matches`, withGlobalStore(async () => {
5158
const nodeString = `Some stuff in a random string
5259
5360
A new line with some stuff!
@@ -70,7 +77,7 @@ test(`Search and replace node strings using regex matches`, async () => {
7077
A new line with some other thing!
7178
7279
We need to test some <a href=\\"https://new-site.com/hi\\" />link</a> as well!`)
73-
})
80+
}))
7481

7582
jest.mock(`../dist/steps/source-nodes/fetch-nodes/fetch-referenced-media-items.js`, () => {
7683
return {
@@ -81,7 +88,7 @@ jest.mock(`../dist/steps/source-nodes/fetch-nodes/fetch-referenced-media-items.j
8188
})
8289

8390

84-
test(`Gatsby Image service works in html fields via replaceNodeHtmlImages`, async () => {
91+
test(`Gatsby Image service works in html fields via replaceNodeHtmlImages`, withGlobalStore(async () => {
8592
const node = {
8693
content: `\n<p>Welcome to WordPress. This is your first post. Edit or deleteit, then start writing!</p>\n\n\n\n<p></p>\n\n\n\n<figureclass="wp-block-image size-large"><img loading="lazy" width="1024" height="768" src="http://wpgatsby.local/wp-content/uploads/2022/02/sasha-set-GURzQwO8Li0-unsplash-1024x768.jpg" alt=""class="wp-image-115" srcset="http://wpgatsby.local/wp-content/uploads/2022/02/sasha-set-GURzQwO8Li0-unsplash-1024x768.jpg 1024w,http://wpgatsby.local/wp-content/uploads/2022/02/sasha-set-GURzQwO8Li0-unsplash-300x225.jpg 300w, http://wpgatsby.local/wp-content/uploads/2022/02/sasha-set-GURzQwO8Li0-unsplash-768x576.jpg 768w,http://wpgatsby.local/wp-content/uploads/2022/02/sasha-set-GURzQwO8Li0-unsplash-1536x1152.jpg 1536w, http://wpgatsby.local/wp-content/uploads/2022/02/sasha-set-GURzQwO8Li0-unsplash-2048x1536.jpg 2048w"sizes="(max-width: 1024px) 100vw, 1024px" /></figure>\n<figure class="wp-block-image size-large"><img src="http://wpgatsby.local/wp-content/uploads/2022/04/gaussian2.svg" alt="" class="wp-image-11836"/></figure>`,
8794
id: `cG9zdDox`,
@@ -149,3 +156,4 @@ test(`Gatsby Image service works in html fields via replaceNodeHtmlImages`, asyn
149156
expect(transformedNodeStringNoHtmlImages).not.toInclude(gatsbyImageUrlPart)
150157
expect(transformedNodeStringNoHtmlImages).not.toInclude(gatsbyFileUrlPart)
151158
})
159+
)

packages/gatsby-source-wordpress/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
},
99
"dependencies": {
1010
"@babel/runtime": "^7.20.13",
11-
"@rematch/core": "^1.4.0",
12-
"@rematch/immer": "^1.2.0",
11+
"@rematch/core": "^2.2.0",
12+
"@rematch/immer": "^2.1.3",
1313
"async-retry": "^1.3.3",
1414
"atob": "^2.1.2",
1515
"axios": "^0.21.1",
@@ -33,6 +33,7 @@
3333
"gatsby-source-filesystem": "^5.11.0-next.1",
3434
"glob": "^7.2.3",
3535
"got": "^11.8.6",
36+
"immer": "^9.0.0",
3637
"json-diff": "^1.0.6",
3738
"lodash": "^4.17.21",
3839
"node-fetch": "^2.6.11",

0 commit comments

Comments
 (0)