Skip to content

Commit

Permalink
chore: Add tests and address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
David Marr committed Jul 29, 2024
1 parent 10d4fea commit 206c1b8
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 8 deletions.
37 changes: 37 additions & 0 deletions packages/nuxt/__tests__/nuxt-layer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @vitest-env node
*/
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import { describe, it, expect } from 'vitest'
import { setup, $fetch } from '@nuxt/test-utils'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

await setup({
server: true,
rootDir: path.join(__dirname, '../layer/playground'),
nuxtConfig: {
hooks: {
'vite:extendConfig'(config, { isClient }) {
config.define!.__BROWSER__ = isClient
},
},
vite: {
define: {
__DEV__: false,
__TEST__: true,
__FEATURE_PROD_DEVTOOLS__: false,
__USE_DEVTOOLS__: false,
},
},
},
})

describe('works with nuxt layers', async () => {
it('loads stores when extending', async () => {
const html = await $fetch('/')
expect(html).toContain('Base store: 3')
expect(html).toContain('App store: xyz')
})
})
22 changes: 22 additions & 0 deletions packages/nuxt/layer/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineNuxtConfig } from 'nuxt/config'
import piniaModule from '../src/module'

const currentDir = dirname(fileURLToPath(import.meta.url))

export default defineNuxtConfig({
modules: [piniaModule],

pinia: {
storesDirs: [join(currentDir, 'stores')],
},

vite: {
define: {
__DEV__: JSON.stringify(process.env.NODE_ENV !== 'production'),
__USE_DEVTOOLS__: true,
__TEST__: false,
},
},
})
10 changes: 10 additions & 0 deletions packages/nuxt/layer/playground/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts" setup>
const baseStore = useExampleStore();
const appStore = useXYZStore();
</script>
<template>
<div>
<p>Base store: {{ baseStore.example }}</p>
<p>App store: {{ appStore.foo }}</p>
</div>
</template>
15 changes: 15 additions & 0 deletions packages/nuxt/layer/playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineNuxtConfig } from 'nuxt/config'

export default defineNuxtConfig({
extends: ['..'],
pinia: {
storesDirs: ['xyz'],
},
vite: {
define: {
__DEV__: JSON.stringify(process.env.NODE_ENV !== 'production'),
__USE_DEVTOOLS__: true,
__TEST__: false,
},
},
})
6 changes: 6 additions & 0 deletions packages/nuxt/layer/playground/xyz/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const useXYZStore = defineStore('xyz', () => {
const foo = ref<string>('xyz')
return {
foo,
}
})
6 changes: 6 additions & 0 deletions packages/nuxt/layer/stores/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const useExampleStore = defineStore('example', () => {
const example = ref<number>(3)
return {
example,
}
})
10 changes: 2 additions & 8 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,8 @@ const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
options.storesDirs = [resolver.resolve(nuxt.options.srcDir, 'stores')]
}

if (options.storesDirs) {
for (const storeDir of [
...options.storesDirs,
/* @ts-expect-error storesDirs isn't on base NuxtOptions type */
...(nuxt.options.pinia?.storesDirs || []),
]) {
addImportsDir(resolver.resolve(nuxt.options.rootDir, storeDir))
}
for (const storeDir of options.storesDirs) {
addImportsDir(resolver.resolve(nuxt.options.rootDir, storeDir))
}
},
})
Expand Down

0 comments on commit 206c1b8

Please sign in to comment.