From f9bca1d3c4dd36e4fa375b3768681399cff7b3b3 Mon Sep 17 00:00:00 2001 From: Abban Dunne Date: Tue, 3 Sep 2024 12:37:27 +0200 Subject: [PATCH] Move comment form ajax post into resource - Pull axios out of the DonationCommentPopUp component and wraps it in a resource. - Add tests for post response handling Ticket: https://phabricator.wikimedia.org/T367387 --- src/api/CommentResource.ts | 35 ++++++++++++++ .../DonationCommentPopUp.vue | 39 ++++++++------- src/pages/donation_confirmation.ts | 2 + tests/unit/TestDoubles/FakeCommentResource.ts | 15 ++++++ .../components/DonationCommentPopUp.spec.ts | 48 +++++++++++++++++++ .../DonationConfirmation.spec.ts | 4 ++ 6 files changed, 126 insertions(+), 17 deletions(-) create mode 100644 src/api/CommentResource.ts create mode 100644 tests/unit/TestDoubles/FakeCommentResource.ts diff --git a/src/api/CommentResource.ts b/src/api/CommentResource.ts new file mode 100644 index 000000000..7839ec735 --- /dev/null +++ b/src/api/CommentResource.ts @@ -0,0 +1,35 @@ +import axios, { AxiosResponse } from 'axios'; + +export interface CommentRequest { + donationId: number; + updateToken: string; + comment: string; + withName: boolean; + isPublic: boolean; +} + +interface CommentResponse { + status: string; + message: string; +} + +export interface CommentResource { + post: ( data: CommentRequest ) => Promise; +} + +export class ApiCommentResource implements CommentResource { + postEndpoint: string; + + constructor( postEndpoint: string ) { + this.postEndpoint = postEndpoint; + } + + post( data: CommentRequest ): Promise { + return axios.post( this.postEndpoint, data ).then( ( validationResult: AxiosResponse ) => { + if ( validationResult.data.status === 'OK' ) { + return Promise.reject(); + } + return validationResult.data.message; + } ); + } +} diff --git a/src/components/pages/donation_confirmation/DonationCommentPopUp.vue b/src/components/pages/donation_confirmation/DonationCommentPopUp.vue index 512e8e30e..97bcbf9a5 100644 --- a/src/components/pages/donation_confirmation/DonationCommentPopUp.vue +++ b/src/components/pages/donation_confirmation/DonationCommentPopUp.vue @@ -3,9 +3,10 @@
-

+

@@ -66,8 +67,7 @@