Skip to content

Commit 06e9337

Browse files
TylerBarnesgatsbybot
and
gatsbybot
authored
feat(gatsby-source-wordpress): Add missing tests (#29406)
Co-authored-by: gatsbybot <[email protected]>
1 parent d8772ec commit 06e9337

31 files changed

+7764
-0
lines changed

.circleci/config.yml

+8
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ jobs:
221221
image: "14"
222222
<<: *test_template
223223

224+
integration_tests_gatsby_source_wordpress:
225+
executor: node
226+
steps:
227+
- e2e-test:
228+
test_path: integration-tests/gatsby-source-wordpress
229+
224230
integration_tests_long_term_caching:
225231
executor: node
226232
steps:
@@ -579,6 +585,8 @@ workflows:
579585
- lint
580586
- typecheck
581587
- bootstrap
588+
- integration_tests_gatsby_source_wordpress:
589+
<<: *e2e-test-workflow
582590
- integration_tests_long_term_caching:
583591
<<: *e2e-test-workflow
584592
- integration_tests_cache_resilience:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
WPGRAPHQL_URL="https://gatsbyinttests.wpengine.com/graphql"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
WordPress

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

+5,999
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require(`dotenv`).config({
2+
path: `.env.test`,
3+
})
4+
5+
const on = require(`wait-on`)
6+
const {
7+
spawnGatsbyProcess,
8+
gatsbyCleanBeforeAll,
9+
} = require(`../test-fns/test-utils/get-gatsby-process`)
10+
11+
jest.setTimeout(100000)
12+
13+
// we run these tests twice in a row
14+
// to make sure everything passes on a warm cache build
15+
// we don't need to re-run some tests the second time,
16+
// so the following allows us to do that:
17+
const isWarmCache = process.env.WARM_CACHE
18+
const testOnColdCacheOnly = isWarmCache ? test.skip : test
19+
20+
describe(`[gatsby-source-wordpress] Build default options`, () => {
21+
beforeAll(done => {
22+
if (isWarmCache) {
23+
done()
24+
} else {
25+
gatsbyCleanBeforeAll(done)
26+
}
27+
})
28+
29+
testOnColdCacheOnly(`Default options build succeeded`, async () => {
30+
const gatsbyProcess = spawnGatsbyProcess(`build`, {
31+
DEFAULT_PLUGIN_OPTIONS: `1`,
32+
})
33+
34+
const exitCode = await new Promise(resolve =>
35+
gatsbyProcess.on(`exit`, resolve)
36+
)
37+
38+
expect(exitCode).toEqual(0)
39+
40+
gatsbyProcess.kill()
41+
})
42+
})
43+
44+
describe(`[gatsby-source-wordpress] Run tests on develop build`, () => {
45+
let gatsbyDevelopProcess
46+
47+
beforeAll(async done => {
48+
if (!isWarmCache) {
49+
await gatsbyCleanBeforeAll()
50+
}
51+
52+
gatsbyDevelopProcess = spawnGatsbyProcess(`develop`)
53+
54+
await on({ resources: [`http://localhost:8000`] })
55+
done()
56+
})
57+
58+
require(`../test-fns/index`)
59+
60+
afterAll(done => {
61+
gatsbyDevelopProcess.kill()
62+
done()
63+
})
64+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
require(`dotenv`).config({
2+
path: `.env.test`,
3+
})
4+
5+
console.log(`Sourcing data from ` + process.env.WPGRAPHQL_URL)
6+
7+
const requestConcurrency = 1
8+
9+
const mediaItemTypeSettings = {
10+
localFile: {
11+
requestConcurrency,
12+
maxFileSizeBytes: 10485760,
13+
},
14+
}
15+
16+
// this is it's own conditional object so we can run
17+
// an int test with all default plugin options
18+
const wpPluginOptions = !process.env.DEFAULT_PLUGIN_OPTIONS
19+
? {
20+
excludeFieldNames: [`commentCount`],
21+
debug: {
22+
graphql: {
23+
writeQueriesToDisk: true,
24+
},
25+
},
26+
type: {
27+
MediaItem: mediaItemTypeSettings,
28+
TypeLimitTest: {
29+
limit: 1,
30+
},
31+
TypeLimit0Test: {
32+
limit: 0,
33+
},
34+
Comment: {
35+
excludeFieldNames: [`databaseId`],
36+
},
37+
Page: {
38+
excludeFieldNames: [`enclosure`],
39+
},
40+
DatabaseIdentifier: {
41+
exclude: true,
42+
},
43+
User: {
44+
excludeFieldNames: [
45+
`extraCapabilities`,
46+
`capKey`,
47+
`email`,
48+
`registeredDate`,
49+
],
50+
},
51+
Post: {
52+
limit:
53+
process.env.NODE_ENV === `development`
54+
? // Lets just pull 50 posts in development to make it easy on ourselves.
55+
50
56+
: // and we don't actually need more than 1000 in production
57+
1000,
58+
},
59+
},
60+
}
61+
: {
62+
type: {
63+
MediaItem: mediaItemTypeSettings,
64+
},
65+
}
66+
67+
module.exports = {
68+
plugins: [
69+
`gatsby-plugin-sharp`,
70+
`gatsby-transformer-sharp`,
71+
{
72+
resolve: `gatsby-source-filesystem`,
73+
options: {
74+
name: `images`,
75+
path: `${__dirname}/src/assets/images`,
76+
},
77+
},
78+
{
79+
resolve: `gatsby-source-wordpress`,
80+
options: {
81+
url: process.env.WPGRAPHQL_URL,
82+
schema: {
83+
requestConcurrency: 10,
84+
},
85+
production: {
86+
hardCacheMediaFiles: true,
87+
},
88+
develop: {
89+
hardCacheMediaFiles: true,
90+
},
91+
...wpPluginOptions,
92+
},
93+
},
94+
],
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
testPathIgnorePatterns: [`/node_modules/`, `__tests__/fixtures`, `.cache`],
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "gatsby-source-wordpress-integration-tests",
3+
"version": "0.1.0",
4+
"private": true,
5+
"description": "A simple Gatsby WP site for running integrations tests",
6+
"scripts": {
7+
"run-jest": "jest --config=jest.config.js --runInBand",
8+
"test": "npm run run-jest && WARM_CACHE=true npm run run-jest"
9+
},
10+
"author": "Tyler Barnes <[email protected]>",
11+
"license": "MIT",
12+
"dependencies": {
13+
"gatsby": "next",
14+
"gatsby-plugin-sharp": "next",
15+
"gatsby-source-filesystem": "next",
16+
"gatsby-source-wordpress": "next",
17+
"gatsby-transformer-sharp": "next"
18+
},
19+
"devDependencies": {
20+
"dotenv": "^8.2.0",
21+
"jest": "^26.6.3",
22+
"rimraf": "^3.0.2",
23+
"wait-on": "^5.2.1"
24+
}
25+
}

integration-tests/gatsby-source-wordpress/src/assets/images/fake.png

Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
5+
const {
6+
default: fetchGraphql,
7+
} = require("gatsby-source-wordpress/dist/utils/fetch-graphql")
8+
9+
const { testResolvedData } = require("./test-utils/test-resolved-data")
10+
const { queries } = require("./test-utils/queries")
11+
12+
jest.setTimeout(100000)
13+
14+
const url = `http://localhost:8000/___graphql`
15+
16+
describe(`data resolution`, () => {
17+
it(`resolves correct number of nodes`, async () => {
18+
const { data } = await fetchGraphql({
19+
url,
20+
query: queries.nodeCounts,
21+
})
22+
23+
expect(data[`allWpMediaItem`].nodes).toBeTruthy()
24+
expect(data[`allWpMediaItem`].nodes).toMatchSnapshot()
25+
expect(data[`allWpMediaItem`].totalCount).toBe(7)
26+
27+
expect(data[`allWpTag`].totalCount).toBe(5)
28+
expect(data[`allWpUser`].totalCount).toBe(1)
29+
expect(data[`allWpPage`].totalCount).toBe(1)
30+
expect(data[`allWpPost`].totalCount).toBe(1)
31+
expect(data[`allWpComment`].totalCount).toBe(1)
32+
// expect(data[`allWpProject`].totalCount).toBe(1)
33+
expect(data[`allWpTaxonomy`].totalCount).toBe(3)
34+
expect(data[`allWpCategory`].totalCount).toBe(9)
35+
expect(data[`allWpMenu`].totalCount).toBe(1)
36+
expect(data[`allWpMenuItem`].totalCount).toBe(4)
37+
// expect(data[`allWpTeamMember`].totalCount).toBe(1)
38+
expect(data[`allWpPostFormat`].totalCount).toBe(0)
39+
expect(data[`allWpContentType`].totalCount).toBe(6)
40+
})
41+
42+
testResolvedData({
43+
url,
44+
title: `resolves wp-graphql-acf data`,
45+
gatsbyQuery: queries.acfData,
46+
queryReplace: {
47+
from: `wpPage(title: { eq: "ACF Field Test" }) {`,
48+
to: `page(id: "cG9zdDo3NjQ2") {`,
49+
},
50+
fields: {
51+
gatsby: `wpPage`,
52+
wpgql: `page`,
53+
},
54+
})
55+
56+
it(`resolves hierarchichal categories`, async () => {
57+
const gatsbyResult = await fetchGraphql({
58+
url,
59+
query: /* GraphQL */ `
60+
fragment NestedCats on WpCategory {
61+
name
62+
wpChildren {
63+
nodes {
64+
name
65+
wpChildren {
66+
nodes {
67+
name
68+
wpChildren {
69+
nodes {
70+
name
71+
}
72+
}
73+
}
74+
}
75+
}
76+
}
77+
}
78+
79+
{
80+
allWpCategory {
81+
nodes {
82+
name
83+
}
84+
}
85+
wpPost(id: { eq: "cG9zdDo5MzYx" }) {
86+
id
87+
title
88+
categories {
89+
nodes {
90+
...NestedCats
91+
}
92+
}
93+
}
94+
}
95+
`,
96+
})
97+
98+
const categoryNodes = gatsbyResult.data.allWpCategory.nodes
99+
const categoryNames = categoryNodes.map(({ name }) => name)
100+
101+
expect(categoryNames.includes(`h1`)).toBeTruthy()
102+
expect(categoryNames.includes(`h2`)).toBeTruthy()
103+
expect(categoryNames.includes(`h3`)).toBeTruthy()
104+
expect(categoryNames.includes(`h4`)).toBeTruthy()
105+
})
106+
107+
it(`resolves menus`, async () => {
108+
const result = await fetchGraphql({
109+
url,
110+
query: queries.menus,
111+
})
112+
113+
expect(result).toMatchSnapshot()
114+
})
115+
116+
it(`resolves pages`, async () => {
117+
const result = await fetchGraphql({
118+
url,
119+
query: queries.pages,
120+
})
121+
122+
expect(result).toMatchSnapshot()
123+
124+
// expect(result.data.testPage.title).toEqual(`Sample Page`)
125+
})
126+
127+
it(`resolves posts`, async () => {
128+
const result = await fetchGraphql({
129+
url,
130+
query: queries.posts,
131+
})
132+
133+
expect(result).toMatchSnapshot()
134+
135+
expect(result.data.testPost.title).toEqual(`Hello world!`)
136+
})
137+
138+
it(`resolves users`, async () => {
139+
const result = await fetchGraphql({
140+
url,
141+
query: queries.users,
142+
})
143+
144+
expect(result).toMatchSnapshot()
145+
146+
expect(result.data.testUser.firstName).toEqual(`Tyler`)
147+
})
148+
149+
it(`resolves root fields`, async () => {
150+
const result = await fetchGraphql({
151+
url,
152+
query: queries.rootFields,
153+
})
154+
155+
expect(result).toMatchSnapshot()
156+
})
157+
})

0 commit comments

Comments
 (0)