Skip to content

Commit 43106ca

Browse files
author
mfrachet
committed
test(create-gatsby): snapshotting plugin-options-form
1 parent 92742df commit 43106ca

File tree

2 files changed

+175
-30
lines changed

2 files changed

+175
-30
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import { makePluginConfigQuestions } from "../plugin-options-form"
2+
import pluginSchemas from "../plugin-schemas.json"
3+
4+
describe(`plugin-options-form`, () => {
5+
it(`returns an empty array when nothing is passed`, () => {
6+
const plugins = []
7+
expect(makePluginConfigQuestions(plugins)).toEqual([])
8+
})
9+
10+
it(`returns an empty array when the plugin is not available for configu`, () => {
11+
const plugins = [`not-valid-plugin`]
12+
expect(makePluginConfigQuestions(plugins)).toEqual([])
13+
})
14+
15+
it(`returns an arry containing only the wordpress options (choices are included)`, () => {
16+
const plugins = [`gatsby-source-wordpress-experimental`]
17+
expect(makePluginConfigQuestions(plugins)).toEqual([
18+
{
19+
type: `forminput`,
20+
name: `gatsby-source-wordpress-experimental`,
21+
multiple: true,
22+
message: `Configure the WordPress plugin.\nSee \u001b[94mhttps://www.gatsbyjs.com/plugins/gatsby-source-wordpress-experimental/\u001b[39m for help.`,
23+
choices: [
24+
{
25+
name: `url`,
26+
message: `url`,
27+
hint: `This should be the full url of your GraphQL endpoint set up by WP GraphQL`,
28+
},
29+
],
30+
},
31+
])
32+
})
33+
34+
it(`returns an array with all the plugins schemas`, () => {
35+
expect(makePluginConfigQuestions(Object.keys(pluginSchemas))).toEqual([
36+
{
37+
type: `forminput`,
38+
name: `gatsby-source-wordpress-experimental`,
39+
multiple: true,
40+
message: `Configure the WordPress plugin.\nSee \u001b[94mhttps://www.gatsbyjs.com/plugins/gatsby-source-wordpress-experimental/\u001b[39m for help.`,
41+
choices: [
42+
{
43+
name: `url`,
44+
message: `url`,
45+
hint: `This should be the full url of your GraphQL endpoint set up by WP GraphQL`,
46+
},
47+
],
48+
},
49+
{
50+
type: `forminput`,
51+
name: `gatsby-source-contentful`,
52+
multiple: true,
53+
message: `Configure the Contentful plugin.\nSee \u001b[94mhttps://www.gatsbyjs.com/plugins/gatsby-source-contentful/\u001b[39m for help.\n\u001b[32mUse arrow keys to move between fields, and enter to finish\u001b[39m`,
54+
choices: [
55+
{
56+
name: `accessToken`,
57+
message: `accessToken`,
58+
hint: `Contentful delivery api key, when using the Preview API use your Preview API key`,
59+
},
60+
{ name: `spaceId`, message: `spaceId`, hint: `Contentful spaceId` },
61+
],
62+
},
63+
{
64+
type: `forminput`,
65+
name: `gatsby-source-sanity`,
66+
multiple: true,
67+
message: `Configure the Sanity plugin.\nSee \u001b[94mhttps://www.gatsbyjs.com/plugins/gatsby-source-sanity/\u001b[39m for help.\n\u001b[32mUse arrow keys to move between fields, and enter to finish\u001b[39m`,
68+
choices: [
69+
{
70+
name: `projectId`,
71+
message: `projectId`,
72+
hint: `Your Sanity project's ID`,
73+
},
74+
{
75+
name: `dataset`,
76+
message: `dataset`,
77+
hint: `The dataset to fetch from`,
78+
},
79+
],
80+
},
81+
{
82+
type: `forminput`,
83+
name: `gatsby-source-shopify`,
84+
multiple: true,
85+
message: `Configure the Shopify plugin.\nSee \u001b[94mhttps://www.gatsbyjs.com/plugins/gatsby-source-shopify/\u001b[39m for help.\n\u001b[32mUse arrow keys to move between fields, and enter to finish\u001b[39m`,
86+
choices: [
87+
{
88+
name: `shopName`,
89+
message: `shopName`,
90+
hint: `The domain name of your Shopify shop`,
91+
},
92+
{
93+
name: `accessToken`,
94+
message: `accessToken`,
95+
hint: `An API access token to your Shopify shop`,
96+
},
97+
],
98+
},
99+
{
100+
type: `forminput`,
101+
name: `gatsby-source-datocms`,
102+
multiple: true,
103+
message: `Configure the DatoCMS plugin.\nSee \u001b[94mhttps://www.gatsbyjs.com/plugins/gatsby-source-datocms/\u001b[39m for help.`,
104+
choices: [
105+
{
106+
name: `apiToken`,
107+
message: `apiToken`,
108+
hint: `Your read-only API token under the Settings > API tokens section of your administrative area in DatoCMS`,
109+
},
110+
],
111+
},
112+
{
113+
type: `forminput`,
114+
name: `gatsby-source-agility`,
115+
multiple: true,
116+
message: `Configure the gatsby-source-agility plugin.\nSee \u001b[94mhttps://www.gatsbyjs.com/plugins/gatsby-source-agility/\u001b[39m for help.`,
117+
choices: [
118+
{
119+
name: `guid`,
120+
message: `guid`,
121+
hint: `your Agility Content Fetch API Guid`,
122+
},
123+
],
124+
},
125+
{
126+
type: `forminput`,
127+
name: `gatsby-plugin-google-analytics`,
128+
multiple: true,
129+
message: `Configure the gatsby-plugin-google-analytics plugin.\nSee \u001b[94mhttps://www.gatsbyjs.com/plugins/gatsby-plugin-google-analytics/\u001b[39m for help.`,
130+
choices: [
131+
{
132+
name: `trackingId`,
133+
message: `trackingId`,
134+
hint: `The property ID; the tracking code won't be generated without it`,
135+
},
136+
],
137+
},
138+
])
139+
})
140+
})

packages/create-gatsby/src/plugin-options-form.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,22 @@ type Schema = Joi.Description & {
1616

1717
type PluginName = keyof typeof pluginSchemas
1818

19+
interface IChoice {
20+
name: string
21+
initial: unknown
22+
message: string
23+
hint?: string
24+
}
25+
type Choices = Array<IChoice>
26+
27+
type Option = Record<string, Schema> | undefined
28+
1929
interface IFormPrompt {
2030
type: string
2131
name: string
2232
multiple: boolean
2333
message: string
24-
choices: Array<{
25-
name: string
26-
initial: unknown
27-
message: string
28-
hint?: string
29-
}>
34+
choices: Choices
3035
}
3136

3237
function getName(key: string): string | undefined {
@@ -49,51 +54,50 @@ function docsLink(pluginName: string): string {
4954
)
5055
}
5156

57+
const isOptionRequired = ([_, option]: [string, Schema]): boolean =>
58+
option?.flags?.presence === `required`
59+
60+
const schemaToChoice = ([name, option]: [string, Schema]): IChoice => {
61+
const hasDefaultValue =
62+
option.flags?.default &&
63+
supportedOptionTypes.includes(typeof option.flags?.default)
64+
65+
return {
66+
name,
67+
initial: hasDefaultValue ? option.flags?.default.toString() : undefined,
68+
message: name,
69+
hint: option.flags?.description,
70+
}
71+
}
72+
5273
export const makePluginConfigQuestions = (
5374
selectedPlugins: Array<string>
5475
): Array<IFormPrompt> => {
5576
const formPrompts: Array<IFormPrompt> = []
5677

5778
selectedPlugins.forEach((pluginName: string): void => {
5879
const schema = pluginSchemas[pluginName as PluginName]
80+
5981
if (!schema || typeof schema === `string` || !(`keys` in schema)) {
6082
return
6183
}
62-
const options: Record<string, Schema> | undefined = schema?.keys
63-
const choices: Array<{
64-
name: string
65-
initial: string
66-
message: string
67-
hint?: string
68-
}> = []
84+
const options: Option = schema?.keys
6985

7086
if (!options) {
7187
return
7288
}
7389

74-
Object.entries(options).forEach(([name, option]) => {
75-
if (option?.flags?.presence !== `required`) {
76-
return
77-
}
78-
choices.push({
79-
name,
80-
initial:
81-
option.flags?.default &&
82-
supportedOptionTypes.includes(typeof option.flags?.default)
83-
? option.flags?.default.toString()
84-
: undefined,
85-
message: name,
86-
hint: option.flags?.description,
87-
})
88-
})
90+
const choices: Choices = Object.entries(options)
91+
.filter(isOptionRequired)
92+
.map(schemaToChoice)
8993

90-
if (choices.length) {
94+
if (choices.length > 0) {
9195
formPrompts.push({
9296
type: `forminput`,
9397
name: pluginName,
9498
multiple: true,
9599
message: stripIndent`
96-
Configure the ${getName(pluginName)} plugin.
100+
Configure the ${getName(pluginName)} plugin.
97101
See ${docsLink(pluginName)} for help.
98102
${
99103
choices.length > 1
@@ -107,5 +111,6 @@ export const makePluginConfigQuestions = (
107111
})
108112
}
109113
})
114+
110115
return formPrompts
111116
}

0 commit comments

Comments
 (0)