Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update resolvePageComponent helper to work with both glob and glogEager #11

Merged
merged 6 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/inertia-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export function resolvePageComponent<T>(path: string, pages: Record<string, () => Promise<T>>) {
if (typeof pages[path] === 'undefined') {
export async function resolvePageComponent<T>(path: string, pages: Record<string, Promise<T> | (() => Promise<T>)>): Promise<T> {
const page = pages[path]

if (typeof page === 'undefined') {
throw new Error(`Page not found: ${path}`)
}

return pages[path]()
return typeof page === 'function' ? page() : page
}
1 change: 1 addition & 0 deletions tests/__data__/dummy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'Dummy File'
14 changes: 14 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import laravel from '../src'
import { resolvePageComponent } from '../src/inertia-helpers';

describe('laravel-vite-plugin', () => {
afterEach(() => {
Expand Down Expand Up @@ -218,3 +219,16 @@ describe('laravel-vite-plugin', () => {
expect(stringNoExternalConfig.ssr.noExternal).toEqual(['foo', 'laravel-vite-plugin'])
})
})

describe('inertia-helpers', () => {
const path = './__data__/dummy.ts'
it('pass glob value to resolvePageComponent', async () => {
const file = await resolvePageComponent<{ default: string }>(path, import.meta.glob('./__data__/*.ts'))
expect(file.default).toBe('Dummy File')
})

it('pass globEager value to resolvePageComponent', async () => {
const file = await resolvePageComponent<{ default: string }>(path, import.meta.globEager('./__data__/*.ts'))
expect(file.default).toBe('Dummy File')
})
})