2019-02-04 10:34:04 +00:00
|
|
|
import hash from 'hash-sum'
|
|
|
|
import consola from 'consola'
|
|
|
|
import serialize from 'serialize-javascript'
|
|
|
|
|
|
|
|
import devalue from '@nuxt/devalue'
|
|
|
|
import { r, wp, wChunk, serializeFunction } from '@nuxt/utils'
|
|
|
|
import TemplateContext from '../../src/context/template'
|
|
|
|
|
|
|
|
jest.mock('lodash', () => ({ test: 'test lodash', warn: 'only once' }))
|
|
|
|
|
|
|
|
describe('builder: buildContext', () => {
|
|
|
|
const builder = {
|
|
|
|
template: { files: ['template.js'] },
|
2019-11-26 22:42:39 +00:00
|
|
|
globals: ['globals'],
|
|
|
|
plugins: ['plugins'],
|
2019-02-04 10:34:04 +00:00
|
|
|
relativeToBuild: jest.fn((...args) => `relativeBuild(${args.join(', ')})`)
|
|
|
|
}
|
|
|
|
const options = {
|
2019-09-05 15:15:27 +00:00
|
|
|
features: { store: true },
|
2019-11-26 22:42:39 +00:00
|
|
|
extensions: ['test', 'ext'],
|
2019-02-04 10:34:04 +00:00
|
|
|
messages: { test: 'test message' },
|
|
|
|
build: {
|
|
|
|
splitChunks: { testSC: true }
|
|
|
|
},
|
|
|
|
dev: 'test_dev',
|
|
|
|
test: 'test_test',
|
|
|
|
debug: 'test_debug',
|
|
|
|
vue: { config: 'test_config' },
|
|
|
|
mode: 'test mode',
|
|
|
|
router: { route: 'test' },
|
|
|
|
env: 'test_env',
|
|
|
|
head: 'test_head',
|
|
|
|
store: 'test_store',
|
|
|
|
globalName: 'test_global',
|
2019-11-26 22:42:39 +00:00
|
|
|
css: ['test.css'],
|
2019-02-04 10:34:04 +00:00
|
|
|
layouts: {
|
|
|
|
'test-layout': 'test.template'
|
|
|
|
},
|
|
|
|
srcDir: 'test_src_dir',
|
2019-05-06 10:46:04 +00:00
|
|
|
rootDir: 'test_root_dir',
|
2019-02-04 10:34:04 +00:00
|
|
|
loading: 'test-loading',
|
2019-04-20 10:01:59 +00:00
|
|
|
pageTransition: { name: 'test_trans' },
|
2019-02-04 10:34:04 +00:00
|
|
|
layoutTransition: { name: 'test_layout_trans' },
|
|
|
|
dir: ['test_dir'],
|
|
|
|
ErrorPage: 'test_error_page'
|
|
|
|
}
|
|
|
|
|
|
|
|
test('should construct context', () => {
|
|
|
|
const context = new TemplateContext(builder, options)
|
|
|
|
expect(context).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should return object loading template options', () => {
|
|
|
|
const context = new TemplateContext(builder, {
|
|
|
|
...options,
|
|
|
|
loading: { name: 'test_loading' }
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(context.templateVars.loading).toEqual({ name: 'test_loading' })
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should return object loading template options', () => {
|
|
|
|
const context = new TemplateContext(builder, options)
|
|
|
|
const templateOptions = context.templateOptions
|
|
|
|
expect(templateOptions).toEqual({
|
|
|
|
imports: {
|
|
|
|
serialize,
|
|
|
|
serializeFunction,
|
|
|
|
devalue,
|
|
|
|
hash,
|
|
|
|
r,
|
|
|
|
wp,
|
|
|
|
wChunk,
|
|
|
|
_: {}
|
|
|
|
},
|
|
|
|
interpolate: /<%=([\s\S]+?)%>/g
|
|
|
|
})
|
|
|
|
expect(templateOptions.imports._.test).toEqual('test lodash')
|
|
|
|
expect(templateOptions.imports._.warn).toEqual('only once')
|
|
|
|
expect(consola.warn).toBeCalledTimes(1)
|
|
|
|
expect(consola.warn).toBeCalledWith('Avoid using _ inside templates')
|
|
|
|
})
|
|
|
|
})
|