diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-12-11 18:47:59 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-12-11 18:55:07 +0100 |
| commit | 1c20e06da5a9817c15c80ca5a25cfacf8eeb0485 (patch) | |
| tree | 0162c375602baa70e51d38bdec143dc645628e96 /src/services | |
| parent | 93db24b7f7650abac1bb7095026e3a1f367b0c0a (diff) | |
test(services): add tests for createComment mutation
* add Jest test
* add a Cypress test in article pages spec
Diffstat (limited to 'src/services')
| -rw-r--r-- | src/services/graphql/mutators/create-comment.test.ts | 23 | ||||
| -rw-r--r-- | src/services/graphql/mutators/create-comment.ts | 44 |
2 files changed, 35 insertions, 32 deletions
diff --git a/src/services/graphql/mutators/create-comment.test.ts b/src/services/graphql/mutators/create-comment.test.ts new file mode 100644 index 0000000..b3742f5 --- /dev/null +++ b/src/services/graphql/mutators/create-comment.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from '@jest/globals'; +import { type CreateCommentInput, createComment } from './create-comment'; + +describe('create-comment', () => { + it('successfully create a new comment', async () => { + const email: CreateCommentInput = { + author: 'Bruce_Lowe12', + authorEmail: 'Wiley_Wolf18@example.net', + authorUrl: '', + clientMutationId: 'aliquid', + commentOn: 2, + content: 'Error vel fugit nisi accusantium.', + }; + const result = await createComment(email); + + // eslint-disable-next-line @typescript-eslint/no-magic-numbers + expect.assertions(3); + + expect(result.clientMutationId).toBe(email.clientMutationId); + expect(result.comment?.approved).toBe(true); + expect(result.success).toBe(true); + }); +}); diff --git a/src/services/graphql/mutators/create-comment.ts b/src/services/graphql/mutators/create-comment.ts index d9d177d..37d5348 100644 --- a/src/services/graphql/mutators/create-comment.ts +++ b/src/services/graphql/mutators/create-comment.ts @@ -1,43 +1,23 @@ -import type { Nullable } from '../../../types'; +import type { Nullable, WPComment } from '../../../types'; import { fetchGraphQL, getGraphQLUrl } from '../../../utils/helpers'; -type CreatedComment = { - clientMutationId: string; +export type CreateCommentPayload = { + clientMutationId: Nullable<string>; success: boolean; - comment: Nullable<{ - approved: boolean; - }>; + comment: Nullable<Pick<WPComment, 'approved'>>; }; -type CreateCommentResponse = { - createComment: CreatedComment; +export type CreateCommentResponse = { + createComment: CreateCommentPayload; }; -export const createCommentMutation = `mutation CreateComment( - $author: String! - $authorEmail: String! - $authorUrl: String! - $content: String! - $parent: ID = null - $commentOn: Int! - $clientMutationId: String! -) { - createComment( - input: { - author: $author - authorEmail: $authorEmail - authorUrl: $authorUrl - content: $content - parent: $parent - commentOn: $commentOn - clientMutationId: $clientMutationId - } - ) { +export const createCommentMutation = `mutation CreateComment($input: CreateCommentInput!) { + createComment(input: $input) { clientMutationId - success comment { approved } + success } }`; @@ -55,15 +35,15 @@ export type CreateCommentInput = { * Create a new comment using GraphQL API. * * @param {CreateCommentInput} input - The comment data. - * @returns {Promise<CreatedComment>} The created comment. + * @returns {Promise<CreateCommentPayload>} The created comment. */ export const createComment = async ( input: CreateCommentInput -): Promise<CreatedComment> => { +): Promise<CreateCommentPayload> => { const response = await fetchGraphQL<CreateCommentResponse>({ query: createCommentMutation, url: getGraphQLUrl(), - variables: { ...input }, + variables: { input }, }); return response.createComment; |
