Skip to content

Commit

Permalink
Improve tests for magic-modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterp committed May 21, 2021
1 parent c6dacb8 commit 168919b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
29 changes: 19 additions & 10 deletions packages/internal/src/__tests__/findFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,30 @@ afterAll(() => {
})

import { findCells, findDirectoryNamedModules } from '../findFiles'
import { ensurePosixPath } from '../paths'

test('finds all the cells', () => {
const paths = findCells()

const p = paths.map((p) => p.replace(FIXTURE_PATH, ''))

expect(p[0]).toContain('NumTodosCell.js')
expect(p[1]).toContain('TodoListCell.tsx')
const p = paths.map((p) => p.replace(FIXTURE_PATH, '')).map(ensurePosixPath)

expect(p).toMatchInlineSnapshot(`
Array [
"/web/src/components/NumTodosCell/NumTodosCell.js",
"/web/src/components/TodoListCell/TodoListCell.tsx",
]
`)
})

test('finds directory named modules', () => {
const paths = findDirectoryNamedModules()
const p = paths.map((p) => p.replace(FIXTURE_PATH, ''))

expect(p[0]).toContain('todos.js')
expect(p[1]).toContain('AddTodo.js')
expect(p[2]).toContain('AddTodoControl.js')
const p = paths.map((p) => p.replace(FIXTURE_PATH, '')).map(ensurePosixPath)

expect(p).toMatchInlineSnapshot(`
Array [
"/api/src/services/todos/todos.js",
"/web/src/components/AddTodo/AddTodo.js",
"/web/src/components/Check/Check.js",
"/web/src/components/TodoItem/TodoItem.js",
]
`)
})
38 changes: 27 additions & 11 deletions packages/internal/src/findFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,44 @@ import path from 'path'

import { glob } from 'glob'

import { getNamedExports } from './ast'
import { getNamedExports, hasDefaultExport } from './ast'
import { getPaths } from './paths'

/**
* Find all the Cell components in a project's web side.
*
*/
// TODO: Cells must be directory named modules.
export const findCells = (webSrcDir: string = getPaths().web.src) => {
const cellPaths = glob.sync('**/*Cell.{js,jsx,ts,tsx}', {
const modules = glob.sync('**/*Cell.{js,jsx,ts,tsx}', {
cwd: webSrcDir,
})

return cellPaths
return modules
.map((p) => path.join(webSrcDir, p))
.map((p) => {
// A Cell must be a directory named module.
const { dir, name } = path.parse(p)
if (!dir.endsWith(name)) {
return false
}

const code = fs.readFileSync(p, 'utf-8')

// A Cell should not have a default export.
if (hasDefaultExport(code)) {
return false
}

// A Cell must export QUERY and Success
const exports = getNamedExports(code)
const exportedQUERY = exports.findIndex((v) => v.name === 'QUERY') !== -1
const exportedSuccess =
exports.findIndex((v) => v.name === 'Success') !== -1
if (exportedQUERY && exportedSuccess) {
return p
if (!exportedQUERY && !exportedSuccess) {
return false
}
return false

return p
})
.filter(Boolean) as string[]
}
Expand All @@ -39,14 +53,16 @@ export const findCells = (webSrcDir: string = getPaths().web.src) => {
export const findDirectoryNamedModules = (
projectBaseDir: string = getPaths().base
) => {
const modules = glob.sync('**/*.{ts,js,jsx,tsx}', { cwd: projectBaseDir })
const modules = glob.sync('**/*[!Cell].{ts,js,jsx,tsx}', {
cwd: projectBaseDir,
})
return modules
.map((m) => {
const { dir, name } = path.parse(m)
.map((p) => {
const { dir, name } = path.parse(p)
if (!dir.endsWith(name)) {
return false
}
return path.join(projectBaseDir, m)
return path.join(projectBaseDir, p)
})
.filter(Boolean) as string[]
}

0 comments on commit 168919b

Please sign in to comment.