Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Apr 4, 2024
1 parent 2dedfc3 commit 7e480a3
Showing 1 changed file with 322 additions and 0 deletions.
322 changes: 322 additions & 0 deletions packages/internal/src/loaders/__tests__/query.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
import { expect, describe, it } from 'vitest';
import { print } from '@0no-co/graphql.web';

import { makeIntrospectionQuery, makeIntrospectSupportQuery, toSupportedFeatures } from '../query';

describe('makeIntrospectSupportQuery', () => {
it('prints to introspection support query', () => {
expect(print(makeIntrospectSupportQuery())).toMatchInlineSnapshot(`
"query IntrospectSupportQuery {
directive: __type(name: "__Directive") {
fields {
name
}
}
type: __type(name: "__Type") {
fields {
name
}
}
inputValue: __type(name: "__InputValue") {
fields {
name
}
}
}"
`);
});
});

describe('toSupportedFeatures', () => {
it('outputs default with no features enabled', () => {
expect(toSupportedFeatures({ type: null, inputValue: null, directive: null })).toEqual({
directiveIsRepeatable: false,
specifiedByURL: false,
inputValueDeprecation: false,
});
});

it('detects `isRepeatable` support on directives', () => {
const input = {
type: null, // stubbed
inputValue: null, // stubbed
directive: {
fields: [{ name: 'isRepeatable' }],
},
};

expect(toSupportedFeatures(input)).toMatchObject({
directiveIsRepeatable: true,
});
});

it('detects `specifiedByURL` support on scalars', () => {
const input = {
inputValue: null, // stubbed
directive: null, // stubbed
type: {
fields: [{ name: 'specifiedByURL' }],
},
};

expect(toSupportedFeatures(input)).toMatchObject({
specifiedByURL: true,
});
});

it('detects `isDeprecated` support on input values', () => {
const input = {
type: null, // stubbed
directive: null, // stubbed
inputValue: {
fields: [{ name: 'isDeprecated' }],
},
};

expect(toSupportedFeatures(input)).toMatchObject({
inputValueDeprecation: true,
});
});
});

describe('makeIntrospectionQuery', () => {
it('correctly outputs introspection query with all features enabled', () => {
const support = {
directiveIsRepeatable: true,
specifiedByURL: true,
inputValueDeprecation: true,
};

expect(print(makeIntrospectionQuery(support))).toMatchInlineSnapshot(`
"query IntrospectionQuery {
__schema {
queryType {
name
}
mutationType {
name
}
subscriptionType {
name
}
types {
...FullType
}
directives {
name
description
locations
args(includeDeprecated: true) {
...InputValue
}
isRepeatable
}
}
}
fragment FullType on __Type {
kind
name
description
specifiedByURL
fields(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
args(includeDeprecated: true) {
...InputValue
}
type {
...TypeRef
}
}
interfaces {
...TypeRef
}
possibleTypes {
...TypeRef
}
inputFields(includeDeprecated: true) {
...InputValue
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
}
fragment InputValue on __InputValue {
name
description
defaultValue
type {
...TypeRef
}
isDeprecated
deprecationReason
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
}
}"
`);
});

it('correctly outputs introspection query with all features disabled', () => {
const support = {
directiveIsRepeatable: false,
specifiedByURL: false,
inputValueDeprecation: false,
};

expect(print(makeIntrospectionQuery(support))).toMatchInlineSnapshot(`
"query IntrospectionQuery {
__schema {
queryType {
name
}
mutationType {
name
}
subscriptionType {
name
}
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
args {
...InputValue
}
type {
...TypeRef
}
}
interfaces {
...TypeRef
}
possibleTypes {
...TypeRef
}
inputFields {
...InputValue
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
}
fragment InputValue on __InputValue {
name
description
defaultValue
type {
...TypeRef
}
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
}
}"
`);
});
});

0 comments on commit 7e480a3

Please sign in to comment.