Skip to content

Commit

Permalink
Fix TS issues in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Feb 14, 2025
1 parent 1f5e906 commit 929251e
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/introspection/getFieldsFromEntities.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GraphQLString, GraphQLID, GraphQLNonNull } from 'graphql';
import { expect, test } from 'vitest';
import getFieldsFromEntities from './getFieldsFromEntities';

test('does infer field types', () => {
Expand Down
1 change: 1 addition & 0 deletions src/introspection/getFilterTypesFromData.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from 'vitest';
import getFilterTypesFromData from './getFilterTypesFromData';

const data = {
Expand Down
76 changes: 48 additions & 28 deletions src/introspection/getSchemaFromData.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {
GraphQLID,
GraphQLInputObjectType,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLString,
} from 'graphql';
import getSchemaFromData from './getSchemaFromData';
import type { ObjMap } from 'graphql/jsutils/ObjMap';
import { expect, test } from 'vitest';
import getSchemaFromData from './getSchemaFromData';

const data = {
posts: [
Expand Down Expand Up @@ -105,36 +107,38 @@ const QueryType = new GraphQLObjectType({

test('creates one type per data type', () => {
const typeMap = getSchemaFromData(
data,
data
).getTypeMap() as ObjMap<GraphQLObjectType>;
expect(typeMap.Post.name).toEqual(PostType.name);
expect(Object.keys(typeMap.Post.getFields())).toEqual(
Object.keys(PostType.getFields()),
Object.keys(PostType.getFields())
);
expect(typeMap.User.name).toEqual(UserType.name);
expect(Object.keys(typeMap.User.getFields())).toEqual(
Object.keys(UserType.getFields()),
Object.keys(UserType.getFields())
);
});

test('creates one field per relationship', () => {
const typeMap = getSchemaFromData(
data,
data
).getTypeMap() as ObjMap<GraphQLObjectType>;
expect(Object.keys(typeMap.Post.getFields())).toContain('User');
});

test('creates one field per reverse relationship', () => {
const typeMap = getSchemaFromData(
data,
data
).getTypeMap() as ObjMap<GraphQLObjectType>;
expect(Object.keys(typeMap.User.getFields())).toContain('Posts');
});

test('creates three query fields per data type', () => {
// biome-ignore lint/style/noNonNullAssertion: It's only a test
const queries = getSchemaFromData(data).getQueryType()!.getFields();
expect(queries.Post.type.name).toEqual(PostType.name);
expect((queries.Post.type as GraphQLObjectType).name).toEqual(
PostType.name
);
expect(queries.Post.args).toEqual([
expect.objectContaining({
name: 'id',
Expand All @@ -154,7 +158,9 @@ test('creates three query fields per data type', () => {
expect(queries.allPosts.args[4].type.toString()).toEqual('PostFilter');
expect(queries._allPostsMeta.type.toString()).toEqual('ListMetadata');

expect(queries.User.type.name).toEqual(UserType.name);
expect((queries.User.type as GraphQLObjectType).name).toEqual(
UserType.name
);
expect(queries.User.args).toEqual([
expect.objectContaining({
name: 'id',
Expand All @@ -178,7 +184,9 @@ test('creates three query fields per data type', () => {
test('creates three mutation fields per data type', () => {
// biome-ignore lint/style/noNonNullAssertion: It's only a test
const mutations = getSchemaFromData(data).getMutationType()!.getFields();
expect(mutations.createPost.type.name).toEqual(PostType.name);
expect((mutations.createPost.type as GraphQLObjectType).name).toEqual(
PostType.name
);
expect(mutations.createPost.args).toEqual([
expect.objectContaining({
name: 'title',
Expand All @@ -193,7 +201,9 @@ test('creates three mutation fields per data type', () => {
type: new GraphQLNonNull(GraphQLID),
}),
]);
expect(mutations.updatePost.type.name).toEqual(PostType.name);
expect((mutations.updatePost.type as GraphQLObjectType).name).toEqual(
PostType.name
);
expect(mutations.updatePost.args).toEqual([
expect.objectContaining({
name: 'id',
Expand All @@ -212,21 +222,27 @@ test('creates three mutation fields per data type', () => {
type: GraphQLID,
}),
]);
expect(mutations.removePost.type.name).toEqual(PostType.name);
expect((mutations.removePost.type as GraphQLObjectType).name).toEqual(
PostType.name
);
expect(mutations.removePost.args).toEqual([
expect.objectContaining({
name: 'id',
type: new GraphQLNonNull(GraphQLID),
}),
]);
expect(mutations.createUser.type.name).toEqual(UserType.name);
expect((mutations.createUser.type as GraphQLObjectType).name).toEqual(
UserType.name
);
expect(mutations.createUser.args).toEqual([
expect.objectContaining({
name: 'name',
type: new GraphQLNonNull(GraphQLString),
}),
]);
expect(mutations.updateUser.type.name).toEqual(UserType.name);
expect((mutations.updateUser.type as GraphQLObjectType).name).toEqual(
UserType.name
);
expect(mutations.updateUser.args).toEqual([
expect.objectContaining({
name: 'id',
Expand All @@ -237,7 +253,9 @@ test('creates three mutation fields per data type', () => {
type: GraphQLString,
}),
]);
expect(mutations.removeUser.type.name).toEqual(UserType.name);
expect((mutations.removeUser.type as GraphQLObjectType).name).toEqual(
UserType.name
);
expect(mutations.removeUser.args).toEqual([
expect.objectContaining({
name: 'id',
Expand All @@ -251,20 +269,22 @@ test('creates the mutation *Input type for createMany', () => {
const mutations = getSchemaFromData(data).getMutationType()!.getFields();
const createManyPostInputType = mutations.createManyPost.args[0].type;
expect(createManyPostInputType.toString()).toEqual('[PostInput]');
expect(createManyPostInputType.ofType.getFields()).toEqual({
title: expect.objectContaining({
type: new GraphQLNonNull(GraphQLString),
name: 'title',
}),
views: expect.objectContaining({
type: new GraphQLNonNull(GraphQLInt),
name: 'views',
}),
user_id: expect.objectContaining({
type: new GraphQLNonNull(GraphQLID),
name: 'user_id',
}),
});
expect((createManyPostInputType as GraphQLList<any>).ofType.getFields()).toEqual(
{
title: expect.objectContaining({
type: new GraphQLNonNull(GraphQLString),
name: 'title',
}),
views: expect.objectContaining({
type: new GraphQLNonNull(GraphQLInt),
name: 'views',
}),
user_id: expect.objectContaining({
type: new GraphQLNonNull(GraphQLID),
name: 'user_id',
}),
}
);
});

test('pluralizes and capitalizes correctly', () => {
Expand Down
1 change: 1 addition & 0 deletions src/introspection/getTypeFromValues.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
GraphQLString,
} from 'graphql';
import { GraphQLJSON } from 'graphql-type-json';
import { expect, test } from 'vitest';
import DateType from './DateType';
import getTypeFromValues from './getTypeFromValues';

Expand Down
1 change: 1 addition & 0 deletions src/introspection/getTypesFromData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
GraphQLInt,
GraphQLNonNull,
} from 'graphql';
import { expect, test } from 'vitest';
import getTypesFromData from './getTypesFromData';

test('Integration test', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/introspection/getValuesFromEntities.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from 'vitest';
import getValuesFromEntities from './getValuesFromEntities';

test('does not take empty values into account', () => {
Expand All @@ -10,7 +11,7 @@ test('returns an array of values for every field', () => {
getValuesFromEntities([
{ id: 1, foo: 'bar' },
{ id: 2, foo: 'baz' },
]),
])
).toEqual({
id: [1, 2],
foo: ['bar', 'baz'],
Expand All @@ -27,7 +28,7 @@ test('can handle sparse fieldsets', () => {
{ id: 1, foo: 'foo1' },
{ id: 2, foo: 'foo2', bar: 'bar1' },
{ id: 3, bar: 'bar2' },
]),
])
).toEqual({
id: [1, 2, 3],
foo: ['foo1', 'foo2'],
Expand Down
1 change: 1 addition & 0 deletions src/resolver/Entity/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from 'vitest';
import entity from './index';

test('provides empty resolver for data without relationship', () =>
Expand Down
1 change: 1 addition & 0 deletions src/resolver/Mutation/create.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from 'vitest';
import create from './create';

test('returns a new object with id 0 on empty datastore', () => {
Expand Down
1 change: 1 addition & 0 deletions src/resolver/Mutation/createMany.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from 'vitest';
import createMany from './createMany';

test('returns a new object with id 0 on empty datastore', () => {
Expand Down
1 change: 1 addition & 0 deletions src/resolver/Mutation/remove.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from 'vitest';
import remove from './remove';

test('returns undefined by default', () => {
Expand Down
1 change: 1 addition & 0 deletions src/resolver/Mutation/update.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from 'vitest';
import update from './update';

test('returns undefined by default', () => {
Expand Down
1 change: 1 addition & 0 deletions src/resolver/Query/all.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, expect, test } from 'vitest';
import all from './all';

const data = [
Expand Down
1 change: 1 addition & 0 deletions src/resolver/Query/applyFilters.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from 'vitest';
import applyFilters from './applyFilters';

const data = [
Expand Down
1 change: 1 addition & 0 deletions src/resolver/Query/single.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, test } from 'vitest';
import single from './single';

test('returns undefined by default', () => {
Expand Down

0 comments on commit 929251e

Please sign in to comment.