@hankei6km/jest-mock-nuxt-content
Version:
nuxt-content mock for jest
194 lines (125 loc) • 4.12 kB
Markdown
[](https://content.nuxtjs.org/) mock for [jest](https://jestjs.io/),
jest-mock-nuxt-content is inspired by [jest-mock-axios](https://www.npmjs.com/package/jest-mock-axios).
```
$ npm install --save-dev @hankei6km/jest-mock-nuxt-content
```
or
```
$ yarn add --dev @hankei6km/jest-mock-nuxt-content
```
Basic flow.
- call `asyncData` with injecting mock(`$content`)
- `fetch()` has been called in`asyncData`(waiting mock data)
- ensure `$content` has been called with content path etc.
- pass mock data via `context.mockResponse()` to waiting `fetch()` and receive chain list of [methods](https://content.nuxtjs.org/fetching)(history to chain sequence)
- ensure chain list
- fimaly, verify a return value from `asyncData()`
```typescript
// pages/index.vue
export default Vue.extend({
async asyncData({ $content, params }) {
const article = await $content('pages/home').fetch()
const images = await $content('gallery').sortBy('position').fetch()
return { article, images }
},
// snip...
})
```
```typescript
// test/pages/index.ts
import { shallowMount } from '@vue/test-utils'
import { mockContent } from '@hankei6km/jest-mock-nuxt-content'
import indexPage from '~/pages/index.vue'
describe('IndexPage', () => {
it('should calls $content.fetch() method in asyncData', async () => {
const content = mockContent()
const $content = content.$content
const mockDataArticle = { title: 'home' }
const mockDataImages = [
// snip...
]
const wrapperAsyncData = shallowMount(indexPage, {
// snip...
})
if (wrapperAsyncData.vm.$options.asyncData) {
// Note: this flow diffrent from [Nuxt lifecycle](https://nuxtjs.org/docs/concepts/nuxt-lifecycle).
const data = wrapperAsyncData.vm.$options.asyncData({
$content,
params: {},
} as any)
// ensure frist fetch() called
expect($content).toHaveBeenLastCalledWith('pages/home')
await content.mockResponse(mockDataArticle)
// ensure second fetch() called
expect($content).toHaveBeenLastCalledWith('gallery')
const imagesChain = await content.mockResponse(mockDataImages)
expect(imagesChain.at(0).getMockName()).toEqual('sortBy')
expect(imagesChain.at(0)).toHaveBeenCalledWith('position')
// verify a return value from asyncData()
expect(await data).toEqual({
article: mockDataArticle,
images: mockDataImages,
})
// render actual page by using mock data from asyncData()
// Note: this flow diffrent from [Nuxt lifecycle](https://nuxtjs.org/docs/concepts/nuxt-lifecycle).
const mockData = await data
const wrapper = shallowMount(indexPage, {
data() {
return mockData
},
// snip...
})
// snip..
}
})
})
```
return instance of `content`.
entry point of mocked methods for inject into the context.
- pass mock data to `fetch()` that is waiting response data
- return the chain list made when `fetch()` has been called
mock data to `fetch()`.
`Promise<ChainList>`
- recject `fetch()` that is waiting response data
- return the chain list made when `fetch()` has been called
reason to reject.
`Promise<ChainList>`
count of called mocked methods.
`number`
return mocked method at passed index.
index of mocked method.
`jest.Mock<any, any>`
return the first mocked method found.
error thrown if mocked method could not be found.
name of mocked method.
`jest.Mock<any, any>`
return all mocked methods found.
return empty array if mocked method could not be found.
name of mocked method.
`jest.Mock<any ,any>[]`
MIT License
Copyright (c) 2021 hankei6km