Skip to content

Commit

Permalink
refactor: make new group for .ts links
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Nov 11, 2023
1 parent b36bbb3 commit 2ac278d
Showing 1 changed file with 89 additions and 63 deletions.
152 changes: 89 additions & 63 deletions tests/linker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,55 +149,6 @@ test.group('Views Linker | .edge', () => {
`)
})

test('ts source type', async ({ assert, fs }) => {
await fs.create('resources/views/components/button.edge', '')
await fs.create('resources/views/pages/admin.edge', '')

const indexer = new TemplateIndexer({
rootPath: fs.basePath,
disks: { default: 'resources/views' },
})
await indexer.scan()

const template = dedent`
return view.render('components/button', {
props: 42
})
return view.renderSync('pages/admin', {})
`

const result = await Linker.getLinks({
fileContent: template,
indexer,
sourceType: 'ts',
})

const positions = result.map((r) => r.position)

assert.snapshot(positions).matchInline(`
[
{
"colEnd": 37,
"colStart": 20,
"line": 0,
},
{
"colEnd": 35,
"colStart": 24,
"line": 4,
},
]
`)

const paths = result.map((r) => r.templatePath)

assert.sameDeepMembers(paths, [
slash(join(indexer.project.rootPath, 'resources/views/components/button.edge')),
slash(join(indexer.project.rootPath, 'resources/views/pages/admin.edge')),
])
})

test('should not detect internal tags as components', async ({ assert, fs }) => {
const template = dedent`
@if(true)
Expand Down Expand Up @@ -339,21 +290,51 @@ test.group('Views Linker | .edge', () => {
assert.deepEqual(positions, [{ colEnd: 29, colStart: 12, line: 3 }])
})

test('match template in ts file in correct position', async ({ assert, fs }) => {
test('should find link even when the component props include "(" character', async ({
assert,
fs,
}) => {
await fs.create('resources/views/components/button.edge', '')

const template = dedent`
@!button('foo', { test: route('bar') })
`

const indexer = new TemplateIndexer({
rootPath: fs.basePath,
disks: { default: 'resources/views' },
})

await indexer.scan()

const template = dedent`
console.log('components/button')
const result = await Linker.getLinks({
fileContent: template,
indexer,
sourceType: 'edge',
})

const positions = result.map((r) => r.position)
assert.deepEqual(positions, [{ colEnd: 8, colStart: 2, line: 0 }])
})
})

test.group('Views Linker | .ts', () => {
test('ts source type', async ({ assert, fs }) => {
await fs.create('resources/views/components/button.edge', '')
await fs.create('resources/views/pages/admin.edge', '')

const indexer = new TemplateIndexer({
rootPath: fs.basePath,
disks: { default: 'resources/views' },
})
await indexer.scan()

const template = dedent`
return view.render('components/button', {
props: 42
})
return view.renderSync('pages/admin', {})
`

const result = await Linker.getLinks({
Expand All @@ -363,33 +344,78 @@ test.group('Views Linker | .edge', () => {
})

const positions = result.map((r) => r.position)
assert.deepEqual(positions, [{ colEnd: 37, colStart: 20, line: 2 }])
})

test('should find link even when the component props include "(" character', async ({
assert,
fs,
}) => {
await fs.create('resources/views/components/button.edge', '')
assert.snapshot(positions).matchInline(`
[
{
"colEnd": 37,
"colStart": 20,
"line": 0,
},
{
"colEnd": 35,
"colStart": 24,
"line": 4,
},
]
`)

const template = dedent`
@!button('foo', { test: route('bar') })
const paths = result.map((r) => r.templatePath)

assert.sameDeepMembers(paths, [
slash(join(indexer.project.rootPath, 'resources/views/components/button.edge')),
slash(join(indexer.project.rootPath, 'resources/views/pages/admin.edge')),
])
})

test('should not mark emails as components', async ({ assert, fs }) => {
const template = `
router.get('/send-email', async ({ response, session }) => {
message.from('[email protected]')
.text('Hello world')
.to('[email protected]').subject('My subject')
})
`

const indexer = new TemplateIndexer({
rootPath: fs.basePath,
disks: { default: 'resources/views' },
})
await indexer.scan()

const result = await Linker.getLinks({
fileContent: template,
indexer,
sourceType: 'ts',
})

assert.deepEqual(result, [])
})

test('match template in ts file in correct position', async ({ assert, fs }) => {
await fs.create('resources/views/components/button.edge', '')

const indexer = new TemplateIndexer({
rootPath: fs.basePath,
disks: { default: 'resources/views' },
})
await indexer.scan()

const template = dedent`
console.log('components/button')
return view.render('components/button', {
props: 42
})
`

const result = await Linker.getLinks({
fileContent: template,
indexer,
sourceType: 'edge',
sourceType: 'ts',
})

const positions = result.map((r) => r.position)
assert.deepEqual(positions, [{ colEnd: 8, colStart: 2, line: 0 }])
assert.deepEqual(positions, [{ colEnd: 37, colStart: 20, line: 2 }])
})
})

0 comments on commit 2ac278d

Please sign in to comment.