-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1463 from resessh/update-unit-tests
Update unit tests
- Loading branch information
Showing
9 changed files
with
264 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,25 @@ | ||
import { computed } from 'vue'; | ||
import { computed, type Plugin } from 'vue'; | ||
import { useHead } from '@unhead/vue'; | ||
|
||
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); | ||
export const vuePlugin: Plugin = { | ||
install(Vue) { | ||
Vue.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, | ||
}, | ||
], | ||
})); | ||
}, | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { defineNuxtPlugin } from 'nuxt/app'; | ||
import plugin from './plugin-impl'; | ||
import { vuePlugin } from './plugin-impl'; | ||
|
||
export default defineNuxtPlugin(plugin); | ||
export default defineNuxtPlugin((nuxtApp) => { | ||
nuxtApp.vueApp.use(vuePlugin); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { describe, beforeEach, it, expect, vi, type Mock } from 'vitest'; | ||
import { toValue } from 'vue'; | ||
import { mount } from '@vue/test-utils'; | ||
|
||
import { vuePlugin } from '../src/runtime/plugin-impl'; | ||
|
||
const { useHead } = vi.hoisted(() => { | ||
return { | ||
useHead: vi.fn(), | ||
}; | ||
}); | ||
vi.mock('@unhead/vue', () => ({ | ||
useHead, | ||
})); | ||
|
||
const getLastCalledParams = (mock: Mock<any>) => mock.mock.calls[mock.mock.calls.length - 1]; | ||
|
||
describe('plugin-impl', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('does not call useHead when $jsonld is not set', () => { | ||
mount( | ||
{ | ||
template: '<div />', | ||
}, | ||
{ | ||
global: { | ||
plugins: [vuePlugin], | ||
}, | ||
} | ||
); | ||
|
||
expect(useHead).not.toBeCalled(); | ||
}); | ||
|
||
it('calls useHead', () => { | ||
mount( | ||
{ | ||
template: '<div />', | ||
data() { | ||
return { | ||
count: 0, | ||
}; | ||
}, | ||
jsonld() { | ||
return { | ||
'@context': 'https://schema.org', | ||
'@type': 'Thing', | ||
name: this.count, | ||
}; | ||
}, | ||
}, | ||
{ | ||
global: { | ||
plugins: [vuePlugin], | ||
}, | ||
} | ||
); | ||
|
||
expect(useHead).toBeCalledTimes(1); | ||
expect(toValue(getLastCalledParams(useHead)[0])).toEqual({ | ||
script: [ | ||
{ | ||
type: 'application/ld+json', | ||
children: '{"@context":"https://schema.org","@type":"Thing","name":0}', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('calls useHead with enpty when $jsonld response is undefined', () => { | ||
mount( | ||
{ | ||
template: '<div />', | ||
jsonld() { | ||
return undefined; | ||
}, | ||
}, | ||
{ | ||
global: { | ||
plugins: [vuePlugin], | ||
}, | ||
} | ||
); | ||
|
||
expect(useHead).toBeCalled(); | ||
expect(toValue(getLastCalledParams(useHead)[0])).toEqual({ | ||
script: [ | ||
{ | ||
type: 'application/ld+json', | ||
children: undefined, | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { describe, beforeEach, it, expect, vi, type Mock } from 'vitest'; | ||
|
||
import plugin from '../src/runtime/plugin'; | ||
|
||
const { vuePlugin, vueAppUse } = vi.hoisted(() => { | ||
return { | ||
vuePlugin: vi.fn(), | ||
vueAppUse: vi.fn(), | ||
}; | ||
}); | ||
vi.mock('../src/runtime/plugin-impl.ts', () => ({ | ||
vuePlugin, | ||
})); | ||
vi.mock('nuxt/app', () => ({ | ||
defineNuxtPlugin: vi.fn().mockImplementation((fn) => fn), | ||
})); | ||
|
||
describe('plugin', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('calles vuePlugin', () => { | ||
plugin({ vueApp: { use: vueAppUse } } as any); | ||
expect(vueAppUse).toBeCalledWith(vuePlugin); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.