Skip to content

Commit

Permalink
chore(#693): update druxt-router tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Aug 13, 2024
1 parent c8f524f commit f139c90
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions packages/router/test/nuxt/index.test.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
import DruxtRouterNuxtModule from '../../nuxt'
import DruxtRouterNuxtModule from '../../src/nuxt'

const mock = {
addModule: jest.fn(),
addPlugin: jest.fn(),
jest.mock('@nuxt/kit', () => ({
addPluginTemplate: jest.fn(),
addTemplate: jest.fn(),
extendRoutes: jest.fn((func) => {
const routes = []
const resolve = jest.fn()
defineNuxtModule: (module) => module,
extendPages: jest.fn(),
installModule: jest.fn(),
}))

func(routes, resolve)
import { addPluginTemplate, addTemplate, extendPages, installModule } from '@nuxt/kit'

const nuxtMock = {
hook: jest.fn((hook, fn) => {
const arg = {
'components:dirs': [],
'storybook:config': { stories: [] }
}
return fn(arg[hook])
}),
nuxt: {
hook: jest.fn((hook, fn) => {
const arg = {
'components:dirs': [],
'storybook:config': { stories: [] }
}
return fn(arg[hook])
}),
options: { build: {} }
},
DruxtRouterNuxtModule
}


test('Nuxt module', async () => {
mock.options = {
nuxtMock.options = {
build: {},
buildDir: '',
dir: { pages: 'pages' },
druxt: { baseUrl: 'https://demo-api.druxtjs.org' },
srcDir: __dirname,
}

mock.options.druxt.router = { pages: true }
await mock.DruxtRouterNuxtModule()
expect(mock.addPlugin).toHaveBeenCalledTimes(2)
expect(mock.addTemplate).toHaveBeenCalledTimes(2)
expect(mock.nuxt.hook).toHaveBeenCalledTimes(2)
nuxtMock.options.druxt.router = { pages: true }
await DruxtRouterNuxtModule.setup({}, nuxtMock)
expect(addPluginTemplate).toHaveBeenCalledTimes(2)
expect(addTemplate).toHaveBeenCalledTimes(1)
expect(nuxtMock.hook).toHaveBeenCalledTimes(1)
jest.clearAllMocks()

mock.options.druxt.router = { wildcard: false }
await mock.DruxtRouterNuxtModule()
expect(mock.addPlugin).toHaveBeenCalledTimes(2)
expect(mock.addTemplate).toHaveBeenCalledTimes(1)
expect(mock.nuxt.hook).toHaveBeenCalledTimes(2)
nuxtMock.options.druxt.router = { wildcard: false }
await DruxtRouterNuxtModule.setup({}, nuxtMock)
expect(addPluginTemplate).toHaveBeenCalledTimes(2)
expect(addTemplate).toHaveBeenCalledTimes(0)
expect(nuxtMock.hook).toHaveBeenCalledTimes(1)
jest.clearAllMocks()

mock.options.druxt.router = { pages: false }
await mock.DruxtRouterNuxtModule()
expect(mock.addPlugin).toHaveBeenCalledTimes(2)
expect(mock.addTemplate).toHaveBeenCalledTimes(2)
expect(mock.nuxt.hook).toHaveBeenCalledTimes(3)
expect(mock.nuxt.options.build.createRoutes()).toStrictEqual([])
nuxtMock.options.druxt.router = { pages: false }
await DruxtRouterNuxtModule.setup({}, nuxtMock)
expect(addPluginTemplate).toHaveBeenCalledTimes(2)
expect(addTemplate).toHaveBeenCalledTimes(1)
expect(nuxtMock.hook).toHaveBeenCalledTimes(2)
expect(nuxtMock.options.build.createRoutes()).toStrictEqual([])
jest.clearAllMocks()
})

0 comments on commit f139c90

Please sign in to comment.