Skip to content

Commit

Permalink
fix(create-gatsby): fix missing site title (#35272) (#35341)
Browse files Browse the repository at this point in the history
(cherry picked from commit 37a87ee)

Co-authored-by: Jude Agboola <[email protected]>
  • Loading branch information
gatsbybot and marvinjude authored Apr 5, 2022
1 parent 3f703d1 commit b08d256
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
26 changes: 23 additions & 3 deletions packages/create-gatsby/src/__tests__/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import path from "path"
import { reporter } from "../utils/reporter"
import { initStarter } from "../init-starter"
import { setSiteMetadata } from "../utils/site-metadata"
import { trackCli } from "../tracking"
import { run, DEFAULT_STARTERS } from "../index"

Expand All @@ -16,7 +18,7 @@ jest.mock(`enquirer`, () => {
// Mock answers
this.answers = {
// First prompt answer
name: `hello-world`,
name: `my-site`,

// Main question set answers
project: `hello-world`,
Expand Down Expand Up @@ -61,6 +63,7 @@ jest.mock(`../utils/hash`, () => {
md5: jest.fn(args => args),
}
})

jest.mock(`../utils/question-helpers`, () => {
const originalQuestionHelpers = jest.requireActual(
`../utils/question-helpers`
Expand All @@ -78,6 +81,9 @@ jest.mock(`../components/utils`, () => {
})

const dirName = `hello-world`
const siteName = `my-site`
const fullPath = path.resolve(`hello-world`)

let parseArgsMock

describe(`run`, () => {
Expand All @@ -86,6 +92,20 @@ describe(`run`, () => {
parseArgsMock = require(`../utils/parse-args`).parseArgs
})

describe(`gatsby-config.js `, () => {
beforeEach(() => {
parseArgsMock.mockReturnValueOnce({
flags: { yes: false },
siteName,
})
})

it(`should use siteName as site title`, async () => {
await run()
expect(setSiteMetadata).toHaveBeenCalledWith(fullPath, `title`, siteName)
})
})

describe(`no skip flag`, () => {
beforeEach(() => {
parseArgsMock.mockReturnValueOnce({
Expand Down Expand Up @@ -188,7 +208,7 @@ describe(`run`, () => {
DEFAULT_STARTERS.js,
dirName,
[],
dirName
siteName
)
})
it(`should track JS was selected as language`, async () => {
Expand All @@ -214,7 +234,7 @@ describe(`run`, () => {
DEFAULT_STARTERS.ts,
dirName,
[],
dirName
siteName
)
})

Expand Down
5 changes: 4 additions & 1 deletion packages/create-gatsby/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ ${center(colors.blueBright.bold.underline(`Welcome to Gatsby!`))}

// If we aren't skipping prompts, get a site name first to use as a default folder name
let npmSafeSiteName
let siteName = ``

if (!flags.yes) {
const { name } = await enquirer.prompt({
Expand All @@ -114,9 +115,11 @@ ${center(colors.blueBright.bold.underline(`Welcome to Gatsby!`))}
} as any)

npmSafeSiteName = makeNpmSafe(name)
siteName = name
} else {
const valid = validateProjectName(dirName)

siteName = dirName
if (!valid) {
return
}
Expand Down Expand Up @@ -302,7 +305,7 @@ ${colors.bold(`Thanks! Here's what we'll now do:`)}
reporter.info(`${maybeUseEmoji(`🔌 `)}Setting-up plugins...`)
await installPlugins(plugins, pluginConfig, fullPath, [])
}
await setSiteMetadata(fullPath, `title`, dirName)
await setSiteMetadata(fullPath, `title`, siteName)

await gitSetup(answers.project)

Expand Down

0 comments on commit b08d256

Please sign in to comment.