Skip to content

Commit

Permalink
test: fix plugin test
Browse files Browse the repository at this point in the history
  • Loading branch information
ymmooot committed Nov 18, 2022
1 parent 7d98d17 commit 27da37d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
moduleFileExtensions: ['js', 'mjs', 'ts'],
collectCoverageFrom: ['src/runtime/**/*.{js,ts}', '!src/**/*.d.ts'],
collectCoverageFrom: ['src/runtime/**/*.{js,ts}', '!src/**/*.d.ts', '!src/runtime/plugin.ts'],
moduleNameMapper: {
'^@/(.*)': '<rootDir>/$1',
'#app': '<rootDir>/node_modules/nuxt/dist/app/index.mjs',
Expand Down
27 changes: 27 additions & 0 deletions src/runtime/plugin-impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { computed } from 'vue';
import { useHead } from '#head';

export default (nuxtApp) => {
const mixin = {
created() {
if (typeof this.$options?.jsonld !== 'function') {
return;
}
const jsonComputed = computed(() => this.$options.jsonld.call(this));
useHead(() => ({
script: [
{
type: 'application/ld+json',
children: jsonComputed.value ? JSON.stringify(jsonComputed.value, null, '') : undefined,
},
],
}));
},
};
const plugin = {
install(Vue) {
Vue.mixin(mixin);
},
};
nuxtApp.vueApp.use(plugin);
};
28 changes: 2 additions & 26 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
import { computed } from 'vue';
import { defineNuxtPlugin } from '#app';
import { useHead } from '#head';
import plugin from './plugin-impl';

export default defineNuxtPlugin((nuxtApp) => {
const mixin = {
created() {
if (typeof this.$options?.jsonld !== 'function') {
return;
}
const jsonComputed = computed(() => this.$options.jsonld.call(this));
useHead(() => ({
script: [
{
type: 'application/ld+json',
children: jsonComputed.value ? JSON.stringify(jsonComputed.value, null, '') : undefined,
},
],
}));
},
};
const plugin = {
install(Vue) {
Vue.mixin(mixin);
},
};
nuxtApp.vueApp.use(plugin);
});
export default defineNuxtPlugin(plugin);
2 changes: 1 addition & 1 deletion test/plugin.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import plugin from '../src/runtime/plugin';
import plugin from '../src/runtime/plugin-impl';

let useHeadArg = undefined;
jest.mock('#head', () => ({
Expand Down

0 comments on commit 27da37d

Please sign in to comment.