Skip to content

Commit

Permalink
test: add basic test of onFetch
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Apr 27, 2020
1 parent 91754b1 commit b1e2502
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 194 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
lib/*
!lib/.gitkeep
node_modules
coverage
coverage
.nuxt
9 changes: 0 additions & 9 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
module.exports = {
verbose: true,
collectCoverage: true,
coveragePathIgnorePatterns: ['test', '.babelrc.js'],
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
}
15 changes: 5 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,25 @@
"@babel/preset-env": "^7.9.5",
"@babel/preset-typescript": "^7.9.0",
"@nuxt/types": "^0.7.5",
"@release-it/conventional-changelog": "^1.1.3",
"@nuxtjs/module-test-utils": "^1.6.1",
"@release-it/conventional-changelog": "^1.1.4",
"@types/jest": "^25.2.1",
"@types/jsdom": "^16.2.1",
"@types/memory-fs": "^0.3.2",
"@types/webpack": "^4.41.12",
"@typescript-eslint/eslint-plugin": "^2.29.0",
"@typescript-eslint/parser": "^2.29.0",
"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
"@vue/composition-api": "^0.5.0",
"@vue/test-utils": "^1.0.0-beta.33",
"babel-loader": "^8.1.0",
"codecov": "^3.6.5",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-jest": "^23.8.2",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-promise": "^4.2.1",
"flush-promises": "^1.0.2",
"jest": "^25.4.0",
"lint-staged": "^10.1.7",
"memory-fs": "^0.5.0",
"npm-run-all": "^4.1.5",
"nuxt": "^2.12.2",
"prettier": "^2.0.5",
"prettier-eslint-cli": "^5.0.0",
"release-it": "13.5.5",
"release-it": "13.5.6",
"tsd": "^0.11.0",
"yorkie": "^2.0.0"
},
Expand Down
3 changes: 2 additions & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {
onServerPrefetch,
onBeforeMount,
} from '@vue/composition-api'
import { ComponentInstance } from '@vue/composition-api/dist/component'
import { normalizeError } from '@nuxt/vue-app'

import { ComponentInstance } from '@vue/composition-api/dist/component'

interface Fetch {
(context: ComponentInstance): void
}
Expand Down
5 changes: 0 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
import Vue from 'vue'
import CompositionApi from '@vue/composition-api'

Vue.use(CompositionApi)

export { onFetch } from './fetch'
export { withContext } from './context'
26 changes: 26 additions & 0 deletions test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// eslint-disable-next-line
const { resolve } = require('path')

module.exports = {
rootDir: resolve(__dirname, '../..'),
buildDir: resolve(__dirname, '.nuxt'),
srcDir: __dirname,
render: {
resourceHints: false,
},
plugins: [require.resolve('./plugins/composition-api')],
build: {
babel: {
presets: [
[
'@nuxt/babel-preset-app',
{
corejs: {
version: 3,
},
},
],
],
},
},
}
32 changes: 32 additions & 0 deletions test/fixture/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<template>
<div>
<div>name-{{ name }}</div>
</div>
</template>

<script>
import { defineComponent, ref } from '@vue/composition-api'
import { onFetch } from '../../..'
export function fetcher(result) {
return new Promise(resolve => {
return setTimeout(() => {
resolve(result)
}, 100)
})
}
export default defineComponent({
setup() {
const name = ref('')
onFetch(async () => {
name.value = await fetcher('Full Name')
})
return {
name
}
}
})
</script>
4 changes: 4 additions & 0 deletions test/fixture/plugins/composition-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Vue from 'vue'
import CompositionApi from '@vue/composition-api'

Vue.use(CompositionApi)
22 changes: 22 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
jest.setTimeout(60000)

/* eslint-disable @typescript-eslint/no-var-requires */
const { setup, get } = require('@nuxtjs/module-test-utils')
const config = require('./fixture/nuxt.config')
/* eslint-enable */

let nuxt

describe('onFetch', () => {
beforeAll(async () => {
nuxt = (await setup(config)).nuxt
}, 60000)

afterAll(async () => {
await nuxt.close()
})
test('fetches on ssr', async () => {
const html = await get('/')
expect(html).toContain('name-Full Name')
})
})
12 changes: 11 additions & 1 deletion test/tsd/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
import { expectError } from 'tsd'
import { expectType } from 'tsd'
import { Context } from '@nuxt/types'

import { withContext } from '../../src'

// eslint-disable-next-line
expectType<void>(withContext(() => {}))

withContext(ctx => {
expectType<Context>(ctx)
})
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"compilerOptions": {
"strict": true,
"sourceMap": true,
"target": "es5",
"module": "commonjs",
"target": "esnext",
"module": "esnext",
"lib": ["esnext", "dom"],
"moduleResolution": "node",
"rootDir": "src",
Expand Down
Loading

0 comments on commit b1e2502

Please sign in to comment.