Refactor 'updateGroupMutation' to a function that returns GQL

- Add and refactor some tests for the user resolvers.
- Refactor minor in general.
This commit is contained in:
Wolfgang Huß 2022-09-18 08:35:31 +02:00
parent 7d9e42806c
commit 6df10e5588
11 changed files with 172 additions and 122 deletions

View File

@ -55,57 +55,59 @@ export const createGroupMutation = () => {
` `
} }
export const updateGroupMutation = gql` export const updateGroupMutation = () => {
mutation ( return gql`
$id: ID! mutation (
$name: String $id: ID!
$slug: String $name: String
$about: String $slug: String
$description: String $about: String
$actionRadius: GroupActionRadius $description: String
$categoryIds: [ID] $actionRadius: GroupActionRadius
$avatar: ImageInput $categoryIds: [ID]
$locationName: String # empty string '' sets it to null $avatar: ImageInput
) { $locationName: String # empty string '' sets it to null
UpdateGroup(
id: $id
name: $name
slug: $slug
about: $about
description: $description
actionRadius: $actionRadius
categoryIds: $categoryIds
avatar: $avatar
locationName: $locationName
) { ) {
id UpdateGroup(
name id: $id
slug name: $name
createdAt slug: $slug
updatedAt about: $about
disabled description: $description
deleted actionRadius: $actionRadius
about categoryIds: $categoryIds
description avatar: $avatar
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
}
# avatar # test this as result
locationName
location {
name
nameDE
nameEN
}
myRole
} }
# avatar # test this as result
locationName
location {
name
nameDE
nameEN
}
myRole
} }
} `
` }
export const joinGroupMutation = gql` export const joinGroupMutation = gql`
mutation ($groupId: ID!, $userId: ID!) { mutation ($groupId: ID!, $userId: ID!) {

View File

@ -51,6 +51,7 @@ beforeEach(async () => {
await Factory.build('category', { await Factory.build('category', {
id: 'cat9', id: 'cat9',
name: 'Democracy & Politics', name: 'Democracy & Politics',
slug: 'democracy-politics',
icon: 'university', icon: 'university',
}) })
authenticatedUser = await admin.toJson() authenticatedUser = await admin.toJson()
@ -93,6 +94,7 @@ describe('slugifyMiddleware', () => {
actionRadius: 'national', actionRadius: 'national',
}, },
}, },
errors: undefined,
}) })
}) })
@ -111,6 +113,7 @@ describe('slugifyMiddleware', () => {
slug: 'the-group', slug: 'the-group',
}, },
}, },
errors: undefined,
}) })
}) })
}) })
@ -144,6 +147,7 @@ describe('slugifyMiddleware', () => {
slug: 'pre-existing-group-1', slug: 'pre-existing-group-1',
}, },
}, },
errors: undefined,
}) })
}) })
@ -213,7 +217,7 @@ describe('slugifyMiddleware', () => {
it('has the new slug', async () => { it('has the new slug', async () => {
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: createGroupResult.data.CreateGroup.id, id: createGroupResult.data.CreateGroup.id,
name: 'My Best Group', name: 'My Best Group',
@ -231,6 +235,7 @@ describe('slugifyMiddleware', () => {
myRole: 'owner', myRole: 'owner',
}, },
}, },
errors: undefined,
}) })
}) })
}) })
@ -239,7 +244,7 @@ describe('slugifyMiddleware', () => {
it('has the new slug', async () => { it('has the new slug', async () => {
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: createGroupResult.data.CreateGroup.id, id: createGroupResult.data.CreateGroup.id,
slug: 'my-best-group', slug: 'my-best-group',
@ -257,6 +262,7 @@ describe('slugifyMiddleware', () => {
myRole: 'owner', myRole: 'owner',
}, },
}, },
errors: undefined,
}) })
}) })
}) })
@ -282,7 +288,7 @@ describe('slugifyMiddleware', () => {
it('has unique slug "*-1"', async () => { it('has unique slug "*-1"', async () => {
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: createGroupResult.data.CreateGroup.id, id: createGroupResult.data.CreateGroup.id,
name: 'Pre-Existing Group', name: 'Pre-Existing Group',
@ -300,6 +306,7 @@ describe('slugifyMiddleware', () => {
myRole: 'owner', myRole: 'owner',
}, },
}, },
errors: undefined,
}) })
}) })
}) })
@ -309,7 +316,7 @@ describe('slugifyMiddleware', () => {
try { try {
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: createGroupResult.data.CreateGroup.id, id: createGroupResult.data.CreateGroup.id,
slug: 'pre-existing-group', slug: 'pre-existing-group',
@ -368,6 +375,7 @@ describe('slugifyMiddleware', () => {
slug: 'i-am-a-brand-new-post', slug: 'i-am-a-brand-new-post',
}, },
}, },
errors: undefined,
}) })
}) })
@ -386,6 +394,7 @@ describe('slugifyMiddleware', () => {
slug: 'the-post', slug: 'the-post',
}, },
}, },
errors: undefined,
}) })
}) })
}) })
@ -422,6 +431,7 @@ describe('slugifyMiddleware', () => {
slug: 'pre-existing-post-1', slug: 'pre-existing-post-1',
}, },
}, },
errors: undefined,
}) })
}) })
@ -504,6 +514,7 @@ describe('slugifyMiddleware', () => {
slug: 'i-am-a-user', slug: 'i-am-a-user',
}, },
}, },
errors: undefined,
}) })
}) })
@ -522,6 +533,7 @@ describe('slugifyMiddleware', () => {
slug: 'the-user', slug: 'the-user',
}, },
}, },
errors: undefined,
}) })
}) })
}) })
@ -546,6 +558,7 @@ describe('slugifyMiddleware', () => {
slug: 'i-am-a-user-1', slug: 'i-am-a-user-1',
}, },
}, },
errors: undefined,
}) })
}) })

View File

@ -86,6 +86,7 @@ export default {
CreateGroup: async (_parent, params, context, _resolveInfo) => { CreateGroup: async (_parent, params, context, _resolveInfo) => {
const { categoryIds } = params const { categoryIds } = params
delete params.categoryIds delete params.categoryIds
params.locationName = params.locationName === '' ? null : params.locationName
if (CONFIG.CATEGORIES_ACTIVE && (!categoryIds || categoryIds.length < CATEGORIES_MIN)) { if (CONFIG.CATEGORIES_ACTIVE && (!categoryIds || categoryIds.length < CATEGORIES_MIN)) {
throw new UserInputError('Too view categories!') throw new UserInputError('Too view categories!')
} }
@ -137,6 +138,7 @@ export default {
}) })
try { try {
const group = await writeTxResultPromise const group = await writeTxResultPromise
// TODO: put in a middleware, see "UpdateGroup", "UpdateUser"
await createOrUpdateLocations('Group', params.id, params.locationName, session) await createOrUpdateLocations('Group', params.id, params.locationName, session)
return group return group
} catch (error) { } catch (error) {
@ -212,10 +214,8 @@ export default {
}) })
try { try {
const group = await writeTxResultPromise const group = await writeTxResultPromise
// TODO: put in a middleware, see "UpdateUser" // TODO: put in a middleware, see "CreateGroup", "UpdateUser"
if (params.locationName !== undefined) { await createOrUpdateLocations('Group', params.id, params.locationName, session)
await createOrUpdateLocations('Group', params.id, params.locationName, session)
}
return group return group
} catch (error) { } catch (error) {
if (error.code === 'Neo.ClientError.Schema.ConstraintValidationFailed') if (error.code === 'Neo.ClientError.Schema.ConstraintValidationFailed')

View File

@ -2610,7 +2610,7 @@ describe('in mode', () => {
describe('unauthenticated', () => { describe('unauthenticated', () => {
it('throws authorization error', async () => { it('throws authorization error', async () => {
const { errors } = await mutate({ const { errors } = await mutate({
query: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
slug: 'my-best-group', slug: 'my-best-group',
@ -2693,7 +2693,7 @@ describe('in mode', () => {
it('has updated the settings', async () => { it('has updated the settings', async () => {
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
name: 'The New Group For Our Country', name: 'The New Group For Our Country',
@ -2726,7 +2726,7 @@ describe('in mode', () => {
it('has left locaton unchanged as "Berlin"', async () => { it('has left locaton unchanged as "Berlin"', async () => {
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
}, },
@ -2753,7 +2753,7 @@ describe('in mode', () => {
it('has updated the location to unset location', async () => { it('has updated the location to unset location', async () => {
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
locationName: null, locationName: null,
@ -2777,7 +2777,7 @@ describe('in mode', () => {
it('has updated the location to "Paris"', async () => { it('has updated the location to "Paris"', async () => {
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
locationName: 'Paris, France', locationName: 'Paris, France',
@ -2805,7 +2805,7 @@ describe('in mode', () => {
it('has updated the location to "Hamburg"', async () => { it('has updated the location to "Hamburg"', async () => {
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
locationName: 'Hamburg, Germany', locationName: 'Hamburg, Germany',
@ -2833,7 +2833,7 @@ describe('in mode', () => {
it('has updated the location to unset', async () => { it('has updated the location to unset', async () => {
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
locationName: '', // empty string '' sets it to null locationName: '', // empty string '' sets it to null
@ -2859,7 +2859,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: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
description: description:
@ -2882,7 +2882,7 @@ describe('in mode', () => {
it('has new categories', async () => { it('has new categories', async () => {
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
categoryIds: ['cat4', 'cat27'], categoryIds: ['cat4', 'cat27'],
@ -2908,7 +2908,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: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
categoryIds: [], categoryIds: [],
@ -2922,7 +2922,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: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
categoryIds: ['cat9', 'cat4', 'cat15', 'cat27'], categoryIds: ['cat9', 'cat4', 'cat15', 'cat27'],
@ -2938,7 +2938,7 @@ describe('in mode', () => {
it('throws authorization error', async () => { it('throws authorization error', async () => {
authenticatedUser = await usualMemberUser.toJson() authenticatedUser = await usualMemberUser.toJson()
const { errors } = await mutate({ const { errors } = await mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
name: 'The New Group For Our Country', name: 'The New Group For Our Country',
@ -2956,7 +2956,7 @@ describe('in mode', () => {
it('throws authorization error', async () => { it('throws authorization error', async () => {
authenticatedUser = await noMemberUser.toJson() authenticatedUser = await noMemberUser.toJson()
const { errors } = await mutate({ const { errors } = await mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables: { variables: {
id: 'my-group', id: 'my-group',
name: 'The New Group For Our Country', name: 'The New Group For Our Country',

View File

@ -170,10 +170,8 @@ export default {
}) })
try { try {
const user = await writeTxResultPromise const user = await writeTxResultPromise
// TODO: put in a middleware, see "CreateGroup, UpdateGroup" // TODO: put in a middleware, see "CreateGroup", "UpdateGroup"
if (params.locationName !== undefined) { await createOrUpdateLocations('User', params.id, params.locationName, session)
await createOrUpdateLocations('User', params.id, params.locationName, session)
}
return user return user
} catch (error) { } catch (error) {
throw new UserInputError(error.message) throw new UserInputError(error.message)

View File

@ -174,6 +174,11 @@ describe('UpdateUser', () => {
termsAndConditionsAgreedVersion termsAndConditionsAgreedVersion
termsAndConditionsAgreedAt termsAndConditionsAgreedAt
locationName locationName
location {
name
nameDE
nameEN
}
} }
} }
` `
@ -289,11 +294,39 @@ describe('UpdateUser', () => {
expect(errors[0]).toHaveProperty('message', 'Invalid version format!') expect(errors[0]).toHaveProperty('message', 'Invalid version format!')
}) })
it('supports updating location', async () => { describe('supports updating location', () => {
variables = { ...variables, locationName: 'Hamburg, New Jersey, United States' } describe('change location to "Hamburg, New Jersey, United States"', () => {
await expect(mutate({ mutation: updateUserMutation, variables })).resolves.toMatchObject({ it('has updated location to "Hamburg, New Jersey, United States"', async () => {
data: { UpdateUser: { locationName: 'Hamburg, New Jersey, United States' } }, variables = { ...variables, locationName: 'Hamburg, New Jersey, United States' }
errors: undefined, await expect(mutate({ mutation: updateUserMutation, variables })).resolves.toMatchObject({
data: {
UpdateUser: {
locationName: 'Hamburg, New Jersey, United States',
location: expect.objectContaining({
name: 'Hamburg',
nameDE: 'Hamburg',
nameEN: 'Hamburg',
}),
},
},
errors: undefined,
})
})
})
describe('change location to unset location', () => {
it('has updated location to unset location', async () => {
variables = { ...variables, locationName: '' }
await expect(mutate({ mutation: updateUserMutation, variables })).resolves.toMatchObject({
data: {
UpdateUser: {
locationName: null,
location: null,
},
},
errors: undefined,
})
})
}) })
}) })
}) })

View File

@ -62,6 +62,8 @@ const createLocation = async (session, mapboxData) => {
} }
export const createOrUpdateLocations = async (nodeLabel, nodeId, locationName, session) => { export const createOrUpdateLocations = async (nodeLabel, nodeId, locationName, session) => {
if (locationName === undefined) return
let locationId let locationId
if (locationName !== null) { if (locationName !== null) {

View File

@ -29,7 +29,7 @@ export default {
}, },
props: { props: {
profile: { type: Object, required: true }, profile: { type: Object, required: true },
updateMutation: { type: Object, required: true }, updateMutation: { type: Function, required: true },
}, },
data() { data() {
return { return {
@ -69,7 +69,7 @@ export default {
const avatarUpload = file[0] const avatarUpload = file[0]
this.$apollo this.$apollo
.mutate({ .mutate({
mutation: this.updateMutation, mutation: this.updateMutation(),
variables: { variables: {
avatar: { avatar: {
upload: avatarUpload, upload: avatarUpload,

View File

@ -50,53 +50,55 @@ export const createGroupMutation = () => {
` `
} }
export const updateGroupMutation = gql` export const updateGroupMutation = () => {
mutation ( return gql`
$id: ID! mutation (
$name: String $id: ID!
$slug: String $name: String
$about: String $slug: String
$description: String $about: String
$actionRadius: GroupActionRadius $description: String
$categoryIds: [ID] $actionRadius: GroupActionRadius
$avatar: ImageInput $categoryIds: [ID]
$locationName: String # empty string '' sets it to null $avatar: ImageInput
) { $locationName: String # empty string '' sets it to null
UpdateGroup(
id: $id
name: $name
slug: $slug
about: $about
description: $description
actionRadius: $actionRadius
categoryIds: $categoryIds
avatar: $avatar
locationName: $locationName
) { ) {
id UpdateGroup(
name id: $id
slug name: $name
createdAt slug: $slug
updatedAt about: $about
disabled description: $description
deleted actionRadius: $actionRadius
about categoryIds: $categoryIds
description avatar: $avatar
descriptionExcerpt locationName: $locationName
groupType ) {
actionRadius
categories {
id id
slug
name name
icon slug
createdAt
updatedAt
disabled
deleted
about
description
descriptionExcerpt
groupType
actionRadius
categories {
id
slug
name
icon
}
# avatar # test this as result
locationName # test this as result
myRole
} }
# avatar # test this as result
locationName # test this as result
myRole
} }
} `
` }
export const joinGroupMutation = gql` export const joinGroupMutation = gql`
mutation ($groupId: ID!, $userId: ID!) { mutation ($groupId: ID!, $userId: ID!) {

View File

@ -27,7 +27,7 @@ export default {
const variables = { id, name, about, description, groupType, actionRadius, categoryIds } const variables = { id, name, about, description, groupType, actionRadius, categoryIds }
try { try {
await this.$apollo.mutate({ await this.$apollo.mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation(),
variables, variables,
}) })
this.$toast.success(this.$t('group.group-updated')) this.$toast.success(this.$t('group.group-updated'))

View File

@ -237,7 +237,7 @@ export default {
followedByCountStartValue: 0, followedByCountStartValue: 0,
followedByCount: followListVisibleCount, followedByCount: followListVisibleCount,
followingCount: followListVisibleCount, followingCount: followListVisibleCount,
updateUserMutation: updateUserMutation(), updateUserMutation,
} }
}, },
computed: { computed: {