diff --git a/packages/router/test/nuxt/index.test.js b/packages/router/test/nuxt/index.test.js index 58559174d..6faaa2c03 100644 --- a/packages/router/test/nuxt/index.test.js +++ b/packages/router/test/nuxt/index.test.js @@ -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() })