Skip to content

Commit

Permalink
Merge branch 'master' into cloudinary-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Apr 7, 2021
2 parents 79f71bd + 184b631 commit 9265dac
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 197 deletions.
52 changes: 16 additions & 36 deletions tests/api-tests/relationships/crud/many-to-many-one-sided.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ type IdType = any;
const alphanumGenerator = gen.alphaNumString.notEmpty();

const createInitialData = async (context: KeystoneContext) => {
type T = {
data: { createLocations: { id: IdType }[]; createCompanies: { id: IdType }[] };
errors: unknown;
};
const { data, errors }: T = await context.executeGraphQL({
type T = { createLocations: { id: IdType }[]; createCompanies: { id: IdType }[] };
const data = (await context.graphql.run({
query: `
mutation {
createCompanies(data: [
Expand All @@ -28,28 +25,20 @@ const createInitialData = async (context: KeystoneContext) => {
{ data: { name: "${sampleOne(alphanumGenerator)}" } }
]) { id }
}`,
});
expect(errors).toBe(undefined);
})) as T;
return { locations: data.createLocations, companies: data.createCompanies };
};

const createCompanyAndLocation = async (context: KeystoneContext) => {
type T = {
data: { createCompany: { id: IdType; locations: { id: IdType }[] } };
errors: unknown;
};
const {
data: { createCompany },
errors,
}: T = await context.executeGraphQL({
type T = { createCompany: { id: IdType; locations: { id: IdType }[] } };
const { createCompany } = (await context.graphql.run({
query: `
mutation {
createCompany(data: {
locations: { create: [{ name: "${sampleOne(alphanumGenerator)}" }] }
}) { id locations { id } }
}`,
});
expect(errors).toBe(undefined);
})) as T;
const { Company, Location } = await getCompanyAndLocation(
context,
createCompany.id,
Expand All @@ -70,13 +59,13 @@ const getCompanyAndLocation = async (
type T = {
data: { Company: { id: IdType; locations: { id: IdType }[] }; Location: { id: IdType } };
};
const { data }: T = await context.executeGraphQL({
const { data } = (await context.graphql.raw({
query: `
{
Company(where: { id: "${companyId}"} ) { id locations { id } }
Location(where: { id: "${locationId}"} ) { id }
}`,
});
})) as T;
return data;
};

Expand Down Expand Up @@ -147,10 +136,9 @@ multiAdapterRunners().map(({ runner, provider }) =>
['C', 3],
['D', 0],
].map(async ([name, count]) => {
const { data, errors } = await context.executeGraphQL({
const data = await context.graphql.run({
query: `{ allCompanies(where: { locations_some: { name: "${name}"}}) { id }}`,
});
expect(errors).toBe(undefined);
expect(data.allCompanies.length).toEqual(count);
})
);
Expand Down Expand Up @@ -225,10 +213,9 @@ multiAdapterRunners().map(({ runner, provider }) =>
['C', 3],
['D', 0],
].map(async ([name, count]) => {
const { data, errors } = await context.executeGraphQL({
const data = await context.graphql.run({
query: `{ _allCompaniesMeta(where: { locations_some: { name: "${name}"}}) { count }}`,
});
expect(errors).toBe(undefined);
expect(data._allCompaniesMeta.count).toEqual(count);
})
);
Expand Down Expand Up @@ -280,20 +267,16 @@ multiAdapterRunners().map(({ runner, provider }) =>
runner(setupKeystone, async ({ context }) => {
const { locations } = await createInitialData(context);
const location = locations[0];
type T = {
data: { createCompany: { id: IdType; locations: { id: IdType }[] } };
errors: unknown;
};
const { data, errors }: T = await context.executeGraphQL({
type T = { createCompany: { id: IdType; locations: { id: IdType }[] } };
const data = (await context.graphql.run({
query: `
mutation {
createCompany(data: {
locations: { connect: [{ id: "${location.id}" }] }
}) { id locations { id } }
}
`,
});
expect(errors).toBe(undefined);
})) as T;
expect(data.createCompany.locations.map(({ id }) => id.toString())).toEqual([
location.id,
]);
Expand Down Expand Up @@ -340,7 +323,7 @@ multiAdapterRunners().map(({ runner, provider }) =>
test(
'With null',
runner(setupKeystone, async ({ context }) => {
const { data, errors } = await context.executeGraphQL({
const data = await context.graphql.run({
query: `
mutation {
createCompany(data: {
Expand All @@ -349,7 +332,6 @@ multiAdapterRunners().map(({ runner, provider }) =>
}
`,
});
expect(errors).toBe(undefined);

// Locations should be empty
expect(data.createCompany.locations).toHaveLength(0);
Expand Down Expand Up @@ -396,7 +378,7 @@ multiAdapterRunners().map(({ runner, provider }) =>
const { companies } = await createInitialData(context);
let company = companies[0];
const locationName = sampleOne(alphanumGenerator);
const { data, errors } = await context.executeGraphQL({
const data = await context.graphql.run({
query: `
mutation {
updateCompany(
Expand All @@ -406,7 +388,6 @@ multiAdapterRunners().map(({ runner, provider }) =>
}
`,
});
expect(errors).toBe(undefined);

const { Company, Location } = await getCompanyAndLocation(
context,
Expand All @@ -428,7 +409,7 @@ multiAdapterRunners().map(({ runner, provider }) =>
const { location, company } = await createCompanyAndLocation(context);

// Run the query to disconnect the location from company
const { data, errors } = await context.executeGraphQL({
const data = await context.graphql.run({
query: `
mutation {
updateCompany(
Expand All @@ -438,7 +419,6 @@ multiAdapterRunners().map(({ runner, provider }) =>
}
`,
});
expect(errors).toBe(undefined);
expect(data.updateCompany.id).toEqual(company.id);
expect(data.updateCompany.locations).toEqual([]);

Expand Down
Loading

0 comments on commit 9265dac

Please sign in to comment.