Skip to content

Commit

Permalink
refactor(ui/testing): generators
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoenescu committed Mar 28, 2024
1 parent 679f0d4 commit 2bb6ce4
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 331 deletions.
3 changes: 2 additions & 1 deletion ui/testing/specs/cmd.createTestFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import prompts from 'prompts'
*/
export async function cmdCreateTestFile ({
ctx,
testFile,
ignoredTestFiles
}) {
const { action } = await prompts({
Expand All @@ -31,7 +32,7 @@ export async function cmdCreateTestFile ({
return
}

const testFileContent = ctx.testFile.createContent()
const testFileContent = testFile.createContent()

console.log()
console.log(testFileContent)
Expand Down
8 changes: 6 additions & 2 deletions ui/testing/specs/cmd.generateSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ const jsonPathRE = /^[^.]+\.[^.]+$/
/**
* Generates a targeted section of a json path
*/
export async function cmdGenerateSection ({ ctx, jsonPath }) {
export async function cmdGenerateSection ({
ctx,
testFile,
jsonPath
}) {
if (jsonPathRE.test(jsonPath) === false) {
console.log(` ❌ Invalid json path: "${ jsonPath }"`)
return
}

const output = ctx.testFile.generateSection(jsonPath)
const output = testFile.generateSection(jsonPath)

if (output === null) {
console.log(` ❌ No such json path found: "${ jsonPath }"`)
Expand Down
38 changes: 23 additions & 15 deletions ui/testing/specs/cmd.validateTestFile.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { basename } from 'node:path'
import prompts from 'prompts'

import { plural } from './specs.utils.js'

/**
* Validates a test file
*/
export async function cmdValidateTestFile ({
ctx,
testFile,
argv
}) {
const { errors, warnings } = ctx.testFile.getMisconfiguration()
const { errors, warnings } = testFile.getMisconfiguration()

if (errors.length !== 0) {
if (argv.interactive === true) {
Expand Down Expand Up @@ -54,20 +57,22 @@ export async function cmdValidateTestFile ({
})
}

const missingTests = ctx.testFile.getMissingTests()
const missingTests = testFile.getMissingTests()

if (missingTests === null) {
console.log(` ✅ ${ ctx.testFileRelative }`)
return
}

const pluralSuffix = plural(missingTests.length)

if (argv.interactive === false) {
// TODO: process.exit(1) when all test files have been added
console.log(` ❌ ${ ctx.testFileRelative } is missing ${ missingTests.length } test-cases`)
console.log(` ❌ ${ ctx.testFileRelative } is missing ${ missingTests.length } test-case${ pluralSuffix }`)
return
}

console.log(`\n ❌ ${ ctx.testFileRelative } is missing ${ missingTests.length } test-cases:`)
console.log(`\n ❌ ${ ctx.testFileRelative } is missing ${ missingTests.length } test-case${ pluralSuffix }:`)

missingTests.forEach(test => {
console.log(test.content)
Expand All @@ -76,15 +81,17 @@ export async function cmdValidateTestFile ({
const { action } = await prompts({
type: 'select',
name: 'action',
message: `🔥 Missing ${ missingTests.length } test-cases in "${ ctx.testFileRelative }":`,
message: `🔥 Missing ${ missingTests.length } test-case${ pluralSuffix } in "${ ctx.testFileRelative }":`,
initial: 0,
choices: [
{ title: 'Skip', value: 'skip' },
{ title: 'Accept test-cases (notice test.todo calls)', value: 'add-missing-test-cases' },
{ title: 'Handle individually', value: 'handle-individually' },
{ title: 'Add ignore comments', value: 'add-ignore-comments' },
{ title: `Accept test-case${ pluralSuffix } (notice test.todo calls)`, value: 'add-missing-test-cases' },
missingTests.length > 1
? { title: 'Handle individually', value: 'handle-individually' }
: void 0,
{ title: `Add ignore comment${ pluralSuffix }`, value: 'add-ignore-comments' },
{ title: 'Abort & exit', value: 'exit' }
]
].filter(v => v)
})

// allow user to exit
Expand All @@ -93,13 +100,13 @@ export async function cmdValidateTestFile ({
if (action === 'skip') return

if (action === 'add-missing-test-cases') {
ctx.testFile.addTestCases(missingTests)
testFile.addTestCases(missingTests)
console.log(` 🎉 Injected the missing test-cases into "${ ctx.testFileRelative }"`)
return
}

if (action === 'add-ignore-comments') {
ctx.testFile.addIgnoreComments(missingTests.map(test => test.testId))
testFile.addIgnoreComments(missingTests.map(test => test.testId || test.categoryId))
console.log(` 🎉 Injected the ignore comments into "${ ctx.testFileRelative }"`)
return
}
Expand All @@ -111,18 +118,19 @@ export async function cmdValidateTestFile ({

for (const test of missingTests) {
index++
const testCaseName = test.testId || test.categoryId

console.log()
console.log(' -------')
console.log(` | 🍕 [ ${ index } / ${ missingTestsLen } of ${ testFileName } ]`)
console.log(` | 🔥 Missing test-case: "${ test.testId }"`)
console.log(` | 🔥 Missing test-case: "${ testCaseName }"`)
console.log(' -------')
console.log(test.content)

const { action } = await prompts({
type: 'select',
name: 'action',
message: `🔋 How to proceed with "${ test.testId }" from above?`,
message: `🔋 How to proceed with "${ testCaseName }" from above?`,
initial: 0,
choices: [
{ title: 'Skip', value: 'skip' },
Expand All @@ -140,13 +148,13 @@ export async function cmdValidateTestFile ({
if (action === 'skip-the-rest') return

if (action === 'accept-missing-test-case') {
ctx.testFile.addTestCases([ test ])
testFile.addTestCases([ test ])
console.log(` 🎉 Injected the missing test-case into "${ ctx.testFileRelative }"`)
continue
}

if (action === 'add-ignore-comment') {
ctx.testFile.addIgnoreComments([ test.testId ])
testFile.addIgnoreComments([ testCaseName ])
console.log(` 🎉 Injected the ignore comment into "${ ctx.testFileRelative }"`)
continue
}
Expand Down
7 changes: 2 additions & 5 deletions ui/testing/specs/ctx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { resolve, dirname, basename, relative } from 'node:path'
import { fileURLToPath } from 'node:url'
import fse from 'fs-extra'

import { toPascalCase } from './specs.utils.js'
import { getTestFile } from './testFile.js'
import { pascalCase } from './specs.utils.js'

const rootFolder = fileURLToPath(new URL('../..', import.meta.url))

Expand All @@ -27,7 +26,7 @@ export function createCtx (target) {
const dirAbsolute = dirname(resolve(rootFolder, target))
const testName = localName.replace('.js', '.test.js')
const testFileAbsolute = resolve(dirAbsolute, testName)
const pascalName = toPascalCase(localName.replace('.js', ''))
const pascalName = pascalCase(localName.replace('.js', ''))

const ctx = {
targetRelative: target,
Expand All @@ -41,8 +40,6 @@ export function createCtx (target) {
testTreeRootId: `[${ pascalName } API]`
}

ctx.testFile = getTestFile(ctx)

let cachedJson
Object.defineProperty(ctx, 'json', {
get () {
Expand Down
Loading

0 comments on commit 2bb6ce4

Please sign in to comment.