Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silverbackdan committed Sep 28, 2024
1 parent b30da22 commit 04eb096
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@vueuse/core": "^10.11.1",
"consola": "^3.2.3",
"dayjs": "^1.11.13",
"defu": "^6.1.4",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"luxon": "^3.5.0",
Expand Down
13 changes: 8 additions & 5 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { defineNuxtConfig } from 'nuxt/config'

const API_URL = process.env.API_URL || 'https://localhost:8443'
const API_URL_BROWSER = process.env.API_URL_BROWSER || API_URL

export default defineNuxtConfig({
runtimeConfig: {
public: {
cwa: {
apiUrl: 'https://localhost',
apiUrlBrowser: 'https://localhost'
}
}
},
app: {
head: {
titleTemplate: '%s - CWA Playground',
Expand Down Expand Up @@ -36,8 +41,6 @@ export default defineNuxtConfig({
},
cwa: {
appName: 'CWA Module Test Playground',
apiUrl: API_URL,
apiUrlBrowser: API_URL_BROWSER,
resources: {
NavigationLink: {
name: 'Link',
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions src/module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'path'
import { describe, expect, Mock, test, vi } from 'vitest'
import { describe, expect, type Mock, test, vi } from 'vitest'
import * as nuxtKit from '@nuxt/kit'

vi.mock('@nuxt/kit', async () => {
Expand Down Expand Up @@ -31,12 +31,12 @@ vi.mock('node:fs', () => {
}
})

async function prepareMockNuxt (options = {}, nuxt?) {
async function prepareMockNuxt (options = {}, nuxt?: any) {
await import('./module')

const [{ setup }] = (nuxtKit.defineNuxtModule as Mock).mock.lastCall

const mockNuxt = Object.assign({ hook: vi.fn(), options: { alias: {}, css: [], build: { transpile: [] }, srcDir: '' } }, nuxt || {})
const mockNuxt = Object.assign({ hook: vi.fn(), options: { runtimeConfig: { public: { cwa: {} } }, alias: {}, css: [], build: { transpile: [] }, srcDir: '' } }, nuxt || {})

await setup(Object.assign({ tailwind: { base: true } }, options), mockNuxt)

Expand Down Expand Up @@ -124,6 +124,7 @@ describe('CWA module', () => {
}
}),
options: {
runtimeConfig: { public: { cwa: {} } },
alias: {},
css: [],
build: {
Expand Down Expand Up @@ -215,6 +216,7 @@ export const currentModulePackageInfo:{ version: string, name: string } = {
}
}),
options: {
runtimeConfig: { public: { cwa: {} } },
alias: {},
css: [],
build: {
Expand Down Expand Up @@ -246,6 +248,7 @@ export const currentModulePackageInfo:{ version: string, name: string } = {
}
}),
options: {
runtimeConfig: { public: { cwa: {} } },
alias: {},
css: [],
build: {
Expand Down
19 changes: 17 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join } from 'path'
import path from 'node:path'
import { statSync, readFileSync } from 'node:fs'
import { defu } from 'defu'
import _mergeWith from 'lodash/mergeWith.js'
import _isArray from 'lodash/isArray.js'
import {
Expand Down Expand Up @@ -42,9 +43,9 @@ export interface CwaUiMeta {
export interface CwaModuleOptions {
appName: string
storeName: string
pagesDepth?: number
apiUrlBrowser?: string
apiUrl?: string
apiUrlBrowser?: string
pagesDepth?: number
resources?: CwaResourcesMeta
layouts?: {
[type: string]: CwaUiMeta
Expand All @@ -61,6 +62,15 @@ export interface CwaModuleOptions {
layoutName?: string
}

declare module '@nuxt/schema' {
interface PublicRuntimeConfig {
cwa: {
apiUrl: string
apiUrlBrowser: string
}
}
}

function createDefaultCwaPages (
pages: NuxtPage[],
pageComponentFilePath: string,
Expand Down Expand Up @@ -118,6 +128,11 @@ export default defineNuxtModule<CwaModuleOptions>({
}
},
async setup (options: CwaModuleOptions, nuxt) {
nuxt.options.runtimeConfig.public.cwa = defu(nuxt.options.runtimeConfig.public.cwa, {
apiUrl: options.apiUrl || options.apiUrlBrowser || '',
apiUrlBrowser: options.apiUrlBrowser || options.apiUrl || ''
})

const { resolve } = createResolver(import.meta.url)

const { version, name } = JSON.parse(
Expand Down
20 changes: 18 additions & 2 deletions src/runtime/cwa.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, test, vi, beforeEach } from 'vitest'
import * as nuxtApp from '#app/nuxt'
import type { CwaModuleOptions } from '../module'
import Cwa from './cwa'
import { Storage } from './storage/storage'
Expand All @@ -20,6 +21,12 @@ vi.mock('#app/composables/cookie.js', () => {
}
})

vi.mock('#app/nuxt', () => {
return {
useRuntimeConfig: vi.fn(() => ({ public: { cwa: { apiUrl: '', apiUrlBrowser: '' } } }))
}
})

vi.mock('./storage/storage', () => {
return {
Storage: vi.fn(() => ({
Expand Down Expand Up @@ -90,15 +97,24 @@ const path = 'something'
const storeName = 'dummystore'
const $router = vi.fn()
function createCwa ({ apiUrlBrowser, apiUrl }: CwaModuleOptions) {
vi.spyOn(nuxtApp, 'useRuntimeConfig').mockImplementation(() => ({
public: {
cwa: {
apiUrlBrowser,
apiUrl
}
}
}))
return new Cwa({
_route: {
path
},
$router
}, {
apiUrlBrowser,
apiUrl,
storeName
}, {
version: 'abc',
name: 'named'
})
}

Expand Down
7 changes: 4 additions & 3 deletions src/runtime/cwa.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RouteLocationNormalizedLoaded, Router } from 'vue-router'
import type { NuxtApp } from '#app/nuxt'
import { useRuntimeConfig, type NuxtApp } from '#app/nuxt'
import { useCookie } from '#app/composables/cookie.js'
import type { CwaModuleOptions, CwaResourcesMeta } from '../module'
import { Storage } from './storage/storage'
Expand Down Expand Up @@ -47,11 +47,12 @@ export default class Cwa {
constructor (nuxtApp: Pick<NuxtApp, '_route'|'_middleware'|'$router'|'cwaResources'>, options: CwaModuleOptions, currentModulePackageInfo: { version: string, name: string }) {
this.currentModulePackageInfo = currentModulePackageInfo
const { isClient } = useProcess()
const { public: { cwa: { apiUrl, apiUrlBrowser } } } = useRuntimeConfig()
const defaultApiUrl = 'https://api-url-not-set.com'
if (isClient) {
this.apiUrl = options.apiUrlBrowser || options.apiUrl || defaultApiUrl
this.apiUrl = apiUrlBrowser || apiUrl || defaultApiUrl
} else {
this.apiUrl = options.apiUrl || options.apiUrlBrowser || defaultApiUrl
this.apiUrl = apiUrl || apiUrlBrowser || defaultApiUrl
}

this.cwaFetch = new CwaFetch(this.apiUrl)
Expand Down

0 comments on commit 04eb096

Please sign in to comment.