2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-05-20 05:19:38 +03:00
Files
vue-meta/test/unit/generators.test.js
T
Pim fc71e1f1c4 feat: enable onload callbacks (#414)
* refactor(examples): run ssr example from server

* chore: switch to babel for build

buble complains too much

* feat: enable loaded callbacks

feat: add skip option

* examples: add async-callback browser example

* examples: fix server

* examples(ssr): add reactive script with callback

* fix: also skip on ssr

* chore: remove unused var

* feat: only add mutationobserver if DOM is still loading

feat: disconnect mutation observer once DOM has loaded

* examples: pass vmid to loadCallback instead of el

* feat: also support load callbacks for link/style tags

* test: add unit tests for load

* test: add load e2e test

* chore: fix lint

* chore: remove unused files

* test: fix e2e load callback test

* test: fix attempt

* examples: ie9 compatiblity

destructuring doesnt work in ie9

* fix: add onload attribute on ssr

dont rely on mutationobserver

* chore: lint ci conf

* refactor: remove loadCallbackAttribute config option

test: fix coverage for load

* test: improve coverage

* fix: only use console when it exists (for ie9)

* chore: fix coverage
2019-07-24 10:18:40 +02:00

101 lines
3.1 KiB
JavaScript

import _generateServerInjector from '../../src/server/generateServerInjector'
import { defaultOptions } from '../../src/shared/constants'
import metaInfoData from '../utils/meta-info-data'
import { titleGenerator } from '../../src/server/generators'
const generateServerInjector = (type, data) => _generateServerInjector(defaultOptions, type, data)
describe('generators', () => {
for (const type in metaInfoData) {
const typeTests = metaInfoData[type]
const testCases = {
add: (tags) => {
let html = tags.text()
// ssr only returns the attributes, convert to full tag
if (['htmlAttrs', 'headAttrs', 'bodyAttrs'].includes(type)) {
html = `<${type.substr(0, 4)} ${html}>`
}
typeTests.add.expect.forEach((expected) => {
expect(html).toContain(expected)
})
}
}
describe(`${type} type tests`, () => {
Object.keys(typeTests).forEach((action) => {
const testInfo = typeTests[action]
// return when no test case available
if (!testCases[action] && !testInfo.test) {
return
}
const defaultTestFn = () => {
const tags = generateServerInjector(type, testInfo.data)
testCases[action](tags)
return tags
}
let testFn
if (testInfo.test) {
testFn = testInfo.test('server', defaultTestFn)
if (testFn === true) {
testFn = defaultTestFn
}
} else {
testFn = defaultTestFn
}
if (testFn && typeof testFn === 'function') {
test(`${action} a tag`, () => {
expect.hasAssertions()
testFn()
})
}
})
})
}
})
describe('extra tests', () => {
test('title generator should return an empty string when title is null', () => {
const title = null
const generatedTitle = titleGenerator({}, 'title', title)
expect(generatedTitle.text()).toEqual('')
})
test('auto add ssrAttribute', () => {
const htmlAttrs = generateServerInjector('htmlAttrs', {})
expect(htmlAttrs.text(true)).toBe('data-vue-meta-server-rendered')
const headAttrs = generateServerInjector('headAttrs', {})
expect(headAttrs.text(true)).toBe('')
const bodyAttrs = generateServerInjector('bodyAttrs', {})
expect(bodyAttrs.text(true)).toBe('')
})
test('script prepend body', () => {
const tags = [{ src: '/script.js', pbody: true }]
const scriptTags = generateServerInjector('script', tags)
expect(scriptTags.text()).toBe('')
expect(scriptTags.text({ body: true })).toBe('')
expect(scriptTags.text({ pbody: true })).toBe('<script data-vue-meta="ssr" src="/script.js" data-pbody="true"></script>')
})
test('script append body', () => {
const tags = [{ src: '/script.js', body: true }]
const scriptTags = generateServerInjector('script', tags)
expect(scriptTags.text()).toBe('')
expect(scriptTags.text({ body: true })).toBe('<script data-vue-meta="ssr" src="/script.js" data-body="true"></script>')
expect(scriptTags.text({ pbody: true })).toBe('')
})
})