Skip to content

Commit

Permalink
Convert relationship tests to typescript (#5120)
Browse files Browse the repository at this point in the history
* Convert relationship tests to typescript

* Apply review comments
  • Loading branch information
timleslie authored Mar 15, 2021
1 parent f58dd1d commit 95cc08c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
const { gen, sampleOne } = require('testcheck');
const { text, relationship } = require('@keystone-next/fields');
const { createSchema, list } = require('@keystone-next/keystone/schema');
const { multiAdapterRunners, setupFromConfig } = require('@keystone-next/test-utils-legacy');
const { createItem, createItems } = require('@keystone-next/server-side-graphql-client-legacy');
import { AdapterName, testConfig } from '@keystone-next/test-utils-legacy';
import { KeystoneContext } from '@keystone-next/types';
import { gen, sampleOne } from 'testcheck';
import { text, relationship } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
import { multiAdapterRunners, setupFromConfig } from '@keystone-next/test-utils-legacy';
// @ts-ignore
import { createItem, createItems } from '@keystone-next/server-side-graphql-client-legacy';

const alphanumGenerator = gen.alphaNumString.notEmpty();

const createInitialData = async context => {
const { data, errors } = await context.executeGraphQL({
type IdType = any;

const createInitialData = async (context: KeystoneContext) => {
type T = {
data: { createCompanies: { id: IdType }[]; createLocations: { id: IdType }[] };
errors: unknown;
};
const { data, errors }: T = await context.executeGraphQL({
query: `
mutation {
createCompanies(data: [
Expand Down Expand Up @@ -41,7 +50,7 @@ const createInitialData = async context => {
return { locations: data.createLocations, companies: data.createCompanies, owners, custodians };
};

const createCompanyAndLocation = async context => {
const createCompanyAndLocation = async (context: KeystoneContext) => {
const [cu1, cu2] = await createItems({
context,
listKey: 'Custodian',
Expand Down Expand Up @@ -92,11 +101,11 @@ const createCompanyAndLocation = async context => {
});
};

const setupKeystone = adapterName =>
const setupKeystone = (adapterName: AdapterName) =>
setupFromConfig({
adapterName,
config: createSchema({
lists: {
config: testConfig({
lists: createSchema({
Owner: list({
fields: {
name: text(),
Expand All @@ -123,7 +132,7 @@ const setupKeystone = adapterName =>
locations: relationship({ ref: 'Location.custodians', many: true }),
},
}),
},
}),
}),
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
const { text, relationship } = require('@keystone-next/fields');
const { createSchema, list } = require('@keystone-next/keystone/schema');
const { multiAdapterRunners, setupFromConfig } = require('@keystone-next/test-utils-legacy');
const { createItems, updateItems } = require('@keystone-next/server-side-graphql-client-legacy');
import { text, relationship } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
import {
AdapterName,
multiAdapterRunners,
setupFromConfig,
testConfig,
} from '@keystone-next/test-utils-legacy';
// @ts-ignore
import { createItems, updateItems } from '@keystone-next/server-side-graphql-client-legacy';
import { KeystoneContext } from '@keystone-next/types';

const createInitialData = async context => {
const roles = await createItems({
type IdType = any;

const createInitialData = async (context: KeystoneContext) => {
const roles: { id: IdType; name: string }[] = await createItems({
context,
listKey: 'Role',
items: [{ data: { name: 'RoleA' } }, { data: { name: 'RoleB' } }, { data: { name: 'RoleC' } }],
returnFields: 'id name',
});
const companies = await createItems({
const companies: { id: IdType; name: string }[] = await createItems({
context,
listKey: 'Company',
items: [
Expand All @@ -20,29 +29,29 @@ const createInitialData = async context => {
],
returnFields: 'id name',
});
const employees = await createItems({
const employees: { id: IdType; name: string }[] = await createItems({
context,
listKey: 'Employee',
items: [
{
data: {
name: 'EmployeeA',
company: { connect: { id: companies.find(({ name }) => name === 'CompanyA').id } },
role: { connect: { id: roles.find(({ name }) => name === 'RoleA').id } },
company: { connect: { id: companies.find(({ name }) => name === 'CompanyA')!.id } },
role: { connect: { id: roles.find(({ name }) => name === 'RoleA')!.id } },
},
},
{
data: {
name: 'EmployeeB',
company: { connect: { id: companies.find(({ name }) => name === 'CompanyB').id } },
role: { connect: { id: roles.find(({ name }) => name === 'RoleB').id } },
company: { connect: { id: companies.find(({ name }) => name === 'CompanyB')!.id } },
role: { connect: { id: roles.find(({ name }) => name === 'RoleB')!.id } },
},
},
{
data: {
name: 'EmployeeC',
company: { connect: { id: companies.find(({ name }) => name === 'CompanyC').id } },
role: { connect: { id: roles.find(({ name }) => name === 'RoleC').id } },
company: { connect: { id: companies.find(({ name }) => name === 'CompanyC')!.id } },
role: { connect: { id: roles.find(({ name }) => name === 'RoleC')!.id } },
},
},
],
Expand Down Expand Up @@ -90,35 +99,35 @@ const createInitialData = async context => {
listKey: 'Role',
items: [
{
id: roles.find(({ name }) => name === 'RoleA').id,
id: roles.find(({ name }) => name === 'RoleA')!.id,
data: {
company: { connect: { id: companies.find(({ name }) => name === 'CompanyA').id } },
employees: { connect: [{ id: employees.find(({ name }) => name === 'EmployeeA').id }] },
company: { connect: { id: companies.find(({ name }) => name === 'CompanyA')!.id } },
employees: { connect: [{ id: employees.find(({ name }) => name === 'EmployeeA')!.id }] },
},
},
{
id: roles.find(({ name }) => name === 'RoleB').id,
id: roles.find(({ name }) => name === 'RoleB')!.id,
data: {
company: { connect: { id: companies.find(({ name }) => name === 'CompanyB').id } },
employees: { connect: [{ id: employees.find(({ name }) => name === 'EmployeeB').id }] },
company: { connect: { id: companies.find(({ name }) => name === 'CompanyB')!.id } },
employees: { connect: [{ id: employees.find(({ name }) => name === 'EmployeeB')!.id }] },
},
},
{
id: roles.find(({ name }) => name === 'RoleC').id,
id: roles.find(({ name }) => name === 'RoleC')!.id,
data: {
company: { connect: { id: companies.find(({ name }) => name === 'CompanyC').id } },
employees: { connect: [{ id: employees.find(({ name }) => name === 'EmployeeC').id }] },
company: { connect: { id: companies.find(({ name }) => name === 'CompanyC')!.id } },
employees: { connect: [{ id: employees.find(({ name }) => name === 'EmployeeC')!.id }] },
},
},
],
});
};

const setupKeystone = adapterName =>
const setupKeystone = (adapterName: AdapterName) =>
setupFromConfig({
adapterName,
config: createSchema({
lists: {
config: testConfig({
lists: createSchema({
Employee: list({
fields: {
name: text(),
Expand All @@ -145,7 +154,7 @@ const setupKeystone = adapterName =>
employees: relationship({ ref: 'Employee', many: true }),
},
}),
},
}),
}),
});

Expand Down

1 comment on commit 95cc08c

@vercel
Copy link

@vercel vercel bot commented on 95cc08c Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.