|
1 | 1 | import { Nuxt } from '@nuxt/core-edge'
|
2 |
| -import { Builder } from '@nuxt/builder-edge' |
3 |
| -import { BundleBuilder } from '@nuxt/webpack-edge' |
4 |
| -import { Configuration } from '@nuxt/types' |
| 2 | +import dartSass from 'sass' |
5 | 3 | import VuetifyLoaderPlugin from 'vuetify-loader/lib/plugin'
|
6 | 4 |
|
7 |
| -import vuetifyModule, { VuetifyLoaderOptions } from '../src' |
| 5 | +import _vuetifyModule from '../src' |
| 6 | +import _initOptions, { defaults as defaultOptions, Options, VuetifyLoaderOptions } from '../src/options' |
| 7 | +import _setupBuild from '../src/build' |
| 8 | +import _setupFont, { FontOptions } from '../src/font' |
| 9 | +import _setupIcons, { IconPreset } from '../src/icons' |
| 10 | +import _setupSass from '../src/sass' |
8 | 11 |
|
9 |
| -jest.setTimeout(60000) |
10 | 12 | jest.mock('vuetify-loader/lib/plugin')
|
11 | 13 |
|
12 |
| -const buildWithVuetifyModule = async (config: Partial<Configuration> = {}) => { |
13 |
| - const nuxt = new Nuxt({ |
14 |
| - buildModules: [vuetifyModule], |
15 |
| - ...config |
16 |
| - } as Configuration) |
| 14 | +let nuxt |
17 | 15 |
|
18 |
| - try { |
19 |
| - await nuxt.ready() |
20 |
| - await new Builder(nuxt, BundleBuilder).build() |
21 |
| - } catch (err) { |
| 16 | +const vuetifyModule = async (options?: Options) => { |
| 17 | + _vuetifyModule.call(nuxt.moduleContainer, options) |
| 18 | + await nuxt.callHook('build:before') |
| 19 | +} |
| 20 | +const initOptions = (options?: Options): Required<Options> => _initOptions.call(nuxt.moduleContainer, options) |
| 21 | +const setupBuild = (options?: Options) => { |
| 22 | + _setupBuild.call(nuxt.moduleContainer, options) |
| 23 | + nuxt.options.build.extend && nuxt.options.build.extend({ plugins: [] }) |
| 24 | +} |
| 25 | +const setupFont = (options?: FontOptions) => _setupFont.call(nuxt.moduleContainer, options) |
| 26 | +const setupIcons = (preset?: IconPreset) => _setupIcons.call(nuxt.moduleContainer, preset) |
| 27 | +const setupSass = (options?: Options) => _setupSass.call(nuxt.moduleContainer, options) |
22 | 28 |
|
23 |
| - } |
| 29 | +beforeEach(async () => { |
| 30 | + nuxt = new Nuxt() |
| 31 | + await nuxt.ready() |
| 32 | +}) |
24 | 33 |
|
25 |
| - return nuxt |
26 |
| -} |
| 34 | +describe('initOptions', () => { |
| 35 | + test('default', () => { |
| 36 | + const options = initOptions() |
27 | 37 |
|
28 |
| -describe('module', () => { |
29 |
| - let nuxt |
| 38 | + expect(options).toEqual(defaultOptions) |
| 39 | + }) |
| 40 | +}) |
30 | 41 |
|
31 |
| - beforeEach(() => { |
32 |
| - jest.clearAllMocks() |
| 42 | +describe('setupFont', () => { |
| 43 | + test('default', () => { |
| 44 | + setupFont(defaultOptions.defaultAssets.font) |
| 45 | + |
| 46 | + expect(nuxt.options.head.link).toContainEqual({ |
| 47 | + rel: 'stylesheet', |
| 48 | + type: 'text/css', |
| 49 | + href: `https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900&display=swap` |
| 50 | + }) |
33 | 51 | })
|
34 | 52 |
|
35 |
| - test('with default options', async () => { |
36 |
| - nuxt = await buildWithVuetifyModule({ |
37 |
| - dir: { |
38 |
| - app: '' |
| 53 | + test('with nuxt-webfontloader', () => { |
| 54 | + nuxt.options.modules = ['nuxt-webfontloader'] |
| 55 | + |
| 56 | + setupFont({ |
| 57 | + family: 'Montserrat', |
| 58 | + size: 20 } |
| 59 | + ) |
| 60 | + |
| 61 | + expect(nuxt.options.webfontloader).toEqual({ |
| 62 | + google: { |
| 63 | + families: ['Montserrat:100,300,400,500,700,900&display=swap'] |
39 | 64 | }
|
40 | 65 | })
|
41 | 66 |
|
42 |
| - expect(nuxt.options.head.link).toHaveLength(2) |
43 |
| - expect(nuxt.options.build.templates).toHaveLength(2) |
44 |
| - expect(nuxt.options.build.templates.map(t => t.dst)).toEqual(['vuetify/options.js', 'vuetify/plugin.js']) |
| 67 | + expect(nuxt.options.build.loaders.sass.prependData).toContain("$body-font-family: 'Montserrat', sans-serif") |
| 68 | + expect(nuxt.options.build.loaders.sass.prependData).toContain('$font-size-root: 20px') |
45 | 69 | })
|
| 70 | +}) |
46 | 71 |
|
47 |
| - test('without defaultAssets', async () => { |
48 |
| - nuxt = await buildWithVuetifyModule({ |
49 |
| - vuetify: { |
50 |
| - defaultAssets: false |
51 |
| - } |
| 72 | +describe('setupIcons', () => { |
| 73 | + test('default', () => { |
| 74 | + setupIcons(defaultOptions.defaultAssets.icons) |
| 75 | + |
| 76 | + expect(nuxt.options.head.link).toContainEqual({ |
| 77 | + rel: 'stylesheet', |
| 78 | + type: 'text/css', |
| 79 | + href: 'https://cdn.jsdelivr.net/npm/@mdi/font@latest/css/materialdesignicons.min.css' |
52 | 80 | })
|
| 81 | + }) |
| 82 | +}) |
53 | 83 |
|
54 |
| - expect(nuxt.options.head.link).toHaveLength(0) |
| 84 | +describe('setupSass', () => { |
| 85 | + test('default', () => { |
| 86 | + setupSass(defaultOptions) |
| 87 | + |
| 88 | + expect(nuxt.options.build.loaders.sass.indentedSyntax).toBeUndefined() |
| 89 | + expect(nuxt.options.build.loaders.sass.implementation).toEqual(dartSass) |
| 90 | + expect(nuxt.options.build.loaders.scss.implementation).toEqual(dartSass) |
55 | 91 | })
|
56 | 92 |
|
57 |
| - test('with customVariables', async () => { |
58 |
| - nuxt = await buildWithVuetifyModule({ |
59 |
| - vuetify: { |
60 |
| - customVariables: ['/path/to/variables.scss'] |
61 |
| - } |
| 93 | + test('customVariables', () => { |
| 94 | + setupSass({ |
| 95 | + customVariables: ['/path/to/variables.scss'] |
62 | 96 | })
|
63 | 97 |
|
64 |
| - expect(nuxt.options.build.loaders.sass.prependData).toEqual("@import '/path/to/variables.scss'") |
| 98 | + expect(nuxt.options.build.loaders.sass.prependData).toContain("@import '/path/to/variables.scss'") |
65 | 99 | })
|
| 100 | +}) |
66 | 101 |
|
67 |
| - test('with customVariables (with existing prependData)', async () => { |
68 |
| - nuxt = await buildWithVuetifyModule({ |
69 |
| - build: { |
70 |
| - loaders: { |
71 |
| - sass: { |
72 |
| - prependData: '$someVariable: #000000' |
73 |
| - } |
74 |
| - } |
75 |
| - }, |
76 |
| - vuetify: { |
77 |
| - customVariables: ['/path/to/variables.scss'] |
78 |
| - } |
79 |
| - }) |
| 102 | +describe('setupBuild', () => { |
| 103 | + test('default', () => { |
| 104 | + nuxt.options.dir.app = '' |
80 | 105 |
|
81 |
| - expect(nuxt.options.build.loaders.sass.prependData).toEqual("$someVariable: #000000\n@import '/path/to/variables.scss'") |
| 106 | + setupBuild(defaultOptions) |
| 107 | + |
| 108 | + expect(nuxt.options.css).toContain('vuetify/dist/vuetify.css') |
| 109 | + expect(nuxt.options.build.templates.map(t => t.dst)).toEqual(['vuetify/options.js', 'vuetify/plugin.js']) |
82 | 110 | })
|
83 | 111 |
|
84 |
| - test('with optionsPath', async () => { |
85 |
| - nuxt = await buildWithVuetifyModule({ |
86 |
| - vuetify: { |
87 |
| - optionsPath: 'test/fixture/vuetify.options.ts' |
88 |
| - } |
| 112 | + test('optionsPath', () => { |
| 113 | + setupBuild({ |
| 114 | + ...defaultOptions, |
| 115 | + optionsPath: 'test/fixture/vuetify.options.ts' |
89 | 116 | })
|
90 | 117 |
|
91 | 118 | expect(nuxt.options.build.templates.map(t => t.dst)).toContain('vuetify/options.ts')
|
92 | 119 | })
|
93 | 120 |
|
94 |
| - test('with treeShake', async () => { |
95 |
| - nuxt = await buildWithVuetifyModule({ |
96 |
| - vuetify: { |
97 |
| - treeShake: true |
98 |
| - } |
| 121 | + test('treeShake', () => { |
| 122 | + setupBuild({ |
| 123 | + treeShake: true |
99 | 124 | })
|
100 | 125 |
|
101 | 126 | expect(nuxt.options.build.transpile).toContain('vuetify/lib')
|
102 |
| - expect(VuetifyLoaderPlugin).toHaveBeenCalledTimes(2) // client (1) + server (1) = (2) |
| 127 | + expect(VuetifyLoaderPlugin).toHaveBeenCalled() |
103 | 128 | })
|
104 | 129 |
|
105 |
| - test('with treeShake (and loaderOptions)', async () => { |
| 130 | + test('treeShake with loaderOptions', () => { |
106 | 131 | const loaderOptions: VuetifyLoaderOptions = {
|
107 | 132 | match () {
|
108 | 133 | return []
|
109 | 134 | }
|
110 | 135 | }
|
111 | 136 |
|
112 |
| - nuxt = await buildWithVuetifyModule({ |
113 |
| - vuetify: { |
114 |
| - treeShake: { |
115 |
| - loaderOptions |
116 |
| - } |
| 137 | + setupBuild({ |
| 138 | + treeShake: { |
| 139 | + loaderOptions |
117 | 140 | }
|
118 | 141 | })
|
119 | 142 |
|
120 |
| - expect(VuetifyLoaderPlugin).toHaveBeenNthCalledWith(2, loaderOptions) |
| 143 | + expect(nuxt.options.build.transpile).toContain('vuetify/lib') |
| 144 | + expect(VuetifyLoaderPlugin).toHaveBeenCalledWith(loaderOptions) |
| 145 | + }) |
| 146 | +}) |
| 147 | + |
| 148 | +describe('module', () => { |
| 149 | + test('default', async () => { |
| 150 | + await vuetifyModule(defaultOptions) |
| 151 | + }) |
| 152 | + |
| 153 | + test('without defaultAssets', async () => { |
| 154 | + await vuetifyModule({ |
| 155 | + ...defaultOptions, |
| 156 | + defaultAssets: false |
| 157 | + }) |
121 | 158 | })
|
122 | 159 | })
|
0 commit comments