Refactor 'createGroupMutation' to a function that returns GQL

This commit is contained in:
Wolfgang Huß 2022-09-18 07:07:11 +02:00
parent dcfb361793
commit 7d9e42806c
5 changed files with 154 additions and 150 deletions

View File

@ -2,56 +2,58 @@ import gql from 'graphql-tag'
// ------ mutations // ------ mutations
export const createGroupMutation = gql` export const createGroupMutation = () => {
mutation ( return gql`
$id: ID mutation (
$name: String! $id: ID
$slug: String $name: String!
$about: String $slug: String
$description: String! $about: String
$groupType: GroupType! $description: String!
$actionRadius: GroupActionRadius! $groupType: GroupType!
$categoryIds: [ID] $actionRadius: GroupActionRadius!
$locationName: String # empty string '' sets it to null $categoryIds: [ID]
) { $locationName: String # empty string '' sets it to null
CreateGroup(
id: $id
name: $name
slug: $slug
about: $about
description: $description
groupType: $groupType
actionRadius: $actionRadius
categoryIds: $categoryIds
locationName: $locationName
) { ) {
id CreateGroup(
name id: $id
slug name: $name
createdAt slug: $slug
updatedAt about: $about
disabled description: $description
deleted groupType: $groupType
about actionRadius: $actionRadius
description categoryIds: $categoryIds
groupType locationName: $locationName
actionRadius ) {
categories {
id id
name
slug slug
name createdAt
icon updatedAt
disabled
deleted
about
description
groupType
actionRadius
categories {
id
slug
name
icon
}
locationName
location {
name
nameDE
nameEN
}
myRole
} }
locationName
location {
name
nameDE
nameEN
}
myRole
} }
} `
` }
export const updateGroupMutation = gql` export const updateGroupMutation = gql`
mutation ( mutation (

View File

@ -305,7 +305,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl']
authenticatedUser = await peterLustig.toJson() authenticatedUser = await peterLustig.toJson()
await Promise.all([ await Promise.all([
mutate({ mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'g0', id: 'g0',
name: 'Investigative Journalism', name: 'Investigative Journalism',
@ -363,7 +363,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl']
authenticatedUser = await jennyRostock.toJson() authenticatedUser = await jennyRostock.toJson()
await Promise.all([ await Promise.all([
mutate({ mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'g1', id: 'g1',
name: 'School For Citizens', name: 'School For Citizens',
@ -443,7 +443,7 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl']
authenticatedUser = await bobDerBaumeister.toJson() authenticatedUser = await bobDerBaumeister.toJson()
await Promise.all([ await Promise.all([
mutate({ mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'g2', id: 'g2',
name: 'Yoga Practice', name: 'Yoga Practice',

View File

@ -79,7 +79,7 @@ describe('slugifyMiddleware', () => {
it('generates a slug based on name', async () => { it('generates a slug based on name', async () => {
await expect( await expect(
mutate({ mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables, variables,
}), }),
).resolves.toMatchObject({ ).resolves.toMatchObject({
@ -99,7 +99,7 @@ describe('slugifyMiddleware', () => {
it('generates a slug based on given slug', async () => { it('generates a slug based on given slug', async () => {
await expect( await expect(
mutate({ mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
...variables, ...variables,
slug: 'the-group', slug: 'the-group',
@ -118,7 +118,7 @@ describe('slugifyMiddleware', () => {
describe('if slug exists', () => { describe('if slug exists', () => {
beforeEach(async () => { beforeEach(async () => {
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
...variables, ...variables,
name: 'Pre-Existing Group', name: 'Pre-Existing Group',
@ -131,7 +131,7 @@ describe('slugifyMiddleware', () => {
it('chooses another slug', async () => { it('chooses another slug', async () => {
await expect( await expect(
mutate({ mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
...variables, ...variables,
name: 'Pre-Existing Group', name: 'Pre-Existing Group',
@ -152,7 +152,7 @@ describe('slugifyMiddleware', () => {
try { try {
await expect( await expect(
mutate({ mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
...variables, ...variables,
name: 'Pre-Existing Group', name: 'Pre-Existing Group',
@ -194,7 +194,7 @@ describe('slugifyMiddleware', () => {
beforeEach(async () => { beforeEach(async () => {
createGroupResult = await mutate({ createGroupResult = await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
name: 'The Best Group', name: 'The Best Group',
slug: 'the-best-group', slug: 'the-best-group',
@ -265,7 +265,7 @@ describe('slugifyMiddleware', () => {
describe('if new slug exists in another group', () => { describe('if new slug exists in another group', () => {
beforeEach(async () => { beforeEach(async () => {
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
name: 'Pre-Existing Group', name: 'Pre-Existing Group',
slug: 'pre-existing-group', slug: 'pre-existing-group',

View File

@ -156,7 +156,7 @@ const seedComplexScenarioAndClearAuthentication = async () => {
// public-group // public-group
authenticatedUser = await usualMemberUser.toJson() authenticatedUser = await usualMemberUser.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'public-group', id: 'public-group',
name: 'The Best Group', name: 'The Best Group',
@ -184,7 +184,7 @@ const seedComplexScenarioAndClearAuthentication = async () => {
// closed-group // closed-group
authenticatedUser = await ownerMemberUser.toJson() authenticatedUser = await ownerMemberUser.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'closed-group', id: 'closed-group',
name: 'Uninteresting Group', name: 'Uninteresting Group',
@ -198,7 +198,7 @@ const seedComplexScenarioAndClearAuthentication = async () => {
// hidden-group // hidden-group
authenticatedUser = await adminMemberUser.toJson() authenticatedUser = await adminMemberUser.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'hidden-group', id: 'hidden-group',
name: 'Investigative Journalism Group', name: 'Investigative Journalism Group',
@ -283,7 +283,7 @@ describe('in mode', () => {
describe('unauthenticated', () => { describe('unauthenticated', () => {
it('throws authorization error', async () => { it('throws authorization error', async () => {
const { errors } = await mutate({ mutation: createGroupMutation, variables }) const { errors } = await mutate({ mutation: createGroupMutation(), variables })
expect(errors[0]).toHaveProperty('message', 'Not Authorized!') expect(errors[0]).toHaveProperty('message', 'Not Authorized!')
}) })
}) })
@ -294,49 +294,49 @@ describe('in mode', () => {
}) })
it('creates a group', async () => { it('creates a group', async () => {
await expect(mutate({ mutation: createGroupMutation, variables })).resolves.toMatchObject( await expect(
{ mutate({ mutation: createGroupMutation(), variables }),
data: { ).resolves.toMatchObject({
CreateGroup: { data: {
name: 'The Best Group', CreateGroup: {
slug: 'the-group', name: 'The Best Group',
about: 'We will change the world!', slug: 'the-group',
description: 'Some description' + descriptionAdditional100, about: 'We will change the world!',
groupType: 'public', description: 'Some description' + descriptionAdditional100,
actionRadius: 'regional', groupType: 'public',
locationName: 'Hamburg, Germany', actionRadius: 'regional',
location: expect.objectContaining({ locationName: 'Hamburg, Germany',
name: 'Hamburg', location: expect.objectContaining({
nameDE: 'Hamburg', name: 'Hamburg',
nameEN: 'Hamburg', nameDE: 'Hamburg',
}), nameEN: 'Hamburg',
}, }),
}, },
errors: undefined,
}, },
) errors: undefined,
})
}) })
it('assigns the authenticated user as owner', async () => { it('assigns the authenticated user as owner', async () => {
await expect(mutate({ mutation: createGroupMutation, variables })).resolves.toMatchObject( await expect(
{ mutate({ mutation: createGroupMutation(), variables }),
data: { ).resolves.toMatchObject({
CreateGroup: { data: {
name: 'The Best Group', CreateGroup: {
myRole: 'owner', name: 'The Best Group',
}, myRole: 'owner',
}, },
errors: undefined,
}, },
) errors: undefined,
})
}) })
it('has "disabled" and "deleted" default to "false"', async () => { it('has "disabled" and "deleted" default to "false"', async () => {
await expect(mutate({ mutation: createGroupMutation, variables })).resolves.toMatchObject( await expect(
{ mutate({ mutation: createGroupMutation(), variables }),
data: { CreateGroup: { disabled: false, deleted: false } }, ).resolves.toMatchObject({
}, data: { CreateGroup: { disabled: false, deleted: false } },
) })
}) })
describe('description', () => { describe('description', () => {
@ -344,7 +344,7 @@ describe('in mode', () => {
describe('less then 100 chars', () => { describe('less then 100 chars', () => {
it('throws error: "Description too short!"', async () => { it('throws error: "Description too short!"', async () => {
const { errors } = await mutate({ const { errors } = await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
...variables, ...variables,
description: description:
@ -367,7 +367,7 @@ describe('in mode', () => {
it('has new categories', async () => { it('has new categories', async () => {
await expect( await expect(
mutate({ mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
...variables, ...variables,
categoryIds: ['cat4', 'cat27'], categoryIds: ['cat4', 'cat27'],
@ -392,7 +392,7 @@ describe('in mode', () => {
describe('by "categoryIds: null"', () => { describe('by "categoryIds: null"', () => {
it('throws error: "Too view categories!"', async () => { it('throws error: "Too view categories!"', async () => {
const { errors } = await mutate({ const { errors } = await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { ...variables, categoryIds: null }, variables: { ...variables, categoryIds: null },
}) })
expect(errors[0]).toHaveProperty('message', 'Too view categories!') expect(errors[0]).toHaveProperty('message', 'Too view categories!')
@ -402,7 +402,7 @@ describe('in mode', () => {
describe('by "categoryIds: []"', () => { describe('by "categoryIds: []"', () => {
it('throws error: "Too view categories!"', async () => { it('throws error: "Too view categories!"', async () => {
const { errors } = await mutate({ const { errors } = await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { ...variables, categoryIds: [] }, variables: { ...variables, categoryIds: [] },
}) })
expect(errors[0]).toHaveProperty('message', 'Too view categories!') expect(errors[0]).toHaveProperty('message', 'Too view categories!')
@ -413,7 +413,7 @@ describe('in mode', () => {
describe('four', () => { describe('four', () => {
it('throws error: "Too many categories!"', async () => { it('throws error: "Too many categories!"', async () => {
const { errors } = await mutate({ const { errors } = await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { ...variables, categoryIds: ['cat9', 'cat4', 'cat15', 'cat27'] }, variables: { ...variables, categoryIds: ['cat9', 'cat4', 'cat15', 'cat27'] },
}) })
expect(errors[0]).toHaveProperty('message', 'Too many categories!') expect(errors[0]).toHaveProperty('message', 'Too many categories!')
@ -470,7 +470,7 @@ describe('in mode', () => {
) )
authenticatedUser = await otherUser.toJson() authenticatedUser = await otherUser.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'others-group', id: 'others-group',
name: 'Uninteresting Group', name: 'Uninteresting Group',
@ -483,7 +483,7 @@ describe('in mode', () => {
}) })
authenticatedUser = await ownerOfHiddenGroupUser.toJson() authenticatedUser = await ownerOfHiddenGroupUser.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'hidden-group', id: 'hidden-group',
name: 'Investigative Journalism Group', name: 'Investigative Journalism Group',
@ -495,7 +495,7 @@ describe('in mode', () => {
}, },
}) })
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'second-hidden-group', id: 'second-hidden-group',
name: 'Second Investigative Journalism Group', name: 'Second Investigative Journalism Group',
@ -515,7 +515,7 @@ describe('in mode', () => {
}, },
}) })
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'third-hidden-group', id: 'third-hidden-group',
name: 'Third Investigative Journalism Group', name: 'Third Investigative Journalism Group',
@ -536,7 +536,7 @@ describe('in mode', () => {
}) })
authenticatedUser = await user.toJson() authenticatedUser = await user.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
name: 'The Best Group', name: 'The Best Group',
@ -857,7 +857,7 @@ describe('in mode', () => {
// public-group // public-group
authenticatedUser = await ownerOfClosedGroupUser.toJson() authenticatedUser = await ownerOfClosedGroupUser.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'closed-group', id: 'closed-group',
name: 'Uninteresting Group', name: 'Uninteresting Group',
@ -870,7 +870,7 @@ describe('in mode', () => {
}) })
authenticatedUser = await ownerOfHiddenGroupUser.toJson() authenticatedUser = await ownerOfHiddenGroupUser.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'hidden-group', id: 'hidden-group',
name: 'Investigative Journalism Group', name: 'Investigative Journalism Group',
@ -883,7 +883,7 @@ describe('in mode', () => {
}) })
authenticatedUser = await user.toJson() authenticatedUser = await user.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'public-group', id: 'public-group',
name: 'The Best Group', name: 'The Best Group',
@ -1108,7 +1108,7 @@ describe('in mode', () => {
// public-group // public-group
authenticatedUser = await user.toJson() authenticatedUser = await user.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'public-group', id: 'public-group',
name: 'The Best Group', name: 'The Best Group',
@ -1136,7 +1136,7 @@ describe('in mode', () => {
// closed-group // closed-group
authenticatedUser = await ownerOfClosedGroupUser.toJson() authenticatedUser = await ownerOfClosedGroupUser.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'closed-group', id: 'closed-group',
name: 'Uninteresting Group', name: 'Uninteresting Group',
@ -1165,7 +1165,7 @@ describe('in mode', () => {
// hidden-group // hidden-group
authenticatedUser = await ownerOfHiddenGroupUser.toJson() authenticatedUser = await ownerOfHiddenGroupUser.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'hidden-group', id: 'hidden-group',
name: 'Investigative Journalism Group', name: 'Investigative Journalism Group',
@ -2648,7 +2648,7 @@ describe('in mode', () => {
) )
authenticatedUser = await noMemberUser.toJson() authenticatedUser = await noMemberUser.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'others-group', id: 'others-group',
name: 'Uninteresting Group', name: 'Uninteresting Group',
@ -2661,7 +2661,7 @@ describe('in mode', () => {
}) })
authenticatedUser = await user.toJson() authenticatedUser = await user.toJson()
await mutate({ await mutate({
mutation: createGroupMutation, mutation: createGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
name: 'The Best Group', name: 'The Best Group',

View File

@ -2,51 +2,53 @@ import gql from 'graphql-tag'
// ------ mutations // ------ mutations
export const createGroupMutation = gql` export const createGroupMutation = () => {
mutation ( return gql`
$id: ID mutation (
$name: String! $id: ID
$slug: String $name: String!
$about: String $slug: String
$description: String! $about: String
$groupType: GroupType! $description: String!
$actionRadius: GroupActionRadius! $groupType: GroupType!
$categoryIds: [ID] $actionRadius: GroupActionRadius!
$locationName: String # empty string '' sets it to null $categoryIds: [ID]
) { $locationName: String # empty string '' sets it to null
CreateGroup(
id: $id
name: $name
slug: $slug
about: $about
description: $description
groupType: $groupType
actionRadius: $actionRadius
categoryIds: $categoryIds
locationName: $locationName
) { ) {
id CreateGroup(
name id: $id
slug name: $name
createdAt slug: $slug
updatedAt about: $about
disabled description: $description
deleted groupType: $groupType
about actionRadius: $actionRadius
description categoryIds: $categoryIds
groupType locationName: $locationName
actionRadius ) {
categories {
id id
slug
name name
icon slug
createdAt
updatedAt
disabled
deleted
about
description
groupType
actionRadius
categories {
id
slug
name
icon
}
locationName # test this as result
myRole
} }
locationName # test this as result
myRole
} }
} `
` }
export const updateGroupMutation = gql` export const updateGroupMutation = gql`
mutation ( mutation (