nexus/tests/plugins.spec.ts

156 lines
3.5 KiB
TypeScript
Raw Permalink Normal View History

2020-11-28 03:22:48 +08:00
import { lexicographicSortSchema, printSchema } from 'graphql'
import { createPlugin, makeSchema, objectType, PluginConfig, queryType } from '../src'
import { extendType, inputObjectType, NexusAcceptedTypeDef } from '../src/core'
2019-10-17 04:55:53 +08:00
const fooObject = objectType({
name: 'foo',
2019-10-17 04:55:53 +08:00
definition(t) {
t.string('bar')
2019-10-17 04:55:53 +08:00
},
})
2019-10-17 04:55:53 +08:00
const queryField = extendType({
type: 'Query',
2019-10-17 04:55:53 +08:00
definition(t) {
t.string('something')
2019-10-17 04:55:53 +08:00
},
})
2019-10-17 04:55:53 +08:00
describe('runtime config validation', () => {
const spy = jest.spyOn(console, 'error').mockImplementation()
beforeEach(() => {
jest.resetAllMocks()
})
const whenGiven = (config: any) => () => createPlugin(config)
2019-10-17 04:55:53 +08:00
it('checks name present', () => {
expect(whenGiven({})).toThrowErrorMatchingSnapshot()
})
2019-10-17 04:55:53 +08:00
it('checks name is string', () => {
expect(whenGiven({ name: 1 })).toThrowErrorMatchingSnapshot()
})
2019-10-17 04:55:53 +08:00
it('checks name is not empty', () => {
expect(whenGiven({ name: '' })).toThrowErrorMatchingSnapshot()
})
2019-10-17 04:55:53 +08:00
it('checks onInstall is a function if defined', () => {
whenGiven({ name: 'x', onInstall: 'foo' })()
expect(spy).toBeCalledTimes(1)
jest.resetAllMocks()
whenGiven({ name: 'x', onInstall: {} })()
expect(spy).toBeCalledTimes(1)
})
})
2019-10-17 04:55:53 +08:00
describe('a plugin may', () => {
2019-10-17 04:55:53 +08:00
const whenGiven = (pluginConfig: PluginConfig) => () =>
makeSchema({
outputs: false,
2019-10-17 04:55:53 +08:00
types: [],
plugins: [createPlugin(pluginConfig)],
})
2019-10-17 04:55:53 +08:00
it('do nothing', () => {
expect(whenGiven({ name: 'x' }))
})
})
2019-10-17 04:55:53 +08:00
describe('onInstall plugins', () => {
2019-10-17 04:55:53 +08:00
const whenGiven = ({
onInstall,
plugin,
appTypes,
}: {
onInstall?: PluginConfig['onInstall']
plugin?: Omit<PluginConfig, 'name'>
appTypes?: NexusAcceptedTypeDef[]
2019-10-17 04:55:53 +08:00
}) => {
const xPluginConfig = plugin || { onInstall }
2019-10-17 04:55:53 +08:00
return printSchema(
2020-11-28 03:22:48 +08:00
lexicographicSortSchema(
makeSchema({
outputs: false,
types: appTypes || [],
plugins: [createPlugin({ name: 'x', ...xPluginConfig })],
})
)
2022-02-18 01:47:40 +08:00
).trim()
}
2019-10-17 04:55:53 +08:00
it('is an optional hook', () => {
expect(whenGiven({ plugin: {} }))
})
2019-10-17 04:55:53 +08:00
it('may return an empty array of types', () => {
expect(whenGiven({ onInstall: () => {} }))
})
2019-10-17 04:55:53 +08:00
it('may contribute types', () => {
2019-10-17 04:55:53 +08:00
expect(
whenGiven({
onInstall: (builder) => {
builder.addType(queryField)
},
2019-10-17 04:55:53 +08:00
})
).toMatchSnapshot()
})
2019-10-17 04:55:53 +08:00
it('has access to top-level types', () => {
2019-10-17 04:55:53 +08:00
expect(
whenGiven({
onInstall: (builder) => {
if (!builder.hasType('foo')) {
builder.addType(queryField)
}
},
2019-10-17 04:55:53 +08:00
appTypes: [fooObject],
})
).toMatchSnapshot()
})
2019-10-17 04:55:53 +08:00
it('does not see fallback ok-query', () => {
2019-10-17 04:55:53 +08:00
expect(
whenGiven({
onInstall(builder) {
if (builder.hasType('Query')) {
builder.addType(queryField)
}
2019-10-17 04:55:53 +08:00
},
})
).toMatchSnapshot()
})
2019-10-17 04:55:53 +08:00
it('does not have access to inline types', () => {
2019-10-17 04:55:53 +08:00
expect(
whenGiven({
onInstall: (builder) => {
if (builder.hasType('Inline')) {
builder.addType(queryField)
}
},
2019-10-17 04:55:53 +08:00
appTypes: [
queryType({
definition(t) {
t.string('bar', {
2019-10-17 04:55:53 +08:00
args: {
inline: inputObjectType({
name: 'Inline',
2019-10-17 04:55:53 +08:00
definition(t2) {
t2.string('hidden')
2019-10-17 04:55:53 +08:00
},
}),
},
})
2019-10-17 04:55:53 +08:00
},
}),
],
})
).toMatchSnapshot()
})
})