Skip to content

Commit

Permalink
test: add after-create test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sor4chi committed Oct 13, 2023
1 parent 8ca7ece commit 37dd246
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/hooks/after-create.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as path from 'path'
import { describe, expect, it, vi } from 'vitest'
import {
AFTER_CREATE,
addAfterCreateHook,
rewriteWranglerHook,
} from './after-create'

describe('addAfterCreateHook', () => {
it('adds a hook to the AFTER_CREATE map', () => {
const hook = () => {}
addAfterCreateHook('test-template', hook)
expect(AFTER_CREATE.get('test-template')).toEqual([hook])
})
})

describe('rewriteWranglerHook', () => {
it('rewrites the wrangler.toml file with the project name', async () => {
vi.mock('fs', () => {
const wrangler = `
name = "%%PROJECT_NAME%%"
type = "javascript"
account_id = "test-account-id"
workers_dev = true
route = ""
zone_id = ""
`.trim()

return {
readFileSync: vi.fn().mockReturnValue(wrangler),
writeFileSync: vi.fn(),
}
})
const { readFileSync, writeFileSync } = await import('fs')

const projectName = 'test-project'
const directoryPath = './tmp'
const wranglerPath = path.join(directoryPath, 'wrangler.toml')
const replaced = `
name = "${projectName}"
type = "javascript"
account_id = "test-account-id"
workers_dev = true
route = ""
zone_id = ""
`.trim()
rewriteWranglerHook({ projectName, directoryPath })
expect(readFileSync).toHaveBeenCalledWith(wranglerPath, 'utf-8')
expect(writeFileSync).toHaveBeenCalledWith(wranglerPath, replaced)
})
})

0 comments on commit 37dd246

Please sign in to comment.